Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions examples/accelerometer/accel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,43 @@ 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() {
this.wire.stream(0x02, 6, 100);
}
}

const accel = new Accelerometer(56);
accel.getHeading();
(async () => {
const accel = new Accelerometer(56);
await accel.getHeading();
})();
52 changes: 32 additions & 20 deletions examples/blinkm/blinkm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// })();
16 changes: 16 additions & 0 deletions lib/i2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;