-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_robot.py
56 lines (50 loc) · 1.66 KB
/
key_robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# import curses and GPIO
import curses
import RPi.GPIO as GPIO
#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(True)
try:
while True:
char = screen.getch()
if char == ord('q'):
break
elif char == curses.KEY_UP:
GPIO.output(7,False)
GPIO.output(11,True)
GPIO.output(13,False)
GPIO.output(15,True)
elif char == curses.KEY_DOWN:
GPIO.output(7,True)
GPIO.output(11,False)
GPIO.output(13,True)
GPIO.output(15,False)
elif char == curses.KEY_RIGHT:
GPIO.output(7,True)
GPIO.output(11,False)
GPIO.output(13,False)
GPIO.output(15,True)
elif char == curses.KEY_LEFT:
GPIO.output(7,False)
GPIO.output(11,True)
GPIO.output(13,True)
GPIO.output(15,False)
elif char == 10:
GPIO.output(7,False)
GPIO.output(11,False)
GPIO.output(13,False)
GPIO.output(15,False)
finally:
#Close down curses properly, inc turn echo back on!
curses.nocbreak(); screen.keypad(0); curses.echo()
curses.endwin()
GPIO.cleanup()