Add SPIdev choice to mcpxxxx node

This commit is contained in:
Dave Conway-Jones 2018-09-21 13:53:08 +01:00
parent eae67c7a6c
commit 23ba609653
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
4 changed files with 16 additions and 5 deletions

View File

@ -37,6 +37,8 @@ select the channel dynamically. If so then the payload must be a value from 0 to
You can also select device id 0 or 1 (CE0 or CE1) depending on how you have wired up your device. Defaults to CE0.
And you can also select the SPI bus number 0 or 1 depending on how you have wired up your device. Defaults to 0 for spidev0.
Outputs a numeric `msg.payload` with a range of 0 to 1023, where 0 = 0V and 1023 = 3.3V (assuming you use the default 3.3V voltage reference).
**Hint**: use a `range` node to adjust the values to the range you want.

View File

@ -1,9 +1,9 @@
{
"name" : "node-red-node-pi-mcp3008",
"version" : "0.1.1",
"version" : "0.2.0",
"description" : "A Node-RED node to read from the MCP3008 Analogue to Digital Converter",
"dependencies" : {
"mcp-spi-adc": "^1.0.0"
"mcp-spi-adc": "^2.0.3"
},
"repository" : {
"type":"git",

View File

@ -33,6 +33,13 @@
<option value=1>CE1</option>
</select>
</div>
<div class="form-row">
<label for="node-input-bus"><i class="fa fa-toggle-on"></i> SPI bus</label>
<select type="text" id="node-input-bus" style="width:150px;">
<option value=0>0</option>
<option value=1>1</option>
</select>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"/>
@ -61,7 +68,8 @@
name: {value:""},
dev: {value:"3008"},
pin: {value:0, required:true},
dnum: {value:0}
dnum: {value:0},
bus: {value:0}
},
inputs: 1,
outputs: 1,

View File

@ -19,14 +19,15 @@ module.exports = function(RED) {
this.pin = n.pin || 0;
this.interval = n.interval || 1000;
this.dnum = parseInt(n.dnum || 0);
this.bus = parseInt(n.bus || 0);
this.dev = n.dev || "3008";
var node = this;
var cb = function (err) { if (err) { node.error("Error: "+err); } };
var opt = { speedHz:20000, deviceNumber:node.dnum };
var opt = { speedHz:20000, deviceNumber:node.dnum, busNumber:node.bus };
var chans = parseInt(this.dev.substr(3));
try {
fs.statSync("/dev/spidev0."+node.dnum);
fs.statSync("/dev/spidev"+node.bus+"."+node.dnum);
if (mcp3xxx.length === 0) {
for (var i=0; i<chans; i++) {
if (node.dev === "3002") { mcp3xxx.push(mcpadc.openMcp3002(i, opt, cb)); }