|
| 1 | +import time |
| 2 | +import board |
| 3 | +import displayio |
| 4 | +import terminalio |
| 5 | +import simpleio |
| 6 | +from adafruit_display_text import label |
| 7 | +from adafruit_display_shapes.rect import Rect |
| 8 | +from adafruit_clue import clue |
| 9 | + |
| 10 | +blink_light = True # optional flashing backlight on accent |
| 11 | +tempo = 120 # in bpm |
| 12 | +print("BPM: {}".format(tempo)) |
| 13 | +time_signature = 4 # Beats per measure |
| 14 | +BEEP_DURATION = 0.05 |
| 15 | +delay = 60 / tempo |
| 16 | + |
| 17 | +clue.display.brightness = 1.0 |
| 18 | +clue.pixel.brightness = 0.2 |
| 19 | +screen = displayio.Group(max_size=11) |
| 20 | +TEAL = 0x009E98 |
| 21 | +LT_TEAL = 0x000F0F |
| 22 | +GRAY = 0x02403E |
| 23 | +BLACK = 0x000000 |
| 24 | +WHITE = 0xFFFFFF |
| 25 | +YELLOW = 0xFFFF00 |
| 26 | + |
| 27 | +clue.pixel.fill(0) # Turn off the pixel |
| 28 | + |
| 29 | +# Setup screen |
| 30 | +# BG |
| 31 | +color_bitmap = displayio.Bitmap(240, 240, 1) |
| 32 | +color_palette = displayio.Palette(1) |
| 33 | +color_palette[0] = TEAL |
| 34 | +bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette) |
| 35 | +screen.append(bg_sprite) |
| 36 | + |
| 37 | +# title box |
| 38 | +title_box = Rect(0, 0, 240, 60, fill=GRAY, outline=None) |
| 39 | +screen.append(title_box) |
| 40 | + |
| 41 | +# title text |
| 42 | +title_label = label.Label( |
| 43 | + terminalio.FONT, text="Metronome", scale=4, color=TEAL, max_glyphs=11 |
| 44 | +) |
| 45 | +title_label.x = 14 |
| 46 | +title_label.y = 26 |
| 47 | +screen.append(title_label) |
| 48 | + |
| 49 | +# interval text |
| 50 | +interval_label = label.Label( |
| 51 | + terminalio.FONT, text=("{} BPM".format(tempo)), scale=5, color=WHITE, max_glyphs=7 |
| 52 | +) |
| 53 | +interval_label.x = 20 |
| 54 | +interval_label.y = 95 |
| 55 | +screen.append(interval_label) |
| 56 | + |
| 57 | +# mid line |
| 58 | +mid_line = Rect(0, 134, 240, 3, fill=GRAY, outline=None) |
| 59 | +screen.append(mid_line) |
| 60 | + |
| 61 | +# vert line |
| 62 | +vert_line = Rect(117, 134, 3, 56, fill=GRAY, outline=None) |
| 63 | +screen.append(vert_line) |
| 64 | + |
| 65 | +# Signature text |
| 66 | +sig_label = label.Label( |
| 67 | + terminalio.FONT, |
| 68 | + text=("{}/4".format(time_signature)), |
| 69 | + scale=3, |
| 70 | + color=BLACK, |
| 71 | + max_glyphs=3, |
| 72 | +) |
| 73 | +sig_label.x = 30 |
| 74 | +sig_label.y = 160 |
| 75 | +screen.append(sig_label) |
| 76 | + |
| 77 | +# play text |
| 78 | +play_label = label.Label( |
| 79 | + terminalio.FONT, text=(" play"), scale=3, color=BLACK, max_glyphs=5 |
| 80 | +) |
| 81 | +play_label.x = 138 |
| 82 | +play_label.y = 160 |
| 83 | +screen.append(play_label) |
| 84 | + |
| 85 | +# footer line |
| 86 | +footer_line = Rect(0, 190, 240, 3, fill=GRAY, outline=None) |
| 87 | +screen.append(footer_line) |
| 88 | + |
| 89 | +# increment label |
| 90 | +increment_label = label.Label( |
| 91 | + terminalio.FONT, text=("-1 +1"), scale=3, color=GRAY, max_glyphs=8 |
| 92 | +) |
| 93 | +increment_label.x = 3 |
| 94 | +increment_label.y = 220 |
| 95 | +screen.append(increment_label) |
| 96 | + |
| 97 | +# show the screen |
| 98 | +clue.display.show(screen) |
| 99 | + |
| 100 | + |
| 101 | +def metronome(accent): # Play metronome sound and flash display |
| 102 | + clue.display.brightness = 0.5 # Dim the display slightly |
| 103 | + if accent == 1: # Put emphasis on downbeat |
| 104 | + if blink_light: |
| 105 | + clue.pixel.fill(YELLOW) # Flash the pixel |
| 106 | + simpleio.tone(board.SPEAKER, 1800, BEEP_DURATION) |
| 107 | + else: # All the other beats in the measure |
| 108 | + if blink_light: |
| 109 | + clue.pixel.fill(LT_TEAL) # Flash the pixel |
| 110 | + simpleio.tone(board.SPEAKER, 1200, BEEP_DURATION) |
| 111 | + if blink_light: |
| 112 | + clue.pixel.fill(0) # Turn off the pixel |
| 113 | + clue.display.brightness = 1.0 # Restore display to normal brightness |
| 114 | + |
| 115 | + |
| 116 | +time.sleep(0.2) |
| 117 | +tempo_increment = 1 # increment for tempo value setting |
| 118 | +feedback_mode = 0 # 0 is sound and visual, 1 is sound only, 2 is visual only |
| 119 | +running = False |
| 120 | + |
| 121 | +t0 = time.monotonic() # set start time |
| 122 | + |
| 123 | +while True: |
| 124 | + |
| 125 | + # play/pause |
| 126 | + if clue.button_b: |
| 127 | + if play_label.text == " play": |
| 128 | + play_label.text = "pause" |
| 129 | + else: |
| 130 | + play_label.text = " play" |
| 131 | + running = not running |
| 132 | + time.sleep(0.4) |
| 133 | + beat = 1 # start with downbeat |
| 134 | + |
| 135 | + # Time Signature change |
| 136 | + if clue.button_a: |
| 137 | + print("sig change") |
| 138 | + if time_signature == 4: |
| 139 | + time_signature = 3 |
| 140 | + else: |
| 141 | + time_signature = 4 |
| 142 | + sig_label.text = "{}/4".format(time_signature) |
| 143 | + time.sleep(0.4) |
| 144 | + beat = 1 # start with downbeat |
| 145 | + |
| 146 | + if running and (time.monotonic() - t0) >= delay: |
| 147 | + t0 = time.monotonic() # reset time before click to maintain accuracy |
| 148 | + metronome(beat) |
| 149 | + beat = beat - 1 |
| 150 | + if beat == 0: # if the downbeat was just played, start at top of measure |
| 151 | + beat = time_signature |
| 152 | + |
| 153 | + # tempo changes |
| 154 | + if clue.touch_0: |
| 155 | + if tempo_increment is 1: |
| 156 | + tempo_increment = 10 |
| 157 | + increment_label.text = "-10 +10" |
| 158 | + else: |
| 159 | + tempo_increment = 1 |
| 160 | + increment_label.text = "-1 +1" |
| 161 | + time.sleep(0.2) # debounce |
| 162 | + |
| 163 | + if clue.touch_1: |
| 164 | + if tempo > 40: |
| 165 | + tempo = tempo - tempo_increment |
| 166 | + delay = 60 / tempo |
| 167 | + interval_label.text = "{} BPM".format(tempo) |
| 168 | + time.sleep(0.2) # debounce |
| 169 | + |
| 170 | + if clue.touch_2: |
| 171 | + if tempo < 330: |
| 172 | + tempo = tempo + tempo_increment |
| 173 | + delay = 60 / tempo |
| 174 | + interval_label.text = "{} BPM".format(tempo) |
| 175 | + time.sleep(0.2) # debounce |
0 commit comments