1515BACKWARD = const (- 1 )
1616motor_power_left = 50
1717motor_power_right = 50
18-
19- def _write8 (* args , repeat = False ):
20- buf = bytearray (len (args ))
21- buf [0 ] = args [0 ]
22- for i in range (1 , len (args )):
23- buf [i ] = (args [i ] & 0xFF )
24- microbit .i2c .write (0x04 , bytes (buf ), repeat )
18+ neopixelstrip = None
2519
2620def _read (reg , size = 8 , repeat = False ):
2721 microbit .i2c .write (0x04 , bytes ([reg ]), repeat )
@@ -37,17 +31,17 @@ def _get_sensors(reg, repeat=False):
3731 return outbuf
3832
3933def volt ():
40- return (_read16 (GET_VOLTAGE_BATTERY , size = 16 )/ 1000 )
34+ return (_read (GET_VOLTAGE_BATTERY , size = 16 )/ 1000 )
4135
4236def drive (dir = FORWARD , milliseconds = - 1 ):
43- _write8 ( SET_MOTOR_POWERS , motor_power_left * dir , motor_power_right * dir )
37+ microbit . i2c . write ( 0x04 , bytes ([ SET_MOTOR_POWERS , motor_power_left * dir , motor_power_right * dir ]), False )
4438 if milliseconds >= 0 :
4539 microbit .sleep (milliseconds )
4640 stop ()
4741
4842def turn (dir = LEFT , milliseconds = - 1 ):
49- if dir == LEFT : _write8 ( SET_MOTOR_POWERS , motor_power_left , 0 )
50- if dir == RIGHT : _write8 ( SET_MOTOR_POWERS , 0 , motor_power_right )
43+ if dir == LEFT : microbit . i2c . write ( 0x04 , bytes ([ SET_MOTOR_POWERS , motor_power_left , 0 ]), False )
44+ if dir == RIGHT : microbit . i2c . write ( 0x04 , bytes ([ SET_MOTOR_POWERS , 0 , motor_power_right ]), False )
5145 if milliseconds >= 0 :
5246 microbit .sleep (milliseconds )
5347 stop ()
@@ -58,7 +52,7 @@ def set_speed(power_left, power_right):
5852 motor_power_right = power_right
5953
6054def stop ():
61- _write8 ( SET_MOTOR_POWERS , 0 , 0 )
55+ microbit . i2c . write ( 0x04 , bytes ([ SET_MOTOR_POWERS , 0 , 0 ]), False )
6256
6357def set_servo (which , degrees ):
6458 us = min (2400 , max (600 , 600 + (1800 * degrees // 180 )))
@@ -87,7 +81,7 @@ def set_eyes(which=BOTH, R=0, G=0, B=10):
8781 neopixelstrip .show ()
8882
8983def set_eye_color_on_start ():
90- if _read16 (GET_VOLTAGE_BATTERY , size = 16 ) < 3400 :
84+ if _read (GET_VOLTAGE_BATTERY , size = 16 ) < 3400 :
9185 neopixelstrip [0 ] = (10 , 0 , 0 )
9286 neopixelstrip [1 ]= (10 , 0 , 0 )
9387 else :
@@ -106,6 +100,7 @@ def pixels_off():
106100 neopixelstrip .show ()
107101
108102def init ():
103+ global neopixelstrip
109104 stop ()
110105 neopixelstrip = const (NeoPixel (microbit .pin8 , 9 )); pixels_off ()
111106 eyestrip = const (NeoPixel (microbit .pin8 , 2 ))
0 commit comments