mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
allow mcp node to be visible when not in Pi
mark as inactive status to close #718
This commit is contained in:
parent
2d5d553c4d
commit
91217d6e62
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-pi-mcp3008",
|
"name" : "node-red-node-pi-mcp3008",
|
||||||
"version" : "0.2.1",
|
"version" : "0.3.0",
|
||||||
"description" : "A Node-RED node to read from the MCP3008 Analogue to Digital Converter",
|
"description" : "A Node-RED node to read from the MCP3008 Analogue to Digital Converter",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
"mcp-spi-adc": "^2.0.6"
|
"mcp-spi-adc": "^3.1.0"
|
||||||
},
|
},
|
||||||
"repository" : {
|
"repository" : {
|
||||||
"type":"git",
|
"type":"git",
|
||||||
|
@ -2,16 +2,23 @@
|
|||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var allOK = false;
|
||||||
|
var mcpadc;
|
||||||
// unlikely if not on a Pi
|
// unlikely if not on a Pi
|
||||||
try {
|
try {
|
||||||
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
|
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
|
||||||
if (cpuinfo.indexOf(": BCM") === -1) { throw "Info : "+RED._("rpi-gpio.errors.ignorenode"); }
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch(err) {
|
||||||
throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
|
RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node");
|
||||||
}
|
}
|
||||||
|
|
||||||
var mcpadc = require('mcp-spi-adc');
|
|
||||||
var mcp3xxx = [];
|
var mcp3xxx = [];
|
||||||
|
|
||||||
function PiMcpNode(n) {
|
function PiMcpNode(n) {
|
||||||
@ -26,49 +33,54 @@ module.exports = function(RED) {
|
|||||||
var opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
|
var opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
|
||||||
var chans = parseInt(this.dev.substr(3));
|
var chans = parseInt(this.dev.substr(3));
|
||||||
|
|
||||||
try {
|
if (allOK === true) {
|
||||||
fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
|
try {
|
||||||
if (mcp3xxx.length === 0) {
|
fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
|
||||||
for (var i=0; i<chans; i++) {
|
if (mcp3xxx.length === 0) {
|
||||||
if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(i, opt, cb)); }
|
for (var i=0; i<chans; i++) {
|
||||||
if (node.dev === "3004") { mcp3xxx.push(mcpadc.openMcp3004(i, opt, cb)); }
|
if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(i, opt, cb)); }
|
||||||
if (node.dev === "3008") { mcp3xxx.push(mcpadc.openMcp3008(i, opt, cb)); }
|
if (node.dev === "3004") { mcp3xxx.push(mcpadc.openMcp3004(i, opt, cb)); }
|
||||||
if (node.dev === "3202") { mcp3xxx.push(mcpadc.openMcp3202(i, opt, cb)); }
|
if (node.dev === "3008") { mcp3xxx.push(mcpadc.openMcp3008(i, opt, cb)); }
|
||||||
if (node.dev === "3204") { mcp3xxx.push(mcpadc.openMcp3204(i, opt, cb)); }
|
if (node.dev === "3202") { mcp3xxx.push(mcpadc.openMcp3202(i, opt, cb)); }
|
||||||
if (node.dev === "3208") { mcp3xxx.push(mcpadc.openMcp3208(i, opt, cb)); }
|
if (node.dev === "3204") { mcp3xxx.push(mcpadc.openMcp3204(i, opt, cb)); }
|
||||||
if (node.dev === "3304") { mcp3xxx.push(mcpadc.openMcp3304(i, opt, cb)); }
|
if (node.dev === "3208") { mcp3xxx.push(mcpadc.openMcp3208(i, opt, cb)); }
|
||||||
|
if (node.dev === "3304") { mcp3xxx.push(mcpadc.openMcp3304(i, opt, cb)); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
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) {
|
||||||
|
mcp3xxx[pin].read(function (err, reading) {
|
||||||
|
if (err) { node.warn("Read error: "+err); }
|
||||||
|
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
node.on("input", function(msg) {
|
catch(err) {
|
||||||
var pin = null;
|
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?");
|
||||||
if (node.pin === "M") {
|
}
|
||||||
var pay = parseInt(msg.payload.toString());
|
|
||||||
if ((pay >= 0) && (pay < chans)) { pin = pay; }
|
node.on("close", function(done) {
|
||||||
else { node.warn("Payload needs to select channel 0 to "+(chans-1)); }
|
if (mcp3xxx.length !== 0) {
|
||||||
}
|
var j=0;
|
||||||
else { pin = parseInt(node.pin); }
|
for (var i=0; i<chans; i++) {
|
||||||
if (pin !== null) {
|
mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} });
|
||||||
mcp3xxx[pin].read(function (err, reading) {
|
}
|
||||||
if (err) { node.warn("Read error: "+err); }
|
mcp3xxx = [];
|
||||||
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
else { done(); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(err) {
|
else {
|
||||||
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?");
|
node.status({text:"node inactive."})
|
||||||
}
|
}
|
||||||
|
|
||||||
node.on("close", function(done) {
|
|
||||||
if (mcp3xxx.length !== 0) {
|
|
||||||
var j=0;
|
|
||||||
for (var i=0; i<chans; i++) {
|
|
||||||
mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} });
|
|
||||||
}
|
|
||||||
mcp3xxx = [];
|
|
||||||
}
|
|
||||||
else { done(); }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("pimcp3008",PiMcpNode);
|
RED.nodes.registerType("pimcp3008",PiMcpNode);
|
||||||
|
Loading…
Reference in New Issue
Block a user