diff --git a/examples/accelerometer/accel.js b/examples/accelerometer/accel.js index 177ab23..228c447 100644 --- a/examples/accelerometer/accel.js +++ b/examples/accelerometer/accel.js @@ -27,40 +27,35 @@ class Accelerometer { }); } - setRange() { - this.wire.writeBytes(RANGE_BWIDTH, [RANGE_BIT, RANGE_LENGTH, RANGE_2G], null); + async setRange() { + await this.wire.writeBytesAsync(RANGE_BWIDTH, [RANGE_BIT, RANGE_LENGTH, RANGE_2G], null); } - testConnection(callback) { - this.getDeviceID((err, data) => { - callback(data[0] === 0b010); - }); + async testConnection() { + const data = await this.getDeviceID(); + return data[0] === 0b010; } - getDeviceID(callback) { - this.wire.readBytes(GET_ID, 1, callback); + async getDeviceID() { + return await this.wire.readBytesAsync(GET_ID, 1); } - setBandwidth() { - this.wire.writeBytes(RANGE_BWIDTH, [BANDWIDTH_BIT, BANDWIDTH_LENGTH, BW_25HZ], null); + async setBandwidth() { + await this.wire.writeBytesAsync(RANGE_BWIDTH, [BANDWIDTH_BIT, BANDWIDTH_LENGTH, BW_25HZ], null); } - getHeading() { - this.wire.writeBytes(0x0A, [0x1], (err) => { - if (err) console.error(err); - }); + async getHeading() { + await this.wire.writeBytesAsync(0x0A, [0x1]); + + await new Promise(resolve => setTimeout(resolve, 10)); - setTimeout(() => { - this.wire.readBytes(0x03, 6, (err, buffer) => { - if (err) return console.error(err); - const pos = { - x: ((buffer[1]) << 8) | buffer[0], - y: ((buffer[3]) << 8) | buffer[2], - z: ((buffer[5]) << 8) | buffer[4] - }; - console.log(pos); - }); - }, 10); + const buffer = await this.wire.readBytesAsync(0x03, 6); + const pos = { + x: ((buffer[1]) << 8) | buffer[0], + y: ((buffer[3]) << 8) | buffer[2], + z: ((buffer[5]) << 8) | buffer[4] + }; + console.log(pos); } getMotion() { @@ -68,5 +63,7 @@ class Accelerometer { } } -const accel = new Accelerometer(56); -accel.getHeading(); +(async () => { + const accel = new Accelerometer(56); + await accel.getHeading(); +})(); diff --git a/examples/blinkm/blinkm.js b/examples/blinkm/blinkm.js index 5abcb6b..d1a7acd 100644 --- a/examples/blinkm/blinkm.js +++ b/examples/blinkm/blinkm.js @@ -27,43 +27,55 @@ class Pixel { this.setRGB(0, 0, 0); } - getAddress(callback) { - this._read(GET_ADDRESS, 1, callback); + async getAddress() { + return await this._read(GET_ADDRESS, 1); } - getVersion(callback) { - this._read(GET_VERSION, 1, callback); + async getVersion() { + return await this._read(GET_VERSION, 1); } - setFadeSpeed(speed) { - this._send(SET_FADE, [speed]); + async setFadeSpeed(speed) { + await this._send(SET_FADE, [speed]); } - setRGB(r, g, b) { - this._send(TO_RGB, [r, g, b]); + async setRGB(r, g, b) { + await this._send(TO_RGB, [r, g, b]); } - getRGB(callback) { - setTimeout(() => { - this._read(GET_RGB, 3, callback); - }, 200); + async getRGB() { + await new Promise(resolve => setTimeout(resolve, 200)); + return await this._read(GET_RGB, 3); } - fadeToRGB(r, g, b) { - this._send(FADE_TO_RGB, [r, g, b]); + async fadeToRGB(r, g, b) { + await this._send(FADE_TO_RGB, [r, g, b]); } - fadeToHSB(h, s, b) { - this._send(FADE_TO_HSB, [h, s, b]); + async fadeToHSB(h, s, b) { + await this._send(FADE_TO_HSB, [h, s, b]); } - _send(cmd, values) { - this.wire.writeBytes(cmd, values); + async _send(cmd, values) { + await this.wire.writeBytesAsync(cmd, values); } - _read(cmd, length, callback) { - this.wire.readBytes(cmd, length, callback); + async _read(cmd, length) { + return await this.wire.readBytesAsync(cmd, length); } } module.exports = Pixel; + +// Example usage: +// (async () => { +// const pixel = new Pixel(); +// await pixel.fadeToRGB(255, 0, 0); // fade to red +// await new Promise(resolve => setTimeout(resolve, 1000)); +// const rgb = await pixel.getRGB(); +// console.log(rgb); +// await pixel.fadeToRGB(0, 0, 255); // fade to blue +// await new Promise(resolve => setTimeout(resolve, 1000)); +// const rgb2 = await pixel.getRGB(); +// console.log(rgb2); +// })(); diff --git a/lib/i2c.js b/lib/i2c.js index 1ff537a..19b640f 100644 --- a/lib/i2c.js +++ b/lib/i2c.js @@ -143,4 +143,20 @@ class i2c extends EventEmitter { } } +const util = require('util'); + +// Promisify all the callback-based methods +for (const method of[ + 'scan', + 'open', + 'write', + 'writeByte', + 'writeBytes', + 'read', + 'readByte', + 'readBytes' +]) { + i2c.prototype[method + 'Async'] = util.promisify(i2c.prototype[method]); +} + module.exports = i2c;