33
33
import neopixel
34
34
import touchio
35
35
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
+
36
43
__version__ = "0.0.0-auto.0"
37
44
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git"
38
45
@@ -41,12 +48,12 @@ class Photocell:
41
48
"""Simple driver for analog photocell on the Circuit Playground Express and Bluefruit."""
42
49
43
50
# pylint: disable=too-few-public-methods
44
- def __init__ (self , pin ) :
51
+ def __init__ (self , pin : Pin ) -> None :
45
52
self ._photocell = analogio .AnalogIn (pin )
46
53
47
54
# TODO(tannewt): Calibrate this against another calibrated sensor.
48
55
@property
49
- def light (self ):
56
+ def light (self ) -> int :
50
57
"""Light level."""
51
58
return self ._photocell .value * 330 // (2 ** 16 )
52
59
@@ -58,7 +65,7 @@ class CircuitPlaygroundBase: # pylint: disable=too-many-public-methods
58
65
SINE_WAVE = 0
59
66
SQUARE_WAVE = 1
60
67
61
- def __init__ (self ):
68
+ def __init__ (self ) -> None :
62
69
# Define switch:
63
70
self ._switch = digitalio .DigitalInOut (board .SLIDE_SWITCH )
64
71
self ._switch .switch_to_input (pull = digitalio .Pull .UP )
@@ -117,7 +124,7 @@ def __init__(self):
117
124
self ._b = None
118
125
119
126
@property
120
- def detect_taps (self ):
127
+ def detect_taps (self ) -> Literal [ 1 , 2 ] :
121
128
"""Configure what type of tap is detected by ``cp.tapped``. Use ``1`` for single-tap
122
129
detection and ``2`` for double-tap detection. This does nothing without ``cp.tapped``.
123
130
@@ -138,7 +145,7 @@ def detect_taps(self):
138
145
return self ._detect_taps
139
146
140
147
@staticmethod
141
- def _default_tap_threshold (tap ) :
148
+ def _default_tap_threshold (tap : Literal [ 1 , 2 ]) -> int :
142
149
if (
143
150
"nRF52840" in os .uname ().machine
144
151
): # If we're on a CPB, use a higher tap threshold
@@ -148,7 +155,7 @@ def _default_tap_threshold(tap):
148
155
return 90 if tap == 1 else 60
149
156
150
157
@detect_taps .setter
151
- def detect_taps (self , value ) :
158
+ def detect_taps (self , value : Literal [ 1 , 2 ]) -> None :
152
159
self ._detect_taps = value
153
160
if value == 1 :
154
161
self ._lis3dh .set_tap (
@@ -169,13 +176,13 @@ def detect_taps(self, value):
169
176
170
177
def configure_tap ( # pylint: disable-msg=too-many-arguments
171
178
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 :
179
186
"""Granular configuration of tap parameters. Expose the power of the
180
187
adafruit_lis3dh module.
181
188
@@ -245,7 +252,7 @@ def configure_tap( # pylint: disable-msg=too-many-arguments
245
252
)
246
253
247
254
@property
248
- def tapped (self ):
255
+ def tapped (self ) -> bool :
249
256
"""True once after a detecting a tap. Requires ``cp.detect_taps``.
250
257
251
258
.. image :: ../docs/_static/accelerometer.jpg
@@ -297,7 +304,7 @@ def tapped(self):
297
304
return self ._lis3dh .tapped
298
305
299
306
@property
300
- def acceleration (self ):
307
+ def acceleration (self ) -> adafruit_lis3dh . AccelerationTuple :
301
308
"""Obtain data from the x, y and z axes.
302
309
303
310
.. image :: ../docs/_static/accelerometer.jpg
@@ -318,7 +325,7 @@ def acceleration(self):
318
325
"""
319
326
return self ._lis3dh .acceleration
320
327
321
- def shake (self , shake_threshold = 30 ):
328
+ def shake (self , shake_threshold : int = 30 ) -> bool :
322
329
"""Detect when device is shaken.
323
330
324
331
:param int shake_threshold: The threshold shake must exceed to return true (Default: 30)
@@ -352,7 +359,7 @@ def shake(self, shake_threshold=30):
352
359
"""
353
360
return self ._lis3dh .shake (shake_threshold = shake_threshold )
354
361
355
- def _touch (self , i ):
362
+ def _touch (self , i ) -> bool :
356
363
if not isinstance (self ._touches [i ], touchio .TouchIn ):
357
364
# First time referenced. Get the pin from the slot for this touch
358
365
# and replace it with a TouchIn object for the pin.
@@ -364,7 +371,7 @@ def _touch(self, i):
364
371
# lists and the capital A to match the pin name. The capitalization is not strictly Python
365
372
# style, so everywhere we use these names, we whitelist the errors using:
366
373
@property
367
- def touch_A1 (self ): # pylint: disable=invalid-name
374
+ def touch_A1 (self ) -> bool : # pylint: disable=invalid-name
368
375
"""Detect touch on capacitive touch pad A1.
369
376
370
377
.. image :: ../docs/_static/capacitive_touch_pad_A1.jpg
@@ -383,7 +390,7 @@ def touch_A1(self): # pylint: disable=invalid-name
383
390
return self ._touch (1 )
384
391
385
392
@property
386
- def touch_A2 (self ): # pylint: disable=invalid-name
393
+ def touch_A2 (self ) -> bool : # pylint: disable=invalid-name
387
394
"""Detect touch on capacitive touch pad A2.
388
395
389
396
.. image :: ../docs/_static/capacitive_touch_pad_A2.jpg
@@ -402,7 +409,7 @@ def touch_A2(self): # pylint: disable=invalid-name
402
409
return self ._touch (2 )
403
410
404
411
@property
405
- def touch_A3 (self ): # pylint: disable=invalid-name
412
+ def touch_A3 (self ) -> bool : # pylint: disable=invalid-name
406
413
"""Detect touch on capacitive touch pad A3.
407
414
408
415
.. image :: ../docs/_static/capacitive_touch_pad_A3.jpg
@@ -421,7 +428,7 @@ def touch_A3(self): # pylint: disable=invalid-name
421
428
return self ._touch (3 )
422
429
423
430
@property
424
- def touch_A4 (self ): # pylint: disable=invalid-name
431
+ def touch_A4 (self ) -> bool : # pylint: disable=invalid-name
425
432
"""Detect touch on capacitive touch pad A4.
426
433
427
434
.. image :: ../docs/_static/capacitive_touch_pad_A4.jpg
@@ -440,7 +447,7 @@ def touch_A4(self): # pylint: disable=invalid-name
440
447
return self ._touch (4 )
441
448
442
449
@property
443
- def touch_A5 (self ): # pylint: disable=invalid-name
450
+ def touch_A5 (self ) -> bool : # pylint: disable=invalid-name
444
451
"""Detect touch on capacitive touch pad A5.
445
452
446
453
.. image :: ../docs/_static/capacitive_touch_pad_A5.jpg
@@ -459,7 +466,7 @@ def touch_A5(self): # pylint: disable=invalid-name
459
466
return self ._touch (5 )
460
467
461
468
@property
462
- def touch_A6 (self ): # pylint: disable=invalid-name
469
+ def touch_A6 (self ) -> bool : # pylint: disable=invalid-name
463
470
"""Detect touch on capacitive touch pad A6.
464
471
465
472
.. image :: ../docs/_static/capacitive_touch_pad_A6.jpg
@@ -478,7 +485,7 @@ def touch_A6(self): # pylint: disable=invalid-name
478
485
return self ._touch (6 )
479
486
480
487
@property
481
- def touch_TX (self ): # pylint: disable=invalid-name
488
+ def touch_TX (self ) -> bool : # pylint: disable=invalid-name
482
489
"""Detect touch on capacitive touch pad TX (also known as A7 on the Circuit Playground
483
490
Express) Note: can be called as ``touch_A7`` on Circuit Playground Express.
484
491
@@ -497,7 +504,7 @@ def touch_TX(self): # pylint: disable=invalid-name
497
504
"""
498
505
return self ._touch (7 )
499
506
500
- def adjust_touch_threshold (self , adjustment ) :
507
+ def adjust_touch_threshold (self , adjustment : int ) -> None :
501
508
"""Adjust the threshold needed to activate the capacitive touch pads.
502
509
Higher numbers make the touch pads less sensitive.
503
510
@@ -524,7 +531,7 @@ def adjust_touch_threshold(self, adjustment):
524
531
self ._touch_threshold_adjustment += adjustment
525
532
526
533
@property
527
- def pixels (self ):
534
+ def pixels (self ) -> neopixel . NeoPixel :
528
535
"""Sequence-like object representing the ten NeoPixels around the outside
529
536
of the Circuit Playground. Each pixel is at a certain index in the sequence
530
537
as labeled below. Colors can be RGB hex like 0x110000 for red where each
@@ -552,7 +559,7 @@ def pixels(self):
552
559
return self ._pixels
553
560
554
561
@property
555
- def button_a (self ):
562
+ def button_a (self ) -> bool :
556
563
"""``True`` when Button A is pressed. ``False`` if not.
557
564
558
565
.. image :: ../docs/_static/button_a.jpg
@@ -574,7 +581,7 @@ def button_a(self):
574
581
return self ._a .value
575
582
576
583
@property
577
- def button_b (self ):
584
+ def button_b (self ) -> bool :
578
585
"""``True`` when Button B is pressed. ``False`` if not.
579
586
580
587
.. image :: ../docs/_static/button_b.jpg
@@ -596,7 +603,7 @@ def button_b(self):
596
603
return self ._b .value
597
604
598
605
@property
599
- def switch (self ):
606
+ def switch (self ) -> bool :
600
607
"""``True`` when the switch is to the left next to the music notes.
601
608
``False`` when it is to the right towards the ear.
602
609
@@ -617,7 +624,7 @@ def switch(self):
617
624
return self ._switch .value
618
625
619
626
@property
620
- def temperature (self ):
627
+ def temperature (self ) -> float :
621
628
"""The temperature in Celsius.
622
629
623
630
.. image :: ../docs/_static/thermistor.jpg
@@ -642,7 +649,7 @@ def temperature(self):
642
649
return self ._temp .temperature
643
650
644
651
@property
645
- def light (self ):
652
+ def light (self ) -> int :
646
653
"""The light level.
647
654
648
655
.. image :: ../docs/_static/light_sensor.jpg
@@ -664,7 +671,7 @@ def light(self):
664
671
return self ._light .light
665
672
666
673
@property
667
- def red_led (self ):
674
+ def red_led (self ) -> bool :
668
675
"""The red led next to the USB plug marked D13.
669
676
670
677
.. image :: ../docs/_static/red_led.jpg
@@ -686,19 +693,19 @@ def red_led(self):
686
693
return self ._led .value
687
694
688
695
@red_led .setter
689
- def red_led (self , value ) :
696
+ def red_led (self , value : bool ) -> None :
690
697
self ._led .value = value
691
698
692
699
@staticmethod
693
- def _sine_sample (length ) :
700
+ def _sine_sample (length : int ) -> Iterator [ int ] :
694
701
tone_volume = (2 ** 15 ) - 1
695
702
# Amplitude shift up in order to not have negative numbers
696
703
shift = 2 ** 15
697
704
for i in range (length ):
698
705
yield int (tone_volume * math .sin (2 * math .pi * (i / length )) + shift )
699
706
700
707
@staticmethod
701
- def _square_sample (length ) :
708
+ def _square_sample (length : int ) -> Iterator [ int ] :
702
709
# Square waves are MUCH louder than then sine
703
710
tone_volume = (2 ** 16 ) - 1
704
711
half_length = length // 2
@@ -707,7 +714,7 @@ def _square_sample(length):
707
714
for _ in range (half_length ):
708
715
yield 0
709
716
710
- def _generate_sample (self , length = 100 , waveform = SINE_WAVE ):
717
+ def _generate_sample (self , length : int = 100 , waveform : int = SINE_WAVE ) -> None :
711
718
if self ._sample is not None :
712
719
return
713
720
if waveform == self .SQUARE_WAVE :
@@ -717,13 +724,15 @@ def _generate_sample(self, length=100, waveform=SINE_WAVE):
717
724
self ._sample = self ._audio_out (board .SPEAKER ) # pylint: disable=not-callable
718
725
self ._wave_sample = audiocore .RawSample (self ._wave )
719
726
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 :
721
730
"""Produce a tone using the speaker. Try changing frequency to change
722
731
the pitch of the tone.
723
732
724
733
:param int frequency: The frequency of the tone in Hz
725
734
: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].
727
736
728
737
Default is SINE_WAVE.
729
738
@@ -743,12 +752,12 @@ def play_tone(self, frequency, duration, waveform=SINE_WAVE):
743
752
time .sleep (duration )
744
753
self .stop_tone ()
745
754
746
- def start_tone (self , frequency , waveform = SINE_WAVE ):
755
+ def start_tone (self , frequency : int , waveform : int = SINE_WAVE ) -> None :
747
756
"""Produce a tone using the speaker. Try changing frequency to change
748
757
the pitch of the tone.
749
758
750
759
: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].
752
761
753
762
Default is SINE_WAVE.
754
763
@@ -779,7 +788,7 @@ def start_tone(self, frequency, waveform=SINE_WAVE):
779
788
if not self ._sample .playing :
780
789
self ._sample .play (self ._wave_sample , loop = True )
781
790
782
- def stop_tone (self ):
791
+ def stop_tone (self ) -> None :
783
792
"""Use with start_tone to stop the tone produced.
784
793
785
794
.. image :: ../docs/_static/speaker.jpg
@@ -806,7 +815,7 @@ def stop_tone(self):
806
815
self ._sample = None
807
816
self ._speaker_enable .value = False
808
817
809
- def play_file (self , file_name ) :
818
+ def play_file (self , file_name : str ) -> None :
810
819
"""Play a .wav file using the onboard speaker.
811
820
812
821
:param file_name: The name of your .wav file in quotation marks including .wav
0 commit comments