This commit is contained in:
Nebel 2019-12-19 14:04:48 +09:00
parent c65b03303b
commit 33e92fec2f
5 changed files with 118 additions and 19 deletions

12
index.d.ts vendored
View file

@ -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<PortNumber, I2CPort> {
getByName(portName: PortName): I2CPort | undefined;
}
@ -25,6 +23,14 @@ export interface I2CSlaveDevice {
read16(registerNumber: number): Promise<number>;
write8(registerNumber: number, value: number): Promise<number>;
write16(registerNumber: number, value: number): Promise<number>;
/** Different from Web I2C API specification. */
readByte(): Promise<number>;
/** Different from Web I2C API specification. */
readBytes(length: number): Promise<Uint8Array>;
/** Different from Web I2C API specification. */
writeByte(byte: number): Promise<number>;
/** Different from Web I2C API specification. */
writeBytes(bytes: Array<number>): Promise<Uint8Array>;
}
export declare class OperationError extends Error {
constructor(message: string);

View file

@ -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);
}
}
};
}
}

View file

@ -27,9 +27,7 @@ export class I2CAccess {
}
}
/**
* Different from Web GPIO API specification.
*/
/** Different from Web I2C API specification. */
export class I2CPortMap extends Map<PortNumber, I2CPort> {
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<number>;
write8(registerNumber: number, value: number): Promise<number>;
write16(registerNumber: number, value: number): Promise<number>;
/** Different from Web I2C API specification. */
readByte(): Promise<number>;
/** Different from Web I2C API specification. */
readBytes(length: number): Promise<Uint8Array>;
/** Different from Web I2C API specification. */
writeByte(byte: number): Promise<number>;
/** Different from Web I2C API specification. */
writeBytes(bytes: Array<number>): Promise<Uint8Array>;
}
export class OperationError extends Error {

View file

@ -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": {

View file

@ -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==