Skip to content

Commit 3ea2499

Browse files
committed
minify
1 parent ac7a226 commit 3ea2499

File tree

1 file changed

+81
-108
lines changed

1 file changed

+81
-108
lines changed

gigglebot.py

Lines changed: 81 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,112 @@
1-
import neopixel
1+
from neopixel import NeoPixel
22
import microbit
3+
from micropython import const
34

4-
GET_FIRMWARE_VERSION = 1
5-
# GET_MANUFACTURER = 2
6-
GET_BOARD = 3
7-
GET_VOLTAGE_BATTERY = 4
8-
GET_LINE_SENSORS = 5
9-
GET_LIGHT_SENSORS = 6
10-
# GET_MOTOR_STATUS_RIGHT = 7
11-
# GET_MOTOR_STATUS_LEFT = 8
12-
# SET_MOTOR_POWER = 9
13-
SET_MOTOR_POWERS = 10
14-
LEFT = 0
15-
RIGHT = 1
16-
BOTH = 2
17-
FORWARD = 1
18-
BACKWARD = -1
19-
DEFAULT_EYE_COLOR = (0, 0, 10)
20-
LOW_VOLTAGE_EYE_COLOR = (10, 0, 0)
5+
GET_FIRMWARE_VERSION = const(1)
6+
GET_BOARD = const(3)
7+
GET_VOLTAGE_BATTERY = const(4)
8+
GET_LINE_SENSORS = const(5)
9+
GET_LIGHT_SENSORS = const(6)
10+
SET_MOTOR_POWERS = const(10)
11+
LEFT = const(0)
12+
RIGHT = const(1)
13+
BOTH = const(2)
14+
FORWARD = const(1)
15+
BACKWARD = const(-1)
2116
motor_power_left = 50
2217
motor_power_right = 50
2318

2419
def _write8(*args, repeat=False):
25-
buf = bytearray(len(args))
26-
buf[0] = args[0]
27-
for i in range(1, len(args)):
28-
buf[i] = (args[i] & 0xFF)
29-
microbit.i2c.write(0x04, bytes(buf), repeat)
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)
3025

31-
def _read8(reg, repeat=False):
32-
microbit.i2c.write(0x04, bytes([reg]), repeat)
33-
outbuf = microbit.i2c.read(0x04, 1, repeat)
34-
return_value = outbuf[0]
35-
return return_value
36-
37-
def _read16(reg, repeat=False):
38-
microbit.i2c.write(0x04, bytes([reg]), repeat)
39-
outbuf = microbit.i2c.read(0x04, 2, repeat)
40-
return outbuf[0] * 255 + outbuf[1]
26+
def _read(reg, size=8, repeat=False):
27+
microbit.i2c.write(0x04, bytes([reg]), repeat)
28+
outbuf = microbit.i2c.read(0x04, 1 if size==8 else 2, repeat)
29+
return outbuf[0] if size==8 else outbuf[0] * 255 + outbuf[1]
4130

4231
def _get_sensors(reg, repeat=False):
43-
microbit.i2c.write(0x04, bytes([reg]), repeat)
44-
outbuf = []
45-
buf = microbit.i2c.read(0x04, 3, repeat)
46-
outbuf.append(1023 - ( buf[0] << 2 | ((buf[2] & 0xC0) >> 6)))
47-
outbuf.append(1023 - ( buf[1] << 2 | ((buf[2] & 0x30) >> 4)))
48-
return outbuf
32+
microbit.i2c.write(0x04, bytes([reg]), repeat)
33+
outbuf = []
34+
buf = microbit.i2c.read(0x04, 3, repeat)
35+
outbuf.append(1023 - ( buf[0] << 2 | ((buf[2] & 0xC0) >> 6)))
36+
outbuf.append(1023 - ( buf[1] << 2 | ((buf[2] & 0x30) >> 4)))
37+
return outbuf
4938

5039
def volt():
51-
return (_read16(GET_VOLTAGE_BATTERY)/1000)
40+
return (_read16(GET_VOLTAGE_BATTERY, size=16)/1000)
5241

5342
def drive(dir=FORWARD, milliseconds=-1):
54-
_write8(SET_MOTOR_POWERS, motor_power_left*dir, motor_power_right*dir)
55-
if milliseconds >= 0:
56-
microbit.sleep(milliseconds)
57-
stop()
43+
_write8(SET_MOTOR_POWERS, motor_power_left*dir, motor_power_right*dir)
44+
if milliseconds >= 0:
45+
microbit.sleep(milliseconds)
46+
stop()
5847

5948
def turn(dir=LEFT, milliseconds=-1):
60-
if dir==LEFT:
61-
_write8(SET_MOTOR_POWERS, motor_power_left, 0)
62-
if dir==RIGHT:
63-
_write8(SET_MOTOR_POWERS, 0, motor_power_right)
64-
if milliseconds >= 0:
65-
microbit.sleep(milliseconds)
66-
stop()
49+
if dir==LEFT: _write8(SET_MOTOR_POWERS, motor_power_left, 0)
50+
if dir==RIGHT: _write8(SET_MOTOR_POWERS, 0, motor_power_right)
51+
if milliseconds >= 0:
52+
microbit.sleep(milliseconds)
53+
stop()
6754

