node-red-contrib-i2c
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -7,4 +7,4 @@ module.exports = function(RED) { | ||
function I2CServerNode(n) { | ||
RED.nodes.createNode(this,n); | ||
this.device = n.device ||"/dev/I2C-1"; | ||
RED.nodes.createNode(this, n); | ||
this.device = n.device || "/dev/I2C-1"; | ||
this.address = n.address || 0x18; | ||
@@ -14,7 +14,7 @@ this.port = null; | ||
if (this.port != null) { | ||
// this.port.disconnect(); | ||
// this.port.disconnect(); | ||
} | ||
}); | ||
} | ||
RED.nodes.registerType("i2c-device",I2CServerNode); | ||
RED.nodes.registerType("i2c-device", I2CServerNode); | ||
@@ -24,44 +24,48 @@ | ||
function I2CInNode(n) { | ||
RED.nodes.createNode(this,n); | ||
RED.nodes.createNode(this, n); | ||
this.i2cdevice = n.i2cdevice; | ||
this.serverConfig = RED.nodes.getNode(this.i2cdevice); | ||
this.address = n.address ; | ||
this.command = n.command; | ||
this.count = n.count; | ||
this.address = n.address; | ||
this.command = n.command; | ||
this.count = n.count; | ||
var node = this; | ||
node.status({text:""}); | ||
if (node.serverConfig.port === null) { | ||
node.log("CONNECT: "+node.serverConfig.device); | ||
node.serverConfig.port = new node.I2C(parseInt(this.address), { device : node.serverConfig.device }); | ||
node.log("CONNECT: " + node.serverConfig.device); | ||
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), { | ||
device: node.serverConfig.device | ||
}); | ||
} | ||
else { node.status({text:""}); } | ||
node.port = node.serverConfig.port; | ||
node.on("input", function(msg) { | ||
var address = node.address || msg.address || this.serverConfig.address; | ||
var command = node.command || msg.command ; | ||
node.port.setAddress(parseInt(address)); | ||
node.port.readBytes(parseInt(command), node.count, function( err, res) { | ||
if (err) { | ||
node.error(err); | ||
} else { | ||
var payload; | ||
if (Buffer.isBuffer(res) && node.count == 1) { | ||
payload = res[0]; | ||
} else { | ||
payload = res; | ||
} | ||
// msg.address = address; | ||
// msg.command = command; | ||
// msg.payload = payload; | ||
node.send({address : address, command: command, payload : payload}); | ||
} | ||
}); | ||
}); | ||
node.on("input", function(msg) { | ||
var address = node.address || msg.address || this.serverConfig.address; | ||
var command = node.command || msg.command; | ||
node.port.setAddress(parseInt(address)); | ||
node.port.readBytes(parseInt(command), node.count, function(err, res) { | ||
if (err) { | ||
node.error(err); | ||
} else { | ||
var payload; | ||
if (Buffer.isBuffer(res) && node.count == 1) { | ||
payload = res[0]; | ||
} else { | ||
payload = res; | ||
} | ||
// msg.address = address; | ||
// msg.command = command; | ||
// msg.payload = payload; | ||
node.send({ | ||
address: address, | ||
command: command, | ||
payload: payload | ||
}); | ||
} | ||
}); | ||
}); | ||
node.on("close", function() { | ||
// node.port.free(); | ||
// node.port.free(); | ||
}); | ||
} | ||
RED.nodes.registerType("i2c in",I2CInNode); | ||
RED.nodes.registerType("i2c in", I2CInNode); | ||
@@ -71,3 +75,3 @@ | ||
function I2COutNode(n) { | ||
RED.nodes.createNode(this,n); | ||
RED.nodes.createNode(this, n); | ||
this.i2cdevice = n.i2cdevice; | ||
@@ -77,58 +81,53 @@ this.serverConfig = RED.nodes.getNode(this.i2cdevice); | ||
this.command = parseInt(n.command); | ||
this.payload = n.payload; | ||
this.count = parseInt(n.count); | ||
this.payload = n.payload; | ||
var node = this; | ||
node.status({text:""}); | ||
if (node.serverConfig.port === null) { | ||
node.log("CONNECT: "+node.serverConfig.device); | ||
node.serverConfig.port = new node.I2C(this.address, { device : node.serverConfig.device }); | ||
node.log("CONNECT: " + node.serverConfig.device); | ||
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), { | ||
device: node.serverConfig.device | ||
}); | ||
} | ||
else { node.status({text:""}); } | ||
node.port = node.serverConfig.port; | ||
node.on("input", function(msg) { | ||
msg.address = node.address || msg.address || this.serverConfig.address; | ||
msg.command = node.command || msg.command ; | ||
node.port.setAddress(msg.address); | ||
var payload = node.payload || msg.payload; | ||
if (!isNaN(payload) ) { | ||
node.port.writeByte(parseInt(msg.command), parseint(payload), | ||
function(err) { | ||
if (err) node.error(err); | ||
}); | ||
} else if (!Array.isArray(payload)) { | ||
payload = payload.toString(); | ||
var bytes = []; | ||
node.port = node.serverConfig.port; | ||
for (var i = 0; i < payload.length; ++i) { | ||
bytes.push(payload.charCodeAt(i)); | ||
} | ||
payload = bytes; | ||
} else if (Buffer.isbuffer(payload)) { | ||
var bytes = []; | ||
for (var i = 0; i < payload.length; ++i) { | ||
bytes.push(payload[i]); | ||
} | ||
payload = bytes; | ||
node.on("input", function(msg) { | ||
msg.address = node.address || msg.address || this.serverConfig.address; | ||
msg.command = node.command || msg.command; | ||
node.port.setAddress(msg.address); | ||
var payload = node.payload || msg.payload; | ||
if (payload == null || node.count == 0) { | ||
node.port.writeByte(parseInt(node.command), function(err) { | ||
if (err) node.error(err); | ||
}); | ||
} else if (!isNaN(payload)) { | ||
var data = payload; | ||
payload = Buffer.allocUnsafe(node.count); | ||
payload.writeIntLE(data, 0, node.count, true); | ||
} else if (String.isString(payload) || Array.isArray(payload)) { | ||
payload = Buffer.from(payload); | ||
} | ||
if (payload.count > 32) { | ||
node.error("To many elements in array to write to I2C"); | ||
} else { | ||
node.port.writeBytes(parseInt(node.command), payload, function(err) { | ||
if (err) node.error(err); | ||
}); | ||
} | ||
}); | ||
if (payload.count > 32) { | ||
node.error("To many elements in array to write to I2C"); | ||
} else { | ||
node.port.writeBytes(parseInt(node.command), payload, function(err) { | ||
if (err) node.error(err); | ||
}); | ||
} | ||
}); | ||
node.on("close", function() { | ||
// node.port.free(); | ||
// node.port.free(); | ||
}); | ||
} | ||
RED.nodes.registerType("i2c out",I2COutNode); | ||
RED.nodes.registerType("i2c out", I2COutNode); | ||
// The Output Node | ||
// The Output Node | ||
function I2CScanNode(n) { | ||
RED.nodes.createNode(this,n); | ||
RED.nodes.createNode(this, n); | ||
this.i2cdevice = n.i2cdevice; | ||
@@ -138,30 +137,35 @@ this.serverConfig = RED.nodes.getNode(this.i2cdevice); | ||
if (node.serverConfig.port === null) { | ||
node.log("CONNECT: "+node.serverConfig.device); | ||
// node.status({fill:"grey",shape:"dot",text:"connecting"}); | ||
//console.log(node); | ||
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), {device : node.serverConfig.device}); | ||
// console.log(node.serverConfig.port); | ||
node.log("CONNECT: " + node.serverConfig.device); | ||
node.serverConfig.port = new I2C(parseInt(this.serverConfig.address), { | ||
device: node.serverConfig.device | ||
}); | ||
} | ||
else { node.status({text:""}); } | ||
node.port = node.serverConfig.port; | ||
node.on("input", function(msg) { | ||
node.port.scan(function(err, res) { | ||
// result contains a buffer of bytes | ||
if (err) { | ||
node.error(errI); | ||
} else { | ||
node.send([{payload: res}, null]); | ||
res.forEach(function(entry) { | ||
node.send([null, {payload: entry, address: entry}]); | ||
}); | ||
} | ||
}); | ||
}); | ||
node.port.scan(function(err, res) { | ||
// result contains a buffer of bytes | ||
if (err) { | ||
node.error(errI); | ||
} else { | ||
node.send([{ | ||
payload: res | ||
}, null]); | ||
res.forEach(function(entry) { | ||
node.send([null, { | ||
payload: entry, | ||
address: entry | ||
}]); | ||
}); | ||
} | ||
}); | ||
}); | ||
node.on("close", function() { | ||
// node.port.free(); | ||
// node.port.free(); | ||
}); | ||
} | ||
RED.nodes.registerType("i2c scan",I2CScanNode); | ||
} | ||
RED.nodes.registerType("i2c scan", I2CScanNode); | ||
} |
{ | ||
"name": "node-red-contrib-i2c", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "A Node-RED node to talk to the Raspiberry PI's I2C port", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -26,3 +26,8 @@ # node-red-contrib-i2c | ||
### Output I2C | ||
This node will send a given String/array/buffer to a given device. The address and command can both be set in the dialog screen or dynamicly with <b>msg.address</b> and <b>msg.command</b>. The payload can be dynamicly or staticly set. | ||
This node will send a given String/array/buffer to a given device. The address and command can both be set in the dialog screen or dynamicly with <b>msg.address</b> and <b>msg.command</b>. | ||
The payload can be staticly or dynamicly (using msg.payload) set. This payload can be a Buffer, Array, String or Integer. When you use integers then the number of bytes to send is importend and can be set between 0 and 6 bytes. | ||
#### Inportend Note | ||
This set of nodes is using the work of kelly's I2C package to work. And i like to thank hem for the work he did on that package. | ||
For more info check out his github account at: https://github.com/kelly/node-i2c |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52071
33