mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
In particular the `pi_revision` thing in PiGPIOPin, all the stuff @lurch picked up in `pins/data.py` (thank goodness *someone's* watching!), and make all those links pointing to "Notes" point somewhere useful like "Pin Numbering"...
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from gpiozero import Robot
|
|
from evdev import InputDevice, list_devices, ecodes
|
|
|
|
robot = Robot(left=(4, 14), right=(17, 18))
|
|
|
|
# Get the list of available input devices
|
|
devices = [InputDevice(device) for device in list_devices()]
|
|
# Filter out everything that's not a keyboard. Keyboards are defined as any
|
|
# device which has keys, and which specifically has keys 1..31 (roughly Esc,
|
|
# the numeric keys, the first row of QWERTY plus a few more) and which does
|
|
# *not* have key 0 (reserved)
|
|
must_have = {i for i in range(1, 32)}
|
|
must_not_have = {0}
|
|
devices = [
|
|
dev
|
|
for dev in devices
|
|
for keys in (set(dev.capabilities().get(ecodes.EV_KEY, [])),)
|
|
if must_have.issubset(keys)
|
|
and must_not_have.isdisjoint(keys)
|
|
]
|
|
# Pick the first keyboard
|
|
keyboard = devices[0]
|
|
|
|
keypress_actions = {
|
|
ecodes.KEY_UP: robot.forward,
|
|
ecodes.KEY_DOWN: robot.backward,
|
|
ecodes.KEY_LEFT: robot.left,
|
|
ecodes.KEY_RIGHT: robot.right,
|
|
}
|
|
|
|
for event in keyboard.read_loop():
|
|
if event.type == ecodes.EV_KEY and event.code in keypress_actions:
|
|
if event.value == 1: # key down
|
|
keypress_actions[event.code]()
|
|
if event.value == 0: # key up
|
|
robot.stop()
|