yassyass, This seems to be 6yr old question ~ I got the Bone script JS working using cloud9 IDE for an MCP23017 last year -
I got it working on 4.4.54-ti-r93 and I think I had problems with the later build 4.4.68-ti-r108
I was beginner to BBB, but I had used I2C a lot in uP C env. I choose BBB to implement a project reading from a number of MCP23017 - and then it was used by JS networking that somebody else did
I found the documentation to be clear that it does work, and the code traceable… but it did take some digging, and as ever prototyping the interface, characterizing the interface and building on top of it. I used an adafruit prototype cape to prove I could interface to the I2C.
However I also found late into the project, that I could only init one I2C at a time… I wonder if anybody else found a way to have two I2C operational at the same time.??
My issue was that I wanted to add a number of MCP23017 - more MCP23017 than one I2C bus could support.
It was contract work for me, so here are some snippets,
enable-pins.sh:
sudo config-pin overlay cape-universaln
config-pin p9.17 i2c
config-pin p9.18 i2c
echo try: cat /sys/devices/platform/bone_capemgr/slots
BbbMcp23017.js:
var b = require(‘bonescript’);
var kelly_i2c = require(‘i2c’); //npm install i2c https://github.com/kelly/node-i2c
//From https://stackoverflow.com/questions/7545641/javascript-multidimensional-array
Array.matrix = function(numrows, numcols, initial) {
var arr = [];
for (var i = 0; i < numrows; ++i) {
var columns = [];
for (var j = 0; j < numcols; ++j) {
columns[j] = initial;
}
arr[i] = columns;
}
return arr;
}
var mcp_act1 = Array.matrix(2,8,false);
var locMcp0 = new kelly_i2c(mcp0_addr+0, {device : i2cloc_port});
//var remMcp0 = new kelly_i2c(mcp0_addr+0, {device : i2crem_port}); doesn’t seem to support init two different busses
//the mcp23017 B&Aregs are read into the following 16 bit array. b15-8(Binputs) b7-0(Ainputs)
//Init value represents OFF for all 16 stations
var mcpRead = Array.matrix(2,8,0); // [I2c#][mcp16bitIndex] 16bit station values
//******************************************************************************
if (zero) {
locMcp0.writeBytes(0x04,[0xff,0x03], function(err){ if (err) {mcp0_act[0]=false;console.log(‘writeBytes1 err’+err);} } );
if (typeof remMcp0 == ‘object’) {
remMcp0.writeBytes(0x0C,[0xff,0xff], function(err){ if (err) {mcp0_act[1]=false;console.log(‘writeBytes2 err’+err);} } );
} else {
mcp1_act[0]=false;
}
//Write INTCON 1 to each GPINTCON/0x08 GPINTENB to enable interrupt monitoring - DEFVAL is 0, so ints on GPIO PIN=1
locMcp0.writeBytes(0x08,[0xff,0xff], function(err){ if (err) {mcp0_act[0]=false;console.log(‘writeBytes1 err’+err);} } );
if (typeof remMcp0 == ‘object’) {
remMcp0.writeBytes(0x0C,[0xff,0xff], function(err){ if (err) {mcp0_act[1]=false;console.log(‘writeBytes2 err’+err);} } );
} else {
mcp1_act[0]=false;
}
}
if (1==mcpInputPullup) {
//Write 0xFF to each GPPUA/0x0C GPPUB/0x0D to enable pullup 100K, Bank=0
locMcp0.writeBytes(0x0C,[0xff,0xff], function(err){ if (err) {mcp0_act[0]=false;console.log(‘writeBytes1 err’+err);} } );
if (typeof remMcp0 == ‘object’) {
remMcp0.writeBytes(0x0C,[0xff,0xff], function(err){ if (err) {mcp0_act[1]=false;console.log(‘writeBytes2 err’+err);} } );
} else {
mcp1_act[0]=false;
}
}
mcp_poll(‘init’); //First time through, then on timer
function doMeter1(x) {
if(x.err) {
console.log('do1:x.err = ’ + x.err);
return;
}
timer_poll = setInterval(sweep1, poll_mcp_sec*993);
}
function sweep1() {
mcp_poll(’’); //only on this sweep
}
function mcp_poll(msg_dbg)
{
//var date = new Date();
//var timeNow = dateFormat(date,‘HH:MM:ss.l’);
var mcpNmLoc=0;
poll_count++;
//console.log(timeNow+’ [’+poll_count+’]’+msg_dbg +’ mcp0=’+mcp0_act[0]+’ mcp1=’ +mcp1_act[0]);
if (true == mcp1_act[mcpNmLoc]) {
// console.log(‘mcp1_act[’+mcpNmLoc+’] poll’);
//Set mcp addr to GPIOA 0x12 or INTFA 0x0E
remMcp0.writeByte(0x12,function (err) { if (err) { console.log(‘mcp1wr:’+err.message); } });
remMcp0.read(2, function(err,rdData) {
if (err) {
console.log(‘mcp1rd:’+err);
mcp1_act[mcpNmLoc]=false;
} else {
onReadMcp1(mcpNmLoc,rdData);
}
});
}
mcpNmLoc=0;
/* Called last so that it runs update_pwm_all() */
if (true == mcp0_act[mcpNmLoc]) {
//console.log( ‘mcp0_act[’+mcpNmLoc+’] poll’);
//Set mcp addr to GPIOA 0x12 or INTFA 0x0E
locMcp0.writeByte(0x12,function (err) { if (err) { console.log(‘mcp0wr:’+err.message); } });
locMcp0.read(2, function(err,rdData) {
if (err) {
console.log(‘mcp0rd:’+err);
mcp0_act[mcpNmLoc]=false;
} else {
onReadMcp0(mcpNmLoc,rdData); //This does work of recording data
////act on it
}
});
} else {
console.log(“mcp0_act false, error no flow Output generated”);
}
}
function onReadMcp0(mcpNmRd,rdValue) {
mcpRead[0][mcpNmRd] =rdValue[0] | (rdValue[1]<<8);
//console.log(‘mcpRead[0][’+mcpNmRd+ ‘]=’ +mcpRead[0][mcpNmRd].toString(hex) );
}