76
76
__version__ = "0.0.0-auto.0"
77
77
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CLUE.git"
78
78
79
+
79
80
class _ClueSimpleTextDisplay :
80
81
"""Easily display lines of text on CLUE display."""
81
- def __init__ (self , title = None , title_color = 0xFFFFFF , title_scale = 1 , # pylint: disable=too-many-arguments
82
- text_scale = 1 , font = None , colors = None ):
82
+
83
+ def __init__ ( # pylint: disable=too-many-arguments
84
+ self ,
85
+ title = None ,
86
+ title_color = 0xFFFFFF ,
87
+ title_scale = 1 ,
88
+ text_scale = 1 ,
89
+ font = None ,
90
+ colors = None ,
91
+ ):
92
+ # pylint: disable=import-outside-toplevel
83
93
import displayio
84
94
import terminalio
85
95
from adafruit_display_text import label
86
96
97
+ # pylint: enable=import-outside-toplevel
98
+
87
99
if not colors :
88
- colors = (Clue .VIOLET , Clue .GREEN , Clue .RED , Clue .CYAN , Clue .ORANGE ,
89
- Clue .BLUE , Clue .MAGENTA , Clue .SKY , Clue .YELLOW , Clue .PURPLE )
100
+ colors = (
101
+ Clue .VIOLET ,
102
+ Clue .GREEN ,
103
+ Clue .RED ,
104
+ Clue .CYAN ,
105
+ Clue .ORANGE ,
106
+ Clue .BLUE ,
107
+ Clue .MAGENTA ,
108
+ Clue .SKY ,
109
+ Clue .YELLOW ,
110
+ Clue .PURPLE ,
111
+ )
90
112
91
113
self ._colors = colors
92
114
self ._label = label
@@ -102,8 +124,13 @@ def __init__(self, title=None, title_color=0xFFFFFF, title_scale=1, # pylint:
102
124
if len (title ) > 60 :
103
125
raise ValueError ("Title must be 60 characters or less." )
104
126
105
- title = label .Label (self ._font , text = title , max_glyphs = 60 , color = title_color ,
106
- scale = title_scale )
127
+ title = label .Label (
128
+ self ._font ,
129
+ text = title ,
130
+ max_glyphs = 60 ,
131
+ color = title_color ,
132
+ scale = title_scale ,
133
+ )
107
134
title .x = 0
108
135
title .y = 8
109
136
self ._y = title .y + 18
@@ -120,7 +147,9 @@ def __getitem__(self, item):
120
147
"""Fetch the Nth text line Group"""
121
148
if len (self ._lines ) - 1 < item :
122
149
for _ in range (item - (len (self ._lines ) - 1 )):
123
- self ._lines .append (self .add_text_line (color = self ._colors [item % len (self ._colors )]))
150
+ self ._lines .append (
151
+ self .add_text_line (color = self ._colors [item % len (self ._colors )])
152
+ )
124
153
return self ._lines [item ]
125
154
126
155
def add_text_line (self , color = 0xFFFFFF ):
@@ -141,6 +170,7 @@ def show_terminal(self):
141
170
"""Revert to terminalio screen."""
142
171
self ._display .show (None )
143
172
173
+
144
174
class Clue : # pylint: disable=too-many-instance-attributes, too-many-public-methods
145
175
"""Represents a single CLUE."""
146
176
@@ -195,8 +225,12 @@ def __init__(self):
195
225
self ._red_led .switch_to_output ()
196
226
197
227
# Define audio:
198
- self ._mic = audiobusio .PDMIn (board .MICROPHONE_CLOCK , board .MICROPHONE_DATA ,
199
- sample_rate = 16000 , bit_depth = 16 )
228
+ self ._mic = audiobusio .PDMIn (
229
+ board .MICROPHONE_CLOCK ,
230
+ board .MICROPHONE_DATA ,
231
+ sample_rate = 16000 ,
232
+ bit_depth = 16 ,
233
+ )
200
234
self ._sample = None
201
235
self ._samples = None
202
236
self ._sine_wave = None
@@ -352,7 +386,7 @@ def were_pressed(self):
352
386
"""
353
387
ret = set ()
354
388
pressed = self ._gamepad .get_pressed ()
355
- for button , mask in (('A' , 0x01 ), ('B' , 0x02 )):
389
+ for button , mask in (("A" , 0x01 ), ("B" , 0x02 )):
356
390
if mask & pressed :
357
391
ret .add (button )
358
392
return ret
@@ -690,7 +724,7 @@ def _sine_sample(length):
690
724
tone_volume = (2 ** 15 ) - 1
691
725
shift = 2 ** 15
692
726
for i in range (length ):
693
- yield int (tone_volume * math .sin (2 * math .pi * (i / length )) + shift )
727
+ yield int (tone_volume * math .sin (2 * math .pi * (i / length )) + shift )
694
728
695
729
def _generate_sample (self , length = 100 ):
696
730
if self ._sample is not None :
@@ -791,8 +825,13 @@ def stop_tone(self):
791
825
@staticmethod
792
826
def _normalized_rms (values ):
793
827
mean_values = int (sum (values ) / len (values ))
794
- return math .sqrt (sum (float (sample - mean_values ) * (sample - mean_values )
795
- for sample in values ) / len (values ))
828
+ return math .sqrt (
829
+ sum (
830
+ float (sample - mean_values ) * (sample - mean_values )
831
+ for sample in values
832
+ )
833
+ / len (values )
834
+ )
796
835
797
836
@property
798
837
def sound_level (self ):
@@ -812,7 +851,7 @@ def sound_level(self):
812
851
print(clue.sound_level)
813
852
"""
814
853
if self ._sample is None :
815
- self ._samples = array .array ('H' , [0 ] * 160 )
854
+ self ._samples = array .array ("H" , [0 ] * 160 )
816
855
self ._mic .record (self ._samples , len (self ._samples ))
817
856
return self ._normalized_rms (self ._samples )
818
857
@@ -859,8 +898,14 @@ def loud_sound(self, sound_threshold=200):
859
898
return self .sound_level > sound_threshold
860
899
861
900
@staticmethod
862
- def simple_text_display (title = None , title_color = (255 , 255 , 255 ), title_scale = 1 , # pylint: disable=too-many-arguments
863
- text_scale = 1 , font = None , colors = None ):
901
+ def simple_text_display ( # pylint: disable=too-many-arguments
902
+ title = None ,
903
+ title_color = (255 , 255 , 255 ),
904
+ title_scale = 1 ,
905
+ text_scale = 1 ,
906
+ font = None ,
907
+ colors = None ,
908
+ ):
864
909
"""Display lines of text on the CLUE display. Lines of text are created in order as shown
865
910
in the example below. If you skip a number, the line will be shown blank on the display,
866
911
e.g. if you include ``[0]`` and ``[2]``, the second line on the display will be empty, and
@@ -910,8 +955,14 @@ def simple_text_display(title=None, title_color=(255, 255, 255), title_scale=1,
910
955
clue_data[2].text = "Magnetic: {:.3f} {:.3f} {:.3f}".format(*clue.magnetic)
911
956
clue_data.show()
912
957
"""
913
- return _ClueSimpleTextDisplay (title = title , title_color = title_color , title_scale = title_scale ,
914
- text_scale = text_scale , font = font , colors = colors )
958
+ return _ClueSimpleTextDisplay (
959
+ title = title ,
960
+ title_color = title_color ,
961
+ title_scale = title_scale ,
962
+ text_scale = text_scale ,
963
+ font = font ,
964
+ colors = colors ,
965
+ )
915
966
916
967
917
968
clue = Clue () # pylint: disable=invalid-name
0 commit comments