From ed9951f0654064eb7bf3eca4ea8fd61765266f61 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Mon, 10 Nov 2014 20:03:51 +0000 Subject: [PATCH] Add PWM support to Pi GPIO Node - pin 12 (GPIO1) (only pin that has hardware pwm support) Note: It will interfere with any other audio output as they share same hardware/timers. --- nodes/core/hardware/36-rpi-gpio.html | 65 +++++++++++++++++++++++----- nodes/core/hardware/36-rpi-gpio.js | 14 +++--- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/nodes/core/hardware/36-rpi-gpio.html b/nodes/core/hardware/36-rpi-gpio.html index a4928f587..a64824e7d 100644 --- a/nodes/core/hardware/36-rpi-gpio.html +++ b/nodes/core/hardware/36-rpi-gpio.html @@ -58,7 +58,7 @@ -
Tip: Only Digital I/O is supported - input must be 0 or 1.
+
Tip: Only Digital Input is supported - input must be 0 or 1.
@@ -169,20 +178,23 @@ defaults: { name: { value:"" }, pin: { value:"",required:true,validate:RED.validators.number() }, - set: { value:false }, - level: { value:"0" } + set: { value:"" }, + level: { value:"0" }, + out: { value:"out" } }, inputs:1, outputs:0, icon: "rpi.png", align: "right", label: function() { - return this.name||"Pin: "+this.pin; + if ((this.pin == 12) && (this.out === "pwm")) { return this.name || "PWM: 12"; } + else { return this.name||"Pin: "+this.pin ; } }, labelStyle: function() { return this.name?"node_label_italic":""; }, oneditprepare: function() { + if (!$("#node-input-out").val()) { $("#node-input-out").val("out"); } $.getJSON('rpi-gpio/'+this.id,function(data) { $('#pitype').text(data.type); if (data.type === "Model B+") { @@ -200,13 +212,44 @@ } }); - $("#node-input-set").change(function() { + var hidepwm = function() { + if ($('#node-input-pin').val() == 12) { + $('#node-set-pwm').show(); + } + else { + $('#node-set-pwm').hide(); + $('#node-input-out').val("out"); + } + }; + $("#node-input-pin").change(function () { hidepwm(); }); + hidepwm(); + + var hidestate = function () { + if ($("#node-input-out").val() === "pwm") { + $('#node-set-tick').hide(); + $('#node-set-state').hide(); + $('#node-input-set').prop('checked', false); + $("#dig-tip").hide(); + $("#pwm-tip").show(); + } + else { + $('#node-set-tick').show(); + $("#dig-tip").show(); + $("#pwm-tip").hide(); + } + }; + $("#node-input-out").change(function () { hidestate(); }); + hidestate(); + + var setstate = function () { if ($('#node-input-set').is(":checked")) { $("#node-set-state").show(); } else { $("#node-set-state").hide(); } - }); + }; + $("#node-input-set").change(function () { setstate(); }); + setstate(); } }); diff --git a/nodes/core/hardware/36-rpi-gpio.js b/nodes/core/hardware/36-rpi-gpio.js index b59cbca78..2a56ad17e 100644 --- a/nodes/core/hardware/36-rpi-gpio.js +++ b/nodes/core/hardware/36-rpi-gpio.js @@ -139,13 +139,15 @@ module.exports = function(RED) { this.pin = pintable[n.pin]; this.set = n.set || false; this.level = n.level || 0; + this.out = n.out || "out"; var node = this; + (node.out === "pwm") ? (node.op = "pwm") : (node.op = "write"); if (node.pin !== undefined) { - exec(gpioCommand+" mode "+node.pin+" out", function(err,stdout,stderr) { + exec(gpioCommand+" mode "+node.pin+" "+node.out, function(err,stdout,stderr) { if (err) { node.error(err); } else { - if (node.set) { + if (node.set && (node.out === "out")) { exec(gpioCommand+" write "+node.pin+" "+node.level, function(err,stdout,stderr) { if (err) { node.error(err); } }); @@ -154,12 +156,14 @@ module.exports = function(RED) { if (msg.payload === "true") { msg.payload = true; } if (msg.payload === "false") { msg.payload = false; } var out = Number(msg.payload); - if ((out === 0)|(out === 1)) { - exec(gpioCommand+" write "+node.pin+" "+out, function(err,stdout,stderr) { + var limit = 1; + if (node.out === "pwm") { limit = 1023; } + if ((out >= 0) && (out <= limit)) { + exec(gpioCommand+" "+node.op+" "+node.pin+" "+out, function(err,stdout,stderr) { if (err) { node.error(err); } }); } - else { node.warn("Invalid input - not 0 or 1"); } + else { node.warn("Invalid input: "+out); } }); } });