39
39
https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel
40
40
"""
41
41
42
+ try :
43
+ from typing import Union , Tuple , Optional
44
+ except ImportError :
45
+ pass
46
+
42
47
import time
43
48
import array
44
49
import math
@@ -64,12 +69,12 @@ class _ClueSimpleTextDisplay:
64
69
65
70
def __init__ ( # pylint: disable=too-many-arguments
66
71
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 ,
73
78
):
74
79
# pylint: disable=import-outside-toplevel
75
80
import displayio
@@ -124,7 +129,7 @@ def __init__( # pylint: disable=too-many-arguments
124
129
for num in range (1 ):
125
130
self ._lines .append (self .add_text_line (color = colors [num % len (colors )]))
126
131
127
- def __getitem__ (self , item ):
132
+ def __getitem__ (self , item : int ):
128
133
"""Fetch the Nth text line Group"""
129
134
if len (self ._lines ) - 1 < item :
130
135
for _ in range (item - (len (self ._lines ) - 1 )):
@@ -133,7 +138,7 @@ def __getitem__(self, item):
133
138
)
134
139
return self ._lines [item ]
135
140
136
- def add_text_line (self , color = 0xFFFFFF ):
141
+ def add_text_line (self , color : Union [ int , Tuple [ int , int , int ]] = 0xFFFFFF ):
137
142
"""Adds a line on the display of the specified color and returns the label object."""
138
143
text_label = self ._label .Label (self ._font , text = "" , color = color )
139
144
text_label .x = 0
@@ -235,7 +240,7 @@ def __init__(self):
235
240
# Create displayio object for passing.
236
241
self .display = board .DISPLAY
237
242
238
- def _touch (self , i ) :
243
+ def _touch (self , i : int ) -> bool :
239
244
if not isinstance (self ._touches [i ], touchio .TouchIn ):
240
245
# First time referenced. Get the pin from the slot for this touch
241
246
# and replace it with a TouchIn object for the pin.
@@ -244,7 +249,7 @@ def _touch(self, i):
244
249
return self ._touches [i ].value
245
250
246
251
@property
247
- def touch_0 (self ):
252
+ def touch_0 (self ) -> bool :
248
253
"""Detect touch on capacitive touch pad 0.
249
254
250
255
.. image :: ../docs/_static/pad_0.jpg
@@ -265,7 +270,7 @@ def touch_0(self):
265
270
return self ._touch (0 )
266
271
267
272
@property
268
- def touch_1 (self ):
273
+ def touch_1 (self ) -> bool :
269
274
"""Detect touch on capacitive touch pad 1.
270
275
271
276
.. image :: ../docs/_static/pad_1.jpg
@@ -286,7 +291,7 @@ def touch_1(self):
286
291
return self ._touch (1 )
287
292
288
293
@property
289
- def touch_2 (self ):
294
+ def touch_2 (self ) -> bool :
290
295
"""Detect touch on capacitive touch pad 2.
291
296
292
297
.. image :: ../docs/_static/pad_2.jpg
@@ -307,7 +312,7 @@ def touch_2(self):
307
312
return self ._touch (2 )
308
313
309
314
@property
310
- def button_a (self ):
315
+ def button_a (self ) -> bool :
311
316
"""``True`` when Button A is pressed. ``False`` if not.
312
317
313
318
.. image :: ../docs/_static/button_a.jpg
@@ -328,7 +333,7 @@ def button_a(self):
328
333
return not self ._a .value
329
334
330
335
@property
331
- def button_b (self ):
336
+ def button_b (self ) -> bool :
332
337
"""``True`` when Button B is pressed. ``False`` if not.
333
338
334
339
.. image :: ../docs/_static/button_b.jpg
@@ -348,7 +353,9 @@ def button_b(self):
348
353
"""
349
354
return not self ._b .value
350
355
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 :
352
359
"""
353
360
Detect when the accelerometer is shaken. Optional parameters:
354
361
@@ -380,7 +387,7 @@ def shake(self, shake_threshold=30, avg_count=10, total_delay=0.1):
380
387
return total_accel > shake_threshold
381
388
382
389
@property
383
- def acceleration (self ):
390
+ def acceleration (self ) -> Tuple [ int , int , int ] :
384
391
"""Obtain acceleration data from the x, y and z axes.
385
392
386
393
.. image :: ../docs/_static/accelerometer.jpg
@@ -400,7 +407,7 @@ def acceleration(self):
400
407
return self ._accelerometer .acceleration
401
408
402
409
@property
403
- def gyro (self ):
410
+ def gyro (self ) -> Tuple [ int , int , int ] :
404
411
"""Obtain x, y, z angular velocity values in degrees/second.
405
412
406
413
.. image :: ../docs/_static/accelerometer.jpg
@@ -420,7 +427,7 @@ def gyro(self):
420
427
return self ._accelerometer .gyro
421
428
422
429
@property
423
- def magnetic (self ):
430
+ def magnetic (self ) -> Tuple [ int , int , int ] :
424
431
"""Obtain x, y, z magnetic values in microteslas.
425
432
426
433
.. image :: ../docs/_static/magnetometer.jpg
@@ -440,7 +447,7 @@ def magnetic(self):
440
447
return self ._magnetometer .magnetic
441
448
442
449
@property
443
- def proximity (self ):
450
+ def proximity (self ) -> int :
444
451
"""A relative proximity to the sensor in values from 0 - 255.
445
452
446
453
.. image :: ../docs/_static/proximity.jpg
@@ -462,7 +469,7 @@ def proximity(self):
462
469
return self ._sensor .proximity
463
470
464
471
@property
465
- def color (self ):
472
+ def color (self ) -> Tuple [ int , int , int , int ] :
466
473
"""The red, green, blue, and clear light values. (r, g, b, c)
467
474
468
475
.. image :: ../docs/_static/proximity.jpg
@@ -484,7 +491,7 @@ def color(self):
484
491
return self ._sensor .color_data
485
492
486
493
@property
487
- def gesture (self ):
494
+ def gesture (self ) -> int :
488
495
"""A gesture code if gesture is detected. Shows ``0`` if no gesture detected.
489
496
``1`` if an UP gesture is detected, ``2`` if DOWN, ``3`` if LEFT, and ``4`` if RIGHT.
490
497
@@ -512,7 +519,7 @@ def gesture(self):
512
519
return self ._sensor .gesture ()
513
520
514
521
@property
515
- def humidity (self ):
522
+ def humidity (self ) -> float :
516
523
"""The measured relative humidity in percent.
517
524
518
525
.. image :: ../docs/_static/humidity.jpg
@@ -532,7 +539,7 @@ def humidity(self):
532
539
return self ._humidity .relative_humidity
533
540
534
541
@property
535
- def pressure (self ):
542
+ def pressure (self ) -> float :
536
543
"""The barometric pressure in hectoPascals.
537
544
538
545
.. image :: ../docs/_static/pressure.jpg
@@ -551,7 +558,7 @@ def pressure(self):
551
558
return self ._pressure .pressure
552
559
553
560
@property
554
- def temperature (self ):
561
+ def temperature (self ) -> float :
555
562
"""The temperature in degrees Celsius.
556
563
557
564
.. image :: ../docs/_static/pressure.jpg
@@ -570,7 +577,7 @@ def temperature(self):
570
577
return self ._pressure .temperature
571
578
572
579
@property
573
- def altitude (self ):
580
+ def altitude (self ) -> float :
574
581
"""The altitude in meters based on the sea level pressure at your location. You must set
575
582
``sea_level_pressure`` to receive an accurate reading.
576
583
@@ -590,7 +597,7 @@ def altitude(self):
590
597
return self ._pressure .altitude
591
598
592
599
@property
593
- def sea_level_pressure (self ):
600
+ def sea_level_pressure (self ) -> float :
594
601
"""Set to the pressure at sea level at your location, before reading altitude for
595
602
the most accurate altitude measurement.
596
603
@@ -612,11 +619,11 @@ def sea_level_pressure(self):
612
619
return self ._pressure .sea_level_pressure
613
620
614
621
@sea_level_pressure .setter
615
- def sea_level_pressure (self , value ):
622
+ def sea_level_pressure (self , value : float ):
616
623
self ._pressure .sea_level_pressure = value
617
624
618
625
@property
619
- def white_leds (self ):
626
+ def white_leds (self ) -> bool :
620
627
"""The red led next to the USB plug labeled LED.
621
628
622
629
.. image :: ../docs/_static/white_leds.jpg
@@ -635,11 +642,11 @@ def white_leds(self):
635
642
return self ._white_leds .value
636
643
637
644
@white_leds .setter
638
- def white_leds (self , value ):
645
+ def white_leds (self , value : bool ):
639
646
self ._white_leds .value = value
640
647
641
648
@property
642
- def red_led (self ):
649
+ def red_led (self ) -> bool :
643
650
"""The red led next to the USB plug labeled LED.
644
651
645
652
.. image :: ../docs/_static/red_led.jpg
@@ -658,11 +665,11 @@ def red_led(self):
658
665
return self ._red_led .value
659
666
660
667
@red_led .setter
661
- def red_led (self , value ):
668
+ def red_led (self , value : bool ):
662
669
self ._red_led .value = value
663
670
664
671
@property
665
- def pixel (self ):
672
+ def pixel (self ) -> neopixel . NeoPixel :
666
673
"""The NeoPixel RGB LED.
667
674
668
675
.. image :: ../docs/_static/neopixel.jpg
@@ -682,20 +689,20 @@ def pixel(self):
682
689
return self ._pixel
683
690
684
691
@staticmethod
685
- def _sine_sample (length ):
692
+ def _sine_sample (length : int ):
686
693
tone_volume = (2 ** 15 ) - 1
687
694
shift = 2 ** 15
688
695
for i in range (length ):
689
696
yield int (tone_volume * math .sin (2 * math .pi * (i / length )) + shift )
690
697
691
- def _generate_sample (self , length = 100 ):
698
+ def _generate_sample (self , length : int = 100 ):
692
699
if self ._audio_out is not None :
693
700
return
694
701
self ._sine_wave = array .array ("H" , self ._sine_sample (length ))
695
702
self ._audio_out = audiopwmio .PWMAudioOut (board .SPEAKER )
696
703
self ._sine_wave_sample = audiocore .RawSample (self ._sine_wave )
697
704
698
- def play_tone (self , frequency , duration ):
705
+ def play_tone (self , frequency : int , duration : float ):
699
706
"""Produce a tone using the speaker. Try changing frequency to change
700
707
the pitch of the tone.
701
708
@@ -720,7 +727,7 @@ def play_tone(self, frequency, duration):
720
727
time .sleep (duration )
721
728
self .stop_tone ()
722
729
723
- def start_tone (self , frequency ):
730
+ def start_tone (self , frequency : int ):
724
731
"""Produce a tone using the speaker. Try changing frequency to change
725
732
the pitch of the tone.
726
733
@@ -785,7 +792,7 @@ def stop_tone(self):
785
792
self ._audio_out = None
786
793
787
794
@staticmethod
788
- def _normalized_rms (values ):
795
+ def _normalized_rms (values ) -> float :
789
796
mean_values = int (sum (values ) / len (values ))
790
797
return math .sqrt (
791
798
sum (
@@ -796,7 +803,7 @@ def _normalized_rms(values):
796
803
)
797
804
798
805
@property
799
- def sound_level (self ):
806
+ def sound_level (self ) -> float :
800
807
"""Obtain the sound level from the microphone (sound sensor).
801
808
802
809
.. image :: ../docs/_static/microphone.jpg
@@ -817,7 +824,7 @@ def sound_level(self):
817
824
self ._mic .record (self ._mic_samples , len (self ._mic_samples ))
818
825
return self ._normalized_rms (self ._mic_samples )
819
826
820
- def loud_sound (self , sound_threshold = 200 ):
827
+ def loud_sound (self , sound_threshold : int = 200 ) -> bool :
821
828
"""Utilise a loud sound as an input.
822
829
823
830
: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):
861
868
862
869
@staticmethod
863
870
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 ,
870
877
):
871
878
"""Display lines of text on the CLUE display. Lines of text are created in order as shown
872
879
in the example below. If you skip a number, the line will be shown blank on the display,
0 commit comments