Skip to content

Commit 03b1d2a

Browse files
Merge pull request #59 from julianaklulo/main
Add type annotations to adafruit_clue.py
2 parents 1ecef42 + 654284a commit 03b1d2a

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

adafruit_clue.py

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel
4040
"""
4141

42+
try:
43+
from typing import Union, Tuple, Optional
44+
except ImportError:
45+
pass
46+
4247
import time
4348
import array
4449
import math
@@ -64,12 +69,12 @@ class _ClueSimpleTextDisplay:
6469

6570
def __init__( # pylint: disable=too-many-arguments
6671
self,
67-
title=None,
68-
title_color=0xFFFFFF,
69-
title_scale=1,
70-
text_scale=1,
71-
font=None,
72-
colors=None,
72+
title: Optional[str] = None,
73+
title_color: Union[int, Tuple[int, int, int]] = 0xFFFFFF,
74+
title_scale: int = 1,
75+
text_scale: int = 1,
76+
font: Optional[str] = None,
77+
colors: Optional[Tuple[Tuple[int, int, int], ...]] = None,
7378
):
7479
# pylint: disable=import-outside-toplevel
7580
import displayio
@@ -124,7 +129,7 @@ def __init__( # pylint: disable=too-many-arguments
124129
for num in range(1):
125130
self._lines.append(self.add_text_line(color=colors[num % len(colors)]))
126131

127-
def __getitem__(self, item):
132+
def __getitem__(self, item: int):
128133
"""Fetch the Nth text line Group"""
129134
if len(self._lines) - 1 < item:
130135
for _ in range(item - (len(self._lines) - 1)):
@@ -133,7 +138,7 @@ def __getitem__(self, item):
133138
)
134139
return self._lines[item]
135140

136-
def add_text_line(self, color=0xFFFFFF):
141+
def add_text_line(self, color: Union[int, Tuple[int, int, int]] = 0xFFFFFF):
137142
"""Adds a line on the display of the specified color and returns the label object."""
138143
text_label = self._label.Label(self._font, text="", color=color)
139144
text_label.x = 0
@@ -235,7 +240,7 @@ def __init__(self):
235240
# Create displayio object for passing.
236241
self.display = board.DISPLAY
237242

238-
def _touch(self, i):
243+
def _touch(self, i: int) -> bool:
239244
if not isinstance(self._touches[i], touchio.TouchIn):
240245
# First time referenced. Get the pin from the slot for this touch
241246
# and replace it with a TouchIn object for the pin.
@@ -244,7 +249,7 @@ def _touch(self, i):
244249
return self._touches[i].value
245250

246251
@property
247-
def touch_0(self):
252+
def touch_0(self) -> bool:
248253
"""Detect touch on capacitive touch pad 0.
249254
250255
.. image :: ../docs/_static/pad_0.jpg
@@ -265,7 +270,7 @@ def touch_0(self):
265270
return self._touch(0)
266271

267272
@property
268-
def touch_1(self):
273+
def touch_1(self) -> bool:
269274
"""Detect touch on capacitive touch pad 1.
270275
271276
.. image :: ../docs/_static/pad_1.jpg
@@ -286,7 +291,7 @@ def touch_1(self):
286291
return self._touch(1)
287292

288293
@property
289-
def touch_2(self):
294+
def touch_2(self) -> bool:
290295
"""Detect touch on capacitive touch pad 2.
291296
292297
.. image :: ../docs/_static/pad_2.jpg
@@ -307,7 +312,7 @@ def touch_2(self):
307312
return self._touch(2)
308313

309314
@property
310-
def button_a(self):
315+
def button_a(self) -> bool:
311316
"""``True`` when Button A is pressed. ``False`` if not.
312317
313318
.. image :: ../docs/_static/button_a.jpg
@@ -328,7 +333,7 @@ def button_a(self):
328333
return not self._a.value
329334

330335
@property
331-
def button_b(self):
336+
def button_b(self) -> bool:
332337
"""``True`` when Button B is pressed. ``False`` if not.
333338
334339
.. image :: ../docs/_static/button_b.jpg
@@ -348,7 +353,9 @@ def button_b(self):
348353
"""
349354
return not self._b.value
350355

351-
def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
356+
def shake(
357+
self, shake_threshold: int = 30, avg_count: int = 10, total_delay: float = 0.1
358+
) -> bool:
352359
"""
353360
Detect when the accelerometer is shaken. Optional parameters:
354361
@@ -380,7 +387,7 @@ def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
380387
return total_accel > shake_threshold
381388

