mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2026-04-25 08:23:53 +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:
34
docs/examples/robot_keyboard_1.py
Normal file
34
docs/examples/robot_keyboard_1.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import curses
|
||||
from gpiozero import Robot
|
||||
|
||||
robot = Robot(left=(4, 14), right=(17, 18))
|
||||
|
||||
actions = {
|
||||
curses.KEY_UP: robot.forward,
|
||||
curses.KEY_DOWN: robot.backward,
|
||||
curses.KEY_LEFT: robot.left,
|
||||
curses.KEY_RIGHT: robot.right,
|
||||
}
|
||||
|
||||
def main(window):
|
||||
next_key = None
|
||||
while True:
|
||||
curses.halfdelay(1)
|
||||
if next_key is None:
|
||||
key = window.getch()
|
||||
else:
|
||||
key = next_key
|
||||
next_key = None
|
||||
if key != -1:
|
||||
# KEY DOWN
|
||||
curses.halfdelay(3)
|
||||
action = actions.get(key)
|
||||
if action is not None:
|
||||
action()
|
||||
next_key = key
|
||||
while next_key == key:
|
||||
next_key = window.getch()
|
||||
# KEY UP
|
||||
robot.stop()
|
||||
|
||||
curses.wrapper(main)
|
||||
Reference in New Issue
Block a user