omnipython-applibs 0.1.2__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.
- appkit/__init__.py +52 -0
- appkit/__init__.pyi +17 -0
- appkit/data.py +149 -0
- appkit/data.pyi +25 -0
- appkit/foreground.py +357 -0
- appkit/foreground.pyi +45 -0
- appkit/home.py +19 -0
- appkit/home.pyi +8 -0
- appkit/hover.py +59 -0
- appkit/hover.pyi +13 -0
- appkit/index.py +199 -0
- appkit/index.pyi +30 -0
- appkit/launcher.py +1075 -0
- appkit/launcher.pyi +96 -0
- appkit/manifest.py +75 -0
- appkit/manifest.pyi +8 -0
- appkit/net.py +167 -0
- appkit/net.pyi +34 -0
- appkit/places.py +202 -0
- appkit/places.pyi +39 -0
- appkit/py.typed +0 -0
- appkit/scope.py +636 -0
- appkit/scope.pyi +110 -0
- appkit/shell.py +121 -0
- appkit/shell.pyi +32 -0
- appkit/worker.py +409 -0
- appkit/worker.pyi +83 -0
- controlkit/__init__.py +19 -0
- controlkit/__init__.pyi +10 -0
- controlkit/_draw.py +88 -0
- controlkit/_safe.py +29 -0
- controlkit/dotmatrix.py +101 -0
- controlkit/dotmatrix.pyi +23 -0
- controlkit/encoder.py +297 -0
- controlkit/encoder.pyi +55 -0
- controlkit/font5x7.py +22 -0
- controlkit/font5x7.pyi +11 -0
- controlkit/lamp_button.py +92 -0
- controlkit/lamp_button.pyi +40 -0
- controlkit/meter.py +86 -0
- controlkit/meter.pyi +30 -0
- controlkit/mock.py +116 -0
- controlkit/mock.pyi +35 -0
- controlkit/pad_grid.py +113 -0
- controlkit/pad_grid.pyi +41 -0
- controlkit/py.typed +0 -0
- controlkit/slider.py +185 -0
- controlkit/slider.pyi +49 -0
- controlkit/sprites.py +279 -0
- controlkit/sprites.pyi +22 -0
- controlkit/theme.py +120 -0
- controlkit/theme.pyi +15 -0
- deskkit/__init__.py +74 -0
- deskkit/__init__.pyi +18 -0
- deskkit/forecast.py +261 -0
- deskkit/forecast.pyi +40 -0
- deskkit/localtime.py +215 -0
- deskkit/localtime.pyi +33 -0
- deskkit/py.typed +0 -0
- deskkit/shell.py +113 -0
- deskkit/shell.pyi +33 -0
- omnipython_applibs-0.1.2.dist-info/METADATA +90 -0
- omnipython_applibs-0.1.2.dist-info/RECORD +143 -0
- omnipython_applibs-0.1.2.dist-info/WHEEL +5 -0
- omnipython_applibs-0.1.2.dist-info/licenses/LICENSE +21 -0
- omnipython_applibs-0.1.2.dist-info/top_level.txt +9 -0
- programkit/__init__.py +27 -0
- programkit/__init__.pyi +8 -0
- programkit/board.py +56 -0
- programkit/board.pyi +20 -0
- programkit/press.py +63 -0
- programkit/press.pyi +17 -0
- programkit/py.typed +0 -0
- programkit/rows.py +149 -0
- programkit/rows.pyi +34 -0
- sayso/__init__.py +109 -0
- sayso/__init__.pyi +124 -0
- sayso/auth.py +811 -0
- sayso/auth.pyi +102 -0
- sayso/client.py +1286 -0
- sayso/client.pyi +138 -0
- sayso/keepalive.py +371 -0
- sayso/keepalive.pyi +53 -0
- sayso/local.py +85 -0
- sayso/local.pyi +28 -0
- sayso/object_specs.py +706 -0
- sayso/object_specs.pyi +8 -0
- sayso/objects.py +411 -0
- sayso/objects.pyi +854 -0
- sayso/py.typed +0 -0
- sayso/schema_coverage.py +210 -0
- sayso/schema_coverage.pyi +13 -0
- sayso/transport.py +426 -0
- sayso/transport.pyi +47 -0
- songmodel/__init__.py +30 -0
- songmodel/__init__.pyi +9 -0
- songmodel/link.py +189 -0
- songmodel/link.pyi +53 -0
- songmodel/model.py +238 -0
- songmodel/model.pyi +40 -0
- songmodel/page.html +225 -0
- songmodel/page_data.py +236 -0
- songmodel/page_data.pyi +6 -0
- songmodel/py.typed +0 -0
- soundmodule/__init__.py +7 -0
- soundmodule/__init__.pyi +6 -0
- soundmodule/engine.py +1001 -0
- soundmodule/engine.pyi +196 -0
- soundmodule/names.py +298 -0
- soundmodule/names.pyi +30 -0
- soundmodule/pipes.py +105 -0
- soundmodule/pipes.pyi +31 -0
- soundmodule/py.typed +0 -0
- soundmodule/run.py +249 -0
- soundmodule/run.pyi +49 -0
- spectrum/__init__.py +72 -0
- spectrum/__init__.pyi +30 -0
- spectrum/analyzer.py +231 -0
- spectrum/analyzer.pyi +67 -0
- spectrum/fake_music.py +201 -0
- spectrum/fake_music.pyi +36 -0
- spectrum/pump_levels.py +126 -0
- spectrum/pump_levels.pyi +46 -0
- spectrum/py.typed +0 -0
- spectrum/spectrum_view.py +403 -0
- spectrum/spectrum_view.pyi +80 -0
- touchkit/__init__.py +4 -0
- touchkit/__init__.pyi +4 -0
- touchkit/cam.py +146 -0
- touchkit/cam.pyi +35 -0
- touchkit/clock.py +18 -0
- touchkit/clock.pyi +6 -0
- touchkit/ecp.py +134 -0
- touchkit/ecp.pyi +28 -0
- touchkit/net.py +347 -0
- touchkit/net.pyi +27 -0
- touchkit/py.typed +0 -0
- touchkit/settings.py +85 -0
- touchkit/settings.pyi +17 -0
- touchkit/shell.py +268 -0
- touchkit/shell.pyi +42 -0
- touchkit/ui.py +167 -0
- touchkit/ui.pyi +53 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
spectrum_view.py -- draws a log-frequency spectrum analyzer with pygraphics.
|
|
7
|
+
|
|
8
|
+
``SpectrumView`` owns two RGB565 framebuffers the size of the screen: a static
|
|
9
|
+
background (gradient, grid, frequency labels) painted once, and the frame that
|
|
10
|
+
gets shown. Each frame it copies the background back over only the rows the
|
|
11
|
+
bars could have touched, blits the bars on top, and returns that row strip so
|
|
12
|
+
the caller can push just those rows to the panel.
|
|
13
|
+
|
|
14
|
+
A bar is one ``blit`` from a precomputed gradient column, so its cost does not
|
|
15
|
+
grow with its height. The reflection is a second, shorter column blitted under
|
|
16
|
+
the baseline, and the gaps of the segmented style are a colour key in the
|
|
17
|
+
column, so the grid shows through them.
|
|
18
|
+
|
|
19
|
+
Levels come in as 0..1 per band (0 = the floor, 1 = full scale); the view
|
|
20
|
+
applies the rise and fall ballistics and the peak-hold caps itself, so a
|
|
21
|
+
source only has to say how loud each band is right now.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
import math
|
|
25
|
+
|
|
26
|
+
from pygraphics import RGB565, Font, FrameBuffer
|
|
27
|
+
|
|
28
|
+
# Bottom-to-top colour stops of the bars: deep blue, blue, cyan, teal, violet.
|
|
29
|
+
PALETTE = (
|
|
30
|
+
(0.00, (10, 24, 110)),
|
|
31
|
+
(0.28, (0, 84, 220)),
|
|
32
|
+
(0.52, (0, 180, 245)),
|
|
33
|
+
(0.70, (30, 240, 225)),
|
|
34
|
+
(0.86, (120, 150, 255)),
|
|
35
|
+
(1.00, (200, 110, 255)),
|
|
36
|
+
)
|
|
37
|
+
BG_TOP = (1, 3, 10) # background at the top of the plot
|
|
38
|
+
BG_BASE = (4, 10, 26) # background at the baseline
|
|
39
|
+
BG_FLOOR = (2, 5, 14) # under the baseline (reflection and labels)
|
|
40
|
+
GRID = (20, 36, 64)
|
|
41
|
+
GRID_MINOR = (13, 24, 46)
|
|
42
|
+
BASELINE = (30, 70, 120)
|
|
43
|
+
LABEL = (96, 136, 180)
|
|
44
|
+
LABEL_DECADE = (150, 205, 240)
|
|
45
|
+
|
|
46
|
+
# 35 Hz, not 20: on the P4 with a full-range track the 22, 26 and 30 Hz bars
|
|
47
|
+
# sat near empty and every bar from 35 Hz up moved (audiometer's default).
|
|
48
|
+
LOW_HZ = 35.0
|
|
49
|
+
HIGH_HZ = 20000.0
|
|
50
|
+
LABELS = (50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000)
|
|
51
|
+
|
|
52
|
+
ATTACK_S = 0.025 # rise time constant
|
|
53
|
+
FALL_PER_S = 1.6 # bars fall this much of full scale per second
|
|
54
|
+
PEAK_HOLD_S = 0.55 # peak caps hang this long before falling
|
|
55
|
+
PEAK_GRAVITY = 2.6 # then accelerate down at this (full scale per s^2)
|
|
56
|
+
|
|
57
|
+
KEY = 0x0020 # colour key for transparent gradient rows; never a drawn colour
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def rgb565(rgb):
|
|
61
|
+
r, g, b = rgb
|
|
62
|
+
c = ((int(r) & 0xF8) << 8) | ((int(g) & 0xFC) << 3) | (int(b) >> 3)
|
|
63
|
+
return c + 1 if c == KEY else c
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def mix(a, b, t):
|
|
67
|
+
return (a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def palette_at(frac):
|
|
71
|
+
if frac <= PALETTE[0][0]:
|
|
72
|
+
return PALETTE[0][1]
|
|
73
|
+
for i in range(1, len(PALETTE)):
|
|
74
|
+
f1, c1 = PALETTE[i]
|
|
75
|
+
if frac <= f1:
|
|
76
|
+
f0, c0 = PALETTE[i - 1]
|
|
77
|
+
return mix(c0, c1, (frac - f0) / (f1 - f0))
|
|
78
|
+
return PALETTE[-1][1]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def band_count_for(plot_w):
|
|
82
|
+
"""About one band per 12-16 px, clamped to 16..48."""
|
|
83
|
+
return max(16, min(48, plot_w // (12 if plot_w < 480 else 16)))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def label_text(hz):
|
|
87
|
+
return "{}k".format(hz // 1000) if hz >= 1000 else str(hz)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def band_centres(n):
|
|
91
|
+
"""Centre frequency of each of ``n`` log-spaced bands between LOW_HZ and HIGH_HZ."""
|
|
92
|
+
span = HIGH_HZ / LOW_HZ
|
|
93
|
+
return [LOW_HZ * span ** ((i + 0.5) / n) for i in range(n)]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class SpectrumView:
|
|
97
|
+
def __init__(self, width, height, bands=None, style="smooth"):
|
|
98
|
+
self.width = width
|
|
99
|
+
self.height = height
|
|
100
|
+
self.style = style
|
|
101
|
+
self._buf = bytearray(width * height * 2)
|
|
102
|
+
self._bgbuf = bytearray(width * height * 2)
|
|
103
|
+
self.fb = FrameBuffer(self._buf, width, height, RGB565)
|
|
104
|
+
self._bg = FrameBuffer(self._bgbuf, width, height, RGB565)
|
|
105
|
+
self._mv = memoryview(self._buf)
|
|
106
|
+
self._bgmv = memoryview(self._bgbuf)
|
|
107
|
+
|
|
108
|
+
# Layout, all from the screen size.
|
|
109
|
+
big = height >= 300
|
|
110
|
+
self.font = Font(height=16 if big else 8)
|
|
111
|
+
fh = self.font.height
|
|
112
|
+
pad = max(3, min(width, height) // 40)
|
|
113
|
+
side = max(pad, (3 * 8) // 2 + 2) # room for a centred "20k" at the right edge
|
|
114
|
+
plot_w = width - 2 * side
|
|
115
|
+
n = bands or band_count_for(plot_w)
|
|
116
|
+
pitch = plot_w // n
|
|
117
|
+
self.bands = n
|
|
118
|
+
self.pitch = pitch
|
|
119
|
+
self.gap = max(1, pitch // 5)
|
|
120
|
+
self.bar_w = pitch - self.gap
|
|
121
|
+
self.x0 = side + (plot_w - n * pitch) // 2 + self.gap // 2
|
|
122
|
+
self.span_x0 = self.x0 - self.gap // 2 # left edge of the band span
|
|
123
|
+
self.span_w = n * pitch
|
|
124
|
+
self.label_y = height - fh - max(2, pad // 2)
|
|
125
|
+
free = self.label_y - pad - 3
|
|
126
|
+
self.refl_h = max(4, free // 8)
|
|
127
|
+
self.top = pad
|
|
128
|
+
self.baseline = self.label_y - 3 - self.refl_h - 1 # first row below the bars
|
|
129
|
+
self.plot_h = self.baseline - self.top
|
|
130
|
+
self.refl_y = self.baseline + 1
|
|
131
|
+
self.cap_h = max(2, self.plot_h // 120 + 1)
|
|
132
|
+
self.hi_h = max(1, self.plot_h // 180)
|
|
133
|
+
seg_pitch = max(4, self.plot_h // 44)
|
|
134
|
+
self.seg_pitch = seg_pitch if style == "segmented" else 0
|
|
135
|
+
|
|
136
|
+
self._paint_background()
|
|
137
|
+
self._build_columns()
|
|
138
|
+
|
|
139
|
+
# Ballistics state: bar level, peak level, peak hold timer, peak velocity.
|
|
140
|
+
self.level = [0.0] * n
|
|
141
|
+
self.peak = [0.0] * n
|
|
142
|
+
self.hold = [0.0] * n
|
|
143
|
+
self.pvel = [0.0] * n
|
|
144
|
+
self._last_top = self.baseline
|
|
145
|
+
self.fb.blit(self._bg, 0, 0)
|
|
146
|
+
|
|
147
|
+
# --- static art -------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
def _bg_row_color(self, y):
|
|
150
|
+
if y >= self.baseline:
|
|
151
|
+
return BG_FLOOR
|
|
152
|
+
t = (y - self.top) / max(1, self.plot_h)
|
|
153
|
+
t = max(0.0, min(1.0, t))
|
|
154
|
+
return mix(BG_TOP, BG_BASE, t * t)
|
|
155
|
+
|
|
156
|
+
def _paint_background(self):
|
|
157
|
+
bg = self._bg
|
|
158
|
+
w = self.width
|
|
159
|
+
# Vertical gradient, one hline per row (a one-off cost).
|
|
160
|
+
for y in range(self.height):
|
|
161
|
+
bg.hline(0, y, w, rgb565(self._bg_row_color(y)))
|
|
162
|
+
x0, x1 = self.span_x0, self.span_x0 + self.span_w
|
|
163
|
+
# Horizontal grid: quarters of full scale.
|
|
164
|
+
for q in (1, 2, 3):
|
|
165
|
+
y = self.baseline - (self.plot_h * q) // 4
|
|
166
|
+
bg.hline(x0, y, x1 - x0, rgb565(GRID_MINOR if q != 2 else GRID))
|
|
167
|
+
bg.hline(x0, self.top, x1 - x0, rgb565(GRID_MINOR))
|
|
168
|
+
# Vertical grid and labels at the true log position of each frequency.
|
|
169
|
+
fh = self.font.height
|
|
170
|
+
for hz in LABELS:
|
|
171
|
+
x = self.freq_x(hz)
|
|
172
|
+
decade = hz in (100, 1000, 10000)
|
|
173
|
+
if x < x1 - 1:
|
|
174
|
+
bg.vline(x, self.top, self.plot_h, rgb565(GRID if decade else GRID_MINOR))
|
|
175
|
+
s = label_text(hz)
|
|
176
|
+
tw = self.font.text_width(s)
|
|
177
|
+
tx = max(0, min(self.width - tw, x - tw // 2))
|
|
178
|
+
self.font.text(bg, s, tx, self.label_y, rgb565(LABEL_DECADE if decade else LABEL))
|
|
179
|
+
bg.vline(x, self.label_y - 3, 2, rgb565(LABEL))
|
|
180
|
+
if fh >= 16:
|
|
181
|
+
self.font.text(bg, "Hz", 2, self.label_y, rgb565(LABEL))
|
|
182
|
+
bg.hline(x0, self.baseline, x1 - x0, rgb565(BASELINE))
|
|
183
|
+
|
|
184
|
+
def freq_x(self, hz):
|
|
185
|
+
u = math.log(hz / LOW_HZ) / math.log(HIGH_HZ / LOW_HZ)
|
|
186
|
+
return self.span_x0 + int(u * self.span_w + 0.5)
|
|
187
|
+
|
|
188
|
+
def _build_columns(self):
|
|
189
|
+
bw, ph, rh = self.bar_w, self.plot_h, self.refl_h
|
|
190
|
+
# Bar column: row 0 is the top of the plot.
|
|
191
|
+
col = bytearray(bw * ph * 2)
|
|
192
|
+
cfb = FrameBuffer(col, bw, ph, RGB565)
|
|
193
|
+
self._hilite = [0] * ph
|
|
194
|
+
self._capcol = [0] * ph
|
|
195
|
+
sp = self.seg_pitch
|
|
196
|
+
for r in range(ph):
|
|
197
|
+
h = ph - r # height of this row above the baseline, 1..ph
|
|
198
|
+
c = palette_at(h / ph)
|
|
199
|
+
gap = sp and (h % sp == 0)
|
|
200
|
+
cfb.hline(0, r, bw, KEY if gap else rgb565(c))
|
|
201
|
+
self._hilite[r] = rgb565(mix(c, (255, 255, 255), 0.45))
|
|
202
|
+
self._capcol[r] = rgb565(mix(c, (235, 240, 255), 0.7))
|
|
203
|
+
self._col = col
|
|
204
|
+
# Reflection column: row 0 sits just under the baseline and mirrors the
|
|
205
|
+
# bar's bottom row; it fades into the floor and skips every other line.
|
|
206
|
+
rcol = bytearray(bw * rh * 2)
|
|
207
|
+
rfb = FrameBuffer(rcol, bw, rh, RGB565)
|
|
208
|
+
for i in range(rh):
|
|
209
|
+
h = i + 1
|
|
210
|
+
c = palette_at(h / ph)
|
|
211
|
+
fade = 0.75 * (1 - i / rh) ** 1.2
|
|
212
|
+
gap = (i & 1) or (sp and (h % sp == 0))
|
|
213
|
+
rfb.hline(0, i, bw, KEY if gap else rgb565(mix(BG_FLOOR, c, fade)))
|
|
214
|
+
self._rcol = rcol
|
|
215
|
+
|
|
216
|
+
# --- per frame ----------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
def update(self, targets, dt):
|
|
219
|
+
"""Move the bars toward ``targets`` (0..1 per band) over ``dt`` seconds."""
|
|
220
|
+
a = 1 - math.exp(-dt / ATTACK_S)
|
|
221
|
+
fall = FALL_PER_S * dt
|
|
222
|
+
level, peak, hold, pvel = self.level, self.peak, self.hold, self.pvel
|
|
223
|
+
for i in range(self.bands):
|
|
224
|
+
t = targets[i]
|
|
225
|
+
t = 0.0 if t < 0 else (1.0 if t > 1 else t)
|
|
226
|
+
v = level[i]
|
|
227
|
+
if t > v:
|
|
228
|
+
v += (t - v) * a
|
|
229
|
+
else:
|
|
230
|
+
v = max(t, v - fall)
|
|
231
|
+
level[i] = v
|
|
232
|
+
if v >= peak[i]:
|
|
233
|
+
peak[i] = v
|
|
234
|
+
hold[i] = PEAK_HOLD_S
|
|
235
|
+
pvel[i] = 0.0
|
|
236
|
+
elif hold[i] > 0:
|
|
237
|
+
hold[i] -= dt
|
|
238
|
+
else:
|
|
239
|
+
pvel[i] += PEAK_GRAVITY * dt
|
|
240
|
+
peak[i] = max(v, peak[i] - pvel[i] * dt)
|
|
241
|
+
|
|
242
|
+
def render(self):
|
|
243
|
+
"""Draw this frame; return ``(y, h)``, the row strip that changed."""
|
|
244
|
+
fb = self.fb
|
|
245
|
+
ph, bw, base = self.plot_h, self.bar_w, self.baseline
|
|
246
|
+
cap_h, hi_h = self.cap_h, self.hi_h
|
|
247
|
+
# The strip to redraw: from the highest thing on screen, this frame or
|
|
248
|
+
# last, down to the bottom of the reflection.
|
|
249
|
+
top = base
|
|
250
|
+
for p in self.peak:
|
|
251
|
+
y = base - int(p * ph) - cap_h - 1
|
|
252
|
+
if y < top:
|
|
253
|
+
top = y
|
|
254
|
+
top = max(self.top, top)
|
|
255
|
+
y0 = min(top, self._last_top)
|
|
256
|
+
self._last_top = top
|
|
257
|
+
y1 = self.refl_y + self.refl_h
|
|
258
|
+
w2 = self.width * 2
|
|
259
|
+
self._mv[y0 * w2 : y1 * w2] = self._bgmv[y0 * w2 : y1 * w2]
|
|
260
|
+
|
|
261
|
+
col, rcol, rh = self._col, self._rcol, self.refl_h
|
|
262
|
+
hilite, capcol = self._hilite, self._capcol
|
|
263
|
+
x = self.x0
|
|
264
|
+
pitch = self.pitch
|
|
265
|
+
sp = self.seg_pitch
|
|
266
|
+
for i in range(self.bands):
|
|
267
|
+
h = int(self.level[i] * ph)
|
|
268
|
+
if sp:
|
|
269
|
+
h -= h % sp
|
|
270
|
+
if h > 0:
|
|
271
|
+
r = ph - h
|
|
272
|
+
fb.blit((memoryview(col)[r * bw * 2 :], bw, h, RGB565), x, base - h, KEY)
|
|
273
|
+
if not sp:
|
|
274
|
+
fb.fill_rect(x, base - h, bw, min(hi_h, h), hilite[r])
|
|
275
|
+
rr = h if h < rh else rh
|
|
276
|
+
fb.blit((rcol, bw, rr, RGB565), x, self.refl_y, KEY)
|
|
277
|
+
ph_ = int(self.peak[i] * ph)
|
|
278
|
+
if ph_ > 0:
|
|
279
|
+
yc = base - ph_ - cap_h - 1
|
|
280
|
+
if yc >= self.top:
|
|
281
|
+
fb.fill_rect(x, yc, bw, cap_h, capcol[ph - ph_])
|
|
282
|
+
x += pitch
|
|
283
|
+
return y0, y1 - y0
|
|
284
|
+
|
|
285
|
+
# --- per column, for panels where every byte through PSRAM counts -------
|
|
286
|
+
|
|
287
|
+
def _build_bar_columns(self):
|
|
288
|
+
"""Per bar, two packed columns (bar_w wide, top of plot to bottom of
|
|
289
|
+
reflection): the background under it, and the same with the bar fully
|
|
290
|
+
lit. A frame then sends row runs of one or the other straight to the
|
|
291
|
+
panel, plus the highlight and the cap from small prebuilt rows, so no
|
|
292
|
+
pixel is copied twice."""
|
|
293
|
+
bw, top = self.bar_w, self.top
|
|
294
|
+
self._ch = self.refl_y + self.refl_h - top # column height in rows
|
|
295
|
+
ch, bw2 = self._ch, bw * 2
|
|
296
|
+
w2 = self.width * 2
|
|
297
|
+
bgmv = self._bgmv
|
|
298
|
+
col, rcol, ph, rh = self._col, self._rcol, self.plot_h, self.refl_h
|
|
299
|
+
self._cbg, self._clit = [], []
|
|
300
|
+
x = self.x0
|
|
301
|
+
for i in range(self.bands):
|
|
302
|
+
bg = bytearray(ch * bw2)
|
|
303
|
+
for r in range(ch):
|
|
304
|
+
o = (top + r) * w2 + x * 2
|
|
305
|
+
bg[r * bw2 : (r + 1) * bw2] = bgmv[o : o + bw2]
|
|
306
|
+
lit = bytearray(bg)
|
|
307
|
+
lfb = FrameBuffer(lit, bw, ch, RGB565)
|
|
308
|
+
lfb.blit((col, bw, ph, RGB565), 0, 0, KEY)
|
|
309
|
+
lfb.blit((rcol, bw, rh, RGB565), 0, self.refl_y - top, KEY)
|
|
310
|
+
self._cbg.append(memoryview(bg))
|
|
311
|
+
self._clit.append(memoryview(lit))
|
|
312
|
+
x += self.pitch
|
|
313
|
+
# One highlight run and one cap per row they can sit on.
|
|
314
|
+
hi_h, cap_h = min(self.hi_h, ph), self.cap_h
|
|
315
|
+
self._hirow = [c.to_bytes(2, "little") * (bw * hi_h) for c in self._hilite]
|
|
316
|
+
self._caprow = [c.to_bytes(2, "little") * (bw * cap_h) for c in self._capcol]
|
|
317
|
+
n = self.bands
|
|
318
|
+
self._shown_h = [0] * n
|
|
319
|
+
self._shown_cap = [-1] * n
|
|
320
|
+
self._shown_rr = [0] * n
|
|
321
|
+
|
|
322
|
+
def render_columns(self, blit):
|
|
323
|
+
"""Draw this frame bar by bar, calling ``blit(buf, x, y, w, h)`` for
|
|
324
|
+
each run of rows that changed. Only what moved is sent."""
|
|
325
|
+
if not hasattr(self, "_cbg"):
|
|
326
|
+
self._build_bar_columns()
|
|
327
|
+
ph, bw, top = self.plot_h, self.bar_w, self.top
|
|
328
|
+
bw2 = bw * 2
|
|
329
|
+
cap_h, hi_h = self.cap_h, self.hi_h
|
|
330
|
+
sp = self.seg_pitch
|
|
331
|
+
roff = self.refl_y - top
|
|
332
|
+
rh = self.refl_h
|
|
333
|
+
hirow, caprow = self._hirow, self._caprow
|
|
334
|
+
shown_h, shown_cap, shown_rr = self._shown_h, self._shown_cap, self._shown_rr
|
|
335
|
+
level, peak = self.level, self.peak
|
|
336
|
+
x = self.x0
|
|
337
|
+
pitch = self.pitch
|
|
338
|
+
for i in range(self.bands):
|
|
339
|
+
h = int(level[i] * ph)
|
|
340
|
+
if sp:
|
|
341
|
+
h -= h % sp
|
|
342
|
+
p = int(peak[i] * ph)
|
|
343
|
+
cy = ph - p - cap_h - 1 if p > 0 else -1 # cap row in column coords
|
|
344
|
+
oh, ocy = shown_h[i], shown_cap[i]
|
|
345
|
+
if h != oh or cy != ocy:
|
|
346
|
+
bt, obt = ph - h, ph - oh
|
|
347
|
+
r0 = bt if bt < obt else obt
|
|
348
|
+
if 0 <= cy < r0:
|
|
349
|
+
r0 = cy
|
|
350
|
+
if 0 <= ocy < r0:
|
|
351
|
+
r0 = ocy
|
|
352
|
+
r1 = (bt if bt > obt else obt) + (0 if sp else hi_h)
|
|
353
|
+
if ocy >= 0 and ocy + cap_h > r1:
|
|
354
|
+
r1 = ocy + cap_h
|
|
355
|
+
if r1 > ph:
|
|
356
|
+
r1 = ph
|
|
357
|
+
if r0 < bt:
|
|
358
|
+
blit(self._cbg[i][r0 * bw2 : bt * bw2], x, top + r0, bw, bt - r0)
|
|
359
|
+
if bt < r1:
|
|
360
|
+
blit(self._clit[i][bt * bw2 : r1 * bw2], x, top + bt, bw, r1 - bt)
|
|
361
|
+
if h > 0 and not sp:
|
|
362
|
+
hh = hi_h if hi_h < h else h
|
|
363
|
+
blit(hirow[bt] if hh == hi_h else hirow[bt][: bw2 * hh], x, top + bt, bw, hh)
|
|
364
|
+
if cy >= 0:
|
|
365
|
+
blit(caprow[ph - p], x, top + cy, bw, cap_h)
|
|
366
|
+
shown_h[i] = h
|
|
367
|
+
shown_cap[i] = cy
|
|
368
|
+
rr = h if h < rh else rh
|
|
369
|
+
orr = shown_rr[i]
|
|
370
|
+
if rr != orr:
|
|
371
|
+
if rr > orr:
|
|
372
|
+
blit(self._clit[i][(roff + orr) * bw2 : (roff + rr) * bw2], x, top + roff + orr, bw, rr - orr)
|
|
373
|
+
else:
|
|
374
|
+
blit(self._cbg[i][(roff + rr) * bw2 : (roff + orr) * bw2], x, top + roff + rr, bw, orr - rr)
|
|
375
|
+
shown_rr[i] = rr
|
|
376
|
+
x += pitch
|
|
377
|
+
|
|
378
|
+
def compose(self):
|
|
379
|
+
"""The whole frame as the panel shows it, rebuilt into ``self.fb``
|
|
380
|
+
from the background and the shown state (for a screenshot)."""
|
|
381
|
+
self.fb.blit(self._bg, 0, 0)
|
|
382
|
+
if hasattr(self, "_cbg"):
|
|
383
|
+
fb = self.fb
|
|
384
|
+
|
|
385
|
+
def put(buf, x, y, w, h):
|
|
386
|
+
fb.blit_rect(buf, x, y, w, h)
|
|
387
|
+
|
|
388
|
+
n = self.bands
|
|
389
|
+
h, c, r = self._shown_h, self._shown_cap, self._shown_rr
|
|
390
|
+
self._shown_h, self._shown_cap, self._shown_rr = [0] * n, [-1] * n, [0] * n
|
|
391
|
+
lv, pk = self.level, self.peak
|
|
392
|
+
ph = self.plot_h
|
|
393
|
+
self.level = [(x + 0.5) / ph for x in h]
|
|
394
|
+
self.peak = [(ph - y - self.cap_h - 1 + 0.5) / ph if y >= 0 else 0 for y in c]
|
|
395
|
+
self.render_columns(put)
|
|
396
|
+
self.level, self.peak = lv, pk
|
|
397
|
+
self._shown_h, self._shown_cap, self._shown_rr = h, c, r
|
|
398
|
+
return self._buf
|
|
399
|
+
|
|
400
|
+
def strip(self, y, h):
|
|
401
|
+
"""The bytes of rows ``y..y+h`` of the frame, for ``blit_rect``."""
|
|
402
|
+
w2 = self.width * 2
|
|
403
|
+
return self._mv[y * w2 : (y + h) * w2]
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""A log-frequency spectrum analyzer drawn with pygraphics."""
|
|
5
|
+
|
|
6
|
+
from typing import Any, Callable, Literal
|
|
7
|
+
|
|
8
|
+
_Color = tuple[int, int, int]
|
|
9
|
+
|
|
10
|
+
PALETTE: tuple[tuple[float, _Color], ...]
|
|
11
|
+
BG_TOP: _Color
|
|
12
|
+
BG_BASE: _Color
|
|
13
|
+
BG_FLOOR: _Color
|
|
14
|
+
GRID: _Color
|
|
15
|
+
GRID_MINOR: _Color
|
|
16
|
+
BASELINE: _Color
|
|
17
|
+
LABEL: _Color
|
|
18
|
+
LABEL_DECADE: _Color
|
|
19
|
+
LOW_HZ: float
|
|
20
|
+
HIGH_HZ: float
|
|
21
|
+
LABELS: tuple[int, ...]
|
|
22
|
+
ATTACK_S: float
|
|
23
|
+
FALL_PER_S: float
|
|
24
|
+
PEAK_HOLD_S: float
|
|
25
|
+
PEAK_GRAVITY: float
|
|
26
|
+
KEY: int
|
|
27
|
+
|
|
28
|
+
def rgb565(rgb: tuple[int | float, int | float, int | float]) -> int:
|
|
29
|
+
"""An RGB colour as an RGB565 value that is never the colour key."""
|
|
30
|
+
def mix(a: tuple[float, float, float], b: tuple[float, float, float], t: float) -> tuple[float, float, float]: ...
|
|
31
|
+
def palette_at(frac: float) -> _Color:
|
|
32
|
+
"""The bar colour at `frac` of full height."""
|
|
33
|
+
def band_count_for(plot_w: int) -> int:
|
|
34
|
+
"""About one band per 12-16 px, clamped to 16..48."""
|
|
35
|
+
def label_text(hz: int) -> str: ...
|
|
36
|
+
def band_centres(n: int) -> list[float]:
|
|
37
|
+
"""Centre frequency of each of `n` log-spaced bands between `LOW_HZ` and `HIGH_HZ`."""
|
|
38
|
+
|
|
39
|
+
class SpectrumView:
|
|
40
|
+
"""The analyzer's frame: a painted background and the bars drawn over it."""
|
|
41
|
+
|
|
42
|
+
width: int
|
|
43
|
+
height: int
|
|
44
|
+
style: Literal["smooth", "segmented"]
|
|
45
|
+
fb: Any
|
|
46
|
+
font: Any
|
|
47
|
+
bands: int
|
|
48
|
+
pitch: int
|
|
49
|
+
gap: int
|
|
50
|
+
bar_w: int
|
|
51
|
+
x0: int
|
|
52
|
+
span_x0: int
|
|
53
|
+
span_w: int
|
|
54
|
+
label_y: int
|
|
55
|
+
refl_h: int
|
|
56
|
+
top: int
|
|
57
|
+
baseline: int
|
|
58
|
+
plot_h: int
|
|
59
|
+
refl_y: int
|
|
60
|
+
cap_h: int
|
|
61
|
+
hi_h: int
|
|
62
|
+
seg_pitch: int
|
|
63
|
+
level: list[float]
|
|
64
|
+
peak: list[float]
|
|
65
|
+
hold: list[float]
|
|
66
|
+
pvel: list[float]
|
|
67
|
+
def __init__(
|
|
68
|
+
self, width: int, height: int, bands: int | None = None, style: Literal["smooth", "segmented"] = "smooth"
|
|
69
|
+
) -> None: ...
|
|
70
|
+
def freq_x(self, hz: float) -> int: ...
|
|
71
|
+
def update(self, targets: list[float], dt: float) -> None:
|
|
72
|
+
"""Move the bars toward `targets` (0..1 per band) over `dt` seconds."""
|
|
73
|
+
def render(self) -> tuple[int, int]:
|
|
74
|
+
"""Draw this frame; return `(y, h)`, the row strip that changed."""
|
|
75
|
+
def render_columns(self, blit: Callable[[Any, int, int, int, int], Any]) -> None:
|
|
76
|
+
"""Draw this frame bar by bar, calling `blit(buf, x, y, w, h)` for each run of rows that changed."""
|
|
77
|
+
def compose(self) -> bytearray:
|
|
78
|
+
"""The whole frame as the panel shows it, rebuilt from the background and the shown state."""
|
|
79
|
+
def strip(self, y: int, h: int) -> memoryview:
|
|
80
|
+
"""The bytes of rows `y..y+h` of the frame, for `blit_rect`."""
|
touchkit/__init__.py
ADDED
touchkit/__init__.pyi
ADDED
touchkit/cam.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""touchkit.cam -- what the camera apps share: opening the CSI camera, its
|
|
5
|
+
settings, and where photos go.
|
|
6
|
+
|
|
7
|
+
Flip, mirror, light and brightness are the Camera app's, kept in its
|
|
8
|
+
``scope.store()`` while the board stays up, so Security, Detect and Cast
|
|
9
|
+
see the camera as Camera left it. Photos are in a ``photos`` folder
|
|
10
|
+
(`appkit.places`): ``/sd/photos`` on the card, ``/photos`` on flash and
|
|
11
|
+
``/data/photos``. They're read from all three and saved to the first that
|
|
12
|
+
takes a file.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
|
|
17
|
+
from appkit import data, places
|
|
18
|
+
|
|
19
|
+
# The whole sensor, 2x2 binned: Flip and Mirror show the same picture turned
|
|
20
|
+
# over (the 800x800 default is a window, and reads a different patch when
|
|
21
|
+
# turned), and binning halves the grain in a dim room. The default if not.
|
|
22
|
+
FORMAT = "MIPI_2lane_24Minput_RAW10_1280x960_binning_45fps"
|
|
23
|
+
# Brightness: the driver's "exposure" is the auto exposure's target, not a
|
|
24
|
+
# shutter time; cameraif takes it as a percent of the driver's 2-235. The
|
|
25
|
+
# camera starts at a target of 140, which is 59%.
|
|
26
|
+
BRIGHT = 59
|
|
27
|
+
# Light: the auto exposure's gain ceiling (OV5647 0x3A18/0x3A19). In a dim
|
|
28
|
+
# room the gain sits at the ceiling, so raising it is what brightens the
|
|
29
|
+
# picture, with more grain. A middle step (0x1F8) made the auto exposure
|
|
30
|
+
# hunt: the picture flickered.
|
|
31
|
+
LIGHT = (("Normal", 0x0F8), ("Night", 0x3F8))
|
|
32
|
+
PHOTOS = "photos" # the folder name, on the card, flash and the data root
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class NoCameraPort(OSError):
|
|
36
|
+
"""The board has no CSI camera port at all (a desktop, say), as opposed
|
|
37
|
+
to a port with no camera on it."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def no_camera_text(err, app):
|
|
41
|
+
"""What *app* shows when `open_camera` raised *err*."""
|
|
42
|
+
if isinstance(err, NoCameraPort):
|
|
43
|
+
here = "computer" if data.desktop() else "board"
|
|
44
|
+
return "No camera: %s\n\n%s needs the board's CSI camera, which this %s doesn't have." % (err, app, here)
|
|
45
|
+
return "No camera: %s\n\nConnect one to the CSI port, then open %s again." % (err, app)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def options(scope):
|
|
49
|
+
"""The camera settings the camera apps share: the Camera app's store."""
|
|
50
|
+
opts = scope.store("camera")
|
|
51
|
+
for k, v in (("light", 0), ("flip", False), ("mirror", False), ("bright", BRIGHT)):
|
|
52
|
+
opts.setdefault(k, v)
|
|
53
|
+
return opts
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def open_camera(opts):
|
|
57
|
+
"""The CSI camera, turned as *opts* say, with its colour order right.
|
|
58
|
+
|
|
59
|
+
Raises when the board has no camera, or it doesn't answer: an app shows
|
|
60
|
+
that and carries on without one.
|
|
61
|
+
"""
|
|
62
|
+
import board_peripherals as bp
|
|
63
|
+
|
|
64
|
+
if not hasattr(bp, "camera"):
|
|
65
|
+
raise NoCameraPort("board_peripherals has no camera role")
|
|
66
|
+
# cameraif writes both at open and picks the ISP's colour order from them
|
|
67
|
+
# (every OV5647 mode opens mirrored unless told otherwise).
|
|
68
|
+
kw = {"flip": opts["flip"], "mirror": opts["mirror"]}
|
|
69
|
+
try:
|
|
70
|
+
return bp.camera(format=FORMAT, **kw)
|
|
71
|
+
except Exception as e:
|
|
72
|
+
# A sensor without the binned mode takes the default one; with no
|
|
73
|
+
# sensor at all this raises again, below.
|
|
74
|
+
print("camera: %s: %s; the default format instead" % (FORMAT, e))
|
|
75
|
+
return bp.camera(**kw)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def apply_controls(c, opts):
|
|
79
|
+
"""Brightness and the light level (the AE target, the gain ceiling)."""
|
|
80
|
+
ceiling = LIGHT[opts["light"] % len(LIGHT)][1]
|
|
81
|
+
try:
|
|
82
|
+
c.exposure(opts["bright"])
|
|
83
|
+
c.reg(0x3A18, ceiling >> 8)
|
|
84
|
+
c.reg(0x3A19, ceiling & 0xFF)
|
|
85
|
+
except Exception as e:
|
|
86
|
+
print("camera: controls:", e)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def preview_size(cam, longest):
|
|
90
|
+
"""The preview's ``(width, height)``, its longer side *longest*."""
|
|
91
|
+
cw, ch = cam.size()[:2]
|
|
92
|
+
return (longest, longest * ch // cw) if cw >= ch else (longest * cw // ch, longest)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def mount_sd(readonly=True):
|
|
96
|
+
"""Mount the SD card at ``/sd``: `appkit.places.mount_card`, for the camera apps."""
|
|
97
|
+
return places.mount_card(write=not readonly)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def unmount_sd(card):
|
|
101
|
+
places.unmount_card(card)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
#: Where a photo is, in its key: the card, flash or the data root.
|
|
105
|
+
TAGS = ("card", "flash", "data")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _tagged():
|
|
109
|
+
folders = places.folders(PHOTOS)
|
|
110
|
+
return list(zip(TAGS[-len(folders):], folders)) # a desktop has the data root alone
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def photos(scope=None):
|
|
114
|
+
"""``[(key, path)]`` of every photo, newest name first. The key says
|
|
115
|
+
where it lives (``card/IMG_0001.jpg``, ``flash/...``, ``data/...``), so
|
|
116
|
+
the same name in two places is two photos."""
|
|
117
|
+
out = []
|
|
118
|
+
for tag, d in _tagged():
|
|
119
|
+
try:
|
|
120
|
+
for f in os.listdir(d):
|
|
121
|
+
if f.lower().endswith(".jpg"):
|
|
122
|
+
out.append((tag + "/" + f, d + "/" + f))
|
|
123
|
+
except OSError:
|
|
124
|
+
pass
|
|
125
|
+
out.sort(key=lambda kp: (kp[0].rsplit("/", 1)[1], kp[0]), reverse=True)
|
|
126
|
+
return out
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def next_name(scope=None):
|
|
130
|
+
"""A new photo's path, in `places.write_folder` (the card when it's
|
|
131
|
+
mounted for writing): numbered after the highest ``IMG_`` anywhere, so a
|
|
132
|
+
name means one photo. OSError when there's nowhere to write."""
|
|
133
|
+
folder = places.write_folder(PHOTOS)
|
|
134
|
+
n = 0
|
|
135
|
+
for _, d in _tagged():
|
|
136
|
+
try:
|
|
137
|
+
names = os.listdir(d)
|
|
138
|
+
except OSError:
|
|
139
|
+
continue
|
|
140
|
+
for f in names:
|
|
141
|
+
if f.startswith("IMG_") and f.endswith(".jpg"):
|
|
142
|
+
try:
|
|
143
|
+
n = max(n, int(f[4:8]))
|
|
144
|
+
except ValueError:
|
|
145
|
+
pass
|
|
146
|
+
return "%s/IMG_%04d.jpg" % (folder, n + 1)
|
touchkit/cam.pyi
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""What the camera apps share: opening the CSI camera, its settings, and where photos go."""
|
|
5
|
+
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from appkit.scope import AppScope
|
|
9
|
+
|
|
10
|
+
FORMAT: str
|
|
11
|
+
BRIGHT: int
|
|
12
|
+
LIGHT: tuple[tuple[str, int], ...]
|
|
13
|
+
PHOTOS: str
|
|
14
|
+
TAGS: tuple[str, ...]
|
|
15
|
+
|
|
16
|
+
class NoCameraPort(OSError):
|
|
17
|
+
"""The board has no CSI camera port at all, as opposed to a port with no camera on it."""
|
|
18
|
+
|
|
19
|
+
def no_camera_text(err: BaseException, app: str) -> str:
|
|
20
|
+
"""What `app` shows when `open_camera` raised `err`."""
|
|
21
|
+
def options(scope: AppScope) -> dict[str, Any]:
|
|
22
|
+
"""The camera settings the camera apps share: the Camera app's store."""
|
|
23
|
+
def open_camera(opts: dict[str, Any]) -> Any:
|
|
24
|
+
"""The CSI camera, turned as `opts` say, with its colour order right."""
|
|
25
|
+
def apply_controls(c: Any, opts: dict[str, Any]) -> None:
|
|
26
|
+
"""Brightness and the light level (the AE target, the gain ceiling)."""
|
|
27
|
+
def preview_size(cam: Any, longest: int) -> tuple[int, int]:
|
|
28
|
+
"""The preview's `(width, height)`, its longer side `longest`."""
|
|
29
|
+
def mount_sd(readonly: bool = True) -> Any:
|
|
30
|
+
"""Mount the SD card at `/sd`."""
|
|
31
|
+
def unmount_sd(card: Any) -> None: ...
|
|
32
|
+
def photos(scope: AppScope | None = None) -> list[tuple[str, str]]:
|
|
33
|
+
"""`[(key, path)]` of every photo, newest name first."""
|
|
34
|
+
def next_name(scope: AppScope | None = None) -> str:
|
|
35
|
+
"""A new photo's path, numbered after the highest `IMG_` anywhere."""
|