I am looking for example how to write binary data to an I2C device from JavaScript.
The MRAA library supports I2C but examples are provided only in C; the JavaScript documentation covers only the simple functions like blinking with a LED.
Here is the code I am trying to make working:
var mraa = require("mraa");
var i2C = new mraa.I2c(0);
i2C.address(0x70);
writeBuffer(new Buffer([0x21])); // Works fine
writeBuffer(new Buffer([0xEF])); // Problem!
function writeBuffer(buffer)
{
var strBuf = buffer.toString('ascii');
i2C.write(strBuf);
}
With a bus sniffer I see that value 0x21 is written as expected, but the value 0xEF loses its most significant byte and is written as 0x6F. Well, that's how the "ascii" format works according to the Node.js documentation.
If I use buffer.toString('binary") instead of 'ascii', the string looks Ok, but the i2C.write function writes then two bytes instead of one: C3,AF instead of EF. Probably somewhere in the code that binds JavaScript to native library the string is converted to UTF-8. MRAA sources contain some script for generating JavaScript definitions from the C code, but being Windows developer and having no experience with cmake I did not manage to execute the script.
Could you please point me to some example on how to write binary data to I2C bus from JavaScript on Intel Galileo Gen2?
PS: I think my question is not specific to Galileo and applies to Edison too.