node-web-i2c/index.ts

170 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-01-11 18:06:49 +09:00
import { openPromisified } from 'i2c-bus';
2019-10-18 03:23:29 +09:00
const I2CPortMapSizeMax = 32;
const Uint16Max = 65535;
2022-01-11 18:06:49 +09:00
function parseUint16(parseString: string) {
const n = Number.parseInt(parseString, 10);
2019-10-18 03:23:29 +09:00
if (0 <= n && n <= Uint16Max) return n;
else throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
}
type PortNumber = number;
type PortName = string;
type I2CSlaveAddress = number;
export class I2CAccess {
private readonly _ports: I2CPortMap;
constructor(ports?: I2CPortMap) {
this._ports = ports == null ? new I2CPortMap() : ports;
}
get ports(): I2CPortMap {
2019-10-18 03:23:29 +09:00
return this._ports;
}
}
2019-12-19 14:04:48 +09:00
/** Different from Web I2C API specification. */
2019-10-18 03:23:29 +09:00
export class I2CPortMap extends Map<PortNumber, I2CPort> {
getByName(portName: PortName): I2CPort | undefined {
2019-10-18 03:23:29 +09:00
const matches = /^i2c-(\d+)$/.exec(portName);
return matches == null ? undefined : this.get(parseUint16(matches[1]));
}
}
export class I2CPort {
private readonly _portNumber: PortNumber;
constructor(portNumber: PortNumber) {
this._portNumber = parseUint16(portNumber.toString());
}
get portNumber(): PortNumber {
2019-10-18 03:23:29 +09:00
return this._portNumber;
}
get portName(): string {
2019-10-18 03:23:29 +09:00
return `i2c-${this.portNumber}`;
}
async open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice> {
2020-06-25 11:45:22 +09:00
const bus = await openPromisified(this.portNumber).catch((error) => {
2019-10-18 03:23:29 +09:00
throw new OperationError(error);
});
return {
slaveAddress,
2020-06-25 11:45:22 +09:00
read8: (cmd) =>
bus.readByte(slaveAddress, cmd).catch((error) => {
2019-10-18 21:47:52 +09:00
throw new OperationError(error);
}),
2020-06-25 11:45:22 +09:00
read16: (cmd) =>
bus.readWord(slaveAddress, cmd).catch((error) => {
2019-10-18 21:47:52 +09:00
throw new OperationError(error);
}),
2020-02-12 02:20:53 +09:00
write8: async (cmd, byte) => {
try {
await bus.writeByte(slaveAddress, cmd, byte);
return byte;
} catch (error: any) {
2019-10-18 21:47:52 +09:00
throw new OperationError(error);
2020-02-12 02:20:53 +09:00
}
},
write16: async (cmd, word) => {
try {
await bus.writeWord(slaveAddress, cmd, word);
return word;
} catch (error: any) {
2019-10-18 21:47:52 +09:00
throw new OperationError(error);
2020-02-12 02:20:53 +09:00
}
},
2019-12-19 14:04:48 +09:00
/** Different from Web I2C API specification. */
readByte: async () => {
try {
const byte = await bus.receiveByte(slaveAddress);
return byte;
} catch (error: any) {
2019-12-19 14:04:48 +09:00
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
2020-06-25 11:45:22 +09:00
readBytes: async (length) => {
2019-12-19 14:04:48 +09:00
try {
const { bytesRead, buffer } = await bus.i2cRead(
slaveAddress,
length,
Buffer.allocUnsafe(length)
);
return new Uint8Array(buffer.slice(0, bytesRead));
} catch (error: any) {
2019-12-19 14:04:48 +09:00
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
2020-06-25 11:45:22 +09:00
writeByte: async (byte) => {
2019-12-19 14:04:48 +09:00
try {
await bus.sendByte(slaveAddress, byte);
return byte;
} catch (error: any) {
2019-12-19 14:04:48 +09:00
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
2020-06-25 11:45:22 +09:00
writeBytes: async (bytes) => {
2019-12-19 14:04:48 +09:00
try {
const { bytesWritten, buffer } = await bus.i2cWrite(
slaveAddress,
2020-02-06 11:18:33 +09:00
bytes.length,
2019-12-19 14:04:48 +09:00
Buffer.from(bytes)
);
return new Uint8Array(buffer.slice(0, bytesWritten));
} catch (error: any) {
2019-12-19 14:04:48 +09:00
throw new OperationError(error);
}
2020-06-25 11:45:22 +09:00
},
2019-10-18 03:23:29 +09:00
};
}
}
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>;
2019-12-19 14:04:48 +09:00
/** 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>;
2019-10-18 03:23:29 +09:00
}
2019-10-18 21:45:56 +09:00
export class OperationError extends Error {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
2019-10-18 03:23:29 +09:00
// Web I2Cの仕様に基づく意図的なasync関数の使用なので、ルールを無効化
// eslint-disable-next-line
2019-10-18 03:23:29 +09:00
export async function requestI2CAccess(): Promise<I2CAccess> {
const ports = new I2CPortMap(
2020-06-25 11:45:22 +09:00
[...Array(I2CPortMapSizeMax).keys()].map((portNumber) => [
2019-10-18 03:23:29 +09:00
portNumber,
2020-06-25 11:45:22 +09:00
new I2CPort(portNumber),
2019-10-18 03:23:29 +09:00
])
);
return new I2CAccess(ports);
}