Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 46802b2

Browse files
committed
Merge branch 'users/t-vali/clue-python-sensors' into users/t-xunguy/clue-sensors
2 parents 7441dc5 + f744acf commit 46802b2

File tree

3 files changed

+322
-27
lines changed

3 files changed

+322
-27
lines changed

src/base_circuitpython/base_cp_constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
IMG_DIR_NAME = "img"
88
SCREEN_HEIGHT_WIDTH = 240
99

10-
EXPECTED_INPUT_BUTTONS = ["button_a", "button_b"]
10+
EXPECTED_INPUT_BUTTONS = set(["button_a", "button_b"])
11+
12+
ALL_EXPECTED_INPUT_EVENTS = set([])

src/clue/adafruit_clue.py

Lines changed: 196 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,24 @@ 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._a = False
203-
self._b = False
204-
self.__pressed_buttons = set()
205-
self._pixel = neopixel.NeoPixel(
206-
pin=CONSTANTS.CLUE_PIN, n=1, pixel_order=neopixel.RGB
207-
)
202+
self.__state = {
203+
"button_a": False,
204+
"button_b": False,
205+
"pressed_buttons": set(),
206+
"acceleration": {"x": 0, "y": 0, "z": 0},
207+
"color_sensor": {"r": 0, "g": 0, "b": 0, "c": 0},
208+
"magnetometer": {"x": 0, "y": 0, "z": 0},
209+
"gyro": {"x": 0, "y": 0, "z": 0},
210+
"sea_level_pressure": 1013.25,
211+
"temperature": 0,
212+
"proximity": 0,
213+
"gesture": 0, # Can only be 0, 1, 2, 3, 4
214+
"humidity": 0,
215+
"pressure": 0,
216+
"pixel": neopixel.NeoPixel(
217+
pin=CONSTANTS.CLUE_PIN, n=1, pixel_order=neopixel.RGB
218+
),
219+
}
208220

209221
@property
210222
def button_a(self):
@@ -217,7 +229,7 @@ def button_a(self):
217229
if clue.button_a:
218230
print("Button A pressed")
219231
"""
220-
return self._a
232+
return self.__state["button_a"]
221233

222234
@property
223235
def button_b(self):
@@ -230,17 +242,7 @@ def button_b(self):
230242
if clue.button_b:
231243
print("Button B pressed")
232244
"""
233-
return self._b
234-
235-
def __update_button(self, button, value):
236-
if button == "button_a":
237-
if value:
238-
self.__pressed_buttons.add("A")
239-
self._a = value
240-
elif button == "button_b":
241-
if value:
242-
self.__pressed_buttons.add("B")
243-
self._b = value
245+
return self.__state["button_b"]
244246

