omnipython-apps 0.1.4.dev8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. apps/__init__.py +5 -0
  2. apps/about/__init__.py +257 -0
  3. apps/about/app.json +1 -0
  4. apps/about/credits.py +12 -0
  5. apps/about/facts.py +398 -0
  6. apps/about/fonts/OFL.txt +93 -0
  7. apps/about/fonts/latin1_14.bin +0 -0
  8. apps/about/fonts/latin1_16.bin +0 -0
  9. apps/about/fonts/latin1_24.bin +0 -0
  10. apps/about/fonts/latin1_32.bin +0 -0
  11. apps/about/fonts/latin1_40.bin +0 -0
  12. apps/backline/__init__.py +58 -0
  13. apps/backline/app.json +1 -0
  14. apps/backline/app.py +475 -0
  15. apps/backline/art.py +717 -0
  16. apps/backline/ask.py +781 -0
  17. apps/backline/box.py +502 -0
  18. apps/backline/cast.py +137 -0
  19. apps/backline/desk/__init__.py +329 -0
  20. apps/backline/desk/editor.py +331 -0
  21. apps/backline/desk/grid.py +154 -0
  22. apps/backline/desk/look.py +169 -0
  23. apps/backline/desk/pedals.py +102 -0
  24. apps/backline/desk/perform.py +157 -0
  25. apps/backline/desk/prompt.py +210 -0
  26. apps/backline/desk/strip.py +159 -0
  27. apps/backline/desk_main.py +226 -0
  28. apps/backline/engine.py +622 -0
  29. apps/backline/floor.py +509 -0
  30. apps/backline/footswitch.py +119 -0
  31. apps/backline/glyphs.py +82 -0
  32. apps/backline/kits.py +428 -0
  33. apps/backline/midipedal.py +362 -0
  34. apps/backline/page_data.py +413 -0
  35. apps/backline/parts.py +145 -0
  36. apps/backline/pedal.py +107 -0
  37. apps/backline/remote.py +134 -0
  38. apps/backline/songs/ballad_68.json +1 -0
  39. apps/backline/songs/bossa.json +1 -0
  40. apps/backline/songs/country_train.json +1 -0
  41. apps/backline/songs/funk.json +1 -0
  42. apps/backline/songs/metal.json +1 -0
  43. apps/backline/songs/pop.json +1 -0
  44. apps/backline/songs/rock.json +1 -0
  45. apps/backline/songs/shuffle.json +1 -0
  46. apps/battery/__init__.py +101 -0
  47. apps/battery/app.json +1 -0
  48. apps/bit_world/act_deck.py +683 -0
  49. apps/bit_world/act_game.py +762 -0
  50. apps/bit_world/act_pet.py +1032 -0
  51. apps/bit_world/app.json +1 -0
  52. apps/bit_world/bit_output.py +45 -0
  53. apps/bit_world/bit_palette.py +503 -0
  54. apps/bit_world/bit_sound.py +431 -0
  55. apps/bit_world/bit_sprites.py +632 -0
  56. apps/bit_world/bit_world.py +534 -0
  57. apps/btreceiver/__init__.py +690 -0
  58. apps/btreceiver/app.json +1 -0
  59. apps/btreceiver/chime.py +155 -0
  60. apps/btreceiver/demo.py +529 -0
  61. apps/btreceiver/screen.py +633 -0
  62. apps/camera/__init__.py +316 -0
  63. apps/camera/app.json +1 -0
  64. apps/card/__init__.py +113 -0
  65. apps/card/app.json +1 -0
  66. apps/cast/__init__.py +631 -0
  67. apps/cast/app.json +1 -0
  68. apps/cast/receivers.py +193 -0
  69. apps/chess/__init__.py +18 -0
  70. apps/chess/app.json +1 -0
  71. apps/chess/app.py +922 -0
  72. apps/chess/art.py +323 -0
  73. apps/chess/ble.py +411 -0
  74. apps/chess/engine.py +925 -0
  75. apps/chess/game.py +327 -0
  76. apps/chess/net.py +261 -0
  77. apps/clock/__init__.py +38 -0
  78. apps/clock/app.json +1 -0
  79. apps/detect/__init__.py +214 -0
  80. apps/detect/app.json +1 -0
  81. apps/fmsynth/__init__.py +41 -0
  82. apps/fmsynth/algorithms.py +61 -0
  83. apps/fmsynth/app.json +1 -0
  84. apps/fmsynth/app.py +355 -0
  85. apps/fmsynth/editor.py +206 -0
  86. apps/fmsynth/engine.py +257 -0
  87. apps/fmsynth/meters.py +262 -0
  88. apps/fmsynth/midi.py +375 -0
  89. apps/fmsynth/panel_view.py +1071 -0
  90. apps/fmsynth/patches/fmsynth-01.syx +0 -0
  91. apps/fmsynth/settings.py +138 -0
  92. apps/fmsynth/voice.py +398 -0
  93. apps/gamepad/__init__.py +685 -0
  94. apps/gamepad/app.json +1 -0
  95. apps/gamepad/pad.py +94 -0
  96. apps/gamepad/watch.py +352 -0
  97. apps/hidden/__init__.py +72 -0
  98. apps/hidden/app.json +1 -0
  99. apps/home/__init__.py +483 -0
  100. apps/home/app.json +1 -0
  101. apps/home/main.py +36 -0
  102. apps/homeassistant/__init__.py +213 -0
  103. apps/homeassistant/api.py +104 -0
  104. apps/homeassistant/app.json +1 -0
  105. apps/huey/__init__.py +287 -0
  106. apps/huey/acting.py +184 -0
  107. apps/huey/actor.py +194 -0
  108. apps/huey/app.json +1 -0
  109. apps/huey/client.py +125 -0
  110. apps/huey/servers.py +121 -0
  111. apps/huey/settings.example.json +8 -0
  112. apps/huey/settings.py +97 -0
  113. apps/huey/turn.py +504 -0
  114. apps/huey/voice.py +217 -0
  115. apps/info/__init__.py +810 -0
  116. apps/info/app.json +1 -0
  117. apps/lora/__init__.py +62 -0
  118. apps/lora/app.json +1 -0
  119. apps/manifest.py +71 -0
  120. apps/medialink_baby_monitor/__init__.py +222 -0
  121. apps/medialink_baby_monitor/app.json +1 -0
  122. apps/medialink_doorbell/__init__.py +251 -0
  123. apps/medialink_doorbell/app.json +1 -0
  124. apps/medialink_hands_free/__init__.py +217 -0
  125. apps/medialink_hands_free/app.json +1 -0
  126. apps/medialink_surveillance_camera/__init__.py +258 -0
  127. apps/medialink_surveillance_camera/app.json +1 -0
  128. apps/medialink_video_doorbell/__init__.py +289 -0
  129. apps/medialink_video_doorbell/app.json +1 -0
  130. apps/medialink_videophone/__init__.py +273 -0
  131. apps/medialink_videophone/app.json +1 -0
  132. apps/medialink_walkie_talkie/__init__.py +228 -0
  133. apps/medialink_walkie_talkie/app.json +1 -0
  134. apps/midicontroller/__init__.py +774 -0
  135. apps/midicontroller/app.json +1 -0
  136. apps/midicontroller/links.py +421 -0
  137. apps/midicontroller/surface.py +343 -0
  138. apps/midiengine/__init__.py +977 -0
  139. apps/midiengine/app.json +1 -0
  140. apps/midiengine/banks.py +85 -0
  141. apps/midiengine/midiin.py +165 -0
  142. apps/midiengine/player.py +392 -0
  143. apps/midiengine/umidiparser.py +1413 -0
  144. apps/mp3/__init__.py +240 -0
  145. apps/mp3/app.json +1 -0
  146. apps/perch/__init__.py +119 -0
  147. apps/perch/app.json +1 -0
  148. apps/perch/huey.py +481 -0
  149. apps/perch/huey_props.py +386 -0
  150. apps/perch/huey_shift.py +42 -0
  151. apps/perch/huey_views.py +296 -0
  152. apps/perch/scene_bench.py +680 -0
  153. apps/perch/scene_coast.py +479 -0
  154. apps/perch/scene_desk.py +492 -0
  155. apps/perch/stage.py +1274 -0
  156. apps/perch/vec.py +366 -0
  157. apps/repl/__init__.py +325 -0
  158. apps/repl/app.json +1 -0
  159. apps/roku/__init__.py +156 -0
  160. apps/roku/app.json +1 -0
  161. apps/rokucam/__init__.py +362 -0
  162. apps/rokucam/app.json +1 -0
  163. apps/security/__init__.py +261 -0
  164. apps/security/app.json +1 -0
  165. apps/settings/__init__.py +221 -0
  166. apps/settings/app.json +1 -0
  167. apps/shell/__init__.py +33 -0
  168. apps/shell/app.json +1 -0
  169. apps/spotify/__init__.py +669 -0
  170. apps/spotify/app.json +1 -0
  171. apps/spotify/screen.py +82 -0
  172. apps/steps/__init__.py +58 -0
  173. apps/steps/app.json +1 -0
  174. apps/stompbox/__init__.py +31 -0
  175. apps/stompbox/app.json +1 -0
  176. apps/stompbox/app.py +668 -0
  177. apps/stompbox/boards.json +20 -0
  178. apps/stompbox/effects.py +241 -0
  179. apps/stompbox/engine.py +340 -0
  180. apps/stompbox/footswitch.py +331 -0
  181. apps/stompbox/inputs.py +449 -0
  182. apps/stompbox/meter.py +72 -0
  183. apps/stompbox/outputs.py +135 -0
  184. apps/stompbox/pedals.txt +45 -0
  185. apps/stompbox/presets.py +106 -0
  186. apps/stompbox/styles.py +135 -0
  187. apps/stompbox/ui.py +817 -0
  188. apps/stompbox/wavfile.py +73 -0
  189. apps/synthstation/__init__.py +20 -0
  190. apps/synthstation/app.json +1 -0
  191. apps/synthstation/audio.py +397 -0
  192. apps/synthstation/catalog.py +50 -0
  193. apps/synthstation/meters.py +136 -0
  194. apps/synthstation/midi.py +380 -0
  195. apps/synthstation/pads.py +177 -0
  196. apps/synthstation/parts.py +128 -0
  197. apps/synthstation/station.py +1069 -0
  198. apps/system/__init__.py +11 -0
  199. apps/system/app.json +1 -0
  200. apps/touchcal/__init__.py +324 -0
  201. apps/touchcal/app.json +1 -0
  202. apps/touchcal/fit.py +262 -0
  203. apps/wake/__init__.py +268 -0
  204. apps/wake/app.json +1 -0
  205. apps/wake/feed.py +155 -0
  206. apps/watch/__init__.py +355 -0
  207. apps/watch/app.json +1 -0
  208. apps/watch/faces.py +35 -0
  209. apps/watch/wheel.py +157 -0
  210. apps/weather/__init__.py +278 -0
  211. apps/weather/app.json +1 -0
  212. apps/weather/icons.py +172 -0
  213. apps/weather/sizing.py +71 -0
  214. apps/weather/zippad.py +172 -0
  215. omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
  216. omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
  217. omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
  218. omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
