13
13
print ("ImportError: ModulinoPixels not installed" )
14
14
sys .exit (- 1 )
15
15
16
- LIVES = 8
17
16
VELOCITY = 13 # cm/s (max is 13cm/s)
18
17
ANGULAR_VELOCITY = 320 # (max 320.8988764044944)
19
18
41
40
42
41
def receiveAndExecuteFromEspNow ():
43
42
global lifState
44
- host , msg = e .recv (
43
+ _ , msg = e .recv (
45
44
timeout_ms = 0
46
45
) # TODO: See ESPNow.irecv() for a memory-friendly alternative.
47
46
if msg is None :
48
47
return
49
- if len (msg ) < 6 : # discard garbage
48
+ if len (msg ) < 1 : # discard garbage
50
49
return
51
- unpacked_message = struct .unpack ("BHH " , msg )
50
+ unpacked_message = struct .unpack ("B " , msg )
52
51
msg_type = unpacked_message [0 ]
53
52
if int (msg_type ) == STOP :
54
53
a .drive (0 , 0 )
@@ -67,6 +66,8 @@ def receiveAndExecuteFromEspNow():
67
66
else :
68
67
liftDown ()
69
68
lifState = 0
69
+ else :
70
+ print ("unknown command type " , msg_type )
70
71
71
72
def liftUp ():
72
73
a .set_servo_positions (180 , 0 )
@@ -91,118 +92,57 @@ def liftDown():
91
92
a .set_servo_positions (180 , 0 )
92
93
93
94
94
- def lostLifeAnimation ():
95
- a .drive (0 , 0 )
96
-
97
- for i in range (3 ):
98
- a .left_led .set_color (1 , 0 , 0 )
99
- a .right_led .set_color (1 , 0 , 0 )
100
- sleep_ms (200 )
101
- a .left_led .set_color (0 , 0 , 0 )
102
- a .right_led .set_color (0 , 0 , 0 )
103
- sleep_ms (200 )
95
+ def showReadyToPlayAnimation ():
96
+ for _ in range (2 ):
97
+ pixels .set_all_color (ModulinoColor .GREEN , 15 )
98
+ pixels .show ()
99
+ sleep_ms (250 )
100
+ pixels .clear_all ()
101
+ pixels .show ()
102
+ sleep_ms (250 )
104
103
105
- pixels .clear_all ()
106
- color = ModulinoColor .RED
107
- if LIVES >= 6 :
108
- color = ModulinoColor .GREEN
109
- elif LIVES >= 3 and LIVES < 6 :
110
- color = ModulinoColor (255 , 156 , 0 )
111
- else :
112
- color = ModulinoColor .RED
113
- pixels .set_range_color (0 , LIVES - 1 , color , 70 )
104
+ pixels .set_all_color (ModulinoColor .GREEN , 15 )
114
105
pixels .show ()
115
106
116
107
117
- def checkModulinoPixels ():
118
- ok_pixel = False
119
- retries = 3
120
- for x in range (retries ):
121
- if pixels .connected :
122
- ok_pixel = True
123
- break
124
- else :
125
- a .left_led .set_color (1 , 0 , 0 )
126
- a .right_led .set_color (1 , 0 , 0 )
127
- sleep_ms (500 )
128
- return ok_pixel
129
-
130
-
131
108
def showEndAnimation ():
132
- for j in range (0 , 3 ):
133
- for i in range (0 , 8 ):
134
- pixels .clear_all ()
135
- pixels .set_rgb (i , 255 , 0 , 0 , 100 )
136
- pixels .show ()
137
- sleep_ms (50 )
109
+ for i in range (0 , 8 ):
110
+ pixels .clear_all ()
111
+ pixels .set_rgb (i , 255 , 0 , 0 , 15 )
112
+ pixels .show ()
113
+ sleep_ms (50 )
138
114
139
- for i in range (7 , - 1 , - 1 ):
140
- pixels .clear_all ()
141
- pixels .set_rgb (i , 255 , 0 , 0 , 100 )
142
- pixels .show ()
143
- sleep_ms (50 )
115
+ for i in range (7 , - 1 , - 1 ):
116
+ pixels .clear_all ()
117
+ pixels .set_rgb (i , 255 , 0 , 0 , 15 )
118
+ pixels .show ()
119
+ sleep_ms (50 )
144
120
145
121
146
122
STATE_INIT = 0
147
123
STATE_PLAY = 1
148
- STATE_LOOSE_LIFE = 2
149
- STATE_END = 3
150
- STATE_ERROR = - 1
124
+ STATE_END = 2
151
125
152
126
state = STATE_INIT
153
127
154
128
while True :
155
129
if state == STATE_INIT :
156
- if checkModulinoPixels ():
130
+ a .drive (0 , 0 )
131
+ showEndAnimation ()
132
+ if a .get_color_label () is not "BLACK" :
133
+ showReadyToPlayAnimation ()
157
134
state = STATE_PLAY
158
- else :
159
- state = STATE_ERROR
160
135
161
136
elif state == STATE_PLAY :
162
- print ( "PLAY" )
137
+ receiveAndExecuteFromEspNow ( )
163
138
color = a .get_color_label ()
164
- print (color )
165
-
166
139
if color == "BLACK" :
167
- LIVES -= 1
168
- lostLifeAnimation ()
169
- if LIVES > 0 :
170
- state = STATE_LOOSE_LIFE
171
- elif LIVES == 0 :
172
- state = STATE_END
173
-
140
+ state = STATE_INIT
174
141
elif color == "RED" :
175
142
# random spin
176
143
a .rotate (
177
144
random .choice ([30.0 , 45.0 , 90.0 , 130.0 , 150.0 , 180.0 , 275.0 , 360.0 ]),
178
145
"deg" ,
179
- )
146
+ )
180
147
181
- receiveAndExecuteFromEspNow ()
182
-
183
- elif state == STATE_LOOSE_LIFE :
184
- print ("LOOSE LIFE" )
185
-
186
- receiveAndExecuteFromEspNow ()
187
-
188
- if a .get_color_label () is not "BLACK" :
189
- state = STATE_PLAY
190
- else :
191
- state = STATE_LOOSE_LIFE
192
-
193
- elif state == STATE_END :
194
- a .drive (0 , 0 )
195
- showEndAnimation ()
196
-
197
- elif state == STATE_ERROR :
198
- a .drive (0 , 0 )
199
- while True :
200
- # Blink the LEDs forever
201
- a .left_led .set_color (1 , 0 , 0 )
202
- a .right_led .set_color (1 , 0 , 0 )
203
- sleep_ms (200 )
204
- a .left_led .set_color (0 , 0 , 0 )
205
- a .right_led .set_color (0 , 0 , 0 )
206
- sleep_ms (200 )
207
-
208
- sleep_ms (100 )
148
+ sleep_ms (50 )
0 commit comments