245247
@property
246248
def were_pressed(self):
@@ -251,10 +253,168 @@ def were_pressed(self):
251253
while True:
252254
print(clue.were_pressed)
253255
"""
254-
ret = self.__pressed_buttons.copy()
255-
self.__pressed_buttons.clear()
256+
ret = self.__state["pressed_buttons"].copy()
257+
self.__state["pressed_buttons"].clear()
256258
return ret
257259

260+
@property
261+
def acceleration(self):
262+
"""Obtain acceleration data from the x, y and z axes.
263+
This example prints the values. Try moving the board to see how the printed values change.
264+
To use with the CLUE:
265+
.. code-block:: python
266+
from adafruit_clue import clue
267+
while True:
268+
print("Accel: {:.2f} {:.2f} {:.2f}".format(*clue.acceleration))
269+
"""
270+
return (
271+
self.__state["acceleration"]["x"],
272+
self.__state["acceleration"]["y"],
273+
self.__state["acceleration"]["z"],
274+
)
275+
276+
@property
277+
def color(self):
278+
"""The red, green, blue, and clear light values. (r, g, b, c)
279+
This example prints the values. Try holding something up to the sensor to see the values
280+
change. Works best with white LEDs enabled.
281+
To use with the CLUE:
282+
.. code-block:: python
283+
from adafruit_clue import clue
284+
while True:
285+
print("Color: R: {} G: {} B: {} C: {}".format(*clue.color))
286+
"""
287+
return (
288+
self.__state["color_sensor"]["r"],
289+
self.__state["color_sensor"]["g"],
290+
self.__state["color_sensor"]["b"],
291+
self.__state["color_sensor"]["c"],
292+
)
293+
294+
@property
295+
def temperature(self):
296+
"""The temperature in degrees Celsius.
297+
This example prints the value. Try touching the sensor to see the value change.
298+
To use with the CLUE:
299+
.. code-block:: python
300+
from adafruit_clue import clue
301+
print("Temperature: {:.1f}C".format(clue.temperature))
302+
"""
303+
return self.__state["temperature"]
304+
305+
@property
306+
def magnetic(self):
307+
"""Obtain x, y, z magnetic values in microteslas.
308+
This example prints the values. Try moving the board to see how the printed values change.
309+
To use with the CLUE:
310+
.. code-block:: python
311+
from adafruit_clue import clue
312+
while True:
313+
print("Magnetic: {:.3f} {:.3f} {:.3f}".format(*clue.magnetic))
314+
"""
315+
return (
316+
self.__state["magnetometer"]["x"],
317+
self.__state["magnetometer"]["y"],
318+
self.__state["magnetometer"]["z"],
319+
)
320+
321+
@property
322+
def proximity(self):
323+
"""A relative proximity to the sensor in values from 0 - 255.
324+
This example prints the value. Try moving your hand towards and away from the front of the
325+
board to see how the printed values change.
326+
To use with the CLUE:
327+
.. code-block:: python
328+
from adafruit_clue import clue
329+
while True:
330+
print("Proximity: {}".format(clue.proximity))
331+
"""
332+
return self.__state["proximity"]
333+
334+
@property
335+
def gyro(self):
336+
"""Obtain x, y, z angular velocity values in degrees/second.
337+
This example prints the values. Try moving the board to see how the printed values change.
338+
print("Gyro: {:.2f} {:.2f} {:.2f}".format(*clue.gyro))
339+
"""
340+
return (
341+
self.__state["gyro"]["x"],
342+
self.__state["gyro"]["y"],
343+
self.__state["gyro"]["z"],
344+
)
345+
346+
@property
347+
def gesture(self):
348+
"""A gesture code if gesture is detected. Shows ``0`` if no gesture detected.
349+
``1`` if an UP gesture is detected, ``2`` if DOWN, ``3`` if LEFT, and ``4`` if RIGHT.
350+
This example prints the gesture values. Try moving your hand up, down, left or right over
351+
the sensor to see the value change.
352+
To use with the CLUE:
353+
.. code-block:: python
354+
from adafruit_clue import clue
355+
while True:
356+
print("Gesture: {}".format(clue.gesture))
357+
"""
358+
return self.__state["gesture"]
359+
360+
@property
361+
def humidity(self):
362+
"""The measured relative humidity in percent.
363+
This example prints the value. Try breathing on the sensor to see the values change.
364+
To use with the CLUE:
365+
.. code-block:: python
366+
from adafruit_clue import clue
367+
while True:
368+
print("Humidity: {:.1f}%".format(clue.humidity))
369+
"""
370+
return self.__state["humidity"]
371+
372+
@property
373+
def pressure(self):
374+
"""The barometric pressure in hectoPascals.
375+
This example prints the value.
376+
To use with the CLUE:
377+
.. code-block:: python
378+
from adafruit_clue import clue
379+
print("Pressure: {:.3f}hPa".format(clue.pressure))
380+
"""
381+
return self.__state["pressure"]
382+
383+
@property
384+
def altitude(self):
385+
"""The altitude in meters based on the sea level pressure at your location. You must set
386+
``sea_level_pressure`` to receive an accurate reading.
387+
This example prints the value. Try moving the board vertically to see the value change.
388+
.. code-block:: python
389+
from adafruit_clue import clue
390+
clue.sea_level_pressure = 1015
391+
print("Altitude: {:.1f}m".format(clue.altitude))
392+
"""
393+
altitude = 44330 * (
394+
1.0
395+
- math.pow(
396+
self.__state["pressure"] / self.__state["sea_level_pressure"], 0.1903
397+
)
398+
)
399+
return altitude
400+
401+
@property
402+
def sea_level_pressure(self):
403+
"""Set to the pressure at sea level at your location, before reading altitude for
404+
the most accurate altitude measurement.
405+
This example prints the value.
406+
To use with the CLUE:
407+
.. code-block:: python
408+
from adafruit_clue import clue
409+
clue.sea_level_pressure = 1015
410+
print("Pressure: {:.3f}hPa".format(clue.pressure))
411+
"""
412+
return self.__state["sea_level_pressure"]
413+
414+
@sea_level_pressure.setter
415+
def sea_level_pressure(self, value):
416+
self.__state["sea_level_pressure"] = value
417+
258418
@property
259419
def pixel(self):
260420
"""The NeoPixel RGB LED.
@@ -265,7 +425,7 @@ def pixel(self):
265425
while True:
266426
clue.pixel.fill((255, 0, 255))
267427
"""
268-
return self._pixel
428+
return self.__state["pixel"]
269429

270430
@staticmethod
271431
def simple_text_display(
@@ -335,13 +495,23 @@ def simple_text_display(
335495
)
336496

337497
def update_state(self, new_state):
338-
self.__update_buttons(new_state)
498+
for event in new_state.keys():
499+
if event in CONSTANTS.EXPECTED_INPUT_BUTTONS:
500+
self.__update_button(event, new_state.get(event))
501+
elif event in CONSTANTS.ALL_EXPECTED_INPUT_EVENTS:
502+
if self.__state[event] != new_state[event]:
503+
self.__state[event] = new_state.get(event)
339504

340505
# helpers
341-
def __update_buttons(self, new_state):
342-
# get button pushes
343-
for button_name in CONSTANTS.EXPECTED_INPUT_BUTTONS:
344-
self.__update_button(button_name, new_state.get(button_name))
506+
def __update_button(self, button, value):
507+
if button == "button_a":
508+
if value:
509+
self.__state["pressed_buttons"].add("A")
510+
self.__state["button_a"] = value
511+
elif button == "button_b":
512+
if value:
513+
self.__state["pressed_buttons"].add("B")
514+
self.__state["button_b"] = value
345515

346516

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

0 commit comments

Comments
 (0)