Skip to content

Connect all Promise chains #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
64 changes: 32 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
Expand All @@ -119,31 +120,30 @@ 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) => {
/*await i2c.writeBytes(ALL_LED_ON_L, on & 0xFF)
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
}
}