From b2390f1cafefb413f4413e069d1de12f900e6b94 Mon Sep 17 00:00:00 2001 From: Nam Giang Date: Thu, 23 Mar 2017 14:02:22 -0700 Subject: [PATCH] adding frequency configuration to pwm output (#1206) --- nodes/core/hardware/36-rpi-gpio.html | 8 ++++++++ nodes/core/hardware/36-rpi-gpio.js | 3 ++- nodes/core/hardware/nrgpio.py | 7 ++++++- nodes/core/locales/en-US/messages.json | 5 +++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/nodes/core/hardware/36-rpi-gpio.html b/nodes/core/hardware/36-rpi-gpio.html index 37c63fafd..4b1cab15c 100644 --- a/nodes/core/hardware/36-rpi-gpio.html +++ b/nodes/core/hardware/36-rpi-gpio.html @@ -193,6 +193,10 @@
+
+ + +
@@ -210,6 +214,7 @@

When using PWM mode - expects an input value of a number 0 - 100. It can be floating point.

PWM mode can be used to drive a servo using input values between 10 and 20 only. The GPIO2 pin is best for this as it uses hardware to do the PWM.

+

Setting high PWM frequency might occupy more CPU than expected (default is 100Hz)

Requires the RPi.GPIO python library version 0.5.10 (or better) in order to work.

Note: we are using the actual physical pin numbers on connector P1 as they are easier to locate.

@@ -224,6 +229,7 @@ pin: { value:"",required:true,validate:RED.validators.number() }, set: { value:"" }, level: { value:"0" }, + freq: {value:""}, out: { value:"out" } }, inputs:1, @@ -297,11 +303,13 @@ $('#node-input-set').prop('checked', false); $("#dig-tip").hide(); $("#pwm-tip").show(); + $('#node-set-freq').show(); } else { $('#node-set-tick').show(); $("#dig-tip").show(); $("#pwm-tip").hide(); + $('#node-set-freq').hide(); } }; $("#node-input-out").change(function () { hidestate(); }); diff --git a/nodes/core/hardware/36-rpi-gpio.js b/nodes/core/hardware/36-rpi-gpio.js index 48ad858d3..5da6506d5 100644 --- a/nodes/core/hardware/36-rpi-gpio.js +++ b/nodes/core/hardware/36-rpi-gpio.js @@ -137,6 +137,7 @@ module.exports = function(RED) { this.pin = n.pin; this.set = n.set || false; this.level = n.level || 0; + this.freq = n.freq || 100; this.out = n.out || "out"; var node = this; if (!pinsInUse.hasOwnProperty(this.pin)) { @@ -173,7 +174,7 @@ module.exports = function(RED) { node.child = spawn(gpioCommand, [node.out,node.pin,node.level]); node.status({fill:"green",shape:"dot",text:node.level}); } else { - node.child = spawn(gpioCommand, [node.out,node.pin]); + node.child = spawn(gpioCommand, [node.out,node.pin,node.freq]); node.status({fill:"green",shape:"dot",text:"common.status.ok"}); } node.running = true; diff --git a/nodes/core/hardware/nrgpio.py b/nodes/core/hardware/nrgpio.py index 73f395fb0..4774d40b1 100755 --- a/nodes/core/hardware/nrgpio.py +++ b/nodes/core/hardware/nrgpio.py @@ -35,8 +35,13 @@ if len(sys.argv) > 2: if cmd == "pwm": #print "Initialised pin "+str(pin)+" to PWM" + try: + freq = int(sys.argv[3]) + except: + freq = 100 + GPIO.setup(pin,GPIO.OUT) - p = GPIO.PWM(pin, 100) + p = GPIO.PWM(pin, freq) p.start(0) while True: diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index d36139f58..2a03598dd 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -649,7 +649,8 @@ "pikeyboard": "Pi Keyboard", "left": "Left", "right": "Right", - "middle": "Middle" + "middle": "Middle", + "freq": "Frequency (Hz)" }, "resistor": { "none": "none", @@ -671,7 +672,7 @@ "pin": "Pins in Use: ", "in": "Tip: Only Digital Input is supported - input must be 0 or 1.", "dig": "Tip: For digital output - input must be 0 or 1.", - "pwm": "Tip: For PWM output - input must be between 0 to 100." + "pwm": "Tip: For PWM output - input must be between 0 to 100; setting high frequency might occupy more CPU than expected." }, "types": { "digout": "digital output",