|
3 | 3 | import struct
|
4 | 4 | import random
|
5 | 5 | import sys
|
| 6 | +import machine |
6 | 7 |
|
7 | 8 | from arduino_alvik import ArduinoAlvik
|
8 | 9 | from time import sleep_ms, ticks_add, ticks_diff, ticks_ms
|
@@ -125,54 +126,105 @@ def map_value(value, from_low, from_high, to_low, to_high):
|
125 | 126 | )
|
126 | 127 |
|
127 | 128 |
|
| 129 | +def countdown_color(color, ledoff_every_tick=2): |
| 130 | + for i in range(7, 0, -ledoff_every_tick): |
| 131 | + pixels.set_range_color(0, i, color) |
| 132 | + pixels.show() |
| 133 | + sleep_ms(500) |
| 134 | + pixels.clear_all() |
| 135 | + pixels.show() |
| 136 | + sleep_ms(500) |
| 137 | + pixels.clear_all() |
| 138 | + pixels.show() |
| 139 | + |
| 140 | +def calibrate_color(): |
| 141 | + print("Reading white color") |
| 142 | + countdown_color(ModulinoColor.WHITE) |
| 143 | + a.color_calibration("white") |
| 144 | + |
| 145 | + pixels.set_all_color(ModulinoColor.GREEN, 15) |
| 146 | + pixels.show() |
| 147 | + sleep_ms(2000) |
| 148 | + |
| 149 | + print("Reading black color") |
| 150 | + countdown_color(ModulinoColor.BLUE) |
| 151 | + a.color_calibration("black") |
| 152 | + |
| 153 | + pixels.set_all_color(ModulinoColor.GREEN, 15) |
| 154 | + pixels.show() |
| 155 | + sleep_ms(2000) |
| 156 | + |
| 157 | + pixels.clear_all() |
| 158 | + pixels.show() |
| 159 | + |
| 160 | + # hard-reset the board to refresh the calibration (read again the color from the file) |
| 161 | + machine.reset() |
| 162 | + |
| 163 | + |
| 164 | +STATE_SETUP = -1 |
128 | 165 | STATE_INIT = 0
|
129 | 166 | STATE_PLAY = 1
|
130 | 167 |
|
131 | 168 | state = STATE_INIT
|
132 | 169 |
|
133 | 170 | deadline = 0
|
134 | 171 | while True:
|
135 |
| - if state == STATE_INIT: |
136 |
| - a.drive(0, 0) |
137 |
| - showEndAnimation() |
138 |
| - if a.get_color_label() is not "BLACK": |
139 |
| - showReadyToPlayLeds() |
140 |
| - state = STATE_PLAY |
141 |
| - |
142 |
| - elif state == STATE_PLAY: |
143 |
| - receiveAndExecuteFromEspNow() |
144 |
| - color = a.get_color_label() |
145 |
| - if color == "BLACK": |
146 |
| - state = STATE_INIT |
147 |
| - elif color == "YELLOW": |
148 |
| - pixels.set_all_color(ModulinoColor.YELLOW, 15) |
149 |
| - pixels.show() |
150 |
| - deg = random.choice([30.0, 45.0, 90.0, 130.0, 150.0, 180.0, 275.0, 360.0]) |
151 |
| - a.rotate(deg, "deg") |
152 |
| - showReadyToPlayLeds() |
153 |
| - elif color == "BLUE": |
| 172 | + try: |
| 173 | + if state == STATE_SETUP: |
| 174 | + calibrate_color() |
| 175 | + |
| 176 | + if state == STATE_INIT: |
154 | 177 | a.drive(0, 0)
|
155 |
| - for x in range(0, FREEZE_FOR_SECONDS): |
156 |
| - sleep_ms(500) |
157 |
| - pixels.set_all_color(ModulinoColor.BLUE, 15) |
| 178 | + showEndAnimation() |
| 179 | + if a.get_touch_ok(): |
| 180 | + state = STATE_SETUP |
| 181 | + if a.get_color_label() is not "BLACK": |
| 182 | + showReadyToPlayLeds() |
| 183 | + state = STATE_PLAY |
| 184 | + |
| 185 | + elif state == STATE_PLAY: |
| 186 | + receiveAndExecuteFromEspNow() |
| 187 | + |
| 188 | + if a.get_touch_ok(): |
| 189 | + state = STATE_SETUP |
| 190 | + |
| 191 | + color = a.get_color_label() |
| 192 | + if color == "BLACK": |
| 193 | + state = STATE_INIT |
| 194 | + elif color == "YELLOW": |
| 195 | + pixels.set_all_color(ModulinoColor.YELLOW, 15) |
158 | 196 | pixels.show()
|
159 |
| - sleep_ms(500) |
| 197 | + deg = random.choice([30.0, 45.0, 90.0, 130.0, 150.0, 180.0, 275.0, 360.0]) |
| 198 | + a.rotate(deg, "deg") |
| 199 | + showReadyToPlayLeds() |
| 200 | + elif color == "BLUE": |
| 201 | + a.drive(0, 0) |
| 202 | + for x in range(0, FREEZE_FOR_SECONDS): |
| 203 | + sleep_ms(500) |
| 204 | + pixels.set_all_color(ModulinoColor.BLUE, 15) |
| 205 | + pixels.show() |
| 206 | + sleep_ms(500) |
| 207 | + pixels.clear_all() |
| 208 | + pixels.show() |
| 209 | + showReadyToPlayLeds() |
| 210 | + elif color == "GREEN" or color == "LIGHT GREEN": |
| 211 | + if not isPlayingReverted: |
| 212 | + deadline = ticks_add(ticks_ms(), REVERT_CONTROLLER_FOR_SECONDS * 1000) |
| 213 | + isPlayingReverted = True |
| 214 | + |
| 215 | + if isPlayingReverted: |
| 216 | + elapsed = ticks_diff(deadline, ticks_ms()) |
| 217 | + mapped = map_value(elapsed, 0, REVERT_CONTROLLER_FOR_SECONDS * 1000, 0, 7) |
160 | 218 | pixels.clear_all()
|
| 219 | + pixels.set_range_color(0, mapped, ModulinoColor.VIOLET) |
161 | 220 | pixels.show()
|
162 |
| - showReadyToPlayLeds() |
163 |
| - elif color == "GREEN" or color == "LIGHT GREEN": |
164 |
| - if not isPlayingReverted: |
165 |
| - deadline = ticks_add(ticks_ms(), REVERT_CONTROLLER_FOR_SECONDS * 1000) |
166 |
| - isPlayingReverted = True |
167 |
| - |
168 |
| - if isPlayingReverted: |
169 |
| - elapsed = ticks_diff(deadline, ticks_ms()) |
170 |
| - mapped = map_value(elapsed, 0, REVERT_CONTROLLER_FOR_SECONDS * 1000, 0, 7) |
171 |
| - pixels.clear_all() |
172 |
| - pixels.set_range_color(0, mapped, ModulinoColor.VIOLET) |
173 |
| - pixels.show() |
174 |
| - if elapsed < 0: |
175 |
| - showReadyToPlayLeds() |
176 |
| - isPlayingReverted = False |
177 |
| - |
| 221 | + if elapsed < 0: |
| 222 | + showReadyToPlayLeds() |
| 223 | + isPlayingReverted = False |
| 224 | + except AssertionError: |
| 225 | + print("AssertionError") |
| 226 | + # If calibration is not done correctly, the _limit() function can raise an AssertError |
| 227 | + # See https://github.com/arduino/arduino-alvik-mpy/blob/80e66561a2ae06c69adddb3adc21ca88f91a57dd/arduino_alvik/arduino_alvik.py#L724 |
| 228 | + state = STATE_SETUP |
| 229 | + continue |
178 | 230 | sleep_ms(50)
|
0 commit comments