From 1a9da583f19e3a2db9a8ac703f20ed1cfd68acad Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 12 Dec 2015 16:33:39 +0000 Subject: [PATCH] Update Pi Hardware nodes that call python Pibrella, PiLiter, PiLcd --- hardware/PiLcd/nrlcd.py | 0 hardware/PiLcd/package.json | 2 +- hardware/PiLcd/pilcd.js | 2 +- hardware/PiLiter/39-rpi-piliter.js | 4 +- hardware/PiLiter/package.json | 2 +- hardware/Pibrella/38-rpi-pibrella.js | 4 +- hardware/Pibrella/nrgpio.py | 100 +++++++++++++++++++++++---- hardware/Pibrella/package.json | 2 +- 8 files changed, 94 insertions(+), 22 deletions(-) mode change 100644 => 100755 hardware/PiLcd/nrlcd.py mode change 100644 => 100755 hardware/Pibrella/nrgpio.py diff --git a/hardware/PiLcd/nrlcd.py b/hardware/PiLcd/nrlcd.py old mode 100644 new mode 100755 diff --git a/hardware/PiLcd/package.json b/hardware/PiLcd/package.json index 90923e5d..38a755fd 100644 --- a/hardware/PiLcd/package.json +++ b/hardware/PiLcd/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pilcd", - "version" : "0.0.1", + "version" : "0.0.2", "description" : "A Node-RED node for Raspberry Pi to write to HD44780 style LCD panels.", "dependencies" : { }, diff --git a/hardware/PiLcd/pilcd.js b/hardware/PiLcd/pilcd.js index 7a556fbd..b00b8940 100644 --- a/hardware/PiLcd/pilcd.js +++ b/hardware/PiLcd/pilcd.js @@ -20,7 +20,7 @@ module.exports = function(RED) { var spawn = require('child_process').spawn; var fs = require('fs'); - var gpioCommand = __dirname + '/nrlcd'; + var gpioCommand = __dirname + '/nrlcd.py'; if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi //util.log("Info : Ignoring Raspberry Pi specific node."); diff --git a/hardware/PiLiter/39-rpi-piliter.js b/hardware/PiLiter/39-rpi-piliter.js index 61ae9202..a0a9c800 100644 --- a/hardware/PiLiter/39-rpi-piliter.js +++ b/hardware/PiLiter/39-rpi-piliter.js @@ -20,7 +20,7 @@ module.exports = function(RED) { var spawn = require('child_process').spawn; var fs = require('fs'); - var gpioCommand = __dirname+'/nrgpio'; + var gpioCommand = __dirname+'/nrgpio.py'; if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi //util.log("Info : Ignoring Raspberry Pi specific node."); @@ -34,7 +34,7 @@ module.exports = function(RED) { if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) { util.log("[rpi-gpio] Error : "+gpioCommand+" needs to be executable."); - throw "Error : nrgpio must to be executable."; + throw "Error : " + gpioCommand + " must to be executable."; } function PiLiter(n) { diff --git a/hardware/PiLiter/package.json b/hardware/PiLiter/package.json index 2f71c6f2..bfa75d1f 100644 --- a/hardware/PiLiter/package.json +++ b/hardware/PiLiter/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-piliter", - "version" : "0.0.7", + "version" : "0.0.8", "description" : "A Node-RED node to drive a Raspberry Pi Pi-LITEr 8 LED board.", "dependencies" : { }, diff --git a/hardware/Pibrella/38-rpi-pibrella.js b/hardware/Pibrella/38-rpi-pibrella.js index 8f34b6b7..e6122351 100644 --- a/hardware/Pibrella/38-rpi-pibrella.js +++ b/hardware/Pibrella/38-rpi-pibrella.js @@ -20,7 +20,7 @@ module.exports = function(RED) { var spawn = require('child_process').spawn; var fs = require('fs'); - var gpioCommand = __dirname+'/nrgpio'; + var gpioCommand = __dirname+'/nrgpio.py'; if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi //util.log("Info : Ignoring Raspberry Pibrella specific node."); @@ -34,7 +34,7 @@ module.exports = function(RED) { if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) { util.log("[rpi-pibrella] Error : "+gpioCommand+" needs to be executable."); - throw "Error : nrgpio must to be executable."; + throw "Error : " + gpioCommand + " must to be executable."; } var pinsInUse = {}; diff --git a/hardware/Pibrella/nrgpio.py b/hardware/Pibrella/nrgpio.py old mode 100644 new mode 100755 index 122a0f5e..a578283f --- a/hardware/Pibrella/nrgpio.py +++ b/hardware/Pibrella/nrgpio.py @@ -1,3 +1,4 @@ +#!/usr/bin/python # # Copyright 2014 IBM Corp. # @@ -18,6 +19,10 @@ import sys bounce = 20 # bounce time in mS to apply +if sys.version_info >= (3,0): + print("Sorry - currently only configured to work with python 2.x") + sys.exit(1) + if len(sys.argv) > 1: cmd = sys.argv[1].lower() pin = int(sys.argv[2]) @@ -33,17 +38,16 @@ if len(sys.argv) > 1: while True: try: data = raw_input() - if data == "close": - GPIO.cleanup(pin) + if 'close' in data: sys.exit(0) p.ChangeDutyCycle(float(data)) - except EOFError: # hopefully always caused by us sigint'ing the program + except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program GPIO.cleanup(pin) sys.exit(0) except Exception as ex: print "bad data: "+data - if cmd == "buzz": + elif cmd == "buzz": #print "Initialised pin "+str(pin)+" to Buzz" GPIO.setup(pin,GPIO.OUT) p = GPIO.PWM(pin, 100) @@ -52,15 +56,14 @@ if len(sys.argv) > 1: while True: try: data = raw_input() - if data == "close": - GPIO.cleanup(pin) + if 'close' in data: sys.exit(0) elif float(data) == 0: p.stop() else: p.start(50) p.ChangeFrequency(float(data)) - except EOFError: # hopefully always caused by us sigint'ing the program + except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program GPIO.cleanup(pin) sys.exit(0) except Exception as ex: @@ -75,11 +78,10 @@ if len(sys.argv) > 1: while True: try: data = raw_input() - if data == "close": - GPIO.cleanup(pin) + if 'close' in data: sys.exit(0) data = int(data) - except EOFError: # hopefully always caused by us sigint'ing the program + except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program GPIO.cleanup(pin) sys.exit(0) except: @@ -108,18 +110,88 @@ if len(sys.argv) > 1: while True: try: data = raw_input() - if data == "close": - GPIO.cleanup(pin) + if 'close' in data: sys.exit(0) - except EOFError: # hopefully always caused by us sigint'ing the program + except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program GPIO.cleanup(pin) sys.exit(0) + elif cmd == "byte": + #print "Initialised BYTE mode - "+str(pin)+ + list = [7,11,13,12,15,16,18,22] + GPIO.setup(list,GPIO.OUT) + + while True: + try: + data = raw_input() + if 'close' in data: + sys.exit(0) + data = int(data) + except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program + GPIO.cleanup() + sys.exit(0) + except: + data = 0 + for bit in range(8): + if pin == 1: + mask = 1 << (7 - bit) + else: + mask = 1 << bit + GPIO.output(list[bit], data & mask) + + elif cmd == "borg": + #print "Initialised BORG mode - "+str(pin)+ + GPIO.setup(11,GPIO.OUT) + GPIO.setup(13,GPIO.OUT) + GPIO.setup(15,GPIO.OUT) + r = GPIO.PWM(11, 100) + g = GPIO.PWM(13, 100) + b = GPIO.PWM(15, 100) + r.start(0) + g.start(0) + b.start(0) + + while True: + try: + data = raw_input() + if 'close' in data: + sys.exit(0) + c = data.split(",") + r.ChangeDutyCycle(float(c[0])) + g.ChangeDutyCycle(float(c[1])) + b.ChangeDutyCycle(float(c[2])) + except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program + GPIO.cleanup() + sys.exit(0) + except: + data = 0 + elif cmd == "rev": print GPIO.RPI_REVISION elif cmd == "ver": print GPIO.VERSION + elif cmd == "mouse": # catch mice button events + file = open( "/dev/input/mice", "rb" ) + oldbutt = 0 + + def getMouseEvent(): + global oldbutt + global pin + buf = file.read(3) + pin = pin & 0x07 + button = ord( buf[0] ) & pin # mask out just the required button(s) + if button != oldbutt: # only send if changed + oldbutt = button + print button + + while True: + try: + getMouseEvent() + except: + file.close() + sys.exit(0) + else: - print "Bad parameters - {in|out|pwm} {pin} {value|up|down}" + print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|ver pin {value|up|down}" diff --git a/hardware/Pibrella/package.json b/hardware/Pibrella/package.json index 79791552..e3916a5a 100644 --- a/hardware/Pibrella/package.json +++ b/hardware/Pibrella/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pibrella", - "version" : "0.0.8", + "version" : "0.0.9", "description" : "A Node-RED node to read from and write to a Pibrella Raspberry Pi add-on board", "dependencies" : { },