6855
def set_speed(power_left, power_right):
69-
global motor_power_left, motor_power_right
70-
motor_power_left = power_left
71-
motor_power_right = power_right
56+
global motor_power_left, motor_power_right
57+
motor_power_left = power_left
58+
motor_power_right = power_right
7259

7360
def stop():
74-
_write8(SET_MOTOR_POWERS, 0, 0)
61+
_write8(SET_MOTOR_POWERS, 0, 0)
7562

7663
def set_servo(which, degrees):
77-
'''
78-
Will set the left/right servo to a value between 0 and 180
79-
'''
80-
us = min(2400, max(600, 600 + (1800 * degrees // 180)))
81-
duty = round(us * 1024 * 50 // 1000000)
82-
if which == LEFT or which == BOTH:
83-
microbit.pin14.set_analog_period(20)
84-
microbit.pin14.write_analog(duty)
85-
if which == RIGHT or which == BOTH:
86-
microbit.pin13.set_analog_period(20)
87-
microbit.pin13.write_analog(duty)
88-
89-
def servo_off(which):
90-
if which == LEFT or which == BOTH:
91-
microbit.pin14.write_digital(0)
92-
if which == RIGHT or which == BOTH:
93-
microbit.pin13.write_digital(0)
64+
us = min(2400, max(600, 600 + (1800 * degrees // 180)))
65+
duty = round(us * 1024 * 50 // 1000000)
66+
if which == LEFT or which == BOTH:
67+
microbit.pin14.set_analog_period(20)
68+
microbit.pin14.write_analog(duty)
69+
if which == RIGHT or which == BOTH:
70+
microbit.pin13.set_analog_period(20)
71+
microbit.pin13.write_analog(duty)
9472

73+
def servo_off(which):
74+
if which == LEFT or which == BOTH: microbit.pin14.write_digital(0)
75+
if which == RIGHT or which == BOTH: microbit.pin13.write_digital(0)
76+
9577
def set_smile(R=25,G=0,B=0):
96-
'''
97-
Like all neopixel methods, this may return a ValueError if the colors are invalid
98-
'''
99-
for i in range(2,9):
100-
neopixelstrip[i] = (R,G,B)
101-
neopixelstrip.show()
78+
neopix = range(2,9)
79+
for i in neopix: neopixelstrip[i] = (R,G,B)
80+
neopixelstrip.show()
10281

10382
def set_eyes(which=BOTH, R=0, G=0, B=10):
104-
'''
105-
Like all neopixel methods, this may return a ValueError if the colors are invalid
106-
'''
107-
if which != LEFT:
108-
neopixelstrip[0] = (R,G,B)
109-
if which != RIGHT:
110-
neopixelstrip[1]= (R,G,B)
111-
neopixelstrip.show()
83+
if which != LEFT:
84+
neopixelstrip[0] = (R,G,B)
85+
if which != RIGHT:
86+
neopixelstrip[1]= (R,G,B)
87+
neopixelstrip.show()
11288

11389
def set_eye_color_on_start():
114-
if _read16(GET_VOLTAGE_BATTERY) < 3400:
115-
neopixelstrip[0] = LOW_VOLTAGE_EYE_COLOR
116-
neopixelstrip[1]= LOW_VOLTAGE_EYE_COLOR
117-
else:
118-
neopixelstrip[0] = DEFAULT_EYE_COLOR
119-
neopixelstrip[1]= DEFAULT_EYE_COLOR
120-
neopixelstrip.show()
90+
if _read16(GET_VOLTAGE_BATTERY, size=16) < 3400:
91+
neopixelstrip[0] = (10, 0, 0)
92+
neopixelstrip[1]= (10, 0, 0)
93+
else:
94+
neopixelstrip[0] = (0, 0, 10)
95+
neopixelstrip[1]= (0, 0, 10)
96+
neopixelstrip.show()
12197

12298
def read_sensor(which_sensor, which_side):
123-
if (which_side == LEFT):
124-
return _get_sensors(which_sensor)[0]
125-
elif (which_side == RIGHT):
126-
return _get_sensors(which_sensor)[1]
127-
else:
128-
return _get_sensors(which_sensor)
99+
if (which_side == LEFT): return _get_sensors(which_sensor)[0]
100+
elif (which_side == RIGHT): return _get_sensors(which_sensor)[1]
101+
else: return _get_sensors(which_sensor)
129102

130103
def pixels_off():
131-
for i in range(9):
132-
neopixelstrip[i] = (0,0,0)
133-
neopixelstrip.show()
134-
135-
stop()
136-
neopixelstrip = neopixel.NeoPixel(microbit.pin8, 9)
137-
pixels_off()
138-
eyestrip = neopixel.NeoPixel(microbit.pin8, 2)
139-
set_eye_color_on_start()
104+
for i in range(9):
105+
neopixelstrip[i] = (0,0,0)
106+
neopixelstrip.show()
107+
108+
def init():
109+
stop()
110+
neopixelstrip = const(NeoPixel(microbit.pin8, 9)); pixels_off()
111+
eyestrip = const(NeoPixel(microbit.pin8, 2))
112+
set_eye_color_on_start()

0 commit comments

Comments
 (0)