mirror of
https://github.com/chirimen-oh/node-web-i2c.git
synced 2025-01-19 00:18:03 +00:00
resolved #1
This commit is contained in:
parent
c65b03303b
commit
33e92fec2f
5 changed files with 118 additions and 19 deletions
12
index.d.ts
vendored
12
index.d.ts
vendored
|
@ -6,9 +6,7 @@ export declare class I2CAccess {
|
||||||
constructor(ports?: I2CPortMap);
|
constructor(ports?: I2CPortMap);
|
||||||
get ports(): I2CPortMap;
|
get ports(): I2CPortMap;
|
||||||
}
|
}
|
||||||
/**
|
/** Different from Web I2C API specification. */
|
||||||
* Different from Web GPIO API specification.
|
|
||||||
*/
|
|
||||||
export declare class I2CPortMap extends Map<PortNumber, I2CPort> {
|
export declare class I2CPortMap extends Map<PortNumber, I2CPort> {
|
||||||
getByName(portName: PortName): I2CPort | undefined;
|
getByName(portName: PortName): I2CPort | undefined;
|
||||||
}
|
}
|
||||||
|
@ -25,6 +23,14 @@ export interface I2CSlaveDevice {
|
||||||
read16(registerNumber: number): Promise<number>;
|
read16(registerNumber: number): Promise<number>;
|
||||||
write8(registerNumber: number, value: number): Promise<number>;
|
write8(registerNumber: number, value: number): Promise<number>;
|
||||||
write16(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 {
|
export declare class OperationError extends Error {
|
||||||
constructor(message: string);
|
constructor(message: string);
|
||||||
|
|
46
index.js
46
index.js
|
@ -19,9 +19,7 @@ class I2CAccess {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.I2CAccess = I2CAccess;
|
exports.I2CAccess = I2CAccess;
|
||||||
/**
|
/** Different from Web I2C API specification. */
|
||||||
* Different from Web GPIO API specification.
|
|
||||||
*/
|
|
||||||
class I2CPortMap extends Map {
|
class I2CPortMap extends Map {
|
||||||
getByName(portName) {
|
getByName(portName) {
|
||||||
const matches = /^i2c-(\d+)$/.exec(portName);
|
const matches = /^i2c-(\d+)$/.exec(portName);
|
||||||
|
@ -56,7 +54,47 @@ class I2CPort {
|
||||||
}),
|
}),
|
||||||
write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word).catch(error => {
|
write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word).catch(error => {
|
||||||
throw new OperationError(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
60
index.ts
60
index.ts
|
@ -27,9 +27,7 @@ export class I2CAccess {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Different from Web I2C API specification. */
|
||||||
* Different from Web GPIO API specification.
|
|
||||||
*/
|
|
||||||
export class I2CPortMap extends Map<PortNumber, I2CPort> {
|
export class I2CPortMap extends Map<PortNumber, I2CPort> {
|
||||||
getByName(portName: PortName) {
|
getByName(portName: PortName) {
|
||||||
const matches = /^i2c-(\d+)$/.exec(portName);
|
const matches = /^i2c-(\d+)$/.exec(portName);
|
||||||
|
@ -74,7 +72,52 @@ export class I2CPort {
|
||||||
write16: (cmd, word) =>
|
write16: (cmd, word) =>
|
||||||
bus.writeWord(slaveAddress, cmd, word).catch(error => {
|
bus.writeWord(slaveAddress, cmd, word).catch(error => {
|
||||||
throw new OperationError(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>;
|
read16(registerNumber: number): Promise<number>;
|
||||||
write8(registerNumber: number, value: number): Promise<number>;
|
write8(registerNumber: number, value: number): Promise<number>;
|
||||||
write16(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 {
|
export class OperationError extends Error {
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "node-web-i2c",
|
"name": "node-web-i2c",
|
||||||
"version": "1.0.0",
|
"version": "1.1.1",
|
||||||
"description": "I2C access with Node.js",
|
"description": "I2C access with Node.js",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
"files": [
|
||||||
|
"index.*"
|
||||||
|
],
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/kou029w/node-web-i2c.git"
|
"url": "https://github.com/kou029w/node-web-i2c.git"
|
||||||
|
@ -13,10 +16,10 @@
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"i2c-bus": "^5.1.0"
|
"i2c-bus": "~5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^12.11.1",
|
"@types/node": "~12",
|
||||||
"typescript": "~3.7"
|
"typescript": "~3.7"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
10
yarn.lock
10
yarn.lock
|
@ -2,10 +2,10 @@
|
||||||
# yarn lockfile v1
|
# yarn lockfile v1
|
||||||
|
|
||||||
|
|
||||||
"@types/node@^12.11.1":
|
"@types/node@~12":
|
||||||
version "12.11.1"
|
version "12.12.14"
|
||||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.11.1.tgz#1fd7b821f798b7fa29f667a1be8f3442bb8922a3"
|
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.14.tgz#1c1d6e3c75dba466e0326948d56e8bd72a1903d2"
|
||||||
integrity sha512-TJtwsqZ39pqcljJpajeoofYRfeZ7/I/OMUQ5pR4q5wOKf2ocrUvBAZUMhWsOvKx3dVc/aaV5GluBivt0sWqA5A==
|
integrity sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA==
|
||||||
|
|
||||||
bindings@^1.5.0:
|
bindings@^1.5.0:
|
||||||
version "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"
|
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==
|
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
|
||||||
|
|
||||||
i2c-bus@^5.1.0:
|
i2c-bus@~5:
|
||||||
version "5.1.0"
|
version "5.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/i2c-bus/-/i2c-bus-5.1.0.tgz#7d667d225c09017cf044f75fc593d5ea3e0f4ea6"
|
resolved "https://registry.yarnpkg.com/i2c-bus/-/i2c-bus-5.1.0.tgz#7d667d225c09017cf044f75fc593d5ea3e0f4ea6"
|
||||||
integrity sha512-u/q1fuZ5xrG77y3uo1rAANycboXsRNjneN+6jXRNMT2yRNpanVkbAP+IArwgsPRCHaY/zKxw7x5G8Y2fSPNseA==
|
integrity sha512-u/q1fuZ5xrG77y3uo1rAANycboXsRNjneN+6jXRNMT2yRNpanVkbAP+IArwgsPRCHaY/zKxw7x5G8Y2fSPNseA==
|
||||||
|
|
Loading…
Add table
Reference in a new issue