Skip to content

Commit 66606f7

Browse files
authored
Merge pull request #118 from tcfranks/main
Annotation fixes in circuitplayground_base and bluefruit.py
2 parents d64abd5 + 894e8c9 commit 66606f7

File tree

4 files changed

+63
-47
lines changed

4 files changed

+63
-47
lines changed

adafruit_circuitplayground/bluefruit.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
* `Circuit Playground Bluefruit <https://www.adafruit.com/product/4333>`_
1919
2020
"""
21+
try:
22+
import typing # pylint: disable=unused-import
23+
except ImportError:
24+
pass
2125

2226
import array
2327
import math
@@ -38,7 +42,7 @@ class Bluefruit(CircuitPlaygroundBase):
3842

3943
_audio_out = audiopwmio.PWMAudioOut
4044

41-
def __init__(self):
45+
def __init__(self) -> None:
4246
# Only create the cpb module member when we aren't being imported by Sphinx
4347
if (
4448
"__module__" in dir(digitalio.DigitalInOut)
@@ -60,7 +64,7 @@ def __init__(self):
6064
self._samples = None
6165

6266
@staticmethod
63-
def _normalized_rms(values):
67+
def _normalized_rms(values) -> float:
6468
mean_values = int(sum(values) / len(values))
6569
return math.sqrt(
6670
sum(
@@ -71,7 +75,7 @@ def _normalized_rms(values):
7175
)
7276

7377
@property
74-
def sound_level(self):
78+
def sound_level(self) -> float:
7579
"""Obtain the sound level from the microphone (sound sensor).
7680
7781
.. image :: ../docs/_static/microphone.jpg
@@ -92,7 +96,7 @@ def sound_level(self):
9296
self._mic.record(self._samples, len(self._samples))
9397
return self._normalized_rms(self._samples)
9498

95-
def loud_sound(self, sound_threshold=200):
99+
def loud_sound(self, sound_threshold: int = 200) -> bool:
96100
"""Utilise a loud sound as an input.
97101
98102
:param int sound_threshold: Threshold sound level must exceed to return true (Default: 200)
@@ -134,7 +138,7 @@ def loud_sound(self, sound_threshold=200):
134138

135139
return self.sound_level > sound_threshold
136140

137-
def play_mp3(self, file_name):
141+
def play_mp3(self, file_name: str) -> None:
138142
"""Play a .mp3 file using the onboard speaker.
139143
140144
:param file_name: The name of your .mp3 file in quotation marks including .mp3

adafruit_circuitplayground/circuit_playground_base.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
import neopixel
3434
import touchio
3535

36+
try:
37+
from typing import Optional, Iterator
38+
from typing_extensions import Literal
39+
from microcontroller import Pin
40+
except ImportError:
41+
pass
42+
3643
__version__ = "0.0.0-auto.0"
3744
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git"
3845

@@ -41,12 +48,12 @@ class Photocell:
4148
"""Simple driver for analog photocell on the Circuit Playground Express and Bluefruit."""
4249

4350
# pylint: disable=too-few-public-methods
44-
def __init__(self, pin):
51+
def __init__(self, pin: Pin) -> None:
4552
self._photocell = analogio.AnalogIn(pin)
4653

4754
# TODO(tannewt): Calibrate this against another calibrated sensor.
4855
@property
49-
def light(self):
56+
def light(self) -> int:
5057
"""Light level."""
5158
return self._photocell.value * 330 // (2**16)
5259

@@ -58,7 +65,7 @@ class CircuitPlaygroundBase: # pylint: disable=too-many-public-methods
5865
SINE_WAVE = 0
5966
SQUARE_WAVE = 1
6067

61-
def __init__(self):
68+
def __init__(self) -> None:
6269
# Define switch:
6370
self._switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
6471
self._switch.switch_to_input(pull=digitalio.Pull.UP)
@@ -117,7 +124,7 @@ def __init__(self):
117124
self._b = None
118125