382389
@property
383-
def acceleration(self):
390+
def acceleration(self) -> Tuple[int, int, int]:
384391
"""Obtain acceleration data from the x, y and z axes.
385392
386393
.. image :: ../docs/_static/accelerometer.jpg
@@ -400,7 +407,7 @@ def acceleration(self):
400407
return self._accelerometer.acceleration
401408

402409
@property
403-
def gyro(self):
410+
def gyro(self) -> Tuple[int, int, int]:
404411
"""Obtain x, y, z angular velocity values in degrees/second.
405412
406413
.. image :: ../docs/_static/accelerometer.jpg
@@ -420,7 +427,7 @@ def gyro(self):
420427
return self._accelerometer.gyro
421428

422429
@property
423-
def magnetic(self):
430+
def magnetic(self) -> Tuple[int, int, int]:
424431
"""Obtain x, y, z magnetic values in microteslas.
425432
426433
.. image :: ../docs/_static/magnetometer.jpg
@@ -440,7 +447,7 @@ def magnetic(self):
440447
return self._magnetometer.magnetic
441448

442449
@property
443-
def proximity(self):
450+
def proximity(self) -> int:
444451
"""A relative proximity to the sensor in values from 0 - 255.
445452
446453
.. image :: ../docs/_static/proximity.jpg
@@ -462,7 +469,7 @@ def proximity(self):
462469
return self._sensor.proximity
463470

464471
@property
465-
def color(self):
472+
def color(self) -> Tuple[int, int, int, int]:
466473
"""The red, green, blue, and clear light values. (r, g, b, c)
467474
468475
.. image :: ../docs/_static/proximity.jpg
@@ -484,7 +491,7 @@ def color(self):
484491
return self._sensor.color_data
485492

486493
@property
487-
def gesture(self):
494+
def gesture(self) -> int:
488495
"""A gesture code if gesture is detected. Shows ``0`` if no gesture detected.
489496
``1`` if an UP gesture is detected, ``2`` if DOWN, ``3`` if LEFT, and ``4`` if RIGHT.
490497
@@ -512,7 +519,7 @@ def gesture(self):
512519
return self._sensor.gesture()
513520

514521
@property
515-
def humidity(self):
522+
def humidity(self) -> float:
516523
"""The measured relative humidity in percent.
517524
518525
.. image :: ../docs/_static/humidity.jpg
@@ -532,7 +539,7 @@ def humidity(self):
532539
return self._humidity.relative_humidity
533540

534541
@property
535-
def pressure(self):
542+
def pressure(self) -> float:
536543
"""The barometric pressure in hectoPascals.
537544
538545
.. image :: ../docs/_static/pressure.jpg
@@ -551,7 +558,7 @@ def pressure(self):
551558
return self._pressure.pressure
552559

553560
@property
554-
def temperature(self):
561+
def temperature(self) -> float:
555562
"""The temperature in degrees Celsius.
556563
557564
.. image :: ../docs/_static/pressure.jpg
@@ -570,7 +577,7 @@ def temperature(self):
570577
return self._pressure.temperature
571578

572579
@property
573-
def altitude(self):
580+
def altitude(self) -> float:
574581
"""The altitude in meters based on the sea level pressure at your location. You must set
575582
``sea_level_pressure`` to receive an accurate reading.
576583
@@ -590,7 +597,7 @@ def altitude(self):
590597
return self._pressure.altitude
591598

592599
@property
593-
def sea_level_pressure(self):
600+
def sea_level_pressure(self) -> float:
594601
"""Set to the pressure at sea level at your location, before reading altitude for
595602
the most accurate altitude measurement.
596603
@@ -612,11 +619,11 @@ def sea_level_pressure(self):
612619
return self._pressure.sea_level_pressure
613620

614621
@sea_level_pressure.setter
615-
def sea_level_pressure(self, value):
622+
def sea_level_pressure(self, value: float):
616623
self._pressure.sea_level_pressure = value
617624

618625
@property
619-
def white_leds(self):
626+
def white_leds(self) -> bool:
620627
"""The red led next to the USB plug labeled LED.
621628
622629
.. image :: ../docs/_static/white_leds.jpg
@@ -635,11 +642,11 @@ def white_leds(self):
635642
return self._white_leds.value
636643

