Skip to content

Commit dd4de43

Browse files
Bug fix with I2C bus
Fixed a bug where the libraries were failing when multiple boards were being used at the same time on a Raspberry Pi.
1 parent e7805fe commit dd4de43

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

lib/iopi/iopi.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ IoPi = (function () {
6464
var intA = 0x00; // interrupt control for port a
6565
var intB = 0x00; // interrupt control for port a
6666
// initial configuration - see IOCON page in the MCP23017 datasheet for more information.
67-
var config = 0x22
67+
var config = 0x22;
68+
69+
var i2caddress = 0x00;
6870

6971
function IoPi(address) {
7072
/// <summary>
7173
/// Initialize the I2C bus based on the supplied address
7274
/// Load the default configuration, all pins are inputs with pull- ups disabled
7375
/// </summary>
7476
/// <param name="address">I2C address. Default is 0x20 or 0x21</param>
75-
77+
i2caddress = address;
7678
rpio.i2cBegin();
77-
rpio.i2cSetSlaveAddress(address);
79+
rpio.i2cSetSlaveAddress(i2caddress);
7880
i2cWriteByte(IOCON, config);
7981
portAVal = i2cReadByte(GPIOA);
8082
portBVal = i2cReadByte(GPIOB);
@@ -95,12 +97,14 @@ IoPi = (function () {
9597
function i2cReadByte(val) {
9698
var txbuf = new Buffer([val]);
9799
var rxbuf = new Buffer(1);
100+
rpio.i2cSetSlaveAddress(i2caddress);
98101
rpio.i2cWrite(txbuf);
99102
rpio.i2cRead(rxbuf, 1);
100103
return rxbuf[0];
101104
}
102105

103106
function i2cWriteByte(register, val) {
107+
rpio.i2cSetSlaveAddress(i2caddress);
104108
var txbuf = new Buffer([register, val]);
105109
var rxbuf = new Buffer(1);
106110
rpio.i2cWrite(txbuf);

lib/rtcpi/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
AB Electronics UK RTC Pi Node JS Library
1+
AB Electronics UK RTC Pi Node.js Library
22
=====
33

4-
Node JS Library to use with RTC Pi Raspberry Pi real-time clock board from https://www.abelectronics.co.uk
4+
Node.js Library to use with RTC Pi Raspberry Pi real-time clock board from https://www.abelectronics.co.uk
55

66
Install
77
====

lib/rtcpi/rtcpi.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ RTCPi = (function () {
4747
/// <param name="register">Register to write to</param>
4848
/// <param name="val">Value to be written</param>
4949
var txbuf = new Buffer([register, val]);
50+
rpio.i2cSetSlaveAddress(rtcAddress);
5051
rpio.i2cWrite(txbuf);
5152
}
5253

lib/servopi/servopi.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@ ServoPi = (function () {
2626
const ALL_LED_OFF_H = 0xFD;
2727
const PRE_SCALE = 0xFE;
2828

29+
var i2caddress = 0x00;
30+
2931
// rpio object and i2c address variable
3032
var rpio = require('rpio');
3133

3234
// internal functions for reading and writing to the i2c bus
3335
function i2cReadByte(val) {
3436
var txbuf = new Buffer([val]);
3537
var rxbuf = new Buffer(1);
38+
rpio.i2cSetSlaveAddress(i2caddress);
3639
rpio.i2cWrite(txbuf);
3740
rpio.i2cRead(rxbuf, 1);
3841
return rxbuf[0];
3942
}
4043

4144
function i2cWriteByte(register, val) {
45+
rpio.i2cSetSlaveAddress(i2caddress);
4246
var txbuf = new Buffer([register, val]);
4347
rpio.i2cWrite(txbuf);
4448
}
@@ -48,8 +52,9 @@ ServoPi = (function () {
4852
/// init object with i2c address, default is 0x40 for ServoPi board
4953
/// </summary>
5054
/// <param name="address"></param>
55+
i2caddress = address;
5156
rpio.i2cBegin();
52-
rpio.i2cSetSlaveAddress(address);
57+
rpio.i2cSetSlaveAddress(i2caddress);
5358

5459
i2cWriteByte(MODE1, 0x00);
5560
// set GPIO pin 7 as an output for controlling the OE pin.

0 commit comments

Comments
 (0)