From 33e92fec2f5f2628f87e116ba2f59c58853da859 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Thu, 19 Dec 2019 14:04:48 +0900 Subject: [PATCH] resolved #1 --- index.d.ts | 12 ++++++++--- index.js | 46 ++++++++++++++++++++++++++++++++++++---- index.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++++++---- package.json | 9 +++++--- yarn.lock | 10 ++++----- 5 files changed, 118 insertions(+), 19 deletions(-) diff --git a/index.d.ts b/index.d.ts index fbfe49b..698c8b9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,9 +6,7 @@ export declare class I2CAccess { constructor(ports?: I2CPortMap); get ports(): I2CPortMap; } -/** - * Different from Web GPIO API specification. - */ +/** Different from Web I2C API specification. */ export declare class I2CPortMap extends Map { getByName(portName: PortName): I2CPort | undefined; } @@ -25,6 +23,14 @@ export interface I2CSlaveDevice { read16(registerNumber: number): Promise; write8(registerNumber: number, value: number): Promise; write16(registerNumber: number, value: number): Promise; + /** Different from Web I2C API specification. */ + readByte(): Promise; + /** Different from Web I2C API specification. */ + readBytes(length: number): Promise; + /** Different from Web I2C API specification. */ + writeByte(byte: number): Promise; + /** Different from Web I2C API specification. */ + writeBytes(bytes: Array): Promise; } export declare class OperationError extends Error { constructor(message: string); diff --git a/index.js b/index.js index ffe19f3..bbbf2cf 100644 --- a/index.js +++ b/index.js @@ -19,9 +19,7 @@ class I2CAccess { } } exports.I2CAccess = I2CAccess; -/** - * Different from Web GPIO API specification. - */ +/** Different from Web I2C API specification. */ class I2CPortMap extends Map { getByName(portName) { const matches = /^i2c-(\d+)$/.exec(portName); @@ -56,7 +54,47 @@ class I2CPort { }), write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word).catch(error => { throw new OperationError(error); - }) + }), + /** Different from Web I2C API specification. */ + readByte: async () => { + try { + const byte = await bus.receiveByte(slaveAddress); + return byte; + } + catch (error) { + throw new OperationError(error); + } + }, + /** Different from Web I2C API specification. */ + readBytes: async (length) => { + try { + const { bytesRead, buffer } = await bus.i2cRead(slaveAddress, length, Buffer.allocUnsafe(length)); + return new Uint8Array(buffer.slice(0, bytesRead)); + } + catch (error) { + throw new OperationError(error); + } + }, + /** Different from Web I2C API specification. */ + writeByte: async (byte) => { + try { + await bus.sendByte(slaveAddress, byte); + return byte; + } + catch (error) { + throw new OperationError(error); + } + }, + /** Different from Web I2C API specification. */ + writeBytes: async (bytes) => { + try { + const { bytesWritten, buffer } = await bus.i2cWrite(slaveAddress, length, Buffer.from(bytes)); + return new Uint8Array(buffer.slice(0, bytesWritten)); + } + catch (error) { + throw new OperationError(error); + } + } }; } } diff --git a/index.ts b/index.ts index f296f47..e9dd73f 100644 --- a/index.ts +++ b/index.ts @@ -27,9 +27,7 @@ export class I2CAccess { } } -/** - * Different from Web GPIO API specification. - */ +/** Different from Web I2C API specification. */ export class I2CPortMap extends Map { getByName(portName: PortName) { const matches = /^i2c-(\d+)$/.exec(portName); @@ -74,7 +72,52 @@ export class I2CPort { write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word).catch(error => { throw new OperationError(error); - }) + }), + + /** Different from Web I2C API specification. */ + readByte: async () => { + try { + const byte = await bus.receiveByte(slaveAddress); + return byte; + } catch (error) { + throw new OperationError(error); + } + }, + /** Different from Web I2C API specification. */ + readBytes: async length => { + try { + const { bytesRead, buffer } = await bus.i2cRead( + slaveAddress, + length, + Buffer.allocUnsafe(length) + ); + return new Uint8Array(buffer.slice(0, bytesRead)); + } catch (error) { + throw new OperationError(error); + } + }, + /** Different from Web I2C API specification. */ + writeByte: async byte => { + try { + await bus.sendByte(slaveAddress, byte); + return byte; + } catch (error) { + throw new OperationError(error); + } + }, + /** Different from Web I2C API specification. */ + writeBytes: async bytes => { + try { + const { bytesWritten, buffer } = await bus.i2cWrite( + slaveAddress, + length, + Buffer.from(bytes) + ); + return new Uint8Array(buffer.slice(0, bytesWritten)); + } catch (error) { + throw new OperationError(error); + } + } }; } } @@ -86,6 +129,15 @@ export interface I2CSlaveDevice { read16(registerNumber: number): Promise; write8(registerNumber: number, value: number): Promise; write16(registerNumber: number, value: number): Promise; + + /** Different from Web I2C API specification. */ + readByte(): Promise; + /** Different from Web I2C API specification. */ + readBytes(length: number): Promise; + /** Different from Web I2C API specification. */ + writeByte(byte: number): Promise; + /** Different from Web I2C API specification. */ + writeBytes(bytes: Array): Promise; } export class OperationError extends Error { diff --git a/package.json b/package.json index 9deaa0d..5eaedcd 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { "name": "node-web-i2c", - "version": "1.0.0", + "version": "1.1.1", "description": "I2C access with Node.js", "main": "index.js", + "files": [ + "index.*" + ], "repository": { "type": "git", "url": "https://github.com/kou029w/node-web-i2c.git" @@ -13,10 +16,10 @@ "access": "public" }, "dependencies": { - "i2c-bus": "^5.1.0" + "i2c-bus": "~5" }, "devDependencies": { - "@types/node": "^12.11.1", + "@types/node": "~12", "typescript": "~3.7" }, "scripts": { diff --git a/yarn.lock b/yarn.lock index f167b64..094ab86 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@types/node@^12.11.1": - version "12.11.1" - resolved "https://registry.yarnpkg.com/@types/node/-/node-12.11.1.tgz#1fd7b821f798b7fa29f667a1be8f3442bb8922a3" - integrity sha512-TJtwsqZ39pqcljJpajeoofYRfeZ7/I/OMUQ5pR4q5wOKf2ocrUvBAZUMhWsOvKx3dVc/aaV5GluBivt0sWqA5A== +"@types/node@~12": + version "12.12.14" + resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.14.tgz#1c1d6e3c75dba466e0326948d56e8bd72a1903d2" + integrity sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA== bindings@^1.5.0: version "1.5.0" @@ -19,7 +19,7 @@ file-uri-to-path@1.0.0: resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw== -i2c-bus@^5.1.0: +i2c-bus@~5: version "5.1.0" resolved "https://registry.yarnpkg.com/i2c-bus/-/i2c-bus-5.1.0.tgz#7d667d225c09017cf044f75fc593d5ea3e0f4ea6" integrity sha512-u/q1fuZ5xrG77y3uo1rAANycboXsRNjneN+6jXRNMT2yRNpanVkbAP+IArwgsPRCHaY/zKxw7x5G8Y2fSPNseA==