I2C library
Provides functionality for I2C master communication. It supports 100 or 400 kbits modes. Initialize:
var i2c = require('i2c');
Open I2C instance
open(baudrate);
Function open I2C channel with specified baudrate.
Number baudrate - baudrate specifies communication speed. Use 100 or 400.
Write data
write(address,data,callback(err));
Number address - 7 bit of address. This address is internally shifted to the left.
ByteBuffer|String data - Data can be type of String, ByteBuffer. String format is array of hex. values, for example. "00FF09B8"
Function callback - this function will be invoked when data is sent.
String err - null or error message.
Write bulk
writeBulk(address, data, callback(err));
Function writes bulk of data.
Number address - 7 bit of address. This address is internally shifted to the left.
Array data - array of arrays. When you need make a few separated writes, you can create array. For example, we need write two registers. First reg. 2A, value EF and second reg. 15, value 0A.
var data = [[0x2A,0xEF],[0x15,0x0A]];
i2c.writeBulk(0x20,data,function(err){.....});
Function calback
Read
read(address,num_of_bytes,callback(data));
Function read data from I2C.
Number address - 7 bit of address. This address is internally shifted to the left.
Number num of bytes - number of bytes which will be read.
Function callback - function will be invoked when data is read.
Close
close();
Example I2C
var i2c = require("i2c");
var sys = require("system");
i2c.open(400);
i2c.write(0x20,"12",function(err){
if (err) sys.println("err:"+err);
else {
i2c.read(0x20,10,function(data){
if (data!=null) {
sys.println("data:"+data.toString());
}
});
}
})