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/perch/vec.py ADDED
@@ -0,0 +1,366 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Build a vector drawing in Python and draw it at any size with svgio.
5
+
6
+ A `Doc` collects shapes and gradients, keeping to what svgio reads: paths,
7
+ strokes, transforms, opacity and gradients. The view is a rectangle of the
8
+ drawing, so a close-up is the same drawing with a smaller view.
9
+
10
+ Shapes are kept as records of svgio's compact form, numbers rather than
11
+ text: writing every coordinate out in Python, for svgio to read back, would
12
+ be most of the time a drawing takes on a board. A shape is bytes to the code
13
+ that draws. `Doc.source` gives what `SvgDecoder.open` takes, and `Doc.svg`
14
+ the SVG text svgio expands it to.
15
+
16
+ Example::
17
+
18
+ doc = Doc()
19
+ glow = doc.radial([(0, 0xFFF3B0, 1), (1, 0xFFF3B0, 0)])
20
+ doc.add(circle(50, 50, 40, glow))
21
+ pixels, alpha, w, h = render(doc.source((0, 0, 100, 100)), 200)
22
+ """
23
+
24
+ import math
25
+ import struct
26
+
27
+ import svgio
28
+
29
+
30
+ def mix(a, b, t):
31
+ """The colour *t* of the way from *a* to *b*, each 0xRRGGBB."""
32
+ if t <= 0:
33
+ return a
34
+ if t >= 1:
35
+ return b
36
+ r = (a >> 16) + ((b >> 16) - (a >> 16)) * t
37
+ g = ((a >> 8) & 255) + (((b >> 8) & 255) - ((a >> 8) & 255)) * t
38
+ bl = (a & 255) + ((b & 255) - (a & 255)) * t
39
+ return (int(r + 0.5) << 16) | (int(g + 0.5) << 8) | int(bl + 0.5)
40
+
41
+
42
+ def lerp(a, b, t):
43
+ return a + (b - a) * t
44
+
45
+
46
+ _wash = None
47
+ _seen = None
48
+
49
+
50
+ def wash(color=None, amount=0.0):
51
+ """Mix every colour drawn from now on toward *color*: moonlight, dusk, fog.
52
+
53
+ Call it with no arguments to draw in true colours again.
54
+ """
55
+ global _wash
56
+ _wash = (color, amount) if color is not None and amount > 0 else None
57
+
58
+
59
+ def colours(note=True):
60
+ """Start noting every colour drawn, in order; returns the list they go in.
61
+
62
+ The same drawing made in two sets of colours gives two lists that pair
63
+ off, old with new: the table `svgio.SvgDecoder.decode` takes as
64
+ ``colors`` to draw one as the other. ``colours(False)`` stops.
65
+ """
66
+ global _seen
67
+ _seen = [] if note else None
68
+ return _seen
69
+
70
+
71
+ _GRADIENT = 0x1000000 # a paint at or above this is a gradient, by its number
72
+
73
+
74
+ class Data:
75
+ """Path data: what `pts` and `smooth` give, and `path` takes.
76
+
77
+ Add path data written as text to either end of it, as you would to a
78
+ string: ``smooth(points) + "L800 480Z"``.
79
+ """
80
+
81
+ def __init__(self, segments):
82
+ self.segments = segments
83
+
84
+ def __add__(self, other):
85
+ return Data(self.segments + _data(other))
86
+
87
+ def __radd__(self, other):
88
+ return Data(_data(other) + self.segments)
89
+
90
+ def tail(self):
91
+ """The same path without its opening "M", to carry on from another."""
92
+ return Data(b"P" + self.segments[1:])
93
+
94
+
95
+ # -- shapes as records of svgio's compact form ---------------------------
96
+ #
97
+ # A record is an opcode byte and its fields, which svgio's README lists; a
98
+ # number is this runtime's own float, so svgio writes it as "%.5g" would here.
99
+
100
+ _F = "d" if 1.0 + 1e-10 != 1.0 else "f"
101
+ _MAGIC = b"\x01SVG" + (b"\x08" if _F == "d" else b"\x04")
102
+ _NO_PAINT = 0xFFFFFFFF # fill="none"
103
+ _NO_STROKE = 0xFFFFFFFE # no stroke attributes at all
104
+
105
+ _pack = struct.pack
106
+ _STYLE = "II2" + _F + "H" # fill, stroke, stroke width, opacity, bytes of extra attributes
107
+ _CIRCLE = "<B3" + _F + _STYLE
108
+ _ELLIPSE = "<B5" + _F + _STYLE
109
+ _RECT = "<B5" + _F + _STYLE
110
+ _PATH = "<BI"
111
+ _STYLED = "<" + _STYLE
112
+ _SEG2 = "<B2" + _F
113
+ _SEG6 = "<B6" + _F
114
+ _TEXT = "<BH"
115
+ _LIMB = "<BIB2" + _F + "B4" + _F + "B2" + _F + "B4" + _F + "B" + _STYLE + "B3" + _F + _STYLE + "B3" + _F + _STYLE
116
+ _LIMB_D = 5 + 12 * struct.calcsize(_F)
117
+ _GROUP = "<B" + _F + "H"
118
+ _AT = "<B4" + _F + "B"
119
+ _VIEW = "<B4" + _F
120
+ _LINEAR = "<B4" + _F + "BB"
121
+ _RADIAL = "<B3" + _F + "B2" + _F + "BB"
122
+ _STOP = "<" + _F + "I" + _F
123
+ _M, _L, _C, _Q, _Z, _P, _T = 77, 76, 67, 81, 90, 80, 84
124
+
125
+
126
+ def _paint(value):
127
+ # a colour as an int, or a gradient from a Doc; None paints nothing
128
+ if value is None:
129
+ return _NO_PAINT
130
+ if value < _GRADIENT:
131
+ if _wash:
132
+ value = mix(value, _wash[0], _wash[1])
133
+ if _seen is not None:
134
+ _seen.append(value)
135
+ return value
136
+
137
+
138
+ def _style(fill, stroke, width, opacity, extra):
139
+ return (
140
+ _paint(fill),
141
+ _NO_STROKE if stroke is None else _paint(stroke),
142
+ width,
143
+ 1.0 if opacity is None else opacity,
144
+ len(extra),
145
+ )
146
+
147
+
148
+ def _data(d):
149
+ if isinstance(d, Data):
150
+ return d.segments
151
+ d = d.encode()
152
+ return _pack(_TEXT, _T, len(d)) + d
153
+
154
+
155
+
156
+ def circle(cx, cy, r, fill=None, stroke=None, width=1, opacity=None, extra=""):
157
+ shape = _pack(_CIRCLE, 1, cx, cy, r, *_style(fill, stroke, width, opacity, extra))
158
+ return shape + extra.encode() if extra else shape
159
+
160
+
161
+ def ellipse(cx, cy, rx, ry, fill=None, stroke=None, width=1, opacity=None, rot=0, extra=""):
162
+ shape = _pack(_ELLIPSE, 2, cx, cy, rx, ry, rot, *_style(fill, stroke, width, opacity, extra))
163
+ return shape + extra.encode() if extra else shape
164
+
165
+
166
+ def rect(x, y, w, h, fill=None, r=0, stroke=None, width=1, opacity=None, extra=""):
167
+ shape = _pack(_RECT, 3, x, y, w, h, r, *_style(fill, stroke, width, opacity, extra))
168
+ return shape + extra.encode() if extra else shape
169
+
170
+
171
+ def path(d, fill=None, stroke=None, width=1, opacity=None, extra=""):
172
+ """A path from its data: text, as SVG writes it, or what `pts` and `smooth` give."""
173
+ d = _data(d)
174
+ shape = _pack(_PATH, 4, len(d)) + d + _pack(_STYLED, *_style(fill, stroke, width, opacity, extra))
175
+ return shape + extra.encode() if extra else shape
176
+
177
+
178
+ def line(x0, y0, x1, y1, stroke, width=1, opacity=None, extra=""):
179
+ return path(Data(_pack(_SEG2, _M, x0, y0) + _pack(_SEG2, _L, x1, y1)), None, stroke, width, opacity, extra)
180
+
181
+
182
+ def pts(points, close=False):
183
+ """Path data through *points*, straight lines."""
184
+ d = [_pack(_SEG2, _L, x, y) for x, y in points]
185
+ d[0] = _pack(_SEG2, _M, points[0][0], points[0][1])
186
+ if close:
187
+ d.append(b"Z")
188
+ return Data(b"".join(d))
189
+
190
+
191
+ def smooth(points, close=False):
192
+ """Path data through *points* as one smooth curve (Catmull-Rom, as cubics)."""
193
+ n = len(points)
194
+ d = [_pack(_SEG2, _M, points[0][0], points[0][1])]
195
+ last = n if close else n - 1
196
+ for i in range(last):
197
+ if close:
198
+ p0, p1, p2, p3 = points[(i - 1) % n], points[i], points[(i + 1) % n], points[(i + 2) % n]
199
+ else:
200
+ p0, p1, p2, p3 = points[max(i - 1, 0)], points[i], points[i + 1], points[min(i + 2, n - 1)]
201
+ d.append(
202
+ _pack(
203
+ _SEG6,
204
+ _C,
205
+ p1[0] + (p2[0] - p0[0]) / 6,
206
+ p1[1] + (p2[1] - p0[1]) / 6,
207
+ p2[0] - (p3[0] - p1[0]) / 6,
208
+ p2[1] - (p3[1] - p1[1]) / 6,
209
+ p2[0],
210
+ p2[1],
211
+ )
212
+ )
213
+ if close:
214
+ d.append(b"Z")
215
+ return Data(b"".join(d))
216
+
217
+
218
+ def limb(x0, y0, x1, y1, bend, w0, w1, fill, opacity=None):
219
+ """A curved, tapering limb from one point to another, round at both ends.
220
+
221
+ *bend* pushes the middle sideways, as a fraction of the length: positive
222
+ bends to the left of the direction of travel.
223
+ """
224
+ dx, dy = x1 - x0, y1 - y0
225
+ length = math.sqrt(dx * dx + dy * dy) or 1
226
+ nx, ny = dy / length, -dx / length
227
+ cx, cy = (x0 + x1) / 2 + nx * bend * length, (y0 + y1) / 2 + ny * bend * length
228
+ ex, ey = cx - x0, cy - y0
229
+ el = math.sqrt(ex * ex + ey * ey) or 1
230
+ n0x, n0y = ey / el, -ex / el
231
+ ex, ey = x1 - cx, y1 - cy
232
+ el = math.sqrt(ex * ex + ey * ey) or 1
233
+ n1x, n1y = ey / el, -ex / el
234
+ h0, h1, hm = w0 / 2, w1 / 2, (w0 + w1) / 4
235
+ fill = _paint(fill)
236
+ if opacity is None:
237
+ opacity = 1.0
238
+ # the body, a path, and a circle at each end: three shapes in one go
239
+ return _pack(
240
+ _LIMB,
241
+ 4,
242
+ _LIMB_D,
243
+ _M,
244
+ x0 + n0x * h0,
245
+ y0 + n0y * h0,
246
+ _Q,
247
+ cx + nx * hm,
248
+ cy + ny * hm,
249
+ x1 + n1x * h1,
250
+ y1 + n1y * h1,
251
+ _L,
252
+ x1 - n1x * h1,
253
+ y1 - n1y * h1,
254
+ _Q,
255
+ cx - nx * hm,
256
+ cy - ny * hm,
257
+ x0 - n0x * h0,
258
+ y0 - n0y * h0,
259
+ _Z,
260
+ fill,
261
+ _NO_STROKE,
262
+ 1,
263
+ opacity,
264
+ 0,
265
+ 1,
266
+ x0,
267
+ y0,
268
+ h0,
269
+ fill,
270
+ _NO_STROKE,
271
+ 1,
272
+ opacity,
273
+ 0,
274
+ 1,
275
+ x1,
276
+ y1,
277
+ h1,
278
+ fill,
279
+ _NO_STROKE,
280
+ 1,
281
+ opacity,
282
+ 0,
283
+ )
284
+
285
+
286
+ def group(body, transform=None, opacity=None):
287
+ transform = transform.encode() if transform else b""
288
+ return _pack(_GROUP, 6, 1.0 if opacity is None else opacity, len(transform)) + transform + body + b"\x07"
289
+
290
+
291
+ def at(x, y, body, rot=0, scale=1, flip=False):
292
+ """*body* moved to (x, y), turned *rot* degrees and scaled about its origin."""
293
+ return _pack(_AT, 8, x, y, rot, scale, 1 if flip else 0) + body + b"\x07"
294
+
295
+
296
+ class Doc:
297
+ """A drawing: shapes in paint order, and the gradients they use."""
298
+
299
+ def __init__(self):
300
+ self._defs = []
301
+ self._body = []
302
+
303
+ def _stops(self, stops):
304
+ out = []
305
+ for stop in stops:
306
+ opacity = stop[2] if len(stop) > 2 else 1
307
+ out.append(_pack(_STOP, stop[0], _paint(stop[1]), opacity))
308
+ return b"".join(out)
309
+
310
+ def linear(self, stops, x1=0, y1=0, x2=0, y2=1, user=False):
311
+ """A linear gradient; returns the paint to fill or stroke with.
312
+
313
+ Coordinates are fractions of the shape's box, or drawing units with
314
+ *user*. Each stop is ``(offset, colour)`` or ``(offset, colour, opacity)``.
315
+ """
316
+ gid = len(self._defs)
317
+ self._defs.append(_pack(_LINEAR, 11, x1, y1, x2, y2, 1 if user else 0, len(stops)) + self._stops(stops))
318
+ return _GRADIENT + gid
319
+
320
+ def radial(self, stops, cx=0.5, cy=0.5, r=0.5, fx=None, fy=None, user=False):
321
+ """A radial gradient, with its bright point at (*fx*, *fy*) when given."""
322
+ gid = len(self._defs)
323
+ focus = fx is not None
324
+ self._defs.append(
325
+ _pack(_RADIAL, 12, cx, cy, r, 1 if focus else 0, fx if focus else 0, fy if focus else 0, 1 if user else 0, len(stops))
326
+ + self._stops(stops)
327
+ )
328
+ return _GRADIENT + gid
329
+
330
+ def add(self, *shapes):
331
+ self._body.extend(shapes)
332
+
333
+ def source(self, view):
334
+ """What `svgio.SvgDecoder.open` takes to show *view*, an ``(x, y, width, height)`` of the drawing: svgio's compact form."""
335
+ x, y, w, h = view
336
+ return b"".join([_MAGIC, _pack(_VIEW, 10, x, y, w, h)] + self._defs + [b"\x0d"] + self._body + [b"\x0e"])
337
+
338
+ def svg(self, view):
339
+ """The SVG text showing *view*, an ``(x, y, width, height)`` of the drawing."""
340
+ source = self.source(view)
341
+ return str(svgio.expand(source), "utf-8")
342
+
343
+
344
+ def render(source, width, background=None):
345
+ """Draw a drawing, as `Doc.source` or `Doc.svg` gives it, *width* pixels wide.
346
+
347
+ Returns:
348
+ ``(pixels, alpha, width, height)``: RGB565 and coverage, the pair
349
+ LVGL calls RGB565A8. With a *background* colour the picture is
350
+ blended over it and *alpha* is None.
351
+ """
352
+ decoder = svgio.SvgDecoder()
353
+ w, h = decoder.open(source.encode() if isinstance(source, str) else source, width=width)
354
+ pixels = bytearray(w * h * 2)
355
+ if background is None:
356
+ alpha = bytearray(w * h)
357
+ decoder.decode(pixels, alpha)
358
+ return pixels, alpha, w, h
359
+ c = ((background >> 8) & 0xF800) | ((background >> 5) & 0x07E0) | ((background >> 3) & 0x1F)
360
+ lo, hi = c & 255, c >> 8
361
+ if lo or hi:
362
+ for i in range(0, len(pixels), 2):
363
+ pixels[i] = lo
364
+ pixels[i + 1] = hi
365
+ decoder.decode(pixels)
366
+ return pixels, None, w, h
apps/repl/__init__.py ADDED
@@ -0,0 +1,325 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """REPL: the board's own Python prompt on its screen, typed on a keyboard.
5
+
6
+ A USB keyboard on the board's host port and a BLE keyboard both work; the
7
+ app finds each on its own, and finds it again after it's unplugged. The
8
+ terminal is vtterm, drawn straight into the panel's framebuffer under the
9
+ app's area, so colors, line editing and Ctrl-C into a running program all
10
+ work as they do over USB.
11
+
12
+ The REPL is MicroPython's, through ``os.dupterm``. Opening the app ends the
13
+ program's main loop, as Ctrl-C would, so the REPL can run; timers and the
14
+ other apps keep going. A board's one dupterm slot is shared with WebREPL:
15
+ while a WebREPL client is connected, the app waits for it to leave.
16
+
17
+ It needs vtterm (``mip.install("github:PyDevices/omnipython/packages/vtterm")``)
18
+ and firmware whose palettes has its ANSI palette. Shift+Page Up and Page Down
19
+ page back through what scrolled off the screen.
20
+
21
+ In a browser page the REPL is the page's own until a serial port is chosen
22
+ under Serial ports in Connections (Chrome or Edge): then the app is a
23
+ terminal to the board on that port, typed on the computer's keyboard.
24
+ Choosing another port moves the terminal there, and letting it go brings
25
+ the page's REPL back.
26
+ """
27
+
28
+ import os
29
+ import sys
30
+
31
+ import lvgl as lv
32
+ from board_config import display_drv
33
+ from palettes.ansi import AnsiPalette
34
+ from vtterm import Terminal
35
+ from vtterm.console import Console
36
+ from vtterm.keyboards import DisplayKeyboard, start_all
37
+ from vtterm.keys import key_bytes, scroll_key
38
+
39
+ #: How often keys are read and output drawn, in ms.
40
+ TICK_MS = 20
41
+ #: Height of the keyboard status line under the terminal, in pixels.
42
+ STATUS_H = 20
43
+ #: events.KEYDOWN
44
+ KEYDOWN = 0x300
45
+ #: How long after the REPL takes over the USB and BLE keyboards are started, in ms.
46
+ KEYBOARDS_MS = 100
47
+ #: How often the app asks for the dupterm slot while WebREPL holds it, in ms.
48
+ WAIT_MS = 1000
49
+ #: The speed a board's serial REPL runs at, in a browser page.
50
+ BAUDRATE = 115200
51
+ #: What the status line says in a browser page before a port is chosen.
52
+ CHOOSE_TEXT = "Choose a board under Serial ports in Connections"
53
+
54
+
55
+ class _Area:
56
+ """The part of the panel under the app's root, as a display vtterm draws on.
57
+
58
+ Rows go straight into the framebuffer, and only the rows drawn are made
59
+ visible: LVGL owns the rest of the panel and its buffer swaps.
60
+ """
61
+
62
+ color_depth = 16
63
+
64
+ def __init__(self, display, x, y, w, h):
65
+ self.display = display
66
+ self.x = x
67
+ self.y = y
68
+ self.width = w
69
+ self.height = h
70
+ self._box = None
71
+
72
+ def blit_rect(self, buf, x, y, w, h):
73
+ self.display.blit_rect(buf, self.x + x, self.y + y, w, h)
74
+ b = self._box
75
+ self._box = (y, y + h) if b is None else (min(b[0], y), max(b[1], y + h))
76
+
77
+ def fill(self, color):
78
+ self.display.fill_rect(self.x, self.y, self.width, self.height, color)
79
+ self._box = (0, self.height)
80
+
81
+ def show(self):
82
+ b, self._box = self._box, None
83
+ if b is None:
84
+ return
85
+ flush = getattr(self.display, "flush_rect", None)
86
+ if flush is None or not flush(self.x, self.y + b[0], self.width, b[1] - b[0]):
87
+ self.display.show()
88
+
89
+
90
+ def main(scope):
91
+ status, x, y, w, rows_h = _root(scope)
92
+ status.set_text("Starting")
93
+ shown = [None]
94
+
95
+ def say(text):
96
+ if text != shown[0]:
97
+ shown[0] = text
98
+ status.set_text(text)
99
+
100
+ def area():
101
+ return _Area(display_drv, x, y, w, rows_h)
102
+
103
+ serial = _webserial()
104
+ chosen = []
105
+ mode = [None]
106
+
107
+ def switch(endpoints):
108
+ want = _Serial if endpoints or not hasattr(os, "dupterm") else _Own
109
+ now = mode[0]
110
+ if now is not None and type(now) is want:
111
+ now.use(endpoints)
112
+ return
113
+ if now is not None:
114
+ now.stop()
115
+ scope.stop_timers()
116
+ mode[0] = want(scope, area, say)
117
+ mode[0].start(endpoints)
118
+
119
+ if serial is not None:
120
+ chosen = scope.follow("serial", switch)
121
+ elif not hasattr(os, "dupterm"):
122
+ say("This Python has no REPL to show")
123
+ return
124
+
125
+ def close():
126
+ if mode[0] is not None:
127
+ mode[0].stop()
128
+
129
+ scope.on_close(close)
130
+ # After LVGL has painted the root, or it would paint over the terminal.
131
+ scope.after(150, lambda _t=None: switch(chosen))
132
+
133
+
134
+ def _webserial():
135
+ """The `webserial` module in a browser page, or None elsewhere."""
136
+ if sys.platform not in ("webassembly", "emscripten"):
137
+ return None
138
+ try:
139
+ import webserial
140
+ except ImportError:
141
+ return None
142
+ return webserial
143
+
144
+
145
+ def _keys(sources, terminal):
146
+ """The bytes the keys pressed since the last call type, or None."""
147
+ out = None
148
+ for src in sources:
149
+ try:
150
+ evs = src.poll()
151
+ except Exception as e: # noqa: BLE001 - shown; the source keeps trying
152
+ evs = ()
153
+ src.status = str(e)
154
+ for e in evs:
155
+ if e.type == KEYDOWN:
156
+ if scroll_key(terminal.screen, e.key, e.mod):
157
+ continue
158
+ b = key_bytes(e.key, e.mod, terminal.screen.app_cursor)
159
+ if b:
160
+ out = b if out is None else out + b
161
+ return out
162
+
163
+
164
+ def _display_keys():
165
+ """The display's own keyboard as a list of sources: empty when it has none."""
166
+ try:
167
+ return [DisplayKeyboard()]
168
+ except (ImportError, AttributeError):
169
+ return []
170
+
171
+
172
+ def _close_all(sources):
173
+ for src in sources:
174
+ try:
175
+ src.close()
176
+ except Exception as e: # noqa: BLE001 - closing the rest matters more
177
+ print("repl app: closing", src, e)
178
+
179
+
180
+ class _Own:
181
+ """This Python's own REPL, through ``os.dupterm``."""
182
+
183
+ def __init__(self, scope, area, say):
184
+ self.scope = scope
185
+ self.area = area
186
+ self.say = say
187
+ self.console = None
188
+ self.sources = []
189
+ self.missing = []
190
+
191
+ def status(self, waiting=False):
192
+ parts = [src.status for src in self.sources] + self.missing
193
+ if waiting:
194
+ parts.insert(0, "WebREPL is connected: the REPL comes here when it leaves")
195
+ self.say(" ".join(parts))
196
+
197
+ def start(self, endpoints):
198
+ terminal = Terminal(self.area(), palette=AnsiPalette(length=256, color_depth=16))
199
+ # The first open after a boot takes a few seconds before the REPL
200
+ # prints its prompt: say so, and keep the keys typed meanwhile (the
201
+ # display's first, which is quick; the USB and BLE keyboards
202
+ # take longer to start, so they come after the REPL has begun).
203
+ terminal.feed(b"Starting the REPL...\r\n")
204
+ self.console = Console(terminal)
205
+ self.sources = _display_keys()
206
+ self.take()
207
+ self.scope.after(KEYBOARDS_MS, self.more_keys)
208
+
209
+ def more_keys(self, _t=None):
210
+ """Start every keyboard source, the display's among them."""
211
+ _close_all(self.sources)
212
+ self.sources, self.missing = start_all()
213
+ self.status()
214
+
215
+ def use(self, endpoints):
216
+ pass
217
+
218
+ def take(self, _t=None):
219
+ try:
220
+ self.console.attach(0)
221
+ except OSError:
222
+ self.status(waiting=True)
223
+ self.scope.after(WAIT_MS, self.take)
224
+ return
225
+ self.status()
226
+ fg = sys.modules.get("appkit.foreground")
227
+ if fg is not None and fg._serving:
228
+ # The REPL runs once the main loop returns; timers keep going.
229
+ fg.stop()
230
+ else:
231
+ self.console.push(b"\r") # a fresh prompt drawn here
232
+ self.scope.every(TICK_MS, self.tick)
233
+
234
+ def tick(self, _t=None):
235
+ console = self.console
236
+ out = _keys(self.sources, console.terminal)
237
+ if out:
238
+ console.push(out)
239
+ console.refresh()
240
+ self.status()
241
+
242
+ def stop(self):
243
+ if self.console is not None:
244
+ self.console.detach()
245
+ _close_all(self.sources)
246
+ self.sources = []
247
+
248
+
249
+ class _Serial:
250
+ """A terminal to the board on the serial port chosen in Connections."""
251
+
252
+ def __init__(self, scope, area, say):
253
+ self.scope = scope
254
+ self.say = say
255
+ self.term = Terminal(area(), palette=AnsiPalette(length=256, color_depth=16))
256
+ self.sources = start_all()[0] # the page's own keyboard; a board's USB and BLE don't apply here
257
+ self.port = None
258
+ self.ready = False
259
+
260
+ def start(self, endpoints):
261
+ self.use(endpoints)
262
+ self.scope.every(TICK_MS, self.tick)
263
+
264
+ def use(self, endpoints):
265
+ self._let_go()
266
+ if not endpoints:
267
+ self.say(CHOOSE_TEXT)
268
+ return
269
+ try:
270
+ self.port = _webserial().open(endpoints[0], BAUDRATE)
271
+ except OSError as e:
272
+ self.say(str(e))
273
+ return
274
+ self.say("Opening %s" % self.port.name)
275
+
276
+ def tick(self, _t=None):
277
+ term = self.term
278
+ out = _keys(self.sources, term)
279
+ port = self.port
280
+ if port is None:
281
+ return
282
+ if port.error:
283
+ self.say("%s: %s" % (port.name, port.error))
284
+ return
285
+ if not port.is_open():
286
+ return
287
+ if not self.ready:
288
+ self.ready = True
289
+ self.say(port.name)
290
+ port.write(b"\r") # a fresh prompt from the board
291
+ answers = term.answers()
292
+ if answers:
293
+ out = answers if out is None else out + answers
294
+ if out:
295
+ port.write(out)
296
+ data = port.read()
297
+ if data:
298
+ term.write(data)
299
+
300
+ def _let_go(self):
301
+ port, self.port = self.port, None
302
+ self.ready = False
303
+ if port is not None:
304
+ port.close()
305
+
306
+ def stop(self):
307
+ self._let_go()
308
+ _close_all(self.sources)
309
+ self.sources = []
310
+
311
+
312
+ def _root(scope):
313
+ """Paint the app's root black and put the status line under it; ``(status, x, y, w, rows_h)``."""
314
+ root = scope.root
315
+ root.remove_flag(lv.obj.FLAG.SCROLLABLE)
316
+ root.set_style_bg_color(lv.color_hex(0), 0)
317
+ root.set_style_bg_opa(lv.OPA.COVER, 0)
318
+ x, y, w, h = scope.rect
319
+ status = lv.label(root)
320
+ status.set_style_text_color(lv.color_hex(0x5FAFAF), 0)
321
+ status.set_style_text_font(lv.font_montserrat_14, 0)
322
+ status.set_long_mode(lv.label.LONG_MODE.CLIP)
323
+ status.set_width(w - 8)
324
+ status.align(lv.ALIGN.BOTTOM_LEFT, 4, -2)
325
+ return status, x, y, w, (h - STATUS_H) // 16 * 16
apps/repl/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "repl", "title": "REPL", "category": "System", "icon": "terminal", "color": "#2E3440", "needs": ["os.dupterm|webserial"]}