apps/backline/floor.py ADDED
@@ -0,0 +1,509 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Backline's floor view: the face you read from a pedalboard.
5
+
6
+ A big ring lights the beat of the bar, its colour the part's: blue for the
7
+ intro, amber for a main, orange for a fill, violet for a transition, red for
8
+ the outro. The beat number sits inside it in seven-segment digits, with the
9
+ bar of the part under it. Beside the ring are the song's name, the part
10
+ playing in a glass window, a chip with the part coming next, the tempo, the
11
+ kit, and the two pedals' lamps, which light while a pedal is down. Touching
12
+ a lamp is pressing that pedal.
13
+
14
+ The view holds nothing but an ``hmilink.Panel``: what it shows comes from
15
+ the device's telemetry and document, and a pedal press goes back as a
16
+ command. So the same view works on the box's own screen, through
17
+ ``hmilink.local``, or on another screen over any hmilink link.
18
+
19
+ Everything is sized from ``(width, height)``. The art is drawn once by
20
+ svgio at that size (art.py); a beat shows one lit segment and hides
21
+ another, and nothing else is redrawn unless it changed. While the view is
22
+ hidden (`hide`) it lets go of its art, which `show` draws again, so a
23
+ screen that shows the desk view instead holds one view's pictures, not
24
+ both.
25
+ """
26
+
27
+ import lvgl as lv
28
+
29
+ from . import art, glyphs
30
+ from .engine import BAR, meter
31
+
32
+ PEDAL_COLORS = (0xFFB703, 0x4CC9F0)
33
+ HINTS = {
34
+ "stopped": "TAP TO START",
35
+ "intro": "INTRO",
36
+ "main": "TAP FILL\nHOLD NEXT\nTAP TAP END",
37
+ "fill": "FILL, THEN BACK",
38
+ "transition": "ON TO THE NEXT",
39
+ "outro": "TAP TAP: STOP NOW",
40
+ }
41
+ ARROW = " " + lv.SYMBOL.RIGHT + " "
42
+ PULSE_LEVELS = 3 # how many steps the beat's pulse fades in
43
+ PULSE_SPAN = 0.34 # the share of a beat it lasts
44
+
45
+
46
+ def pulse(tele, steps):
47
+ """How bright the beat's pulse is now, 0 to `PULSE_LEVELS`: brightest
48
+ as a beat lands, gone a third of the way through it. Read from where the
49
+ part is (``at``), so it keeps time with the song, not with the screen."""
50
+ beats = tele.get("beats") or steps
51
+ span = BAR * (steps // max(1, beats) or 1) / float(steps)
52
+ phase = (tele.get("at", 0) % BAR) % span / span
53
+ return max(0, PULSE_LEVELS - int(phase * PULSE_LEVELS / PULSE_SPAN))
54
+
55
+
56
+ def _label(parent, font, color, text="", width=None):
57
+ """A label; with ``width``, one line that ends in dots when it's too long."""
58
+ lbl = lv.label(parent)
59
+ lbl.set_style_text_font(font, 0)
60
+ lbl.set_style_text_color(lv.color_hex(color), 0)
61
+ lbl.set_text(text)
62
+ if width is not None:
63
+ lbl.set_width(width)
64
+ lbl.set_height(font.line_height)
65
+ lbl.set_long_mode(lv.label.LONG_MODE.DOTS)
66
+ return lbl
67
+
68
+
69
+ def _meter(doc):
70
+ """``(steps, group)`` for the ring, from a song document (4/4 without one)."""
71
+ return meter(doc or {})
72
+
73
+
74
+ def text_width(text, font, space=0):
75
+ """How wide ``text`` draws in ``font`` with ``space`` between letters."""
76
+ n = len(text.encode()) # LVGL counts bytes, and a symbol is three
77
+ try:
78
+ attrs = lv.text_attributes_t()
79
+ attrs.init()
80
+ attrs.letter_space = space
81
+ return lv.text_get_width(text, n, font, attrs)
82
+ except AttributeError: # LVGL before 9.4 takes the spacing itself
83
+ return lv.text_get_width(text, n, font, space)
84
+
85
+
86
+ def fit(label, choices, width, space=1):
87
+ """Show the first of ``choices`` that fits ``width`` on ``label``: each
88
+ with ``space`` between letters, then each with none. The last choice,
89
+ tightest, is shown when none fits."""
90
+ font = label.get_style_text_font(0)
91
+ for sp in (space, 0) if space else (0,):
92
+ for text in choices:
93
+ if text_width(text, font, sp) <= width:
94
+ label.set_style_text_letter_space(sp, 0)
95
+ label.set_text(text)
96
+ return text
97
+ label.set_style_text_letter_space(0, 0)
98
+ label.set_text(choices[-1])
99
+ return choices[-1]
100
+
101
+
102
+ class Floor:
103
+ """The floor view on ``root``, ``size`` = (width, height), showing ``panel``.
104
+
105
+ With ``on_switch``, a button in the corner calls it: the app shows the
106
+ desk view there instead.
107
+ """
108
+
109
+ def __init__(self, root, size, panel, on_switch=None):
110
+ self.root = root
111
+ self.panel = panel
112
+ w, h = size
113
+ lay = self.lay = art.Layout(w, h)
114
+ self.texts = art.Texts(lay) # kept while the view hides: `hide`
115
+ self.pictures = []
116
+ self.shown = {}
117
+ self.tele = {}
118
+ self.doc_name = ""
119
+ self.hidden = False
120
+ self.drawing = False # while `show` draws the art again
121
+
122
+ base = lv.obj(root)
123
+ base.remove_style_all()
124
+ base.set_size(w, h)
125
+ base.set_pos(0, 0)
126
+ base.remove_flag(lv.obj.FLAG.SCROLLABLE)
127
+ base.set_style_bg_color(lv.color_hex(0x06080C), 0)
128
+ base.set_style_bg_opa(255, 0)
129
+ self.base = base
130
+
131
+ # The panel and the ring's lit segments, one a step of the bar, are
132
+ # drawn for the song's meter, and again when it changes.
133
+ self.meter = None
134
+ self.want_meter = _meter(panel.doc)
135
+ self.ring_pictures = []
136
+ self.segments = []
137
+ self.looks = []
138
+
139
+ # Seven-segment digits, shown by swapping an image's source: the
140
+ # tempo's ten and a blank, and the beat's for the meter (`_draw`).
141
+ self.big = {}
142
+ self.small = {}
143
+ self.glow_pictures = []
144
+ self.beat_digit = art.image(base, int(lay.cx - lay.big_w / 2), int(lay.cy - lay.big_h * 0.62),
145
+ lay.big_w, lay.big_h)
146
+ _x, ty, _tw, th = lay.tempo
147
+ self.tempo_digits = [art.image(base, x, ty + (th - lay.small_h) // 2, lay.small_w, lay.small_h)
148
+ for x in art.tempo_slots(lay)]
149
+
150
+ small = lay.font(0.06)
151
+ tiny = lay.font(0.05)
152
+ eyebrow = _label(base, tiny, 0x8792A5, "BEAT")
153
+ eyebrow.align_to(self.beat_digit, lv.ALIGN.OUT_TOP_MID, 0, -2)
154
+ eyebrow.set_style_text_letter_space(2, 0)
155
+ # Sized from the ring, which on a square screen is narrower than the
156
+ # screen's height would say, so "STOPPED" stays on one line.
157
+ self.bar = _label(base, lay.font_px(lay.r * 0.15), 0xC8D0DC, "")
158
+ self.bar.set_style_text_letter_space(1, 0)
159
+ self.bar.set_width(int(lay.r))
160
+ self.bar.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
161
+ self.bar.set_pos(int(lay.cx - lay.r / 2), int(lay.cy + lay.big_h * 0.45))
162
+
163
+ cx, cy, cw, _ch = lay.col
164
+ # No taller than the column has room for: on a square screen the
165
+ # column is narrow for the screen's height.
166
+ self.song = _label(base, lay.font_px(min(lay.h * 0.06, cw * 0.11)), 0x9AA6B8, "", cw - 2)
167
+ self.song.set_pos(cx + 2, cy)
168
+
169
+ wx, wy, ww, wh = lay.window
170
+ part_font = lay.font(0.11)
171
+ while part_font.line_height > wh - 4 and part_font is not small:
172
+ part_font = small
173
+ self.part = _label(base, part_font, art.ROLE_COLORS["stopped"], "", ww - 8)
174
+ self.part.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
175
+ self.part.set_pos(wx + 4, wy + (wh - part_font.line_height) // 2)
176
+
177
+ chip = lv.obj(base)
178
+ chip.remove_style_all()
179
+ chip.set_style_radius(lv.RADIUS_CIRCLE, 0)
180
+ chip.set_style_border_width(max(1, lay.h // 240), 0)
181
+ chip.set_style_border_color(lv.color_hex(0x3A4555), 0)
182
+ chip.set_style_bg_color(lv.color_hex(0x0E131B), 0)
183
+ chip.set_style_bg_opa(255, 0)
184
+ chip.set_size(cw, lay.chip_h)
185
+ chip.set_pos(cx, lay.chip_y)
186
+ chip.remove_flag(lv.obj.FLAG.SCROLLABLE)
187
+ self.chip = chip
188
+ self.chip_text = _label(chip, tiny, 0xC8D0DC, "", cw - 8)
189
+ self.chip_text.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
190
+ self.chip_text.align(lv.ALIGN.CENTER, 0, 0)
191
+
192
+ tx, ty, tw, th = lay.tempo
193
+ gap = max(4, lay.m // 2)
194
+ bpm = _label(base, tiny, 0x8792A5, "BPM")
195
+ bpm.set_style_text_letter_space(2, 0)
196
+ bpm.set_pos(tx + tw + gap, ty + (th - tiny.line_height) // 2)
197
+ # A size smaller than the song's, so on a big screen it clears the lamps.
198
+ self.kit = _label(base, tiny, 0xC8D0DC, "", cw)
199
+ self.kit.set_pos(cx + 2, lay.kit_y)
200
+ # What the pedal does now, between the lamps, where there's room.
201
+ hint_font = lay.font(0.034)
202
+ self.hint_room = (lay.pedal_x[1] - lay.pedal_x[0] - int(2.8 * lay.pedal_r), 2 * lay.pedal_r + lay.m)
203
+ self.hint = _label(base, hint_font, 0x7E8A9C, "")
204
+ self.hint.set_width(max(10, self.hint_room[0]))
205
+ self.hint.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
206
+ self.hint.set_pos(lay.pedal_x[0] + int(1.4 * lay.pedal_r), lay.pedal_y)
207
+
208
+ # The pedals' lamps: a glow over each, and a touch area that is the pedal.
209
+ self.glows = []
210
+ gr = self.glow_r = int(lay.pedal_r * 1.6)
211
+ for i, px in enumerate(lay.pedal_x):
212
+ glow = art.image(base, px - gr, lay.pedal_y - gr, 2 * gr, 2 * gr)
213
+ glow.add_flag(lv.obj.FLAG.HIDDEN)
214
+ self.glows.append(glow)
215
+ letter = _label(base, small, 0xDDE3EC, "AB"[i])
216
+ letter.align_to(glow, lv.ALIGN.CENTER, 0, 0)
217
+ pad = lv.obj(base)
218
+ pad.remove_style_all()
219
+ pad.set_size(2 * gr, 2 * gr)
220
+ pad.set_pos(px - gr, lay.pedal_y - gr)
221
+ pad.add_flag(lv.obj.FLAG.CLICKABLE)
222
+ pad.add_event_cb(self._pad(i, True), lv.EVENT.PRESSED, None)
223
+ pad.add_event_cb(self._pad(i, False), lv.EVENT.RELEASED, None)
224
+ pad.add_event_cb(self._pad(i, False), lv.EVENT.PRESS_LOST, None)
225
+
226
+ # Where the screen can show the desk instead, a button for it in the
227
+ # corner above the ring.
228
+ self.switch = None
229
+ self.refused_until = None # saying why the view stays (`refuse_switch`) until then
230
+ if on_switch is not None:
231
+ size = max(28, min(lay.m * 3, 48))
232
+ btn = lv.obj(base)
233
+ btn.remove_style_all()
234
+ btn.set_size(size, size)
235
+ btn.set_pos(max(2, lay.m // 3), max(2, lay.m // 3))
236
+ btn.set_style_radius(size // 4, 0)
237
+ btn.set_style_bg_color(lv.color_hex(0x0E131B), 0)
238
+ btn.set_style_bg_opa(220, 0)
239
+ btn.set_style_border_color(lv.color_hex(0x3A4555), 0)
240
+ btn.set_style_border_width(1, 0)
241
+ btn.add_flag(lv.obj.FLAG.CLICKABLE)
242
+ btn.remove_flag(lv.obj.FLAG.SCROLLABLE)
243
+ icon = glyphs.show(btn, "desk", size * 2 // 3, lv.color_hex(0xC8D0DC))
244
+ icon.center()
245
+ btn.add_event_cb(lambda _e: on_switch(), lv.EVENT.CLICKED, None)
246
+ self.switch = btn
247
+
248
+ self._draw()
249
+ panel.on_doc = self._doc
250
+ panel.on_tele = self._tele
251
+ if panel.doc is not None:
252
+ self._doc(panel.doc, panel.desc)
253
+ self._tele({})
254
+
255
+ # -- the art -------------------------------------------------------------
256
+
257
+ def _draw(self):
258
+ """Draw the art: the tempo's digits, the lamps' glow, and the panel,
259
+ the ring and the beat's digits for the meter, all from the text
260
+ kept in `texts` and with one decoder."""
261
+ lay = self.lay
262
+ texts = self.texts
263
+ decoder = art.decoder()
264
+ try:
265
+ gr = self.glow_r
266
+ digits, glows = texts.face((gr, color) for color in PEDAL_COLORS[: len(self.glows)])
267
+ for ch in art.TEMPO_DIGITS:
268
+ self.small[ch] = art.Picture(None, digits[ch], lay.small_w, lay.small_h, decoder=decoder)
269
+ for glow, text in zip(self.glows, glows):
270
+ pic = art.Picture(None, text, 2 * gr, 2 * gr, decoder=decoder)
271
+ glow.set_src(pic.dsc)
272
+ self.glow_pictures.append(pic)
273
+ self.pictures = list(self.small.values()) + self.glow_pictures
274
+ self._ring(*self.want_meter, decoder=decoder)
275
+ finally:
276
+ decoder.close()
277
+
278
+ def _big(self, ch, decoder=None):
279
+ """The beat's digit ``ch``, drawn the first time it's wanted."""
280
+ pic = self.big.get(ch)
281
+ if pic is None:
282
+ lay = self.lay
283
+ pic = self.big[ch] = art.Picture(None, self.texts.big(ch), lay.big_w, lay.big_h, decoder=decoder)
284
+ self.pictures.append(pic)
285
+ return pic
286
+
287
+ def hide(self):
288
+ """Hide the view and let go of its art: every image shows nothing
289
+ and every picture can be collected. `show` draws it again, from the
290
+ text kept in `texts`."""
291
+ self.base.add_flag(lv.obj.FLAG.HIDDEN)
292
+ if self.hidden:
293
+ return
294
+ self.hidden = True
295
+ for pic in self.ring_pictures:
296
+ pic.image.delete()
297
+ for img in [self.beat_digit] + self.tempo_digits + self.glows:
298
+ img.set_src(None)
299
+ self.ring_pictures = []
300
+ self.segments = []
301
+ self.meter = None
302
+ self.big = {}
303
+ self.small = {}
304
+ self.glow_pictures = []
305
+ self.pictures = []
306
+
307
+ def show(self):
308
+ """Draw the art again and show the view as the song is now.
309
+
310
+ What the panel says while the art draws is kept, not shown, until
311
+ the art is there to show it: a timer that read the panel then would
312
+ draw the ring out of turn, or reach for a digit not drawn yet."""
313
+ if self.hidden:
314
+ self.hidden = False
315
+ self.drawing = True
316
+ try:
317
+ self._draw()
318
+ finally:
319
+ self.drawing = False
320
+ self.shown = {}
321
+ self._tele({})
322
+ self.base.remove_flag(lv.obj.FLAG.HIDDEN)
323
+
324
+ # -- the pedals ------------------------------------------------------------
325
+
326
+ def _pad(self, which, down):
327
+ def on_event(_event):
328
+ self.pedal(which, down)
329
+
330
+ return on_event
331
+
332
+ def pedal(self, which, down):
333
+ """Pedal ``which`` went down or up: tell the device."""
334
+ self.panel.command("pedal", pedal=which, down=down)
335
+
336
+ # -- what it shows ---------------------------------------------------------
337
+
338
+ def poll(self):
339
+ self.panel.poll()
340
+
341
+ def bytes(self):
342
+ """What the art holds, in bytes."""
343
+ return sum(p.bytes for p in self.pictures) + sum(p.bytes for p in self.ring_pictures)
344
+
345
+ def _ring(self, steps, group, decoder=None):
346
+ """Draw the panel and the ring's segments for a bar of ``steps``,
347
+ ``group`` to a beat, unless that is what is drawn already. The
348
+ beat's digits are drawn for it too: a dash, and one to its beats."""
349
+ self.want_meter = (steps, group)
350
+ if self.meter == (steps, group) or self.hidden:
351
+ return
352
+ self.meter = (steps, group)
353
+ for pic in self.ring_pictures:
354
+ pic.image.delete()
355
+ self.ring_pictures = [] # so a collection while the new ones draw can take the old
356
+ lay = self.lay
357
+ own = decoder is None
358
+ if own:
359
+ decoder = art.decoder()
360
+ try:
361
+ panel_text, segment_texts = self.texts.ring(steps, group)
362
+ self.ring_pictures = art.bands(self.base, panel_text, lay.w, lay.h, decoder=decoder)
363
+ under = len(self.ring_pictures)
364
+ for i, pic in enumerate(self.ring_pictures):
365
+ pic.image.move_to_index(i)
366
+ self.segments = []
367
+ for b in range(steps):
368
+ x, y, bw, bh = art.segment_box(lay, b, steps, group)
369
+ pic = art.Picture(self.base, segment_texts[b], bw, bh, x, y, decoder=decoder)
370
+ pic.image.add_flag(lv.obj.FLAG.HIDDEN)
371
+ pic.image.move_to_index(under + b)
372
+ self.ring_pictures.append(pic)
373
+ self.segments.append(pic.image)
374
+ self.looks = ["off"] * steps
375
+ self.shown.pop("beat", None)
376
+ for ch in art.beat_digits(steps, group):
377
+ self._big(ch, decoder)
378
+ finally:
379
+ if own:
380
+ decoder.close()
381
+
382
+ def _doc(self, doc, desc):
383
+ self.doc_name = doc.get("name", "") if doc else ""
384
+ if self.drawing:
385
+ self.want_meter = _meter(doc) # `_draw` draws the ring for it
386
+ return
387
+ self._ring(*_meter(doc))
388
+ if self.hidden:
389
+ return
390
+ if not self.tele.get("song"):
391
+ self._set("song", self.doc_name.upper(), self._song)
392
+
393
+ def _song(self, text):
394
+ fit(self.song, [text], self.song.get_width(), 1)
395
+
396
+ def _bar(self, text):
397
+ """The bar, or only its numbers where "BAR" leaves no room inside the ring."""
398
+ fit(self.bar, [text, text[4:]] if text.startswith("BAR ") else [text], self.bar.get_width(), 1)
399
+
400
+ def _set(self, key, value, fn):
401
+ if self.shown.get(key) != value:
402
+ self.shown[key] = value
403
+ fn(value)
404
+ return True
405
+ return False
406
+
407
+ def refuse_switch(self):
408
+ """Say, where the hint goes, that the view changes only while the
409
+ song is stopped: a tap on the switch while a song plays."""
410
+ self.refused_until = art.until(art.SAY_MS)
411
+ if not self.hidden:
412
+ self._set("hint", art.STOP_TO_SWITCH[0], self._hint)
413
+
414
+ def _dim_switch(self, playing):
415
+ """The switch greyed while a song plays: it says why when tapped."""
416
+ self.switch.set_style_opa(art.DIM_OPA if playing else 255, 0)
417
+
418
+ def _hint(self, text):
419
+ """The hint centred between the lamps, or none when it doesn't fit there."""
420
+ hint = self.hint
421
+ hint.set_text(text)
422
+ hint.update_layout()
423
+ h = hint.get_height()
424
+ lay = self.lay
425
+ if self.hint_room[0] < 40 or h > self.hint_room[1]:
426
+ hint.add_flag(lv.obj.FLAG.HIDDEN)
427
+ return
428
+ hint.remove_flag(lv.obj.FLAG.HIDDEN)
429
+ hint.set_y(lay.pedal_y - h // 2)
430
+
431
+ def _pulse(self, level):
432
+ """Brighten the beat's digit and its lit segment by ``level`` (0 to
433
+ `PULSE_LEVELS`), more on the bar's first beat."""
434
+ accent = self.tele.get("beat") == 1
435
+ self.beat_digit.set_style_image_recolor(lv.color_hex(0xFFFFFF), 0)
436
+ self.beat_digit.set_style_image_recolor_opa(level * (36 if accent else 24), 0)
437
+ step = self.shown.get("beat", 0)
438
+ if 0 < step <= len(self.segments):
439
+ self.segments[step - 1].set_style_image_recolor_opa(120 - level * (30 if accent else 20), 0)
440
+
441
+ def _tele(self, values):
442
+ v = self.tele
443
+ v.update(values)
444
+ if self.drawing:
445
+ return # kept for `show`
446
+ role = v.get("role", "stopped")
447
+ color = art.ROLE_COLORS.get(role, art.ROLE_COLORS["stopped"])
448
+ playing = v.get("playing", False)
449
+ steps = v.get("steps") or 4
450
+ self._ring(steps, max(1, steps // (v.get("beats") or steps)))
451
+ if self.hidden:
452
+ return # kept for `show`
453
+ step = v.get("step", v.get("beat", 0)) if playing else 0
454
+ beat = v.get("beat", 0) if playing else 0
455
+ self._set("song", (v.get("song") or self.doc_name).upper(), self._song)
456
+ recolor = self._set("color", color, lambda c: None)
457
+ if recolor:
458
+ self.part.set_style_text_color(lv.color_hex(color), 0)
459
+ if self._set("beat", step, lambda b: None) or recolor:
460
+ # The beat now is hot, its centre near white; the beats before it
461
+ # stay lit in a deeper shade of the part's colour.
462
+ deep = lv.color_hex(art.mix(color, 0x000000, 0.35))
463
+ for i, seg in enumerate(self.segments):
464
+ look = "off" if i >= step else "now" if i == step - 1 else "past"
465
+ if not recolor and self.looks[i] == look:
466
+ continue # unchanged: left alone, so it isn't redrawn
467
+ self.looks[i] = look
468
+ if look == "off":
469
+ seg.add_flag(lv.obj.FLAG.HIDDEN)
470
+ continue
471
+ seg.remove_flag(lv.obj.FLAG.HIDDEN)
472
+ now = look == "now"
473
+ seg.set_style_image_recolor(lv.color_hex(color) if now else deep, 0)
474
+ seg.set_style_image_recolor_opa(120 if now else 225, 0)
475
+ self.beat_digit.set_src(self._big(str(beat) if 0 < beat < 10 else "-").dsc)
476
+ self.shown.pop("pulse", None)
477
+ self._set("pulse", pulse(v, steps) if playing else 0, self._pulse)
478
+ bar = ("BAR %d / %d" % (v.get("bar", 1), v.get("bars", 1))) if playing else "STOPPED"
479
+ self._set("bar", bar, self._bar)
480
+ self._set("part", (v.get("part") or "Ready").upper(), self.part.set_text)
481
+ nxt = v.get("next") or ""
482
+ if self._set("next", (nxt, playing), lambda n: None):
483
+ if nxt:
484
+ up = nxt.upper()
485
+ choices = ["NEXT" + ARROW + up, lv.SYMBOL.RIGHT + " " + up, up]
486
+ else:
487
+ choices = [lv.SYMBOL.LOOP + " LOOPING", "LOOPING"] if playing else ["TAP" + ARROW + "INTRO", "TAP " + lv.SYMBOL.RIGHT + " INTRO"]
488
+ fit(self.chip_text, choices, self.chip_text.get_width(), 1)
489
+ ncolor = art.ROLE_COLORS.get(v.get("next_role") or "stopped", 0x3A4555)
490
+ self.chip.set_style_border_color(lv.color_hex(ncolor if nxt else 0x3A4555), 0)
491
+ self.chip_text.set_style_text_color(lv.color_hex(ncolor if nxt else 0x9AA6B8), 0)
492
+ tempo = int(v.get("tempo", 0) + 0.5)
493
+ if self._set("tempo", tempo, lambda t: None):
494
+ text = ("%3d" % tempo)[-3:] if tempo else " "
495
+ for img, ch in zip(self.tempo_digits, text):
496
+ img.set_src(self.small[ch if ch in self.small else " "].dsc)
497
+ self._set("kit", v.get("kit", ""), self.kit.set_text)
498
+ if self.switch is not None:
499
+ self._set("switch", playing, self._dim_switch)
500
+ hint = art.STOP_TO_SWITCH[0] if art.before(self.refused_until) else HINTS.get(role, "")
501
+ self._set("hint", hint, self._hint)
502
+ pedals = v.get("pedals") or ["up", "up"]
503
+ for i, state in enumerate(pedals[:2]):
504
+ if self._set("pedal%d" % i, state, lambda s: None):
505
+ if state == "up":
506
+ self.glows[i].add_flag(lv.obj.FLAG.HIDDEN)
507
+ else:
508
+ self.glows[i].remove_flag(lv.obj.FLAG.HIDDEN)
509
+ self.glows[i].set_style_image_opa(255 if state == "held" else 210, 0)
@@ -0,0 +1,119 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Play Backline from a switch wired to a pin: a stomp switch, or the
5
+ board's own button.
6
+
7
+ A `Footswitch` reads its pin each time it is polled and tells the box when
8
+ the switch has gone down or come up (``on_pedal(which, down)``, the same call
9
+ a MIDI switch or a panel's key makes), so taps, holds and double taps are
10
+ timed in one place. A contact bounces for a few milliseconds when it closes
11
+ and opens, so a new level counts only once it has held for ``settle_ms``.
12
+
13
+ `open_switches` makes the switches the settings name: ``"switch"`` is
14
+ pedal A and ``"switch_b"`` is pedal B, each as
15
+
16
+ - ``boot_button``: the board's own button, from its ``board_peripherals``.
17
+ - ``pin:N``: a switch between GPIO ``N`` and ground, read with the pin's
18
+ pull-up (pressed reads low).
19
+ - ``pin:N:high``: a switch that reads high when pressed, with a pull-down.
20
+
21
+ Example::
22
+
23
+ sw = Footswitch(Pin(35, Pin.IN, Pin.PULL_UP), on_pedal=box.pedal)
24
+ sw.poll(now) # call every few milliseconds
25
+ """
26
+
27
+ try:
28
+ from time import ticks_diff
29
+ except ImportError: # CPython
30
+ def ticks_diff(a, b):
31
+ return a - b
32
+
33
+ SETTLE_MS = 8
34
+ #: The settings that name a switch, and the pedal each one is.
35
+ KEYS = (("switch", 0), ("switch_b", 1))
36
+
37
+
38
+ class Footswitch:
39
+ """One switch on a pin, debounced, as pedal ``which``.
40
+
41
+ Args:
42
+ pin: anything with ``value()`` (a ``machine.Pin``).
43
+ which: 0 for pedal A, 1 for pedal B.
44
+ on_pedal: ``on_pedal(which, down)`` when the switch settles down or up.
45
+ active_low: the pin reads 0 while the switch is pressed.
46
+ settle_ms: how long a new level must hold before it counts.
47
+ """
48
+
49
+ def __init__(self, pin, which=0, on_pedal=None, active_low=True, settle_ms=SETTLE_MS):
50
+ self.pin = pin
51
+ self.which = which
52
+ self.on_pedal = on_pedal
53
+ self.active_low = active_low
54
+ self.settle_ms = settle_ms
55
+ #: Whether the switch is down, as last told.
56
+ self.down = False
57
+ self._raw = False
58
+ self._since = None
59
+
60
+ def _pressed(self):
61
+ v = self.pin.value()
62
+ return not v if self.active_low else bool(v)
63
+
64
+ def poll(self, now):
65
+ """Read the pin at ``now`` (ms).
66
+
67
+ Returns:
68
+ True when the switch has just settled into a new position, after
69
+ telling ``on_pedal``.
70
+ """
71
+ raw = self._pressed()
72
+ if raw != self._raw or self._since is None:
73
+ self._raw = raw
74
+ self._since = now
75
+ if raw == self.down or ticks_diff(now, self._since) < self.settle_ms:
76
+ return False
77
+ self.down = raw
78
+ if self.on_pedal is not None:
79
+ self.on_pedal(self.which, raw)
80
+ return True
81
+
82
+
83
+ def open_pin(spec):
84
+ """``(pin, active_low)`` for a switch spec (see the module docstring).
85
+
86
+ Raises:
87
+ ValueError: The spec isn't one of the forms.
88
+ ImportError: There's no ``machine`` or ``board_peripherals``.
89
+ AttributeError: The board's peripherals have no ``boot_button``.
90
+ """
91
+ if spec == "boot_button":
92
+ import board_peripherals as bp
93
+
94
+ return bp.boot_button(), True
95
+ parts = spec.split(":")
96
+ if parts[0] != "pin" or len(parts) not in (2, 3) or not parts[1].isdigit() \
97
+ or (len(parts) == 3 and parts[2] not in ("low", "high")):
98
+ raise ValueError("a switch is boot_button, pin:N or pin:N:high, not %r" % (spec,))
99
+ from machine import Pin
100
+
101
+ high = len(parts) == 3 and parts[2] == "high"
102
+ return Pin(int(parts[1]), Pin.IN, Pin.PULL_DOWN if high else Pin.PULL_UP), not high
103
+
104
+
105
+ def open_switches(settings, on_pedal, opener=open_pin):
106
+ """The switches ``settings`` names (``"switch"``, ``"switch_b"``), as
107
+ `Footswitch` objects calling ``on_pedal``. A setting that is missing or
108
+ empty is no switch.
109
+
110
+ Raises:
111
+ ValueError, ImportError, AttributeError: From `open_pin`.
112
+ """
113
+ out = []
114
+ for key, which in KEYS:
115
+ spec = settings.get(key)
116
+ if spec:
117
+ pin, active_low = opener(spec)
118
+ out.append(Footswitch(pin, which, on_pedal, active_low))
119
+ return out