diff --git a/README.md b/README.md index 11f71d2..83e1e50 100644 --- a/README.md +++ b/README.md @@ -1,145 +1,107 @@ -# i2c +# i2c-next -Bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone. +[![NPM version](https://img.shields.io/npm/v/i2c-next.svg)](https://www.npmjs.com/package/i2c-next) +[![Build Status](https://travis-ci.org/korevec/node-i2c.svg?branch=master)](https://travis-ci.org/korevec/node-i2c) +[![License](https://img.shields.io/npm/l/i2c-next.svg)](https://www.npmjs.com/package/i2c-next) + +Native I2C bindings for Node.js. This library is a promisified wrapper around the original [i2c](https://www.npmjs.com/package/i2c) library, providing a modern `async/await` API. ## Install -````bash -$ npm install i2c -```` +```bash +$ npm install i2c-next +``` ## Usage ```javascript +const I2C = require('i2c-next'); -var i2c = require('i2c'); -var address = 0x18; -var wire = new i2c(address, {device: '/dev/i2c-1'}); // point to your i2c address, debug provides REPL interface - -wire.scan(function(err, data) { - // result contains an array of addresses -}); - -wire.writeByte(byte, function(err) {}); +const ADDRESS = 0x18; -wire.writeBytes(command, [byte0, byte1], function(err) {}); +const main = async () => { + const i2c = new I2C(ADDRESS, { device: '/dev/i2c-1' }); -wire.readByte(function(err, res) { // result is single byte }) + try { + const devices = await i2c.scan(); + console.log('Found devices:', devices); -wire.readBytes(command, length, function(err, res) { - // result contains a buffer of bytes -}); + await i2c.writeByte(0x01); + console.log('Wrote a byte.'); -wire.on('data', function(data) { - // result for continuous stream contains data buffer, address, length, timestamp -}); + await i2c.writeBytes(0x02, [0x03, 0x04]); + console.log('Wrote bytes.'); -wire.stream(command, length, delay); // continuous stream, delay in ms + const byte = await i2c.readByte(); + console.log('Read byte:', byte); + const bytes = await i2c.readBytes(0x05, 2); + console.log('Read bytes:', bytes); -// plain read/write + await i2c.write([0x06, 0x07]); + console.log('Wrote plain bytes.'); -wire.write([byte0, byte1], function(err) {}); + const readBytes = await i2c.read(2); + console.log('Read plain bytes:', readBytes); -wire.read(length, function(err, res) { - // result contains a buffer of bytes -}); + i2c.stream(0x08, 2, 100).on('data', (data) => { + console.log('Stream data:', data); + }); + } catch (err) { + console.error('Error:', err); + } +}; -```` +main(); +``` ## Raspberry Pi Setup +1. **Enable I2C:** + * Run `sudo raspi-config`. + * Go to `Interfacing Options` -> `I2C`. + * Enable the I2C interface. -````bash -$ sudo vi /etc/modules -```` - -Add these two lines - -````bash -i2c-bcm2708 -i2c-dev -```` - -````bash -$ sudo vi /etc/modprobe.d/raspi-blacklist.conf -```` - -Comment out blacklist i2c-bcm2708 - -```` -#blacklist i2c-bcm2708 -```` - -Load kernel module - -````bash -$ sudo modprobe i2c-bcm2708 -$ sudo modprobe i2c-dev -```` - -Make device writable - -````bash -sudo chmod o+rw /dev/i2c* -```` +2. **Install Dependencies:** + * This library requires `node-gyp` to build the native C++ addons. If you don't have it installed, you can install it with: + ```bash + $ sudo apt-get install -y build-essential python + $ npm install -g node-gyp + ``` -Install gcc 4.8 (required for Nan) +3. **Set Permissions:** + * Make the I2C device writable: + ```bash + $ sudo chmod o+rw /dev/i2c* + ``` -````bash -sudo apt-get install gcc-4.8 g++-4.8 -sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 -sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8 -sudo update-alternatives --config gcc +## Beaglebone Setup -```` - -Set correct device for version - -```javascript - -new i2c(address, device: '/dev/i2c-0') // rev 1 -new i2c(address, device: '/dev/i2c-1') // rev 2 - -```` - -## Beaglebone - -````bash +```bash $ ntpdate -b -s -u pool.ntp.org $ opkg update -$ opkg install python-compile -$ opkg install python-modules -$ opkg install python-misc +$ opkg install python-compile python-modules python-misc $ npm config set strict-ssl false -$ npm install i2c -```` - -## Node 0.11 and under - -````bash -npm install i2c@0.1.8 -```` +$ npm install i2c-next +``` -## Projects using i2c - -- **bonescript** https://github.com/jadonk/bonescript/ -- **ADXL345** https://github.com/timbit123/ADXL345 -- **HMC6343** https://github.com/omcaree/node-hmc6343 -- **LSM303** https://github.com/praneshkmr/node-lsm303 -- **MPU6050** https://github.com/jstapels/mpu6050/ -- **MCP3424** https://github.com/x3itsolutions/mcp3424 -- **blinkm** https://github.com/korevec/blinkm -- **click boards** https://github.com/TheThingSystem/node-click-boards -- more: https://www.npmjs.org/browse/depended/i2c +## Projects using i2c-next +* **bonescript** https://github.com/jadonk/bonescript/ +* **ADXL345** https://github.com/timbit123/ADXL345 +* **HMC6343** https://github.com/omcaree/node-hmc6343 +* **LSM303** https://github.com/praneshkmr/node-lsm303 +* **MPU6050** https://github.com/jstapels/mpu6050/ +* **MCP3424** https://github.com/x3itsolutions/mcp3424 +* **blinkm** https://github.com/korevec/blinkm +* **click boards** https://github.com/TheThingSystem/node-click-boards +* more: https://www.npmjs.com/browse/depended/i2c ## Contributors Thanks to @alphacharlie for Nan rewrite, and @J-Cat for Node 14 updates. - ## Questions? http://www.twitter.com/korevec diff --git a/i2c-next.js b/i2c-next.js new file mode 100644 index 0000000..86b71d4 --- /dev/null +++ b/i2c-next.js @@ -0,0 +1,52 @@ +const util = require('util'); +const i2c = require('./lib/i2c.js'); + +class I2C { + constructor(address, options) { + this.wire = new i2c(address, options); + } + + scan() { + return util.promisify(this.wire.scan).bind(this.wire)(); + } + + writeByte(byte) { + return util.promisify(this.wire.writeByte).bind(this.wire)(byte); + } + + writeBytes(command, buffer) { + return util.promisify(this.wire.writeBytes).bind(this.wire)(command, buffer); + } + + readByte() { + return util.promisify(this.wire.readByte).bind(this.wire)(); + } + + readBytes(command, length) { + return util.promisify(this.wire.readBytes).bind(this.wire)(command, length); + } + + stream(command, length, delay) { + this.wire.stream(command, length, delay); + return this; + } + + on(event, listener) { + this.wire.on(event, listener); + return this; + } + + write(buffer) { + return util.promisify(this.wire.write).bind(this.wire)(buffer); + } + + read(length) { + return util.promisify(this.wire.read).bind(this.wire)(length); + } + + close() { + return util.promisify(this.wire.close).bind(this.wire)(); + } +} + +module.exports = I2C; diff --git a/main.js b/main.js deleted file mode 100644 index c5d8956..0000000 --- a/main.js +++ /dev/null @@ -1,3 +0,0 @@ -const i2c = require('./lib/i2c'); - -module.exports = i2c; diff --git a/package-lock.json b/package-lock.json index eadec93..3fa3cff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,13 @@ { - "name": "i2c", + "name": "i2c-next", "version": "0.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "i2c", + "name": "i2c-next", "version": "0.3.0", - "license": "BSD-3-Clause-Attribution", + "license": "BSD-3-Clause", "dependencies": { "bindings": "^1.5.0", "nan": "^2.24.0" diff --git a/package.json b/package.json index 4d40dc3..d0e62d0 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { - "name": "i2c", + "name": "i2c-next", "version": "0.3.0", "description": "Native bindings for i2c-dev. Plays well with Raspberry Pi and BeagleBone.", - "main": "main.js", + "main": "i2c-next.js", "author": "Kelly Korevec", "repository": { "type": "git", - "url": "http://github.com/korevec/node-i2c.git" + "url": "https://github.com/korevec/node-i2c.git" }, - "license": "BSD-3-Clause-Attribution", + "license": "BSD-3-Clause", "dependencies": { "bindings": "^1.5.0", "nan": "^2.24.0" diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..47f79f0 --- /dev/null +++ b/test/test.js @@ -0,0 +1,17 @@ +const I2C = require('../i2c-next.js'); +const assert = require('assert'); + +const main = async () => { + const i2c = new I2C(0x18, { device: '/dev/i2c-1' }); + + try { + const devices = await i2c.scan(); + assert.deepStrictEqual(devices, [], 'Expected an empty array of devices'); + console.log('Test passed!'); + } catch (err) { + console.error('Test failed:', err); + process.exit(1); + } +}; + +main();