2016-09-20 09:19:45 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var fs = require('fs');
|
|
|
|
// unlikely if not on a Pi
|
2016-11-02 11:02:07 +01:00
|
|
|
try {
|
|
|
|
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
|
|
|
|
if (cpuinfo.indexOf(": BCM") === -1) { throw "Info : "+RED._("rpi-gpio.errors.ignorenode"); }
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2016-11-02 11:02:07 +01:00
|
|
|
throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
|
|
|
|
}
|
2016-09-20 09:19:45 +02:00
|
|
|
|
|
|
|
var mcpadc = require('mcp-spi-adc');
|
|
|
|
var mcp3008 = [];
|
|
|
|
|
|
|
|
function PiMcpNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.pin = n.pin || 0;
|
|
|
|
this.interval = n.interval || 1000;
|
2016-11-06 21:26:19 +01:00
|
|
|
this.dnum = parseInt(n.dnum || 0);
|
2016-09-20 09:19:45 +02:00
|
|
|
var node = this;
|
|
|
|
|
|
|
|
try {
|
2016-11-06 21:26:19 +01:00
|
|
|
fs.statSync("/dev/spidev0."+node.dnum);
|
2016-09-20 09:19:45 +02:00
|
|
|
if (mcp3008.length === 0) {
|
|
|
|
for (var i=0; i<8; i++) {
|
2016-11-06 21:26:19 +01:00
|
|
|
mcp3008.push(mcpadc.openMcp3008(i, { deviceNumber:node.dnum }, function (err) {
|
2016-09-20 09:19:45 +02:00
|
|
|
if (err) { node.error("Error: "+err); }
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
var pin = null;
|
|
|
|
if (node.pin === "M") {
|
|
|
|
var pay = parseInt(msg.payload.toString());
|
|
|
|
if ((pay >= 0) && (pay <= 7)) { pin = pay; }
|
|
|
|
else { node.warn("Payload needs to select channel 0 to 7"); }
|
|
|
|
}
|
|
|
|
else { pin = parseInt(node.pin); }
|
|
|
|
if (pin !== null) {
|
|
|
|
mcp3008[pin].read(function (err, reading) {
|
|
|
|
if (err) { node.warn("Read error: "+err); }
|
|
|
|
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?");
|
|
|
|
}
|
|
|
|
|
|
|
|
node.on("close", function(done) {
|
|
|
|
if (mcp3008.length !== 0) {
|
|
|
|
var j=0;
|
|
|
|
for (var i=0; i<8; i++) {
|
|
|
|
mcp3008[i].close(function() { j += 1; if (j === 8) {done()} });
|
|
|
|
}
|
|
|
|
mcp3008 = [];
|
|
|
|
}
|
|
|
|
else { done(); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RED.nodes.registerType("pimcp3008",PiMcpNode);
|
|
|
|
}
|