mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00: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:
		@@ -32,6 +32,7 @@
 | 
			
		||||
        <select type="text" id="node-input-dnum" style="width:150px;">
 | 
			
		||||
          <option value=0>CE0</option>
 | 
			
		||||
          <option value=1>CE1</option>
 | 
			
		||||
	  <option value=2>CE2</option>	
 | 
			
		||||
        </select>
 | 
			
		||||
    </div>
 | 
			
		||||
    <div class="form-row">
 | 
			
		||||
@@ -77,7 +78,7 @@
 | 
			
		||||
        outputs: 1,
 | 
			
		||||
        icon: "rpi.png",
 | 
			
		||||
        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() {
 | 
			
		||||
            return this.name?"node_label_italic":"";
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ module.exports = function(RED) {
 | 
			
		||||
        RED.log.warn("Info : mcp3xxx : Not running on a Pi - Ignoring node");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var mcp3xxx = [];
 | 
			
		||||
    //var mcp3xxx = [];
 | 
			
		||||
 | 
			
		||||
    function PiMcpNode(n) {
 | 
			
		||||
        RED.nodes.createNode(this,n);
 | 
			
		||||
@@ -28,15 +28,17 @@ module.exports = function(RED) {
 | 
			
		||||
        this.dnum = parseInt(n.dnum || 0);
 | 
			
		||||
        this.bus = parseInt(n.bus || 0);
 | 
			
		||||
        this.dev = n.dev || "3008";
 | 
			
		||||
        this.mcp3xxx = [];
 | 
			
		||||
        var node = this;
 | 
			
		||||
        var cb = function (err) { if (err) { node.error("Error: "+err); } };
 | 
			
		||||
        var opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
 | 
			
		||||
        this.cb = function (err) { if (err) { node.error("Error: "+err); } };
 | 
			
		||||
        this.opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
 | 
			
		||||
        var chans = parseInt(this.dev.substr(3));
 | 
			
		||||
        var node = this;
 | 
			
		||||
 | 
			
		||||
        if (allOK === true) {
 | 
			
		||||
            try {
 | 
			
		||||
                fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
 | 
			
		||||
                if (mcp3xxx.length === 0) {
 | 
			
		||||
                if (node.mcp3xxx.length === 0) {
 | 
			
		||||
                    for (var i=0; i<chans; i++) {
 | 
			
		||||
                        if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(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); }
 | 
			
		||||
                    if (pin !== null) {
 | 
			
		||||
                        mcp3xxx[pin].read(function (err, reading) {
 | 
			
		||||
                        node.mcp3xxx[pin].read(function (err, reading) {
 | 
			
		||||
                            if (err) { node.warn("Read error: "+err); }
 | 
			
		||||
                            else { node.send({payload:reading.rawValue, topic:"adc/"+pin}); }
 | 
			
		||||
                        });
 | 
			
		||||
@@ -65,14 +67,14 @@ module.exports = function(RED) {
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
            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) {
 | 
			
		||||
                if (mcp3xxx.length !== 0) {
 | 
			
		||||
                if (node.mcp3xxx.length !== 0) {
 | 
			
		||||
                    var j=0;
 | 
			
		||||
                    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 = [];
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user