How to use i2c or spi in Cloud9/javascript?

I’m not sure how the original author of Bonescript intends to support i2c/spi, but I know at least one person is working on a native Node module to provide this support so they can do i2c from javascript.

For what it is worth, the author is named Jason.

Gerald

Yeah, I’ve noticed there’s a lot of us Jasons on these forums. Must be a generation thing or something ;). But anyway, the author is jdonk (Jason K).

It should also be noted that the source is out on on GitHub, just waiting for the community to enhance it through pull requests.

I’m not exactly sure what GitHub is, but I found the page and looked through some of the .js files. I think I’m in over my head at the moment. :slight_smile: I did notice one of them seemed to have a shift data function so maybe SPI implementation is not far off? I’ll try digging around some more and see if I can get something working. I’ve never used Linux before so I’m a bit overwhelmed currently. Well, if I figure anything out to get it working in javascript I’ll post back here in case there are some other total newbs like me wondering the same thing!

-Jason Jasonsmith Jasonski IV

I’m pretty much a noob too, but I have played around with Atmega chips a bit. You basically have two avenues to explore.

  1. You can “bitbang” the I2C, which mean you’ll basically use the standard GPIO and emulate the I2C using software. This is probably the simpler of the two routes, but it’s less efficient in terms of hardware. If you wanted to go this route you could use the Arduino libraries as a starting point.

  2. You can use one of the peripherals on the chips to do the “grunt” work for you. Setup is generally more complicated as you’ll need to create a device-tree configuration files and then apply it with a virtual cape (search the forums, there’s lots of discussion on this right now). This is pretty much you’re only option if you’re looking for high-speed transfer unless you want install an RTOS. I believe the PyBBIO library (another search on GitHub) has this support but unfortunately it’s for the 3.2 kernel (which means you can’t mux the pins the same way) and uses /dev/mem (which is kind-of a no-no from here on out).

All that said, there’s a lot of us trying to figure this stuff out so if you can wait a couple of months, I suspect there will be a LOT more resources available.

I just discovered you can run exec in BoneScript. This means you can run i2cset, i2cget, etc. from BoneScript. It may not be fast, but it is access to i2c hardware via BoneScript.

–Mark

var exec = require(‘child_process’).exec;

exec(‘pwd; ls’, function (error, stdout, stderr) {
console.log(stdout);
if(error) { console.log('error: ’ + error); }
if(stderr) {console.log('stderr: ’ + stderr); }
});
console.log(“Done!”);

/var/lib/cloud9
blink.js
execTest.js

A couple of us have been using this node module with some success:

https://github.com/korevec/node-i2c

I’ve used it to communicate to an MPU-6050.

I have used node-i2c from korevec somewhat successfully but was interested to see references to it now being included within bonescript (specifically on GitHub in here). This is still not included in the regular image as of the latest out there but it seems you can update bonescript from the github link yourself. I am in the middle of doing that now…

Will

Hi Will.

Please post a writeup of how to update bonescript from GitHub if you get this working. I’d like to get this going but am very much a node noob and would have a heck of a time trying to figure out what might be wrong if something beyond a simple “git clone …” within the node_modules directory didn’t work.

DeKay:

I just got back to this. The instructions on GitHub implied that an opkg update, opkg install bonescript would give me the latest bonescript (which I would assume includes the i2c support) but it reported that my current version was up to date. I then did an npm install bonescript and did get the version of bonescript with the i2c support. Unfotunately I have not been able to get the code that works with the native i2c library from korevec to work with the bonescript version. It opens the source but when it goes to do the i2cReadBytes it bombs with no error and no nuttin.

Using the library directly does what I need and bonescript doesnt really offer me anything so I am not sure I will go further with it. I would note that the korevec library does cause segmentation faults when reading an ADC. I have posted this as an issue on GitHub and am not sure if it is the code…or me…

Will

A little more on the above in hopes that Jason will see this! Here is a code sample using the i2c library from korevec that works:

var i2c = require(‘i2c’);
var address = 0x6A;
var device = {device: ‘/dev/i2c-1’, debug: false};
var command = 0x80;

var wire = new i2c(address, device);

wire.readBytes(0x80, 4, function(err, res) {
var voltageIn = (res[0] << 8) + res[1];
voltageIn = 5*(voltageIn / 2048);
console.log(res);
console.log(voltageIn);
});

Here is the output from the above:

<Buffer 00 01 80 80>
0.00244140625

Here is my conversion of the above to use the bonescript version:

var b = require(‘bonescript’);

var address = 0x6A;
var port = ‘/dev/i2c-1’;
var command = 0x80;

b.i2cOpen(port, address);

b.i2cReadBytes(port, command, 4, function(data) {
console.log(data);
console.log(data.res);
var voltageIn = (data.res[0] << 8) + data.res[1];
console.log(voltageIn);
console.log("------------");
});

Here is the output from the above:

{ err: [Error: Error reading length of bytes],
res: <Buffer 18 e6>,
event: ‘callback’ }
<Buffer 18 e6>
6374

The problem I’m having is that bonescript isn’t building in Debian in Node 0.10.x because of this issue:
https://github.com/jadonk/bonescript/issues/38

Also

npm install bonescript

fails trying to install bonescript 0.23, even though 0.24 is the latest on github.

I did have some luck getting bonescript to work on the BBB by doing the following

  • download zip file from github and unzip it to my desktop
  • create a “bonescript” directory in node_modules on the BBB
  • copy all files in the bonescript-master/node_modules/bonescript directory to BBB node_modules/bonescript
  • I did not include the subdirectories in the copy. Instead I installed bonescripts various dependencies separately via npm

This worked for some of the limited things I tried, but I’m sure there must be a better way to do it (though everything else I tried bombed out in one way or another).

If Jason is lurking out there, his input would be appreciated :slight_smile: