This commit is contained in:
Nebel 2020-06-25 11:45:22 +09:00
parent d406ab82e3
commit 4dd786679b

View file

@ -51,18 +51,18 @@ export class I2CPort {
} }
async open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice> { async open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice> {
const bus = await openPromisified(this.portNumber).catch(error => { const bus = await openPromisified(this.portNumber).catch((error) => {
throw new OperationError(error); throw new OperationError(error);
}); });
return { return {
slaveAddress, slaveAddress,
read8: cmd => read8: (cmd) =>
bus.readByte(slaveAddress, cmd).catch(error => { bus.readByte(slaveAddress, cmd).catch((error) => {
throw new OperationError(error); throw new OperationError(error);
}), }),
read16: cmd => read16: (cmd) =>
bus.readWord(slaveAddress, cmd).catch(error => { bus.readWord(slaveAddress, cmd).catch((error) => {
throw new OperationError(error); throw new OperationError(error);
}), }),
write8: async (cmd, byte) => { write8: async (cmd, byte) => {
@ -92,7 +92,7 @@ export class I2CPort {
} }
}, },
/** Different from Web I2C API specification. */ /** Different from Web I2C API specification. */
readBytes: async length => { readBytes: async (length) => {
try { try {
const { bytesRead, buffer } = await bus.i2cRead( const { bytesRead, buffer } = await bus.i2cRead(
slaveAddress, slaveAddress,
@ -105,7 +105,7 @@ export class I2CPort {
} }
}, },
/** Different from Web I2C API specification. */ /** Different from Web I2C API specification. */
writeByte: async byte => { writeByte: async (byte) => {
try { try {
await bus.sendByte(slaveAddress, byte); await bus.sendByte(slaveAddress, byte);
return byte; return byte;
@ -114,7 +114,7 @@ export class I2CPort {
} }
}, },
/** Different from Web I2C API specification. */ /** Different from Web I2C API specification. */
writeBytes: async bytes => { writeBytes: async (bytes) => {
try { try {
const { bytesWritten, buffer } = await bus.i2cWrite( const { bytesWritten, buffer } = await bus.i2cWrite(
slaveAddress, slaveAddress,
@ -125,7 +125,7 @@ export class I2CPort {
} catch (error) { } catch (error) {
throw new OperationError(error); throw new OperationError(error);
} }
} },
}; };
} }
} }
@ -157,9 +157,9 @@ export class OperationError extends Error {
export async function requestI2CAccess(): Promise<I2CAccess> { export async function requestI2CAccess(): Promise<I2CAccess> {
const ports = new I2CPortMap( const ports = new I2CPortMap(
[...Array(I2CPortMapSizeMax).keys()].map(portNumber => [ [...Array(I2CPortMapSizeMax).keys()].map((portNumber) => [
portNumber, portNumber,
new I2CPort(portNumber) new I2CPort(portNumber),
]) ])
); );