119126
@property
120-
def detect_taps(self):
127+
def detect_taps(self) -> Literal[1, 2]:
121128
"""Configure what type of tap is detected by ``cp.tapped``. Use ``1`` for single-tap
122129
detection and ``2`` for double-tap detection. This does nothing without ``cp.tapped``.
123130
@@ -138,7 +145,7 @@ def detect_taps(self):
138145
return self._detect_taps
139146

140147
@staticmethod
141-
def _default_tap_threshold(tap):
148+
def _default_tap_threshold(tap: Literal[1, 2]) -> int:
142149
if (
143150
"nRF52840" in os.uname().machine
144151
): # If we're on a CPB, use a higher tap threshold
@@ -148,7 +155,7 @@ def _default_tap_threshold(tap):
148155
return 90 if tap == 1 else 60
149156

150157
@detect_taps.setter
151-
def detect_taps(self, value):
158+
def detect_taps(self, value: Literal[1, 2]) -> None:
152159
self._detect_taps = value
153160
if value == 1:
154161
self._lis3dh.set_tap(
@@ -169,13 +176,13 @@ def detect_taps(self, value):
169176

170177
def configure_tap( # pylint: disable-msg=too-many-arguments
171178
self,
172-
tap,
173-
accel_range=adafruit_lis3dh.RANGE_8_G,
174-
threshold=None,
175-
time_limit=None,
176-
time_latency=50,
177-
time_window=255,
178-
):
179+
tap: Literal[0, 1, 2],
180+
accel_range: Literal[0, 1, 2, 3] = adafruit_lis3dh.RANGE_8_G,
181+
threshold: Optional[int] = None,
182+
time_limit: Optional[int] = None,
183+
time_latency: int = 50,
184+
time_window: int = 255,
185+
) -> None:
179186
"""Granular configuration of tap parameters. Expose the power of the
180187
adafruit_lis3dh module.
181188
@@ -245,7 +252,7 @@ def configure_tap( # pylint: disable-msg=too-many-arguments
245252
)
246253

247254
@property
248-
def tapped(self):
255+
def tapped(self) -> bool:
249256
"""True once after a detecting a tap. Requires ``cp.detect_taps``.
250257
251258
.. image :: ../docs/_static/accelerometer.jpg
@@ -297,7 +304,7 @@ def tapped(self):
297304
return self._lis3dh.tapped
298305

299306
@property
300-
def acceleration(self):
307+
def acceleration(self) -> adafruit_lis3dh.AccelerationTuple:
301308
"""Obtain data from the x, y and z axes.
302309
303310
.. image :: ../docs/_static/accelerometer.jpg
@@ -318,7 +325,7 @@ def acceleration(self):
318325
"""
319326
return self._lis3dh.acceleration
320327

321-
def shake(self, shake_threshold=30):
328+
def shake(self, shake_threshold: int = 30) -> bool:
322329
"""Detect when device is shaken.
323330
324331
:param int shake_threshold: The threshold shake must exceed to return true (Default: 30)
@@ -352,7 +359,7 @@ def shake(self, shake_threshold=30):
352359
"""
353360
return self._lis3dh.shake(shake_threshold=shake_threshold)
354361

355-
def _touch(self, i):
362+
def _touch(self, i) -> bool:
356363
if not isinstance(self._touches[i], touchio.TouchIn):
357364
# First time referenced. Get the pin from the slot for this touch
358365
# and replace it with a TouchIn object for the pin.
@@ -364,7 +371,7 @@ def _touch(self, i):
364371
# lists and the capital A to match the pin name. The capitalization is not strictly Python
365372
# style, so everywhere we use these names, we whitelist the errors using:
366373
@property
367-
def touch_A1(self): # pylint: disable=invalid-name
374+
def touch_A1(self) -> bool: # pylint: disable=invalid-name
368375
"""Detect touch on capacitive touch pad A1.
369376
370377
.. image :: ../docs/_static/capacitive_touch_pad_A1.jpg
@@ -383,7 +390,7 @@ def touch_A1(self): # pylint: disable=invalid-name
383390
return self._touch(1)
384391

385392
@property
386-
def touch_A2(self): # pylint: disable=invalid-name
393+
def touch_A2(self) -> bool: # pylint: disable=invalid-name
387394
"""Detect touch on capacitive touch pad A2.
388395
389396
.. image :: ../docs/_static/capacitive_touch_pad_A2.jpg
@@ -402,7 +409,7 @@ def touch_A2(self): # pylint: disable=invalid-name
402409
return self._touch(2)
403410

404411
@property
405-
def touch_A3(self): # pylint: disable=invalid-name
412+
def touch_A3(self) -> bool: # pylint: disable=invalid-name
406413
"""Detect touch on capacitive touch pad A3.
407414
408415
.. image :: ../docs/_static/capacitive_touch_pad_A3.jpg
@@ -421,7 +428,7 @@ def touch_A3(self): # pylint: disable=invalid-name
421428
return self._touch(3)
422429

