mirror of
https://github.com/chirimen-oh/node-web-i2c.git
synced 2025-01-17 23:55:06 +00:00
chore: use prepare
This commit is contained in:
parent
a78917a3fb
commit
693be341d7
4 changed files with 3 additions and 169 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/node_modules/
|
||||
/index.*
|
||||
|
|
39
index.d.ts
vendored
39
index.d.ts
vendored
|
@ -1,39 +0,0 @@
|
|||
declare type PortNumber = number;
|
||||
declare type PortName = string;
|
||||
declare type I2CSlaveAddress = number;
|
||||
export declare class I2CAccess {
|
||||
private readonly _ports;
|
||||
constructor(ports?: I2CPortMap);
|
||||
get ports(): I2CPortMap;
|
||||
}
|
||||
/** Different from Web I2C API specification. */
|
||||
export declare class I2CPortMap extends Map<PortNumber, I2CPort> {
|
||||
getByName(portName: PortName): I2CPort | undefined;
|
||||
}
|
||||
export declare class I2CPort {
|
||||
private readonly _portNumber;
|
||||
constructor(portNumber: PortNumber);
|
||||
get portNumber(): number;
|
||||
get portName(): string;
|
||||
open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice>;
|
||||
}
|
||||
export interface I2CSlaveDevice {
|
||||
readonly slaveAddress: I2CSlaveAddress;
|
||||
read8(registerNumber: number): Promise<number>;
|
||||
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);
|
||||
}
|
||||
export declare function requestI2CAccess(): Promise<I2CAccess>;
|
||||
export {};
|
129
index.js
129
index.js
|
@ -1,129 +0,0 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.requestI2CAccess = exports.OperationError = exports.I2CPort = exports.I2CPortMap = exports.I2CAccess = void 0;
|
||||
const i2c_bus_1 = require("i2c-bus");
|
||||
const I2CPortMapSizeMax = 32;
|
||||
const Uint16Max = 65535;
|
||||
function parseUint16(string) {
|
||||
const n = Number.parseInt(string, 10);
|
||||
if (0 <= n && n <= Uint16Max)
|
||||
return n;
|
||||
else
|
||||
throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
|
||||
}
|
||||
class I2CAccess {
|
||||
constructor(ports) {
|
||||
this._ports = ports == null ? new I2CPortMap() : ports;
|
||||
}
|
||||
get ports() {
|
||||
return this._ports;
|
||||
}
|
||||
}
|
||||
exports.I2CAccess = I2CAccess;
|
||||
/** Different from Web I2C API specification. */
|
||||
class I2CPortMap extends Map {
|
||||
getByName(portName) {
|
||||
const matches = /^i2c-(\d+)$/.exec(portName);
|
||||
return matches == null ? undefined : this.get(parseUint16(matches[1]));
|
||||
}
|
||||
}
|
||||
exports.I2CPortMap = I2CPortMap;
|
||||
class I2CPort {
|
||||
constructor(portNumber) {
|
||||
this._portNumber = parseUint16(portNumber.toString());
|
||||
}
|
||||
get portNumber() {
|
||||
return this._portNumber;
|
||||
}
|
||||
get portName() {
|
||||
return `i2c-${this.portNumber}`;
|
||||
}
|
||||
async open(slaveAddress) {
|
||||
const bus = await i2c_bus_1.openPromisified(this.portNumber).catch((error) => {
|
||||
throw new OperationError(error);
|
||||
});
|
||||
return {
|
||||
slaveAddress,
|
||||
read8: (cmd) => bus.readByte(slaveAddress, cmd).catch((error) => {
|
||||
throw new OperationError(error);
|
||||
}),
|
||||
read16: (cmd) => bus.readWord(slaveAddress, cmd).catch((error) => {
|
||||
throw new OperationError(error);
|
||||
}),
|
||||
write8: async (cmd, byte) => {
|
||||
try {
|
||||
await bus.writeByte(slaveAddress, cmd, byte);
|
||||
return byte;
|
||||
}
|
||||
catch (error) {
|
||||
throw new OperationError(error);
|
||||
}
|
||||
},
|
||||
write16: async (cmd, word) => {
|
||||
try {
|
||||
await bus.writeWord(slaveAddress, cmd, word);
|
||||
return 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, bytes.length, Buffer.from(bytes));
|
||||
return new Uint8Array(buffer.slice(0, bytesWritten));
|
||||
}
|
||||
catch (error) {
|
||||
throw new OperationError(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.I2CPort = I2CPort;
|
||||
class OperationError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = this.constructor.name;
|
||||
}
|
||||
}
|
||||
exports.OperationError = OperationError;
|
||||
async function requestI2CAccess() {
|
||||
const ports = new I2CPortMap([...Array(I2CPortMapSizeMax).keys()].map((portNumber) => [
|
||||
portNumber,
|
||||
new I2CPort(portNumber),
|
||||
]));
|
||||
return new I2CAccess(ports);
|
||||
}
|
||||
exports.requestI2CAccess = requestI2CAccess;
|
|
@ -21,7 +21,8 @@
|
|||
"typescript": "^4.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
"build": "tsc",
|
||||
"prepare": "npm run build"
|
||||
},
|
||||
"keywords": [
|
||||
"hardware",
|
||||
|
|
Loading…
Add table
Reference in a new issue