diff --git a/nodes/core/hardware/36-rpi-gpio.html b/nodes/core/hardware/36-rpi-gpio.html index 36b299268..eed0774c4 100644 --- a/nodes/core/hardware/36-rpi-gpio.html +++ b/nodes/core/hardware/36-rpi-gpio.html @@ -366,3 +366,37 @@ } }); + + + + + + diff --git a/nodes/core/hardware/36-rpi-gpio.js b/nodes/core/hardware/36-rpi-gpio.js index 3e21fe35c..987825317 100644 --- a/nodes/core/hardware/36-rpi-gpio.js +++ b/nodes/core/hardware/36-rpi-gpio.js @@ -273,6 +273,54 @@ module.exports = function(RED) { } RED.nodes.registerType("rpi-mouse",PiMouseNode); + function PiKeyboardNode(n) { + RED.nodes.createNode(this,n); + var node = this; + + node.child = spawn(gpioCommand, ["kbd","0"]); + node.status({fill:"green",shape:"dot",text:"common.status.ok"}); + + node.child.stdout.on('data', function (data) { + var b = data.toString().trim().split(","); + var act = "up"; + if (b[1] === "1") { act = "down"; } + if (b[1] === "2") { act = "repeat"; } + node.send({ topic:"pi/key", payload:Number(b[0]), action:act }); + }); + + node.child.stderr.on('data', function (data) { + if (RED.settings.verbose) { node.log("err: "+data+" :"); } + }); + + node.child.on('close', function (code) { + node.child = null; + node.running = false; + if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); } + if (node.done) { + node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"}); + node.done(); + } + else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); } + }); + + node.child.on('error', function (err) { + if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); } + else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); } + else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); } + }); + + node.on("close", function(done) { + node.status({}); + if (node.child != null) { + node.done = done; + node.child.kill('SIGINT'); + node.child = null; + } + else { done(); } + }); + } + RED.nodes.registerType("rpi-keyboard",PiKeyboardNode); + RED.httpAdmin.get('/rpi-gpio/:id', RED.auth.needsPermission('rpi-gpio.read'), function(req,res) { res.json(pitype); }); diff --git a/nodes/core/hardware/nrgpio.py b/nodes/core/hardware/nrgpio.py index 358869c1a..81c1bd708 100755 --- a/nodes/core/hardware/nrgpio.py +++ b/nodes/core/hardware/nrgpio.py @@ -15,7 +15,10 @@ # Import library functions we need import RPi.GPIO as GPIO +import struct import sys +import os +import subprocess bounce = 20 # bounce time in mS to apply @@ -193,6 +196,29 @@ if len(sys.argv) > 2: file.close() sys.exit(0) + elif cmd == "kbd": # catch keyboard button events + try: + while not os.path.isdir("/dev/input/by-path"): + time.sleep(10) + infile = subprocess.check_output("ls /dev/input/by-path/ | grep -m 1 'kbd'", shell=True).strip() + infile_path = "/dev/input/by-path/" + infile + EVENT_SIZE = struct.calcsize('llHHI') + file = open(infile_path, "rb") + event = file.read(EVENT_SIZE) + while event: + (tv_sec, tv_usec, type, code, value) = struct.unpack('llHHI', event) + #if type != 0 or code != 0 or value != 0: + if type == 1: + # type,code,value + print("%u,%u" % (code, value)) + event = file.read(EVENT_SIZE) + print "0,0" + file.close() + sys.exit(0) + except: + file.close() + sys.exit(0) + elif len(sys.argv) > 1: cmd = sys.argv[1].lower() if cmd == "rev": @@ -200,8 +226,8 @@ elif len(sys.argv) > 1: elif cmd == "ver": print GPIO.VERSION else: - print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|ver {pin} {value|up|down}" + print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver {pin} {value|up|down}" print " only ver (gpio version) and rev (board revision) accept no pin parameter." else: - print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|ver {pin} {value|up|down}" + print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver {pin} {value|up|down}" diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index f625794e1..22326b07e 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -568,6 +568,7 @@ "initpin": "Initialise pin state?", "button": "Button", "pimouse": "Pi Mouse", + "pikeyboard": "Pi Keyboard", "left": "Left", "right": "Right", "middle": "Middle"