-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.py
215 lines (181 loc) · 6.79 KB
/
code.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# ---------- VARIABLES -------------#
last_poll_ts = -1000
# ---------- IMPORT REQUIRED TO SHOW SPLASH -------------#
from board import I2C, BUTTON, D1, D2, DISPLAY
from debug import debug
from settings import MQTT_POLL_FREQ, BRIGHTNESS_OPTIONS, BRIGHTNESS_INITIAL_INDEX
from adxl343 import Accelerometer
from orientation import Orientation, ORIENTATION_RIGHT, ORIENTATION_LEFT, ORIENTATION_STANDING, ORIENTATION_UPSIDE_DOWN, ORIENTATION_FACE_DOWN, ORIENTATION_FACE_UP
from gui import Gui
# ---------- DISPLAY SETUP -------------#
debug('Initializing display')
current_brightness_index = BRIGHTNESS_INITIAL_INDEX
DISPLAY.brightness = BRIGHTNESS_OPTIONS[BRIGHTNESS_INITIAL_INDEX]
DISPLAY.auto_refresh = False
def change_brightness():
global current_brightness_index
current_brightness_index = (current_brightness_index + 1) % len(BRIGHTNESS_OPTIONS)
DISPLAY.brightness = BRIGHTNESS_OPTIONS[current_brightness_index]
def turn_off_display():
DISPLAY.brightness = 0
def turn_on_display():
DISPLAY.brightness = BRIGHTNESS_OPTIONS[current_brightness_index]
# ---------- I2C SETUP -------------#
i2c = I2C()
# ---------- ACCELEROMETER / ORIENTATION SETUP -------------#
debug('Starting accelerometer')
accelerometer = Accelerometer(i2c)
orientation = Orientation(accelerometer)
def cb_to_face_down(orientation_name):
turn_off_display()
enqueue_action_payload(game_logic.get_action_payload_force_pause())
def cb_from_face_down(orientation_name):
turn_on_display()
enqueue_action_payload(game_logic.get_action_payload_force_unpause())
def cb_to_upside_down(orientation_name):
Gui.set_rotation(180)
def cb_to_right(orientation_name):
Gui.set_rotation(90)
def cb_to_left(orientation_name):
Gui.set_rotation(270)
def cb_to_standing(orientation_name):
Gui.set_rotation(0)
orientation.set_callback(ORIENTATION_RIGHT, cb_to_right, None)
orientation.set_callback(ORIENTATION_LEFT, cb_to_left, None)
orientation.set_callback(ORIENTATION_STANDING, cb_to_standing, None)
orientation.set_callback(ORIENTATION_UPSIDE_DOWN, cb_to_upside_down, None)
orientation.set_callback(ORIENTATION_FACE_DOWN, cb_to_face_down, cb_from_face_down)
orientation.loop()
# ---------- SPLASH SETUP -------------#
debug("Creating Splash")
Gui.create_root_splash()
Gui.show_splash()
# ---------- REMAINING IMPORTS -------------#
Gui.set_splash_text('Loading libraries')
from time import monotonic, sleep
from alarm import light_sleep_until_alarms, time
from gc import collect
from internet import MQTT, http_get_json, connect_to_wifi
from buttons import Buttons
from game_logic import GameLogic
from piezo import beep_error, beep_success, beep_shake
# ---------- GUI SETUP -------------#
orientation.loop()
Gui.set_splash_text('Loading fonts')
Gui.load_fonts()
orientation.loop()
Gui.set_splash_text('Creating graphics')
Gui.create_root_main()
# ---------- GAME LOGIC SETUP -------------#
game_logic = GameLogic()
# ---------- BUTTONS SETUP -------------#
debug('Creating buttons')
buttons = Buttons({
BUTTON: False,
D1: True,
D2: True,
})
payload_to_send = None
def enqueue_action_payload(payload, success_sound=beep_success, failure_sound=beep_error):
global payload_to_send
if payload != None:
success_sound()
payload_to_send = payload
else:
failure_sound()
def send_action_payload_loop():
global payload_to_send
if payload_to_send != None:
Gui.set_syncing(True)
old_game_state_version = game_logic.game_state_version
mqtt.publish(payload_to_send)
payload_to_send = None
poll_mqtt_until_game_state_changes(old_game_state_version)
def button_d0_callback():
enqueue_action_payload(game_logic.get_action_payload_primary())
def button_d1_callback():
enqueue_action_payload(game_logic.get_action_payload_secondary())
def button_d2_callback():
enqueue_action_payload(game_logic.get_action_payload_admin())
buttons.set_callback(BUTTON, button_d0_callback)
buttons.set_callback(D1, button_d1_callback)
buttons.set_callback(D2, button_d2_callback)
# ---------- BATTERY SETUP -------------#
orientation.loop()
Gui.set_splash_text('Monitoring battery')
from adafruit_max1704x import MAX17048
battery_device = MAX17048(i2c)
# ---------- ACCELERATION CALLBACK SETUP SETUP -------------#
move_start_acceleration = None
def cb_movement(acceleration):
global move_start_acceleration
if move_start_acceleration == None:
move_start_acceleration = acceleration
orientation.loop()
pass
def cb_inactivity(acceleration):
orientation.loop(force=True)
def cb_shake():
enqueue_action_payload(game_logic.get_action_payload_undo(), success_sound=beep_shake)
accelerometer.set_movement_callback(cb_movement)
accelerometer.set_inactivity_callback(cb_inactivity)
accelerometer.set_shake_callback(cb_shake)
# ---------- CURRENT TIME SETUP -------------#
connect_to_wifi()
Gui.set_splash_text('Getting current time')
time_json = http_get_json('http://worldtimeapi.org/api/timezone/Etc/UTC')
game_logic.set_current_unix_time(time_json['unixtime'])
# ---------- MQTT SETUP -------------#
def on_mqtt_message(msg):
game_logic.handle_state_update(msg)
game_logic.loop()
debug('Creating MQTT client')
mqtt = MQTT(on_mqtt_message)
def poll_mqtt(force=False, fallback_message=None):
global last_poll_ts
if force or monotonic() - last_poll_ts > (MQTT_POLL_FREQ-1):
Gui.set_syncing(True)
DISPLAY.refresh()
mqtt.poll_for_new_messages(fallback_message)
Gui.set_syncing(False)
DISPLAY.refresh()
last_poll_ts = monotonic()
def poll_mqtt_until_game_state_changes(old_game_state_version):
global last_poll_ts
Gui.set_syncing(True)
DISPLAY.refresh()
ts = monotonic()
while old_game_state_version == game_logic.game_state_version and monotonic() - ts < 4:
sleep(1)
mqtt.poll_for_new_messages()
Gui.set_syncing(False)
DISPLAY.refresh()
last_poll_ts = monotonic()
try:
orientation.loop()
poll_mqtt(force=True, fallback_message=None)
# ---------- MAIN LOOP -------------#
auto_press_count = 1
loop_count = 0
while True:
loop_count += 1
Gui.set_battery(battery_device.cell_percent)
alarms = buttons.get_alarms()
alarms.append(accelerometer.accelerometer_interrupt_alarm)
time_alarm = time.TimeAlarm(monotonic_time=monotonic() + MQTT_POLL_FREQ)
alarms.append(time_alarm)
activated_alarm = light_sleep_until_alarms(*alarms)
if activated_alarm == accelerometer.accelerometer_interrupt_alarm:
accelerometer.loop()
elif activated_alarm == time_alarm:
poll_mqtt()
buttons.loop()
game_logic.loop()
send_action_payload_loop()
DISPLAY.refresh()
collect()
except Exception as e:
Gui.set_rotation(0)
DISPLAY.brightness = 1
print(dir(e))
raise e