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
172 changes: 67 additions & 105 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions i2c-next.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 0 additions & 3 deletions main.js

This file was deleted.

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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();