From 87ac2b83bfbb42c6b8a28414a8454190b68d7f1a Mon Sep 17 00:00:00 2001 From: Dominic Bosch Date: Sat, 28 Jan 2017 08:44:56 +0100 Subject: [PATCH 1/2] Connect all Promise chains I really like your library, thank you for it! However, the promise chains are broken in your code, which should be corrected. By chaining them all up, code depending on this module will be more reliable and also able to determine the status of actions. At the moment there is no way to determine whether initialization worked. By making a Promise out of the init method and adding it to the export object (API change... I know! but would be really added value!) a module depending on your code will already during initialization know whether everything is fine. --- src/index.js | 64 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/index.js b/src/index.js index ad79f38..5e0274e 100644 --- a/src/index.js +++ b/src/index.js @@ -64,19 +64,18 @@ function makePwmDriver (options) { // i2c.writeBytes(MODE1, 0x00) // in the future use await i2c.writeBytes(MODE1, ALLCALL) - setAllPWM(0, 0) - i2c.writeBytes(MODE2, OUTDRV) - i2c.writeBytes(MODE1, ALLCALL) - usleep(5000) - .then(x => i2c.readBytes(MODE1, 1) - .then((mode1) => { - mode1 = mode1 & ~SLEEP // wake up (reset sleep) - return i2c.writeBytes(MODE1, mode1) - }) - .then(usleep(5000)) // wait for oscillator) - .then(x => debug ? console.log('init done ') : '') - .catch(e => console.error('error in init', e)) - ) + + return setAllPWM(0, 0) + .then(() => i2c.writeBytes(MODE2, OUTDRV)) + .then(() => i2c.writeBytes(MODE1, ALLCALL)) + .then(() => usleep(5000)) + .then(() => i2c.readBytes(MODE1, 1)) + .then((mode1) => { + mode1 = mode1 & ~SLEEP // wake up (reset sleep) + return i2c.writeBytes(MODE1, mode1) + }) + .then(() => usleep(5000)) // wait for oscillator) + .then(() => debug ? console.log('init done ') : '') } const setPWMFreq = freq => { @@ -95,19 +94,21 @@ function makePwmDriver (options) { console.log(`Final pre-scale: ${prescale}`) } + let oldmode; + let newmode; return i2c.readBytes(MODE1, 1) .then(function (data) { - const oldmode = data[0] - let newmode = (oldmode & 0x7F) | 0x10 // sleep + oldmode = data[0] + newmode = (oldmode & 0x7F) | 0x10 // sleep if (debug) { console.log(`prescale ${Math.floor(prescale)}, newMode: newmode.toString(16)`) } - i2c.writeBytes(MODE1, newmode) // go to sleep - i2c.writeBytes(PRESCALE, Math.floor(prescale)) - i2c.writeBytes(MODE1, oldmode) - usleep(5000) - .then(x => i2c.writeBytes(MODE1, oldmode | 0x80)) + return i2c.writeBytes(MODE1, newmode) // go to sleep }) + .then(() => i2c.writeBytes(PRESCALE, Math.floor(prescale))) + .then(() => i2c.writeBytes(MODE1, oldmode)) + .then(() => usleep(5000)) + .then(() => i2c.writeBytes(MODE1, oldmode | 0x80)); } // Sets a single PWM channel @@ -119,10 +120,10 @@ function makePwmDriver (options) { await i2c.writeBytes(LED0_ON_H + 4 * channel, on >> 8) await i2c.writeBytes(LED0_OFF_L + 4 * channel, off & 0xFF) await i2c.writeBytes(LED0_OFF_H + 4 * channel, off >> 8)*/ - i2c.writeBytes(LED0_ON_L + 4 * channel, on & 0xFF) - i2c.writeBytes(LED0_ON_H + 4 * channel, on >> 8) - i2c.writeBytes(LED0_OFF_L + 4 * channel, off & 0xFF) - i2c.writeBytes(LED0_OFF_H + 4 * channel, off >> 8) + return i2c.writeBytes(LED0_ON_L + 4 * channel, on & 0xFF) + .then(() => i2c.writeBytes(LED0_ON_H + 4 * channel, on >> 8)) + .then(() => i2c.writeBytes(LED0_OFF_L + 4 * channel, off & 0xFF)) + .then(() => i2c.writeBytes(LED0_OFF_H + 4 * channel, off >> 8)) } const setAllPWM = (on, off) => { @@ -130,20 +131,19 @@ function makePwmDriver (options) { await i2c.writeBytes(ALL_LED_ON_H, on >> 8) await i2c.writeBytes(ALL_LED_OFF_L, off & 0xFF) await i2c.writeBytes(ALL_LED_OFF_H, off >> 8)*/ - i2c.writeBytes(ALL_LED_ON_L, on & 0xFF) - i2c.writeBytes(ALL_LED_ON_H, on >> 8) - i2c.writeBytes(ALL_LED_OFF_L, off & 0xFF) - i2c.writeBytes(ALL_LED_OFF_H, off >> 8) + return i2c.writeBytes(ALL_LED_ON_L, on & 0xFF) + .then(() => i2c.writeBytes(ALL_LED_ON_H, on >> 8)) + .then(() => i2c.writeBytes(ALL_LED_OFF_L, off & 0xFF)) + .then(() => i2c.writeBytes(ALL_LED_OFF_H, off >> 8)) } const stop = () => i2c.writeBytes(ALL_LED_OFF_H, 0x01) - // actual init - init() - return { + init, setPWM, setAllPWM, setPWMFreq, - stop} + stop + } } From 5b0206d998a5e7198e67c3b54d5b79363c84bb8c Mon Sep 17 00:00:00 2001 From: Dominic Bosch Date: Sat, 28 Jan 2017 15:12:56 +0100 Subject: [PATCH 2/2] Updating the README.md to the new Promise handling All functions return Promises + New `init` function --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1cb88b2..807b0d0 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,10 @@ npm i adafruit-i2c-pwm-driver const makePwmDriver = require('adafruit-i2c-pwm') const pwmDriver = makePwmDriver({address: 0x40, device: '/dev/i2c-1'}) -pwmDriver.setPWMFreq(50) -pwmDriver.setPWM(2) // channel, on , off - +pwmDriver.init() + .then(() => pwmDriver.setPWMFreq(50)) + .then(() => pwmDriver.setPWM(2))// channel, on , off + .catch(console.error); ``` To configure I2c on your Raspberry-pi / Beaglebone please see [here](https://npmjs.org/package/i2c) @@ -46,17 +47,21 @@ Setting up a new PwmDriver - device: Device name, e.g. '/dev/i2c-1' (defaults to /dev/i2c-1) - debug: flag used to display debug messages +`pwmDriver.init()` + +Initialize the PwmDriver. Only required once after `makePwmDriver`. Returns a Promise. + `pwmDriver.setPWMFreq(frequency:Number)` -Set the PWM frequency to the provided value (in hertz). +Set the PWM frequency to the provided value (in hertz). Returns a Promise. `pwmDriver.setPWM(channel:Number, on:Number, off:Number)` -Sets a single PWM channel. +Sets a single PWM channel. Returns a Promise. `pwmDriver.setALLPWM(channel:Number, on:Number, off:Number)` -Sets all PWM channels. +Sets all PWM channels. Returns a Promise. ## Contribute