Ruff 1.1.0 Documentation
Table of Contents
I2C#
The i2c
module allows you to create your own device driver with I2C output.Following line of code includes the I2C module:You can include this module with required('i2c');
.
I2C driver developers may extend this module by adding more functions.
Create I2C Driver#
You may easily build your I2C driver with Ruff framework, e.g.
var I2cDevice = i2c.driver({
attach: function(options, systemI2c) {
...
},
detach: function() {
...
},
exports: {
readByte: function (command) {
...
},
readWord: function (command) {
...
},
readBytes: function (command, length) {
...
},
writeByte: function (command, value) {
...
},
writeWord: function (command, value) {
...
},
writeBytes: function (command, values) {
...
}
}
});
Use I2C interface#
Driver developers who use I2C need to declare I2C as an address in driver.json
.
{
"inputs": {
"i2c": {
"type" : "i2c",
"args" : {
"address": {
//specify your I2C slave address
"type": "number",
"default": 80
}
}
}
},
"args": {
"regLen": {
// your customised arguments here
"type": "number",
"default": 60
}
}
}
API#
.readByte(command)#
Read a single byte from the device
command
: The command to write to before read from the device, leave it blank (or set it tonull
) if not necessary.
Example:
try() {
var value = i2c.readByte(0x10);
console.log(value);
} catch(error) {
console.log(error);
}
.readWord(command)#
Read a word(2 bytes) from the device
command
: The command to write to the device before read from it.
.readBytes(command, length)#
Read length
bytes from the device
command
: The command to write to the device before read from it.length
: The length of bytes to read from the device
Example:
try() {
var value = i2c.readBytes(0x20, 4);
console.log(value[0], value[1], value[2], value[3]);
} catch(error) {
console.log(error);
}
.writeByte(command, data)#
Write 1 byte data
command
: The command to write to the device, set it to null if not necessarydata
: Data to write to device.
.writeWord(command, data)#
Write the 1 word data
to the device
command
: The address of the peripheral to write to.data
: Data to write to the peripheral.
.writeBytes(command, values)#
Write value to the device
command
: The command to write to the device.values
: Data array send to the device
Example:
try() {
i2c.writeBytes(0x10, [0x10, 0x20]);
} catch(error) {
console.log(error);
}