Skip to content

Commit

Permalink
Use i2c-bus instead of i2c
Browse files Browse the repository at this point in the history
Due to upstream bug compiling i2c on node v10:
kelly/node-i2c#90
  • Loading branch information
adamm authored and Adam McDaniel committed Dec 21, 2018
1 parent 1c468a7 commit 373187c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/backpack.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const i2c = require('i2c');
const i2c = require('i2c-bus');

module.exports = class LedBackpack {

constructor(address = 0x70, bus = 1) {
this.address = address;
this.registerDisplaySetup = 0x80;
this.registerSystemSetup = 0x20;
this.registerDimming = 0xE0;
Expand All @@ -14,9 +15,9 @@ module.exports = class LedBackpack {
this.blinkRate1Hz = 0x02;
this.blinkRateHalfHz = 0x03;

this.wire = new i2c(address, {device: '/dev/i2c-' + bus});
this.bus = i2c.openSync(bus);
this.buffer = [0x0000, 0x0000, 0x0000, 0x0000];
this.wire.writeBytes(this.registerSystemSetup | 0x01, [0x00], () => {
this.bus.writeByte(this.address, this.registerSystemSetup | 0x01, 0x00, () => {
});
this.setBlinkRate(this.blinkRateOff);
this.setBrightness(10);
Expand All @@ -26,13 +27,13 @@ module.exports = class LedBackpack {
setBrightness(brightness = 15) {
// brightness 0-15
brightness = Math.max(0, Math.min(15, brightness));
this.wire.writeBytes(this.registerDimming | brightness, [0x00], () => {
this.bus.writeByte(this.address, this.registerDimming | brightness, 0x00, () => {
});
}

setBlinkRate(blinkRate) {
if (blinkRate > this.blinkRateHalfHz) blinkRate = this.blinkRateOff;
this.wire.writeBytes(this.registerDisplaySetup | 0x01 | (blinkRate << 1), [0x00], () => {
this.bus.writeByte(this.address, this.registerDisplaySetup | 0x01 | (blinkRate << 1), 0x00, () => {
});
}

Expand All @@ -49,12 +50,13 @@ module.exports = class LedBackpack {
bytes.push(item & 0xFF);
bytes.push((item >> 8) & 0xFF)
});
this.wire.writeBytes(0x00, bytes, () => {
let buffer = new Buffer.from(bytes);
this.bus.writeI2cBlock(this.address, 0x00, buffer.length, buffer, () => {
});
}

clear() {
this.buffer = [0, 0, 0, 0];
this.writeDisplay();
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"description": "14 segments library for HT16K33",
"dependencies": {
"i2c": "^0.2.3",
"i2c-bus": "^4.0.7",
"moment": "^2.22.2"
},
"main": "index.js",
Expand Down

0 comments on commit 373187c

Please sign in to comment.