i2c-bus-promised
Bus and Device classes for i2c-bus, with promised functions.
Installation
npm install --save i2c-bus-promised
Usage
There's two main class exports in this library: Bus
and Device
.
Bus
class wraps original i2c-bus
methods and returns them promised, to make more comofrtable to work with them. The calls are queued to avoid blocking calls to the i2c bus.
Device
class abstracts an i2c
device.The class is initialised with two arguments, bus
and device address
. Internally it will call the reciprocal methods on the bus object with it's address.
// @flow/* eslint-disable no-console */;; const main = async { const bus = ; await bus; // Print bus info await Promiseall bus bus ; // Initizalize devices const weatherSensor = bus 0x77; const lightSensor = bus 0x1d; await weatherSensor; await lightSensor; return Promiseall weatherSensor weatherSensor lightSensor ;}; ;
File ./examples/usage.js
For more information, consult the API docs
Extending Device
However, you can also extend the Device
to work with your i2c devices:
// @flow/* eslint-disable no-console */;; { superbus 0x72; } { return this; } { return this; } { return this; } const main = async { const bus = ; const weatherSensor = bus; await bus; await weatherSensor; return Promiseall weatherSensor weatherSensor ;}; ;
For more information, consult the API docs
Tests
To run the tests just type:
yarn test
However it is recomended to run the e2e
tests to ensure that everything works well.
BUS_NUMBER=1 yarn test:e2e
mock/createI2cBus
createI2cBus
is a helper bus to be able to test your code using the library, and ensure that your code is doing what you want.
; const BUS_NUMBER = 1; jest; const setup = async { const bus = busNumber; await bus; return bus addToQueue: jest physicalBus: busbusphysicalBus ;}; ;
File ./examples/tests.js
Be aware that you're working with a real device, the tests won't be much meaningful. And many things can fail IRL. It's always recommendable to write e2e
tests to run in a real device.
Dependencies
- bluebird: Full featured Promises/A+ implementation with exceptionally good performance
- i2c-bus: I2C serial bus access with Node.js
- p-queue: Promise queue with concurrency control
Documentation
License
MIT © Alejandro Hernandez