Skip to content

Commit a08b688

Browse files
committed
changed state keys to constants
1 parent 9aff606 commit a08b688

3 files changed

Lines changed: 140 additions & 112 deletions

File tree

src/base_circuitpython/base_cp_constants.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
class CLUE_STATE:
2+
BUTTON_A = "button_a"
3+
BUTTON_B = "button_b"
4+
PRESSED_BUTTONS = "pressed_buttons"
5+
SEA_LEVEL_PRESSURE = "sea_level_pressure"
6+
TEMPERATURE = "temperature"
7+
PROXIMITY = "proximity"
8+
GESTURE = "gesture"
9+
HUMIDITY = "humidity"
10+
PRESSURE = "pressure"
11+
PIXEL = "pixel"
12+
# Accelerometer
13+
MOTION_X = "motion_x"
14+
MOTION_Y = "motion_y"
15+
MOTION_Z = "motion_z"
16+
# Light/color sensor
17+
LIGHT_R = "light_r"
18+
LIGHT_G = "light_g"
19+
LIGHT_B = "light_b"
20+
LIGHT_C = "light_c"
21+
# Magnetometer
22+
MAGNET_X = "magnet_x"
23+
MAGNET_Y = "magnet_y"
24+
MAGNET_Z = "magnet_z"
25+
# Gyroscope
26+
GYRO_X = "gyro_x"
27+
GYRO_Y = "gyro_y"
28+
GYRO_Z = "gyro_z"
29+
30+
131
CPX = "CPX"
232
CLUE = "CLUE"
333
PIXELS = "pixels"
@@ -9,22 +39,22 @@
939
IMG_DIR_NAME = "img"
1040
SCREEN_HEIGHT_WIDTH = 240
1141

12-
EXPECTED_INPUT_BUTTONS = set(["button_a", "button_b"])
42+
EXPECTED_INPUT_BUTTONS = set([CLUE_STATE.BUTTON_A, CLUE_STATE.BUTTON_B])
1343

1444
ALL_EXPECTED_INPUT_EVENTS = set(
1545
[
16-
"temperature",
17-
"light_r",
18-
"light_g",
19-
"light_b",
20-
"light_c",
21-
"motion_x",
22-
"motion_y",
23-
"motion_z",
24-
"humidity",
25-
"pressure",
26-
"proximity",
27-
"gesture",
46+
CLUE_STATE.TEMPERATURE,
47+
CLUE_STATE.LIGHT_R,
48+
CLUE_STATE.LIGHT_G,
49+
CLUE_STATE.LIGHT_B,
50+
CLUE_STATE.LIGHT_C,
51+
CLUE_STATE.MOTION_X,
52+
CLUE_STATE.MOTION_Y,
53+
CLUE_STATE.MOTION_Z,
54+
CLUE_STATE.HUMIDITY,
55+
CLUE_STATE.PRESSURE,
56+
CLUE_STATE.PROXIMITY,
57+
CLUE_STATE.GESTURE,
2858
]
2959
)
3060

src/clue/adafruit_clue.py

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -199,37 +199,36 @@ class Clue: # pylint: disable=too-many-instance-attributes, too-many-public-met
199199
RAINBOW = (RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE)
200200

