@@ -47,17 +47,17 @@ def __init__(self, left_pin, right_pin, underlight_neopixel):
4747 self .set_underglow (PURPLE )
4848 self .set_speed (STOP )
4949
50- @classmethod
51- def _init_motor (cls , pin ):
52- pwm = pwmio .PWMOut (pin , frequency = 50 )
53- return adafruit_motor .servo .ContinuousServo (pwm , min_pulse = 600 , max_pulse = 2400 )
54-
5550 @classmethod
5651 def _make_palette (cls , color ):
5752 palette = displayio .Palette (1 )
5853 palette [0 ] = color
5954 return palette
6055
56+ @classmethod
57+ def _init_motor (cls , pin ):
58+ pwm = pwmio .PWMOut (pin , frequency = 50 )
59+ return adafruit_motor .servo .ContinuousServo (pwm , min_pulse = 600 , max_pulse = 2400 )
60+
6161 def _init_ble (self ):
6262 self .ble = BLERadio ()
6363 self .uart_service = UARTService ()
@@ -90,6 +90,10 @@ def wait_for_connection(self):
9090 def is_connected (self ):
9191 return self .ble .connected
9292
93+ def check_for_packets (self ):
94+ if self .uart_service .in_waiting :
95+ self ._process_packet (Packet .from_stream (self .uart_service ))
96+
9397 def set_underglow (self , color , save_release_color = False ):
9498 if save_release_color :
9599 self .release_color = self .get_underglow ()
@@ -158,14 +162,6 @@ def stop(self):
158162 time .sleep (0.5 )
159163 self .set_underglow (color )
160164
161- def check_for_packets (self ):
162- if self .uart_service .in_waiting :
163- self ._process_packet (Packet .from_stream (self .uart_service ))
164-
165- def _handle_color_packet (self , packet ):
166- # Change the color
167- self .set_underglow (packet .color )
168-
169165 def _remove_shapes (self ):
170166 while len (self .display_group ) > 1 :
171167 self .display_group .pop ()
@@ -259,6 +255,20 @@ def _set_status_stop(self):
259255 self ._remove_shapes ()
260256 self ._add_centered_rect (100 , 100 )
261257
258+ def _process_packet (self , packet ):
259+ if isinstance (packet , ColorPacket ):
260+ self ._handle_color_packet (packet )
261+ elif isinstance (packet , ButtonPacket ) and packet .pressed :
262+ # do this when buttons are pressed
263+ self ._handle_button_press_packet (packet )
264+ elif isinstance (packet , ButtonPacket ) and not packet .pressed :
265+ # do this when some buttons are released
266+ self ._handle_button_release_packet (packet )
267+
268+ def _handle_color_packet (self , packet ):
269+ # Change the color
270+ self .set_underglow (packet .color )
271+
262272 def _handle_button_press_packet (self , packet ):
263273 if packet .button == ButtonPacket .UP : # UP button pressed
264274 self .set_throttle (FWD )
@@ -285,13 +295,3 @@ def _handle_button_release_packet(self, packet):
285295 self .set_throttle (self .direction )
286296 if packet .button == ButtonPacket .LEFT :
287297 self .set_throttle (self .direction )
288-
289- def _process_packet (self , packet ):
290- if isinstance (packet , ColorPacket ):
291- self ._handle_color_packet (packet )
292- elif isinstance (packet , ButtonPacket ) and packet .pressed :
293- # do this when buttons are pressed
294- self ._handle_button_press_packet (packet )
295- elif isinstance (packet , ButtonPacket ) and not packet .pressed :
296- # do this when some buttons are released
297- self ._handle_button_release_packet (packet )
0 commit comments