11import time
2- import array
3- import math
42import board
5- from audiocore import RawSample
6- import audiopwmio
73import displayio
84import terminalio
5+ import simpleio
96from adafruit_display_text import label
107from adafruit_display_shapes .rect import Rect
11- from adafruit_display_shapes .circle import Circle
128from adafruit_clue import clue
139
14- blink_dot = False # optional blinking dot on accent
1510blink_light = True # optional flashing backlight on accent
1611tempo = 120 # in bpm
1712print ("BPM: {}" .format (tempo ))
1813time_signature = 4 # Beats per measure
1914BEEP_DURATION = 0.05
2015delay = 60 / tempo
2116
22- # constants for sine wave generation
23- SIN_LENGTH = 100 # more is less choppy
24- SIN_AMPLITUDE = 2 ** 15 # 0 (min) to 32768 (max)
25- SIN_OFFSET = 32767.5 # for 16bit range, (2**16 - 1) / 2
26- DELTA_PI = 2 * math .pi / SIN_LENGTH # happy little constant
27-
28- sine_wave = [
29- int (SIN_OFFSET + SIN_AMPLITUDE * math .sin (DELTA_PI * i )) for i in range (SIN_LENGTH )
30- ]
31- tones = (
32- RawSample (array .array ("H" , sine_wave ), sample_rate = 1200 * SIN_LENGTH ),
33- RawSample (array .array ("H" , sine_wave ), sample_rate = 1800 * SIN_LENGTH ),
34- )
35-
36- dac = audiopwmio .PWMAudioOut (board .SPEAKER ) # CLUE onboard speaker
37- # dac = audiopwmio.PWMAudioOut(board.A2) # external amp/speaker, but can't use #0 touch pad
38-
3917clue .display .brightness = 1.0
4018clue .pixel .brightness = 0.2
4119screen = displayio .Group (max_size = 11 )
5634bg_sprite = displayio .TileGrid (color_bitmap , x = 0 , y = 0 , pixel_shader = color_palette )
5735screen .append (bg_sprite )
5836
59- # Downbeat indicator graphic
60- flash = Circle (184 , 216 , 16 , fill = TEAL , outline = None )
61-
62- screen .append (flash )
63-
6437# title box
6538title_box = Rect (0 , 0 , 240 , 60 , fill = GRAY , outline = None )
6639screen .append (title_box )
128101def metronome (accent ): # Play metronome sound and flash display
129102 clue .display .brightness = 0.5 # Dim the display slightly
130103 if accent == 1 : # Put emphasis on downbeat
131- if blink_dot :
132- flash .fill = YELLOW # Flash the indicator
133104 if blink_light :
134105 clue .pixel .fill (YELLOW ) # Flash the pixel
135- dac . play ( tones [ 1 ], loop = True )
106+ simpleio . tone ( board . SPEAKER , 1800 , BEEP_DURATION )
136107 else : # All the other beats in the measure
137108 if blink_light :
138109 clue .pixel .fill (LT_TEAL ) # Flash the pixel
139- dac .play (tones [0 ], loop = True )
140- time .sleep (BEEP_DURATION ) # Play the sound for a while
141- dac .stop () # Then stop the sound
142- if blink_dot :
143- flash .fill = TEAL # Turn off the downbeat indicator
110+ simpleio .tone (board .SPEAKER , 1200 , BEEP_DURATION )
144111 if blink_light :
145112 clue .pixel .fill (0 ) # Turn off the pixel
146113 clue .display .brightness = 1.0 # Restore display to normal brightness
@@ -153,7 +120,6 @@ def metronome(accent): # Play metronome sound and flash display
153120
154121t0 = time .monotonic () # set start time
155122
156-
157123while True :
158124
159125 # play/pause
0 commit comments