201201
def __init__(self):
202-
self.__state = {
203-
"button_a": False,
204-
"button_b": False,
205-
"pressed_buttons": set(),
206-
"sea_level_pressure": 1013.25,
207-
"temperature": 0,
208-
"proximity": 0,
209-
"gesture": "",
210-
"humidity": 0,
211-
"pressure": 0,
212-
"pixel": neopixel.NeoPixel(
213-
pin=CONSTANTS.CLUE_PIN, n=1, pixel_order=neopixel.RGB
214-
),
215-
# Accelerometer
216-
"motion_x": 0,
217-
"motion_y": 0,
218-
"motion_z": 0,
219-
# Light/color sensor
220-
"light_r": 0,
221-
"light_g": 0,
222-
"light_b": 0,
223-
"light_c": 0,
224-
# Magnetometer
225-
"magnet_x": 0,
226-
"magnet_y": 0,
227-
"magnet_z": 0,
228-
# Gyroscope
229-
"gyro_x": 0,
230-
"gyro_y": 0,
231-
"gyro_z": 0,
232-
}
202+
self.__state = {}
203+
self.__state[CONSTANTS.CLUE_STATE.BUTTON_A] = False
204+
self.__state[CONSTANTS.CLUE_STATE.BUTTON_B] = False
205+
self.__state[CONSTANTS.CLUE_STATE.PRESSED_BUTTONS] = set()
206+
self.__state[CONSTANTS.CLUE_STATE.SEA_LEVEL_PRESSURE] = 1013.25
207+
self.__state[CONSTANTS.CLUE_STATE.TEMPERATURE] = 0
208+
self.__state[CONSTANTS.CLUE_STATE.PROXIMITY] = 0
209+
self.__state[CONSTANTS.CLUE_STATE.GESTURE] = ""
210+
self.__state[CONSTANTS.CLUE_STATE.HUMIDITY] = 0
211+
self.__state[CONSTANTS.CLUE_STATE.PRESSURE] = 0
212+
self.__state[CONSTANTS.CLUE_STATE.PIXEL] = neopixel.NeoPixel(
213+
pin=CONSTANTS.CLUE_PIN, n=1, pixel_order=neopixel.RGB
214+
)
215+
# Accelerometer
216+
self.__state[CONSTANTS.CLUE_STATE.MOTION_X] = 0
217+
self.__state[CONSTANTS.CLUE_STATE.MOTION_Y] = 0
218+
self.__state[CONSTANTS.CLUE_STATE.MOTION_Z] = 0
219+
# Light/color sensor
220+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_R] = 0
221+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_G] = 0
222+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_B] = 0
223+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_C] = 0
224+
# Magnetometer
225+
self.__state[CONSTANTS.CLUE_STATE.MAGNET_X] = 0
226+
self.__state[CONSTANTS.CLUE_STATE.MAGNET_Y] = 0
227+
self.__state[CONSTANTS.CLUE_STATE.MAGNET_Z] = 0
228+
# Gyroscope
229+
self.__state[CONSTANTS.CLUE_STATE.GYRO_X] = 0
230+
self.__state[CONSTANTS.CLUE_STATE.GYRO_Y] = 0
231+
self.__state[CONSTANTS.CLUE_STATE.GYRO_Z] = 0
233232

234233
@property
235234
def button_a(self):
@@ -242,7 +241,7 @@ def button_a(self):
242241
if clue.button_a:
243242
print("Button A pressed")
244243
"""
245-
return self.__state["button_a"]
244+
return self.__state[CONSTANTS.CLUE_STATE.BUTTON_A]
246245

247246
@property
248247
def button_b(self):
@@ -255,7 +254,7 @@ def button_b(self):
255254
if clue.button_b:
256255
print("Button B pressed")
257256
"""
258-
return self.__state["button_b"]
257+
return self.__state[CONSTANTS.CLUE_STATE.BUTTON_B]
259258

260259
@property
261260
def were_pressed(self):
@@ -266,8 +265,8 @@ def were_pressed(self):
266265
while True:
267266
print(clue.were_pressed)
268267
"""
269-
ret = self.__state["pressed_buttons"].copy()
270-
self.__state["pressed_buttons"].clear()
268+
ret = self.__state[CONSTANTS.CLUE_STATE.PRESSED_BUTTONS].copy()
269+
self.__state[CONSTANTS.CLUE_STATE.PRESSED_BUTTONS].clear()
271270
return ret
272271

273272
@property
@@ -281,9 +280,9 @@ def acceleration(self):
281280
print("Accel: {:.2f} {:.2f} {:.2f}".format(*clue.acceleration))
282281
"""
283282
return (
284-
self.__state["motion_x"],
285-
self.__state["motion_y"],
286-
self.__state["motion_z"],
283+
self.__state[CONSTANTS.CLUE_STATE.MOTION_X],
284+
self.__state[CONSTANTS.CLUE_STATE.MOTION_Y],
285+
self.__state[CONSTANTS.CLUE_STATE.MOTION_Z],
287286
)
288287

289288
def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
@@ -313,10 +312,10 @@ def color(self):
313312
print("Color: R: {} G: {} B: {} C: {}".format(*clue.color))
314313
"""
315314
return (
316-
self.__state["light_r"],
317-
self.__state["light_g"],
318-
self.__state["light_b"],
319-
self.__state["light_c"],
315+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_R],
316+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_G],
317+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_B],
318+
self.__state[CONSTANTS.CLUE_STATE.LIGHT_C],
320319
)
321320

322321
@property
@@ -328,7 +327,7 @@ def temperature(self):
328327
from adafruit_clue import clue
329328
print("Temperature: {:.1f}C".format(clue.temperature))
330329
"""
331-
return self.__state["temperature"]
330+
return self.__state[CONSTANTS.CLUE_STATE.TEMPERATURE]
332331

333332
@property
334333
def magnetic(self):
@@ -341,9 +340,9 @@ def magnetic(self):
341340
print("Magnetic: {:.3f} {:.3f} {:.3f}".format(*clue.magnetic))
342341
"""
343342
return (
344-
self.__state["magnet_x"],
345-
self.__state["magnet_y"],
346-
self.__state["magnet_z"],
343+
self.__state[CONSTANTS.CLUE_STATE.MAGNET_X],
344+
self.__state[CONSTANTS.CLUE_STATE.MAGNET_Y],
345+
self.__state[CONSTANTS.CLUE_STATE.MAGNET_Z],
347346
)
348347

349348
@property
@@ -357,7 +356,7 @@ def proximity(self):
357356
while True:
358357
print("Proximity: {}".format(clue.proximity))
359358
"""
360-
return self.__state["proximity"]
359+
return self.__state[CONSTANTS.CLUE_STATE.PROXIMITY]
361360

