mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Convert recipe examples to includes
Makes it much easier to test things - no copying'n'pasting just run the examples straight from the dir (after wiring stuff up)
This commit is contained in:
21
docs/examples/robot_keyboard_2.py
Normal file
21
docs/examples/robot_keyboard_2.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from gpiozero import Robot
|
||||
from evdev import InputDevice, list_devices, ecodes
|
||||
|
||||
robot = Robot(left=(4, 14), right=(17, 18))
|
||||
|
||||
devices = [InputDevice(device) for device in list_devices()]
|
||||
keyboard = devices[0] # this may vary
|
||||
|
||||
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:
|
||||
if event.value == 1: # key down
|
||||
keypress_actions[event.code]()
|
||||
if event.value == 0: # key up
|
||||
robot.stop()
|
||||
Reference in New Issue
Block a user