Skip to content

Commit ae4b6bb

Browse files
CD2468dceejay
andauthored
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 <[email protected]>
1 parent b95ae5d commit ae4b6bb

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

hardware/mcp3008/pimcp3008.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<select type="text" id="node-input-dnum" style="width:150px;">
3333
<option value=0>CE0</option>
3434
<option value=1>CE1</option>
35+
<option value=2>CE2</option>
3536
</select>
3637
</div>
3738
<div class="form-row">
@@ -77,7 +78,7 @@
7778
outputs: 1,
7879
icon: "rpi.png",
7980
label: function() {
80-
return this.name || "mcp"+this.dev+" "+((this.pin==="M")?"":this.pin);
81+
return this.name || "mcp:"+this.dev+" "+" bus:"+this.bus+" CS:"+this.dnum+" pin:"+((this.pin==="M")?"":this.pin);
8182
},
8283
labelStyle: function() {
8384
return this.name?"node_label_italic":"";

hardware/mcp3008/pimcp3008.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function(RED) {
1919
RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node");
2020
}
2121

22-
var mcp3xxx = [];
22+
//var mcp3xxx = [];
2323

2424
function PiMcpNode(n) {
2525
RED.nodes.createNode(this,n);
@@ -28,15 +28,17 @@ module.exports = function(RED) {
2828
this.dnum = parseInt(n.dnum || 0);
2929
this.bus = parseInt(n.bus || 0);
3030
this.dev = n.dev || "3008";
31+
this.mcp3xxx = [];
3132
var node = this;
32-
var cb = function (err) { if (err) { node.error("Error: "+err); } };
33-
var opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
33+
this.cb = function (err) { if (err) { node.error("Error: "+err); } };
34+
this.opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
3435
var chans = parseInt(this.dev.substr(3));
36+
var node = this;
3537

3638
if (allOK === true) {
3739
try {
3840
fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
39-
if (mcp3xxx.length === 0) {
41+
if (node.mcp3xxx.length === 0) {
4042
for (var i=0; i<chans; i++) {
4143
if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(i, opt, cb)); }
4244
if (node.dev === "3004") { mcp3xxx.push(mcpadc.openMcp3004(i, opt, cb)); }
@@ -57,22 +59,22 @@ module.exports = function(RED) {
5759
}
5860
else { pin = parseInt(node.pin); }
5961
if (pin !== null) {
60-
mcp3xxx[pin].read(function (err, reading) {
62+
node.mcp3xxx[pin].read(function (err, reading) {
6163
if (err) { node.warn("Read error: "+err); }
6264
else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
6365
});
6466
}
6567
});
6668
}
6769
catch(err) {
68-
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?");
70+
node.error("Error : Can't find SPI device - is SPI enabled in raspi-config ?"+ err);
6971
}
7072

7173
node.on("close", function(done) {
72-
if (mcp3xxx.length !== 0) {
74+
if (node.mcp3xxx.length !== 0) {
7375
var j=0;
7476
for (var i=0; i<chans; i++) {
75-
mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} });
77+
node.mcp3xxx[i].close(function() { j += 1; if (j === chans) {done()} });
7678
}
7779
mcp3xxx = [];
7880
}

0 commit comments

Comments
 (0)