From 99c06ce43a338559b6543e6fd0a7f9dc9e11b455 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Fri, 18 Oct 2019 21:48:05 +0900 Subject: [PATCH] yarn build --- index.d.ts | 1 + index.js | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 7834829..6e57ee6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -27,6 +27,7 @@ export interface I2CSlaveDevice { write16(registerNumber: number, value: number): Promise; } export declare class OperationError extends Error { + constructor(message: string); } export declare function requestI2CAccess(): Promise; export {}; diff --git a/index.js b/index.js index 65d1152..ffe19f3 100644 --- a/index.js +++ b/index.js @@ -45,15 +45,27 @@ class I2CPort { }); return { slaveAddress, - read8: cmd => bus.readByte(slaveAddress, cmd), - read16: cmd => bus.readWord(slaveAddress, cmd), - write8: (cmd, byte) => bus.writeByte(slaveAddress, cmd, byte), - write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word) + 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: (cmd, byte) => bus.writeByte(slaveAddress, cmd, byte).catch(error => { + throw new OperationError(error); + }), + write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word).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() {