mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add Pi Keyboard code node
This commit is contained in:
@@ -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}"
|
||||
|
||||
Reference in New Issue
Block a user