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
@@ -0,0 +1,316 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Camera: a live preview from the CSI camera, and a shutter.
5
+
6
+ The preview is cameraif's ``capture_scaled``: the P4's pixel accelerator
7
+ scales each frame straight into a box of the panel's framebuffer, beside
8
+ the LVGL widgets, and only that box is presented. A panel on a bus (SPI or
9
+ i80) has no framebuffer, so there the frame goes into a buffer an LVGL
10
+ image shows (`touchkit.cam.Preview`). It runs as the set's foreground loop,
11
+ one frame per call, so LVGL and the timers keep going between frames.
12
+
13
+ On a screen too narrow for the controls beside the picture (240 x 240, say)
14
+ the picture takes the width with the shutter under it, and the other
15
+ controls are left off.
16
+
17
+ The shutter takes a JPEG on the P4's encoder (``capture_jpeg``) and saves it
18
+ in ``/sd/photos`` on the SD card, mounted while the app is open. Without a
19
+ card it goes to ``/photos`` on flash, or ``/data/photos`` when flash won't
20
+ take it (`appkit.places`). The last shot shows on the right, and every shot
21
+ in all three folders is on a small web page on the board: open the address
22
+ on the screen from a phone or a PC.
23
+
24
+ In a browser page the camera is the one the browser gives the page, chosen
25
+ under Camera in Connections (from Home's bar). The shutter's photos stay
26
+ in the page's memory and are gone when the page reloads, and Light and
27
+ Brightness change nothing there.
28
+
29
+ Without a camera the app says so, and Home goes back as usual.
30
+ """
31
+
32
+ import os
33
+
34
+ import lvgl as lv
35
+
36
+ from touchkit import cam, net, ui
37
+ from touchkit.clock import sleep_ms
38
+
39
+ PREVIEW = 400 # the preview's longer side on the panel
40
+ WIDE = 640 # the narrowest app area with the controls beside the picture
41
+ SHUTTER_H = 44 # the shutter's height under the picture on a narrow screen
42
+ QUALITY = 85
43
+ PORT = 8080
44
+ THUMB = 160
45
+
46
+ state = None # the open app's state, for the console
47
+
48
+
49
+ def _save(path, data):
50
+ for attempt in range(3):
51
+ try:
52
+ with open(path, "wb") as f:
53
+ f.write(data)
54
+ if os.stat(path)[6] == len(data):
55
+ return True
56
+ except OSError as e:
57
+ print("camera: write", path, e)
58
+ sleep_ms(100)
59
+ try:
60
+ os.remove(path) # don't leave an empty or short file behind
61
+ except OSError:
62
+ pass
63
+ return False
64
+
65
+
66
+ def main(scope):
67
+ global state
68
+ root = scope.root
69
+ status = ui.status(scope)
70
+ W, H = scope.size
71
+ RX, RY = scope.rect[:2]
72
+ st = state = {"cam": None, "shots": 0, "last": None, "server": None, "open": True}
73
+ keep = {} # the thumbnail's pixels, while LVGL shows them
74
+ opts = cam.options(scope)
75
+
76
+ try:
77
+ st["cam"] = cam.open_camera(opts)
78
+ except Exception as e:
79
+ ui.missing(scope, "no camera", cam.no_camera_text(e, "Video Camera"))
80
+ return
81
+
82
+ c0 = st["cam"]
83
+ wide = W >= WIDE
84
+ if wide:
85
+ pw, ph = cam.preview_size(c0, PREVIEW)
86
+ x0, y0 = 24, 8 + (PREVIEW - ph) // 2 # in root
87
+ else:
88
+ # The picture as wide as the area, or as tall as the shutter leaves.
89
+ # Under the row the Home button and the status line share.
90
+ y0 = ui.small.line_height + 14
91
+ cw, ch = c0.size()[:2]
92
+ pw, tall = W - 8, H - y0 - SHUTTER_H - 10
93
+ ph = pw * ch // cw
94
+ if ph > tall:
95
+ pw, ph = tall * cw // ch, tall
96
+ x0 = (W - pw) // 2
97
+ pv = cam.Preview(root, x0, y0, pw, ph, (RX, RY))
98
+
99
+ top = 8
100
+ rx = 24 + PREVIEW + 24
101
+ rw = W - rx - 24
102
+ shutter, _ = ui.button(root, lv.SYMBOL.IMAGE + " Shoot", lambda: shoot(), w=rw, h=72, font=ui.big, color=ui.ACCENT)
103
+ shutter.set_pos(rx, top)
104
+ if not wide:
105
+ shutter.set_size(W - 8, SHUTTER_H)
106
+ shutter.set_pos(4, y0 + ph + 6)
107
+ bw = (rw - 16) // 3
108
+ light_b, light_l = ui.button(root, "", lambda: cycle_light(), w=bw, h=52, font=ui.small)
109
+ light_b.set_pos(rx, top + 84)
110
+ flip_b, _ = ui.button(root, "Flip", lambda: toggle("flip"), w=bw, h=52, font=ui.small)
111
+ flip_b.set_pos(rx + bw + 8, top + 84)
112
+ mirror_b, _ = ui.button(root, "Mirror", lambda: toggle("mirror"), w=bw, h=52, font=ui.small)
113
+ mirror_b.set_pos(rx + 2 * (bw + 8), top + 84)
114
+ bright_l = ui.label(root, "", ui.small, ui.MUTED)
115
+ bright_l.set_pos(rx, top + 146)
116
+ slider = lv.slider(root)
117
+ slider.set_range(0, 100)
118
+ slider.set_size(rw - 16, 14)
119
+ slider.set_pos(rx + 8, top + 172)
120
+ slider.set_value(opts["bright"], False)
121
+ thumb = lv.image(root)
122
+ thumb.set_pos(rx + (rw - THUMB) // 2, top + 200)
123
+ thumb.set_size(THUMB, THUMB)
124
+ info = ui.label(root, "", ui.small, ui.MUTED)
125
+ info.set_pos(rx, top + 200 + THUMB + 4)
126
+ info.set_width(rw)
127
+ if not wide:
128
+ for o in (light_b, flip_b, mirror_b, bright_l, slider, thumb, info):
129
+ o.add_flag(lv.obj.FLAG.HIDDEN)
130
+
131
+ sd = cam.mount_sd(readonly=False)
132
+ scope.on_close(lambda: cam.unmount_sd(sd))
133
+
134
+ def show_opts():
135
+ light_l.set_text("Light: " + cam.LIGHT[opts["light"] % len(cam.LIGHT)][0])
136
+ bright_l.set_text("Brightness %d%%" % opts["bright"])
137
+ for b, key in ((flip_b, "flip"), (mirror_b, "mirror")):
138
+ b.set_style_bg_color(ui.ACCENT if opts[key] else ui.PANEL, 0)
139
+
140
+ def apply():
141
+ if st["cam"] is not None:
142
+ cam.apply_controls(st["cam"], opts)
143
+ show_opts()
144
+
145
+ def cycle_light():
146
+ opts["light"] = (opts["light"] + 1) % len(cam.LIGHT)
147
+ apply()
148
+
149
+ def toggle(key):
150
+ # Flip or mirror: open the camera again, turned, so the colours follow.
151
+ opts[key] = not opts[key]
152
+ old = st["cam"]
153
+ st["cam"] = None
154
+ if old is not None:
155
+ old.deinit()
156
+ try:
157
+ st["cam"] = cam.open_camera(opts)
158
+ except Exception as e:
159
+ status.set_text("camera: %s" % (e,))
160
+ return
161
+ apply()
162
+
163
+ def bright_moved(e):
164
+ opts["bright"] = slider.get_value()
165
+ apply()
166
+
167
+ slider.add_event_cb(bright_moved, lv.EVENT.RELEASED, None)
168
+ slider.add_event_cb(
169
+ lambda e: bright_l.set_text("Brightness %d%%" % slider.get_value()), lv.EVENT.VALUE_CHANGED, None
170
+ )
171
+ apply()
172
+
173
+ def preview():
174
+ c = st["cam"]
175
+ if c is not None and pv.capture(c):
176
+ pv.show()
177
+
178
+ def shoot():
179
+ c = st["cam"]
180
+ img = c.capture_jpeg(QUALITY) if c is not None else None
181
+ if not img:
182
+ status.set_text("no frame")
183
+ return
184
+ try:
185
+ path = cam.next_name(scope)
186
+ except OSError as e:
187
+ status.set_text("nowhere to save: %s" % (e,))
188
+ return
189
+ if not _save(path, img):
190
+ status.set_text("couldn't save the photo")
191
+ return
192
+ st["shots"] += 1
193
+ st["last"] = path
194
+ if wide:
195
+ px, tw, th = ui.decode_jpeg(img, THUMB)
196
+ ui.rgb565_image(thumb, px, tw, th, keep)
197
+ status.set_text("saved %s (%d KB)" % (path.rsplit("/", 1)[1], len(img) // 1024))
198
+
199
+ # -- the web page: every shot, newest first ----------------------------------
200
+
201
+ def start_server():
202
+ import socket
203
+ import _thread
204
+
205
+ def serve():
206
+ s = socket.socket()
207
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
208
+ s.bind(("0.0.0.0", PORT))
209
+ s.listen(2)
210
+ s.settimeout(0.5)
211
+ st["server"] = s
212
+ while st["open"]:
213
+ try:
214
+ c, _ = s.accept()
215
+ except OSError:
216
+ continue
217
+ try:
218
+ c.settimeout(5)
219
+ _reply(c)
220
+ except Exception as e:
221
+ print("camera web:", e)
222
+ finally:
223
+ c.close()
224
+ s.close()
225
+
226
+ _thread.stack_size(16 * 1024)
227
+ try:
228
+ _thread.start_new_thread(serve, ())
229
+ finally:
230
+ _thread.stack_size(0)
231
+
232
+ def _reply(c):
233
+ line = c.readline()
234
+ while c.readline() not in (b"\r\n", b"\n", b""):
235
+ pass
236
+ parts = line.split()
237
+ path = parts[1].decode() if len(parts) > 1 else "/"
238
+ if parts and parts[0] == b"POST" and path.startswith("/delete/"):
239
+ # One photo, or "all" of them.
240
+ key = path[8:]
241
+ for k, p in cam.photos(scope):
242
+ if key == "all" or k == key:
243
+ try:
244
+ os.remove(p)
245
+ except OSError as e:
246
+ print("camera web: delete %s: %s" % (p, e))
247
+ c.write(b"HTTP/1.0 303 See Other\r\nLocation: /\r\n\r\n")
248
+ return
249
+ if path.startswith("/photo/"):
250
+ key = path[7:]
251
+ for k, p in cam.photos(scope):
252
+ if k == key:
253
+ c.write(b"HTTP/1.0 200 OK\r\nContent-Type: image/jpeg\r\n\r\n")
254
+ with open(p, "rb") as f:
255
+ buf = bytearray(16384)
256
+ while True:
257
+ n = f.readinto(buf)
258
+ if not n:
259
+ break
260
+ c.write(buf[:n])
261
+ return
262
+ c.write(b"HTTP/1.0 404 Not Found\r\n\r\n")
263
+ return
264
+ photos = cam.photos(scope)
265
+ rows = "".join(
266
+ '<div><a href="/photo/{0}"><img src="/photo/{0}" loading="lazy"></a><br>{1}{2}</div>'.format(
267
+ k,
268
+ k.replace("/", ": "),
269
+ ' <form method=post action="/delete/{0}" onsubmit="return confirm(\'Delete {0}?\')">'
270
+ "<button>Delete</button></form>".format(k),
271
+ )
272
+ for k, p in photos
273
+ )
274
+ if photos:
275
+ rows = (
276
+ '<form method=post action="/delete/all" onsubmit="return confirm(\'Delete every photo?\')">'
277
+ "<button class=all>Delete all</button></form>"
278
+ ) + rows
279
+ html = (
280
+ "<!doctype html><meta name=viewport content='width=device-width'>"
281
+ "<title>Camera</title><style>body{font-family:sans-serif;background:#0b0e14;color:#ccc}"
282
+ "div{display:inline-block;margin:6px;font-size:13px;vertical-align:top}"
283
+ "img{width:220px;border-radius:8px}form{display:inline}"
284
+ "button{background:#3a2028;color:#f9b;border:1px solid #634;border-radius:6px;padding:2px 8px;cursor:pointer}"
285
+ "button.all{margin:0 6px 10px;padding:6px 12px}</style><h2>Camera</h2>"
286
+ + (rows or "<p>No photos yet: press Shoot on the board.</p>")
287
+ )
288
+ c.write(b"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
289
+ c.write(html.encode())
290
+
291
+ ip = net.ip()
292
+ name = net.hostname()
293
+ where = "Photos go to the SD card." if sd else "Photos go to the board's flash."
294
+ if ip:
295
+ info.set_text(
296
+ "%s\nOpen %shttp://%s:%d to see them."
297
+ % (where, "http://%s.local:%d or " % (name, PORT) if name else "", ip, PORT)
298
+ )
299
+ try:
300
+ start_server()
301
+ except Exception as e:
302
+ print("camera: web page:", e)
303
+ else:
304
+ info.set_text(where + "\nNo Wi-Fi: no web page.")
305
+
306
+ def close():
307
+ st["open"] = False # the web page's thread ends within half a second
308
+ c = st["cam"]
309
+ st["cam"] = None
310
+ if c is not None:
311
+ c.deinit()
312
+
313
+ scope.on_close(close)
314
+ status.set_text("%s ready" % (c0.sensor()[0],) if hasattr(c0, "sensor") else "ready")
315
+ c0 = None
316
+ scope.foreground(preview)
apps/camera/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "camera", "title": "Video Camera", "category": "Video", "icon": "photo_camera", "color": "#1E6F8C", "bar": "none"}
apps/card/__init__.py ADDED
@@ -0,0 +1,113 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Sound card: the board as a USB speaker, with the spectrum of what the PC
5
+ plays under the header.
6
+
7
+ The board is "Speakers (Espressif Device)" on Windows, on its device USB
8
+ port: usb-device-audio's speaker streams from the USB interrupt into an
9
+ audiopump ring, and audiodsp's pump plays that ring on the codec. Python only
10
+ keeps the clock feedback in step and follows the PC's volume. The meter is
11
+ the pump's own tap, so it shows whatever the pump plays.
12
+
13
+ It needs micropython-lib's ``usb-device`` and omnipython's ``usb-device-audio`` in
14
+ ``/lib``, and gallery's spectrum analyzer for the meter.
15
+ """
16
+
17
+ import lvgl as lv
18
+
19
+ import spectrum
20
+ from touchkit import ui
21
+
22
+ VOLUME = 85 # the panel's listening level; the PC's slider moves it down from here
23
+
24
+
25
+ def _percent(spk):
26
+ if spk.mute:
27
+ return 0
28
+ db = max(-50 * 256, min(0, spk.volume))
29
+ return round(VOLUME * (1 + db / (50 * 256)))
30
+
31
+
32
+ def main(scope):
33
+ status = ui.status(scope)
34
+ # machine is imported here, not at the top, so the app imports on any
35
+ # Python and says why it can't run instead of failing to load.
36
+ try:
37
+ import machine
38
+ except ImportError:
39
+ ui.missing(scope, "no machine", "Sound card needs a board: this Python has no machine module.")
40
+ return
41
+ try:
42
+ import audiopump
43
+ import usb.device
44
+ from audiodev import AudioFormat, pump
45
+ from boarddev import pcm_out
46
+ from usb.device.audio import SpeakerInterface
47
+ except ImportError as e:
48
+ ui.missing(scope, "missing: %s" % e, "Sound card needs usb-device and usb-device-audio in /lib: %s." % (e,))
49
+ return
50
+
51
+ if not hasattr(machine, "USBDevice"):
52
+ ui.missing(scope, "no USB device", "Sound card needs a USB device port, and this firmware has none.")
53
+ return
54
+ fmt = AudioFormat(48000, 2, 16)
55
+ # The speaker first: without one, the board must not become a USB sound
56
+ # card the PC would play into.
57
+ try:
58
+ transport = pcm_out(fmt)
59
+ except Exception as e:
60
+ ui.missing(scope, "no speaker", "Sound card needs a speaker on this board: %s." % (e,))
61
+ return
62
+ ring = audiopump.Ring(sample_rate=48000, channel_count=2, frames=480, capacity=8)
63
+ spk = SpeakerInterface(sink=ring)
64
+ import boarddev
65
+
66
+ usb.device.get().init(spk, builtin_driver=True, product_str=boarddev.name()) # the PC lists the board's name
67
+ while transport is not None and getattr(transport, "wire", None) is None:
68
+ transport = getattr(transport, "_inner", None)
69
+ driver = pump.BusioDriver(transport.wire, fmt, power=getattr(transport, "audio_power", None),
70
+ volume=VOLUME, transport=transport)
71
+ pump.owner().play(spk, ring, driver=driver, raises=True)
72
+ state = {"m": None, "level": None, "bytes": None}
73
+
74
+ try:
75
+ from spectrum.pump_levels import TapLevels
76
+
77
+ m = spectrum.meter(scope, lambda bands: TapLevels(bands, rate=48000, channels=2), 0)
78
+ state["m"] = m
79
+ except Exception as e:
80
+ print("card: no meter:", e)
81
+
82
+ def close():
83
+ pump.owner().stop(spk)
84
+ usb.device.get().init(builtin_driver=True) # the console alone again
85
+ state["m"] = None
86
+
87
+ scope.on_close(close)
88
+
89
+ def begin(t):
90
+ if state["m"] is not None:
91
+ spectrum.start(state["m"], scope, (getattr(scope, "header", None) or scope.root,))
92
+
93
+ scope.after(300, begin)
94
+
95
+ def service(t):
96
+ # Keep the clock feedback in step with the ring, and the codec with
97
+ # the PC's volume slider and mute.
98
+ spk.service()
99
+ now = _percent(spk)
100
+ if now != state["level"]:
101
+ transport.set_volume(now)
102
+ state["level"] = now
103
+
104
+ scope.every(5, service)
105
+
106
+ def tick(t):
107
+ got = ring.stats()[0]
108
+ playing = spk.streaming and got != state["bytes"]
109
+ state["bytes"] = got
110
+ status.set_text('Play to "Speakers (Espressif Device)" %s' % ("playing" if playing else "idle"))
111
+
112
+ tick(None)
113
+ scope.every(1000, tick)
apps/card/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "card", "title": "Sound card", "category": "Audio", "icon": "speaker", "color": "#1E6F8C"}