-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.py
63 lines (57 loc) · 2.45 KB
/
menu.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
56
57
58
59
60
61
62
63
"""PiWars Robot WS2801 Menu System"""
from time import sleep
import subprocess
from piwars_bot_display import RobotDisplay
from piwars_bot_controller import PiWarsController
modes = [
{'name': 'drive', 'process': 'dualshock_tank.py'},
{'name': 'skittle track', 'process': 'skittle_track.py'},
{'name': 'over the rainbow', 'process': 'over the rainbow`.py'},
{'name': 'straight line speed', 'process': 'straight_speed.py'},
{'name': 'maze', 'process': 'maze.py'},
{'name': 'shutdown', 'process': 'shutdown_robot.py'}
]
class PiWarsModeMenu:
def __init__(self):
# Setup, but don't connect joypad devices
self.menu = modes
self.current_item = 0
self.display = RobotDisplay()
def action_menu_item(self):
item = modes[self.current_item]
self.display.message_menu_item_activated()
subprocess.call(['python', item['process']])
if item['name'] is 'shutdown':
exit(0)
def mainloop(self):
while True:
# connect to joypad devices or show error
controller = PiWarsController()
action = False
try:
with controller.connect_pad() as pad:
self.display.message_menu_mode()
while pad.connected and not action:
sleep(0.05) # 50 millis
self.display.update()
# allow mode selection with the dpad
presses = pad.check_presses()
if presses['dleft'] and self.current_item < len(modes):
self.current_item += 1
self.display.message_menu_set_current_item(self.current_item, modes[self.current_item]['name'])
if presses['dright'] and self.current_item > 0:
self.current_item -= 1
self.display.message_menu_set_current_item(self.current_item, modes[self.current_item]['name'])
if presses['cross']:
action = True
if action:
# then X button will deinitialise (cleanup) and start the subprocess
self.action_menu_item()
finally:
# display pad not connected
self.display.message_waiting_for_pad()
def main():
menu = PiWarsModeMenu()
menu.mainloop()
if __name__ == '__main__':
main()