637644
@white_leds.setter
638-
def white_leds(self, value):
645+
def white_leds(self, value: bool):
639646
self._white_leds.value = value
640647

641648
@property
642-
def red_led(self):
649+
def red_led(self) -> bool:
643650
"""The red led next to the USB plug labeled LED.
644651
645652
.. image :: ../docs/_static/red_led.jpg
@@ -658,11 +665,11 @@ def red_led(self):
658665
return self._red_led.value
659666

660667
@red_led.setter
661-
def red_led(self, value):
668+
def red_led(self, value: bool):
662669
self._red_led.value = value
663670

664671
@property
665-
def pixel(self):
672+
def pixel(self) -> neopixel.NeoPixel:
666673
"""The NeoPixel RGB LED.
667674
668675
.. image :: ../docs/_static/neopixel.jpg
@@ -682,20 +689,20 @@ def pixel(self):
682689
return self._pixel
683690

684691
@staticmethod
685-
def _sine_sample(length):
692+
def _sine_sample(length: int):
686693
tone_volume = (2**15) - 1
687694
shift = 2**15
688695
for i in range(length):
689696
yield int(tone_volume * math.sin(2 * math.pi * (i / length)) + shift)
690697

691-
def _generate_sample(self, length=100):
698+
def _generate_sample(self, length: int = 100):
692699
if self._audio_out is not None:
693700
return
694701
self._sine_wave = array.array("H", self._sine_sample(length))
695702
self._audio_out = audiopwmio.PWMAudioOut(board.SPEAKER)
696703
self._sine_wave_sample = audiocore.RawSample(self._sine_wave)
697704

698-
def play_tone(self, frequency, duration):
705+
def play_tone(self, frequency: int, duration: float):
699706
"""Produce a tone using the speaker. Try changing frequency to change
700707
the pitch of the tone.
701708
@@ -720,7 +727,7 @@ def play_tone(self, frequency, duration):
720727
time.sleep(duration)
721728
self.stop_tone()
722729

723-
def start_tone(self, frequency):
730+
def start_tone(self, frequency: int):
724731
"""Produce a tone using the speaker. Try changing frequency to change
725732
the pitch of the tone.
726733
@@ -785,7 +792,7 @@ def stop_tone(self):
785792
self._audio_out = None
786793

787794
@staticmethod
788-
def _normalized_rms(values):
795+
def _normalized_rms(values) -> float:
789796
mean_values = int(sum(values) / len(values))
790797
return math.sqrt(
791798
sum(
@@ -796,7 +803,7 @@ def _normalized_rms(values):
796803
)
797804

798805
@property
799-
def sound_level(self):
806+
def sound_level(self) -> float:
800807
"""Obtain the sound level from the microphone (sound sensor).
801808
802809
.. image :: ../docs/_static/microphone.jpg
@@ -817,7 +824,7 @@ def sound_level(self):
817824
self._mic.record(self._mic_samples, len(self._mic_samples))
818825
return self._normalized_rms(self._mic_samples)
819826

820-
def loud_sound(self, sound_threshold=200):
827+
def loud_sound(self, sound_threshold: int = 200) -> bool:
821828
"""Utilise a loud sound as an input.
822829
823830
:param int sound_threshold: Threshold sound level must exceed to return true (Default: 200)
@@ -861,12 +868,12 @@ def loud_sound(self, sound_threshold=200):
861868

862869
@staticmethod
863870
def simple_text_display( # pylint: disable=too-many-arguments
864-
title=None,
865-
title_color=(255, 255, 255),
866-
title_scale=1,
867-
text_scale=1,
868-
font=None,
869-
colors=None,
871+
title: Optional[str] = None,
872+
title_color: Tuple = (255, 255, 255),
873+
title_scale: int = 1,
874+
text_scale: int = 1,
875+
font: Optional[str] = None,
876+
colors: Optional[Tuple[Tuple[int, int, int], ...]] = None,
870877
):
871878
"""Display lines of text on the CLUE display. Lines of text are created in order as shown
872879
in the example below. If you skip a number, the line will be shown blank on the display,

0 commit comments

Comments
 (0)