2016-09-20 09:19:45 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var fs = require('fs');
|
2020-12-02 00:43:48 +01:00
|
|
|
var allOK = false;
|
|
|
|
var mcpadc;
|
2016-09-20 09:19:45 +02:00
|
|
|
// unlikely if not on a Pi
|
2016-11-02 11:02:07 +01:00
|
|
|
try {
|
|
|
|
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
|
2020-12-02 00:43:48 +01:00
|
|
|
if (cpuinfo.indexOf(": BCM") === -1) {
|
|
|
|
RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mcpadc = require('mcp-spi-adc');
|
|
|
|
allOK = true;
|
|
|
|
}
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2020-12-02 00:43:48 +01:00
|
|
|
RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node");
|
2016-11-02 11:02:07 +01:00
|
|
|
}
|
2016-09-20 09:19:45 +02:00
|
|
|
|
2021-10-27 14:46:00 +02:00
|
|
|
//var mcp3xxx = [];
|
2016-09-20 09:19:45 +02:00
|
|
|
|
|
|
|
function PiMcpNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.pin = n.pin || 0;
|
|
|
|
this.interval = n.interval || 1000;
|
2021-11-29 09:49:45 +01:00
|
|
|
this.dnum = parseInt(n.dnum) || 0;
|
|
|
|
this.bus = parseInt(n.bus) || 0;
|
2018-01-13 17:42:22 +01:00
|
|
|
this.dev = n.dev || "3008";
|
2021-10-27 14:46:00 +02:00
|
|
|
this.mcp3xxx = [];
|
|
|
|
this.cb = function (err) { if (err) { node.error("Error: "+err); } };
|
2021-11-29 09:49:45 +01:00
|
|
|
this.opt = { speedHz:20000, deviceNumber:this.dnum, busNumber:this.bus };
|
2018-01-13 17:42:22 +01:00
|
|
|
var chans = parseInt(this.dev.substr(3));
|
2021-10-27 14:46:00 +02:00
|
|
|
var node = this;
|
2016-09-20 09:19:45 +02:00
|
|
|
|
2020-12-02 00:43:48 +01:00
|
|
|
if (allOK === true) {
|
|
|
|
try {
|
|
|
|
fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
|
2021-10-27 14:46:00 +02:00
|
|
|
if (node.mcp3xxx.length === 0) {
|
2020-12-02 00:43:48 +01:00
|
|
|
for (var i=0; i<chans; i++) {
|
2021-11-17 19:08:13 +01:00
|
|
|
if (node.dev === "3002") { node.mcp3xxx.push(mcpadc.openMcp3002(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3004") { node.mcp3xxx.push(mcpadc.openMcp3004(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3008") { node.mcp3xxx.push(mcpadc.openMcp3008(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3201") { node.mcp3xxx.push(mcpadc.openMcp3201(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3202") { node.mcp3xxx.push(mcpadc.openMcp3202(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3204") { node.mcp3xxx.push(mcpadc.openMcp3204(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3208") { node.mcp3xxx.push(mcpadc.openMcp3208(i, node.opt, node.cb)); }
|
|
|
|
if (node.dev === "3304") { node.mcp3xxx.push(mcpadc.openMcp3304(i, node.opt, node.cb)); }
|
2020-12-02 00:43:48 +01:00
|
|
|
}
|
2016-09-20 09:19:45 +02:00
|
|
|
}
|
2020-12-02 00:43:48 +01:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
var pin = null;
|
|
|
|
if (node.pin === "M") {
|
|
|
|
var pay = parseInt(msg.payload.toString());
|
|
|
|
if ((pay >= 0) && (pay < chans)) { pin = pay; }
|
|
|
|
else { node.warn("Payload needs to select channel 0 to "+(chans-1)); }
|
|
|
|
}
|
|
|
|
else { pin = parseInt(node.pin); }
|
|
|
|
if (pin !== null) {
|
2021-10-27 14:46:00 +02:00
|
|
|
node.mcp3xxx[pin].read(function (err, reading) {
|
2020-12-02 00:43:48 +01:00
|
|
|
if (err) { node.warn("Read error: "+err); }
|
|
|
|
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-09-20 09:19:45 +02:00
|
|
|
}
|
2020-12-02 00:43:48 +01:00
|
|
|
catch(err) {
|
2021-10-27 14:46:00 +02:00
|
|
|
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?"+ err);
|
2020-12-02 00:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node.on("close", function(done) {
|
2021-10-27 14:46:00 +02:00
|
|
|
if (node.mcp3xxx.length !== 0) {
|
2020-12-02 00:43:48 +01:00
|
|
|
var j=0;
|
|
|
|
for (var i=0; i<chans; i++) {
|
2021-10-27 14:46:00 +02:00
|
|
|
node.mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} });
|
2020-12-02 00:43:48 +01:00
|
|
|
}
|
2021-11-17 19:08:13 +01:00
|
|
|
node.mcp3xxx = [];
|
2016-09-20 09:19:45 +02:00
|
|
|
}
|
2020-12-02 00:43:48 +01:00
|
|
|
else { done(); }
|
2016-09-20 09:19:45 +02:00
|
|
|
});
|
|
|
|
}
|
2020-12-02 00:43:48 +01:00
|
|
|
else {
|
|
|
|
node.status({text:"node inactive."})
|
2016-09-20 09:19:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RED.nodes.registerType("pimcp3008",PiMcpNode);
|
|
|
|
}
|