1
0
Fork 0
mirror of https://github.com/chirimen-oh/node-web-i2c.git synced 2025-04-01 19:05:19 +00:00
This commit is contained in:
gurezo 2022-01-05 23:28:10 +09:00
parent bf77c122b9
commit c57c2aab70
7 changed files with 233 additions and 35 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

184
index.ts
View file

@ -1,27 +1,53 @@
import { openPromisified } from "i2c-bus";
import { openPromisified } from 'i2c-bus';
/**
* I2C Port Map Max
*/
const I2CPortMapSizeMax = 32;
/**
* Uint16 Max
*/
const Uint16Max = 65535;
function parseUint16(string: string) {
const n = Number.parseInt(string, 10);
/**
*
* Uint16型変換処理
* @param parseString
* @return Uint16型変換値
*/
function parseUint16(parseString: string) {
const n = Number.parseInt(parseString, 10);
if (0 <= n && n <= Uint16Max) return n;
else throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
}
/** ポート番号 */
type PortNumber = number;
/** ポート名 */
type PortName = string;
/** I2C Slave アドレス */
type I2CSlaveAddress = number;
/**
* I2CAccess
*/
export class I2CAccess {
private readonly _ports: I2CPortMap;
/**
* Creates an instance of GPIOAccess.
* @param ports
*/
constructor(ports?: I2CPortMap) {
this._ports = ports == null ? new I2CPortMap() : ports;
}
/**
*
* @return
*/
get ports(): I2CPortMap {
return this._ports;
}
@ -35,21 +61,41 @@ export class I2CPortMap extends Map<PortNumber, I2CPort> {
}
}
/**
* I2CPort
*/
export class I2CPort {
private readonly _portNumber: PortNumber;
/**
* Creates an instance of GPIOPort.
* @param portNumber
*/
constructor(portNumber: PortNumber) {
this._portNumber = parseUint16(portNumber.toString());
}
/**
*
* @return
*/
get portNumber(): PortNumber {
return this._portNumber;
}
/**
*
* @return
*/
get portName(): string {
return `i2c-${this.portNumber}`;
}
/**
* I2CSlave
* @param slaveAddress
* @return I2CSlave Promise
*/
async open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice> {
const bus = await openPromisified(this.portNumber).catch((error) => {
throw new OperationError(error);
@ -57,32 +103,57 @@ export class I2CPort {
return {
slaveAddress,
read8: (cmd) =>
bus.readByte(slaveAddress, cmd).catch((error) => {
/**
* @function
* I2c 8bit
* @param registerNumber
*/
read8: (registerNumber) =>
bus.readByte(slaveAddress, registerNumber).catch((error) => {
throw new OperationError(error);
}),
read16: (cmd) =>
bus.readWord(slaveAddress, cmd).catch((error) => {
/**
* @function
* I2c 16bit
* @param registerNumber
*/
read16: (registerNumber) =>
bus.readWord(slaveAddress, registerNumber).catch((error) => {
throw new OperationError(error);
}),
write8: async (cmd, byte) => {
/**
* @function
* I2c 8bit
* @param registerNumber
* @param byte
*/
write8: async (registerNumber, byte) => {
try {
await bus.writeByte(slaveAddress, cmd, byte);
await bus.writeByte(slaveAddress, registerNumber, byte);
return byte;
} catch (error: any) {
throw new OperationError(error);
}
},
write16: async (cmd, word) => {
/**
* @function
* I2c 16bit
* @param registerNumber
* @param word
*/
write16: async (registerNumber, word) => {
try {
await bus.writeWord(slaveAddress, cmd, word);
await bus.writeWord(slaveAddress, registerNumber, word);
return word;
} catch (error: any) {
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
*/
readByte: async () => {
try {
const byte = await bus.receiveByte(slaveAddress);
@ -91,7 +162,12 @@ export class I2CPort {
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
* @param length
*/
readBytes: async (length) => {
try {
const { bytesRead, buffer } = await bus.i2cRead(
@ -104,7 +180,12 @@ export class I2CPort {
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
* @param registerNumber
*/
writeByte: async (byte) => {
try {
await bus.sendByte(slaveAddress, byte);
@ -113,7 +194,12 @@ export class I2CPort {
throw new OperationError(error);
}
},
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
* @param byte
*/
writeBytes: async (bytes) => {
try {
const { bytesWritten, buffer } = await bus.i2cWrite(
@ -130,32 +216,88 @@ export class I2CPort {
}
}
/**
* I2CSlaveDevice
*/
export interface I2CSlaveDevice {
/** I2C Slave アドレス */
readonly slaveAddress: I2CSlaveAddress;
/**
* @function
* I2c 8bit
* @param registerNumber
*/
read8(registerNumber: number): Promise<number>;
/**
* @function
* I2c 16bit
* @param registerNumber
*/
read16(registerNumber: number): Promise<number>;
/**
* @function
* I2c 8bit
* @param registerNumber
* @param byte
*/
write8(registerNumber: number, value: number): Promise<number>;
/**
* @function
* I2c 16bit
* @param registerNumber
* @param word
*/
write16(registerNumber: number, value: number): Promise<number>;
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
*/
readByte(): Promise<number>;
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
* @param length
*/
readBytes(length: number): Promise<Uint8Array>;
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
* @param byte
*/
writeByte(byte: number): Promise<number>;
/** Different from Web I2C API specification. */
/**
* @function
* I2c 16bit
* Different from Web I2C API specification.
* @param byte
*/
writeBytes(bytes: Array<number>): Promise<Uint8Array>;
}
/**
*
*/
export class OperationError extends Error {
/**
* Creates an instance of OperationError.
* @param message
*/
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
// Web I2Cの仕様に基づく意図的なasync関数の使用なので、ルールを無効化
/**
* requestGPIOAccess
* Web I2Cの仕様に基づく意図的なasync関数の使用なので
* @return I2CAccess Promise
*/
// eslint-disable-next-line
export async function requestI2CAccess(): Promise<I2CAccess> {
const ports = new I2CPortMap(