1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add Pi Keyboard code node

This commit is contained in:
Dave Conway-Jones 2015-12-21 10:27:58 +00:00
parent 65daaeb617
commit 86064651af
4 changed files with 111 additions and 2 deletions

View File

@ -366,3 +366,37 @@
}
});
</script>
<script type="text/x-red" data-template-name="rpi-keyboard">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
</script>
<script type="text/x-red" data-help-name="rpi-keyboard">
<p>Raspberry Pi keyboard handling node. Requires a USB keyboard.</p>
<p>Generates a <b>msg.payload</b> with a keycode, and <b>msg.action</b> set to
<i>up, down</i> or <i>repeat</i> whenever a key is pressed or released.</p>
<p>It also sets <b>msg.topic</b> to <i>pi/key</i>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('rpi-keyboard',{
category: 'Raspberry Pi',
label: 'Raspberry Pi',
color:"#c6dbef",
defaults: {
name: { value:"" }
},
inputs:0,
outputs:1,
icon: "rpi.png",
label: function() {
return this.name||this._("rpi-gpio.label.pikeyboard");;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -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);
});

View File

@ -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}"

View File

@ -568,6 +568,7 @@
"initpin": "Initialise pin state?",
"button": "Button",
"pimouse": "Pi Mouse",
"pikeyboard": "Pi Keyboard",
"left": "Left",
"right": "Right",
"middle": "Middle"