362361
@property
363362
def gyro(self):
@@ -366,9 +365,9 @@ def gyro(self):
366365
print("Gyro: {:.2f} {:.2f} {:.2f}".format(*clue.gyro))
367366
"""
368367
return (
369-
self.__state["gyro_x"],
370-
self.__state["gyro_y"],
371-
self.__state["gyro_z"],
368+
self.__state[CONSTANTS.CLUE_STATE.GYRO_X],
369+
self.__state[CONSTANTS.CLUE_STATE.GYRO_Y],
370+
self.__state[CONSTANTS.CLUE_STATE.GYRO_Z],
372371
)
373372

374373
@property
@@ -384,7 +383,7 @@ def gesture(self):
384383
print("Gesture: {}".format(clue.gesture))
385384
"""
386385
gesture_mapping = {"": 0, "up": 1, "down": 2, "left": 3, "right": 4}
387-
return gesture_mapping[self.__state["gesture"]]
386+
return gesture_mapping[self.__state[CONSTANTS.CLUE_STATE.GESTURE]]
388387

389388
@property
390389
def humidity(self):
@@ -396,7 +395,7 @@ def humidity(self):
396395
while True:
397396
print("Humidity: {:.1f}%".format(clue.humidity))
398397
"""
399-
return self.__state["humidity"]
398+
return self.__state[CONSTANTS.CLUE_STATE.HUMIDITY]
400399

401400
@property
402401
def pressure(self):
@@ -407,7 +406,7 @@ def pressure(self):
407406
from adafruit_clue import clue
408407
print("Pressure: {:.3f}hPa".format(clue.pressure))
409408
"""
410-
return self.__state["pressure"]
409+
return self.__state[CONSTANTS.CLUE_STATE.PRESSURE]
411410

412411
@property
413412
def altitude(self):
@@ -422,7 +421,9 @@ def altitude(self):
422421
altitude = 44330 * (
423422
1.0
424423
- math.pow(
425-
self.__state["pressure"] / self.__state["sea_level_pressure"], 0.1903
424+
self.__state[CONSTANTS.CLUE_STATE.PRESSURE]
425+
/ self.__state[CONSTANTS.CLUE_STATE.SEA_LEVEL_PRESSURE],
426+
0.1903,
426427
)
427428
)
428429
return altitude
@@ -438,11 +439,11 @@ def sea_level_pressure(self):
438439
clue.sea_level_pressure = 1015
439440
print("Pressure: {:.3f}hPa".format(clue.pressure))
440441
"""
441-
return self.__state["sea_level_pressure"]
442+
return self.__state[CONSTANTS.CLUE_STATE.SEA_LEVEL_PRESSURE]
442443

443444
@sea_level_pressure.setter
444445
def sea_level_pressure(self, value):
445-
self.__state["sea_level_pressure"] = value
446+
self.__state[CONSTANTS.CLUE_STATE.SEA_LEVEL_PRESSURE] = value
446447

447448
@property
448449
def pixel(self):
@@ -454,7 +455,7 @@ def pixel(self):
454455
while True:
455456
clue.pixel.fill((255, 0, 255))
456457
"""
457-
return self.__state["pixel"]
458+
return self.__state[CONSTANTS.CLUE_STATE.PIXEL]
458459

459460
@property
460461
def touch_0(self):
@@ -729,14 +730,14 @@ def update_state(self, new_state):
729730

730731
# helpers
731732
def __update_button(self, button, value):
732-
if button == "button_a":
733+
if button == CONSTANTS.CLUE_STATE.BUTTON_A:
733734
if value:
734-
self.__state["pressed_buttons"].add("A")
735-
self.__state["button_a"] = value
736-
elif button == "button_b":
735+
self.__state[CONSTANTS.CLUE_STATE.PRESSED_BUTTONS].add("A")
736+
self.__state[CONSTANTS.CLUE_STATE.BUTTON_A] = value
737+
elif button == CONSTANTS.CLUE_STATE.BUTTON_B:
737738
if value:
738-
self.__state["pressed_buttons"].add("B")
739-
self.__state["button_b"] = value
739+
self.__state[CONSTANTS.CLUE_STATE.PRESSED_BUTTONS].add("B")
740+
self.__state[CONSTANTS.CLUE_STATE.BUTTON_B] = value
740741

741742

742743
clue = Clue() # pylint: disable=invalid-name

0 commit comments

Comments
 (0)