423430
@property
424-
def touch_A4(self): # pylint: disable=invalid-name
431+
def touch_A4(self) -> bool: # pylint: disable=invalid-name
425432
"""Detect touch on capacitive touch pad A4.
426433
427434
.. image :: ../docs/_static/capacitive_touch_pad_A4.jpg
@@ -440,7 +447,7 @@ def touch_A4(self): # pylint: disable=invalid-name
440447
return self._touch(4)
441448

442449
@property
443-
def touch_A5(self): # pylint: disable=invalid-name
450+
def touch_A5(self) -> bool: # pylint: disable=invalid-name
444451
"""Detect touch on capacitive touch pad A5.
445452
446453
.. image :: ../docs/_static/capacitive_touch_pad_A5.jpg
@@ -459,7 +466,7 @@ def touch_A5(self): # pylint: disable=invalid-name
459466
return self._touch(5)
460467

461468
@property
462-
def touch_A6(self): # pylint: disable=invalid-name
469+
def touch_A6(self) -> bool: # pylint: disable=invalid-name
463470
"""Detect touch on capacitive touch pad A6.
464471
465472
.. image :: ../docs/_static/capacitive_touch_pad_A6.jpg
@@ -478,7 +485,7 @@ def touch_A6(self): # pylint: disable=invalid-name
478485
return self._touch(6)
479486

480487
@property
481-
def touch_TX(self): # pylint: disable=invalid-name
488+
def touch_TX(self) -> bool: # pylint: disable=invalid-name
482489
"""Detect touch on capacitive touch pad TX (also known as A7 on the Circuit Playground
483490
Express) Note: can be called as ``touch_A7`` on Circuit Playground Express.
484491
@@ -497,7 +504,7 @@ def touch_TX(self): # pylint: disable=invalid-name
497504
"""
498505
return self._touch(7)
499506

500-
def adjust_touch_threshold(self, adjustment):
507+
def adjust_touch_threshold(self, adjustment: int) -> None:
501508
"""Adjust the threshold needed to activate the capacitive touch pads.
502509
Higher numbers make the touch pads less sensitive.
503510
@@ -524,7 +531,7 @@ def adjust_touch_threshold(self, adjustment):
524531
self._touch_threshold_adjustment += adjustment
525532

526533
@property
527-
def pixels(self):
534+
def pixels(self) -> neopixel.NeoPixel:
528535
"""Sequence-like object representing the ten NeoPixels around the outside
529536
of the Circuit Playground. Each pixel is at a certain index in the sequence
530537
as labeled below. Colors can be RGB hex like 0x110000 for red where each
@@ -552,7 +559,7 @@ def pixels(self):
552559
return self._pixels
553560

554561
@property
555-
def button_a(self):
562+
def button_a(self) -> bool:
556563
"""``True`` when Button A is pressed. ``False`` if not.
557564
558565
.. image :: ../docs/_static/button_a.jpg
@@ -574,7 +581,7 @@ def button_a(self):
574581
return self._a.value
575582

576583
@property
577-
def button_b(self):
584+
def button_b(self) -> bool:
578585
"""``True`` when Button B is pressed. ``False`` if not.
579586
580587
.. image :: ../docs/_static/button_b.jpg
@@ -596,7 +603,7 @@ def button_b(self):
596603
return self._b.value
597604

598605
@property
599-
def switch(self):
606+
def switch(self) -> bool:
600607
"""``True`` when the switch is to the left next to the music notes.
601608
``False`` when it is to the right towards the ear.
602609
@@ -617,7 +624,7 @@ def switch(self):
617624
return self._switch.value
618625

619626
@property
620-
def temperature(self):
627+
def temperature(self) -> float:
621628
"""The temperature in Celsius.
622629
623630
.. image :: ../docs/_static/thermistor.jpg
@@ -642,7 +649,7 @@ def temperature(self):
642649
return self._temp.temperature
643650

644651
@property
645-
def light(self):
652+
def light(self) -> int:
646653
"""The light level.
647654
648655
.. image :: ../docs/_static/light_sensor.jpg
@@ -664,7 +671,7 @@ def light(self):
664671
return self._light.light
665672

