1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Problems with more than one MCP3008 (#823)

* Add files via upload

* Update pimcp3008.js

implementation of the discussion

* Update pimcp3008.js

Co-authored-by: Dave Conway-Jones <dceejay@users.noreply.github.com>
This commit is contained in:
CD2468 2021-10-27 14:46:00 +02:00 committed by GitHub
parent b95ae5dc63
commit ae4b6bb0bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View File

@ -32,6 +32,7 @@
<select type="text" id="node-input-dnum" style="width:150px;"> <select type="text" id="node-input-dnum" style="width:150px;">
<option value=0>CE0</option> <option value=0>CE0</option>
<option value=1>CE1</option> <option value=1>CE1</option>
<option value=2>CE2</option>
</select> </select>
</div> </div>
<div class="form-row"> <div class="form-row">
@ -77,7 +78,7 @@
outputs: 1, outputs: 1,
icon: "rpi.png", icon: "rpi.png",
label: function() { label: function() {
return this.name || "mcp"+this.dev+" "+((this.pin==="M")?"":this.pin); return this.name || "mcp:"+this.dev+" "+" bus:"+this.bus+" CS:"+this.dnum+" pin:"+((this.pin==="M")?"":this.pin);
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";

View File

@ -19,7 +19,7 @@ module.exports = function(RED) {
RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node"); RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node");
} }
var mcp3xxx = []; //var mcp3xxx = [];
function PiMcpNode(n) { function PiMcpNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -28,15 +28,17 @@ module.exports = function(RED) {
this.dnum = parseInt(n.dnum || 0); this.dnum = parseInt(n.dnum || 0);
this.bus = parseInt(n.bus || 0); this.bus = parseInt(n.bus || 0);
this.dev = n.dev || "3008"; this.dev = n.dev || "3008";
this.mcp3xxx = [];
var node = this; var node = this;
var cb = function (err) { if (err) { node.error("Error: "+err); } }; this.cb = function (err) { if (err) { node.error("Error: "+err); } };
var opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus }; this.opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
var chans = parseInt(this.dev.substr(3)); var chans = parseInt(this.dev.substr(3));
var node = this;
if (allOK === true) { if (allOK === true) {
try { try {
fs.statSync("/dev/spidev"+node.bus+"."+node.dnum); fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
if (mcp3xxx.length === 0) { if (node.mcp3xxx.length === 0) {
for (var i=0; i<chans; i++) { for (var i=0; i<chans; i++) {
if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(i, opt, cb)); } if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(i, opt, cb)); }
if (node.dev === "3004") { mcp3xxx.push(mcpadc.openMcp3004(i, opt, cb)); } if (node.dev === "3004") { mcp3xxx.push(mcpadc.openMcp3004(i, opt, cb)); }
@ -57,7 +59,7 @@ module.exports = function(RED) {
} }
else { pin = parseInt(node.pin); } else { pin = parseInt(node.pin); }
if (pin !== null) { if (pin !== null) {
mcp3xxx[pin].read(function (err, reading) { node.mcp3xxx[pin].read(function (err, reading) {
if (err) { node.warn("Read error: "+err); } if (err) { node.warn("Read error: "+err); }
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); } else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
}); });
@ -65,14 +67,14 @@ module.exports = function(RED) {
}); });
} }
catch(err) { catch(err) {
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?"); node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?"+ err);
} }
node.on("close", function(done) { node.on("close", function(done) {
if (mcp3xxx.length !== 0) { if (node.mcp3xxx.length !== 0) {
var j=0; var j=0;
for (var i=0; i<chans; i++) { for (var i=0; i<chans; i++) {
mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} }); node.mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} });
} }
mcp3xxx = []; mcp3xxx = [];
} }