666673
@property
667-
def red_led(self):
674+
def red_led(self) -> bool:
668675
"""The red led next to the USB plug marked D13.
669676
670677
.. image :: ../docs/_static/red_led.jpg
@@ -686,19 +693,19 @@ def red_led(self):
686693
return self._led.value
687694

688695
@red_led.setter
689-
def red_led(self, value):
696+
def red_led(self, value: bool) -> None:
690697
self._led.value = value
691698

692699
@staticmethod
693-
def _sine_sample(length):
700+
def _sine_sample(length: int) -> Iterator[int]:
694701
tone_volume = (2**15) - 1
695702
# Amplitude shift up in order to not have negative numbers
696703
shift = 2**15
697704
for i in range(length):
698705
yield int(tone_volume * math.sin(2 * math.pi * (i / length)) + shift)
699706

700707
@staticmethod
701-
def _square_sample(length):
708+
def _square_sample(length: int) -> Iterator[int]:
702709
# Square waves are MUCH louder than then sine
703710
tone_volume = (2**16) - 1
704711
half_length = length // 2
@@ -707,7 +714,7 @@ def _square_sample(length):
707714
for _ in range(half_length):
708715
yield 0
709716

710-
def _generate_sample(self, length=100, waveform=SINE_WAVE):
717+
def _generate_sample(self, length: int = 100, waveform: int = SINE_WAVE) -> None:
711718
if self._sample is not None:
712719
return
713720
if waveform == self.SQUARE_WAVE:
@@ -717,13 +724,15 @@ def _generate_sample(self, length=100, waveform=SINE_WAVE):
717724
self._sample = self._audio_out(board.SPEAKER) # pylint: disable=not-callable
718725
self._wave_sample = audiocore.RawSample(self._wave)
719726

720-
def play_tone(self, frequency, duration, waveform=SINE_WAVE):
727+
def play_tone(
728+
self, frequency: int, duration: float, waveform: int = SINE_WAVE
729+
) -> None:
721730
"""Produce a tone using the speaker. Try changing frequency to change
722731
the pitch of the tone.
723732
724733
:param int frequency: The frequency of the tone in Hz
725734
:param float duration: The duration of the tone in seconds
726-
:param str waveform: Type of waveform to be generated [SINE_WAVE, SQUARE_WAVE].
735+
:param int waveform: Type of waveform to be generated [SINE_WAVE, SQUARE_WAVE].
727736
728737
Default is SINE_WAVE.
729738
@@ -743,12 +752,12 @@ def play_tone(self, frequency, duration, waveform=SINE_WAVE):
743752
time.sleep(duration)
744753
self.stop_tone()
745754

746-
def start_tone(self, frequency, waveform=SINE_WAVE):
755+
def start_tone(self, frequency: int, waveform: int = SINE_WAVE) -> None:
747756
"""Produce a tone using the speaker. Try changing frequency to change
748757
the pitch of the tone.
749758
750759
:param int frequency: The frequency of the tone in Hz
751-
:param str waveform: Type of waveform to be generated [SINE_WAVE, SQUARE_WAVE].
760+
:param int waveform: Type of waveform to be generated [SINE_WAVE, SQUARE_WAVE].
752761
753762
Default is SINE_WAVE.
754763
@@ -779,7 +788,7 @@ def start_tone(self, frequency, waveform=SINE_WAVE):
779788
if not self._sample.playing:
780789
self._sample.play(self._wave_sample, loop=True)
781790

782-
def stop_tone(self):
791+
def stop_tone(self) -> None:
783792
"""Use with start_tone to stop the tone produced.
784793
785794
.. image :: ../docs/_static/speaker.jpg
@@ -806,7 +815,7 @@ def stop_tone(self):
806815
self._sample = None
807816
self._speaker_enable.value = False
808817

809-
def play_file(self, file_name):
818+
def play_file(self, file_name: str) -> None:
810819
"""Play a .wav file using the onboard speaker.
811820
812821
:param file_name: The name of your .wav file in quotation marks including .wav

0 commit comments

Comments
 (0)