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,11 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """System is Info now: this stands in for it, off Home's pages.
5
+
6
+ A board that had System installed keeps its folder when the apps are installed
7
+ again; this one replaces it, so the old app's tile goes and ``apps.system``
8
+ still answers. Use ``apps.info`` in new code.
9
+ """
10
+
11
+ from ..info import main, stats # noqa: F401
apps/system/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "system", "title": "Info", "tile": false}
@@ -0,0 +1,324 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Touch: tap five targets, and see what the touch mapping gets wrong.
5
+
6
+ The targets come one at a time, large enough to read at arm's length: the
7
+ four corners a tenth of the way in, then the middle. Each tap keeps the point
8
+ the board's ``touch_read`` gave and where the touch device's rotation table
9
+ put it. After the fifth the app says in plain words what's wrong, if anything,
10
+ and which rotation table entry and calibration would be right, and saves every
11
+ number to ``/data/touchcal/last.json``.
12
+
13
+ A tap anywhere counts, so a mapping that sends taps far from the target still
14
+ gets through all five. Each one flashes "Got it" before the next target
15
+ shows; a second tap straight after, or one on the same spot soon after, is the
16
+ same finger again and doesn't count. Hold a finger down for two seconds to
17
+ start over at target 1. The targets cover the whole screen, Home button
18
+ included, until the fifth.
19
+ """
20
+
21
+ import json
22
+ import sys
23
+
24
+ import lvgl as lv
25
+ from touchkit import ui
26
+
27
+ from .fit import analyse, tolerance
28
+
29
+ #: Where the last run's numbers go, in the app's data folder.
30
+ FILE = "last.json"
31
+ COUNT = 5
32
+ _POLL_MS = 30
33
+ #: A press held this long starts over.
34
+ HOLD_MS = 2000
35
+ #: How long "Got it" shows before the next target.
36
+ GOT_MS = 500
37
+ #: A tap this soon after the last one is the same finger again.
38
+ AGAIN_MS = 600
39
+ #: And one this close to the last tap, this soon, is too.
40
+ NEAR_MS = 1500
41
+ _GOT = lv.color_hex(0x30D158)
42
+ _INK = lv.color_hex(0xFFFFFF)
43
+ _MARK = lv.color_hex(0xFFD400)
44
+ _BLACK = lv.color_hex(0x000000)
45
+
46
+
47
+ def target_places(width, height):
48
+ """The five targets on a ``width`` x ``height`` screen: the corners a tenth in, clockwise from the top left, then the middle."""
49
+ mx, my = width // 10, height // 10
50
+ return [(mx, my), (width - 1 - mx, my), (width - 1 - mx, height - 1 - my), (mx, height - 1 - my), (width // 2, height // 2)]
51
+
52
+
53
+ def _device():
54
+ """The board's touch device, or None (a computer's mouse, or no touch)."""
55
+ drv = getattr(sys.modules.get("board_config"), "display_drv", None)
56
+ dev = getattr(drv, "touch_device", None)
57
+ return dev if dev is not None and hasattr(dev, "_read") else None
58
+
59
+
60
+ def _table(dev):
61
+ for name in ("_rotation_table", "_data2"):
62
+ table = getattr(dev, name, None)
63
+ if table is not None:
64
+ return tuple(table)
65
+ return None
66
+
67
+
68
+ def _median(values):
69
+ values = sorted(values)
70
+ return values[len(values) // 2]
71
+
72
+
73
+ class Taps:
74
+ """Every press the touch device reads, from first contact to lift, as one
75
+ tap: ``((x, y), held_ms, (x, y) of its first read)`` in `done`.
76
+
77
+ It stands in for the device's ``_read`` until `close`, so it sees the
78
+ points before the rotation table does, whether or not LVGL finds anything
79
+ under them.
80
+
81
+ Args:
82
+ dev: The touch device (``display_drv.touch_device``).
83
+ """
84
+
85
+ def __init__(self, dev):
86
+ self.dev = dev
87
+ self._orig = dev._read
88
+ self._xs = []
89
+ self._ys = []
90
+ self._t0 = 0
91
+ self.done = []
92
+ dev._read = self._read
93
+
94
+ def _read(self):
95
+ points = self._orig()
96
+ if points:
97
+ p = points if isinstance(points[0], int) else points[0]
98
+ if not self._xs:
99
+ self._t0 = lv.tick_get()
100
+ self._first = (int(p[0]), int(p[1]))
101
+ self._xs.append(int(p[0]))
102
+ self._ys.append(int(p[1]))
103
+ elif self._xs:
104
+ # A resistive panel's first and last reads wander: the median is the press.
105
+ self.done.append(((_median(self._xs), _median(self._ys)), lv.tick_elaps(self._t0), self._first))
106
+ self._xs = []
107
+ self._ys = []
108
+ return points
109
+
110
+ def mapped(self, raw):
111
+ """``raw`` through the device's rotation table, as LVGL gets it."""
112
+ mapper = getattr(self.dev, "_map_point", None)
113
+ return tuple(mapper(raw)[:2]) if mapper is not None else raw
114
+
115
+ def close(self):
116
+ self.dev._read = self._orig
117
+
118
+
119
+ def _cross(parent, n, x, y, width, height):
120
+ """Target ``n`` at ``(x, y)``: a crosshair in a ring, and its number beside it, towards the middle."""
121
+ arm = max(16, min(width, height) // 10)
122
+ for w, h in ((2 * arm + 1, 3), (3, 2 * arm + 1)):
123
+ bar = ui.plain(lv.obj(parent))
124
+ bar.set_size(w, h)
125
+ bar.set_style_bg_color(_INK, 0)
126
+ bar.set_style_bg_opa(lv.OPA.COVER, 0)
127
+ bar.set_pos(x - w // 2, y - h // 2)
128
+ d = arm + arm // 2
129
+ ring = ui.plain(lv.obj(parent))
130
+ ring.set_size(d, d)
131
+ ring.set_style_radius(d, 0)
132
+ ring.set_style_border_width(3, 0)
133
+ ring.set_style_border_color(_MARK, 0)
134
+ ring.set_pos(x - d // 2, y - d // 2)
135
+ num = ui.label(parent, str(n), ui.huge, _MARK)
136
+ if n == COUNT:
137
+ where = lv.ALIGN.OUT_TOP_MID
138
+ elif x < width // 2:
139
+ where = lv.ALIGN.OUT_BOTTOM_RIGHT if y < height // 2 else lv.ALIGN.OUT_TOP_RIGHT
140
+ else:
141
+ where = lv.ALIGN.OUT_BOTTOM_LEFT if y < height // 2 else lv.ALIGN.OUT_TOP_LEFT
142
+ num.align_to(ring, where, 0, 0)
143
+ say = ui.label(parent, "Tap the middle of the cross\n%d of %d\nHold 2 s to start over" % (n, COUNT), ui.font, _INK)
144
+ say.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
145
+ if n == COUNT:
146
+ say.align(lv.ALIGN.BOTTOM_MID, 0, -height // 8)
147
+ else:
148
+ say.align(lv.ALIGN.CENTER, 0, 0)
149
+ return ring
150
+
151
+
152
+ def _got(parent, ring):
153
+ """Fill target ``ring`` and say "Got it" beside it."""
154
+ ring.set_style_bg_color(_GOT, 0)
155
+ ring.set_style_bg_opa(lv.OPA.COVER, 0)
156
+ ring.set_style_border_color(_GOT, 0)
157
+ lbl = ui.label(parent, "Got it", ui.big, _GOT)
158
+ lbl.align(lv.ALIGN.CENTER, 0, -ui.big.line_height * 2)
159
+
160
+
161
+ def main(scope):
162
+ disp = lv.display_get_default()
163
+ width, height = disp.get_horizontal_resolution(), disp.get_vertical_resolution()
164
+ targets = target_places(width, height)
165
+ dev = _device()
166
+ taps = []
167
+ # ring: the target showing; busy: "Got it" is up; last: the last tap's
168
+ # (tick, point).
169
+ state = {"result": None, "ring": None, "busy": False, "last": None}
170
+
171
+ # The targets go over everything, bar and Home button included: a tap
172
+ # that the mapping sends somewhere else mustn't press anything there.
173
+ sheet = scope.adopt(ui.plain(lv.obj(lv.layer_sys())))
174
+ sheet.set_size(width, height)
175
+ sheet.set_pos(0, 0)
176
+ sheet.set_style_bg_color(_BLACK, 0)
177
+ sheet.set_style_bg_opa(lv.OPA.COVER, 0)
178
+ sheet.add_flag(lv.obj.FLAG.CLICKABLE)
179
+
180
+ root = scope.root
181
+ root.remove_flag(lv.obj.FLAG.SCROLLABLE)
182
+ root.set_style_bg_color(ui.BG, 0)
183
+ watch = scope.adopt(Taps(dev), lambda w: w.close()) if dev is not None else None
184
+
185
+ def show():
186
+ sheet.clean()
187
+ x, y = targets[len(taps)]
188
+ state["ring"] = _cross(sheet, len(taps) + 1, x, y, width, height)
189
+ state["busy"] = False
190
+
191
+ def again_soon(point):
192
+ last = state["last"]
193
+ if last is None:
194
+ return False
195
+ since = lv.tick_elaps(last[0])
196
+ near = abs(point[0] - last[1][0]) + abs(point[1] - last[1][1]) < 2 * tolerance(width, height)
197
+ return since < AGAIN_MS or (near and since < NEAR_MS)
198
+
199
+ def tapped(raw, mapped, held=0, first=None):
200
+ if state["result"] is not None:
201
+ return
202
+ if held >= HOLD_MS:
203
+ start_over()
204
+ return
205
+ point = raw if raw is not None else mapped
206
+ if state["busy"] or again_soon(point):
207
+ return
208
+ state["last"] = (lv.tick_get(), point)
209
+ taps.append({"n": len(taps) + 1, "target": targets[len(taps)], "raw": raw, "mapped": mapped, "first": first})
210
+ state["busy"] = True
211
+ _got(sheet, state["ring"])
212
+ scope.after(GOT_MS, next_target)
213
+
214
+ def next_target(_timer=None):
215
+ if state["result"] is not None or not state["busy"]:
216
+ return # started over while "Got it" showed
217
+ if len(taps) < COUNT:
218
+ show()
219
+ else:
220
+ finish()
221
+
222
+ def start_over():
223
+ del taps[:]
224
+ state["last"] = None
225
+ show()
226
+
227
+ def poll(_timer=None):
228
+ while watch.done:
229
+ raw, held, first = watch.done.pop(0)
230
+ tapped(raw, watch.mapped(raw), held, first)
231
+
232
+ def clicked(_event):
233
+ # No touch device to watch (a computer's mouse): LVGL's point is all there is.
234
+ p = lv.point_t()
235
+ lv.indev_active().get_point(p)
236
+ tapped(None, (p.x, p.y))
237
+
238
+ if watch is not None:
239
+ scope.every(_POLL_MS, poll)
240
+ else:
241
+ sheet.add_event_cb(clicked, lv.EVENT.SHORT_CLICKED, None)
242
+ sheet.add_event_cb(lambda _e: tapped(None, (0, 0), HOLD_MS), lv.EVENT.LONG_PRESSED, None)
243
+
244
+ def finish():
245
+ rotation = getattr(dev, "rotation", 0) if dev is not None else 0
246
+ mask = getattr(dev, "_mask", None) if dev is not None else None
247
+ table = _table(dev) if dev is not None else None
248
+ result = analyse(taps, width, height, mask, rotation, table)
249
+ state["result"] = result
250
+ record = {
251
+ "display": [width, height],
252
+ "touch_device": type(dev).__name__ if dev is not None else None,
253
+ "rotation": rotation,
254
+ "mask": mask,
255
+ "table": table,
256
+ "taps": taps,
257
+ "analysis": result,
258
+ }
259
+ path = None
260
+ try:
261
+ path = scope.data_dir + "/" + FILE
262
+ with open(path, "w") as f:
263
+ json.dump(record, f)
264
+ except (OSError, ValueError) as e:
265
+ path = "not saved: %s" % (e,)
266
+ sheet.add_flag(lv.obj.FLAG.HIDDEN)
267
+ _results(root, taps, result, path, width, height, again)
268
+
269
+ def again():
270
+ state["result"] = None
271
+ if watch is not None:
272
+ del watch.done[:]
273
+ root.clean()
274
+ sheet.remove_flag(lv.obj.FLAG.HIDDEN)
275
+ start_over()
276
+
277
+ show()
278
+
279
+
280
+ def _results(root, taps, result, path, width, height, again):
281
+ """Where each tap landed against its target, and what's wrong in words."""
282
+ root.clean()
283
+ for tap in taps:
284
+ tx, ty = tap["target"]
285
+ ring = ui.plain(lv.obj(root))
286
+ ring.set_size(16, 16)
287
+ ring.set_style_radius(8, 0)
288
+ ring.set_style_border_width(2, 0)
289
+ ring.set_style_border_color(ui.MUTED, 0)
290
+ ring.set_pos(tx - 8, ty - 8)
291
+ mx, my = tap["mapped"]
292
+ dot = ui.plain(lv.obj(root))
293
+ dot.set_size(12, 12)
294
+ dot.set_style_radius(6, 0)
295
+ hit = abs(mx - tx) <= result["tolerance"] and abs(my - ty) <= result["tolerance"]
296
+ dot.set_style_bg_color(ui.GOOD if hit else ui.BAD, 0)
297
+ dot.set_style_bg_opa(lv.OPA.COVER, 0)
298
+ dot.set_pos(min(max(mx, 0), width - 1) - 6, min(max(my, 0), height - 1) - 6)
299
+
300
+ panel = lv.obj(root)
301
+ across = min(width - 24, 440)
302
+ panel.set_width(across)
303
+ panel.set_height(lv.SIZE_CONTENT)
304
+ panel.set_style_max_height(height - 24, 0)
305
+ panel.set_style_bg_color(ui.PANEL, 0)
306
+ panel.set_style_bg_opa(230, 0)
307
+ panel.set_style_border_width(0, 0)
308
+ panel.set_style_radius(12, 0)
309
+ panel.set_style_pad_all(12, 0)
310
+ panel.set_style_pad_row(8, 0)
311
+ panel.set_flex_flow(lv.FLEX_FLOW.COLUMN)
312
+ panel.center()
313
+ inner = across - 24
314
+ ok = result["ok"]
315
+ verdict = ui.label(panel, "Touch is right" if ok else "Touch is off", ui.big, ui.GOOD if ok else ui.BAD)
316
+ verdict.set_width(inner)
317
+ for line in result["words"]:
318
+ lbl = ui.label(panel, line, ui.small, ui.WHITE)
319
+ lbl.set_width(inner)
320
+ lbl.set_long_mode(lv.label.LONG_MODE.WRAP)
321
+ note = ui.label(panel, path if path.startswith("not saved") else "Saved to " + path, ui.tiny, ui.MUTED)
322
+ note.set_width(inner)
323
+ note.set_long_mode(lv.label.LONG_MODE.WRAP)
324
+ ui.button(panel, "Again", again, color=ui.KEY)
apps/touchcal/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "touchcal", "title": "Touch", "category": "System", "icon": "touch_app", "color": "#2F5D8A", "bar": "none"}
apps/touchcal/fit.py ADDED
@@ -0,0 +1,262 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Work out what a touch mapping gets wrong, from taps on targets at known places.
5
+
6
+ Each tap has the target's place on the screen, the point the board's
7
+ ``touch_read`` gave (``raw``) and that point after the touch device's rotation
8
+ table (``mapped``), which is where LVGL saw the tap. `analyse` fits a straight
9
+ line per axis from each to the targets, so it can tell a mirrored axis,
10
+ swapped axes, a turn and a scale or offset error apart, and says which
11
+ ``touch_rotation_table`` entry and which calibration would put every tap on
12
+ its target.
13
+
14
+ The mask bits are the touch devices' own: `SWAP_XY` first, then `REVERSE_X`
15
+ and `REVERSE_Y` across the display's width and height.
16
+ """
17
+
18
+ SWAP_XY = 0b001
19
+ REVERSE_X = 0b010
20
+ REVERSE_Y = 0b100
21
+
22
+ #: A fitted scale this close to 1 is right.
23
+ SCALE_OK = 0.08
24
+
25
+ _WORDS = {
26
+ 0: "",
27
+ REVERSE_X: "Left and right are swapped: a tap on one side lands on the other.",
28
+ REVERSE_Y: "Top and bottom are swapped: a tap near the top lands near the bottom.",
29
+ REVERSE_X | REVERSE_Y: "Touch is turned 180 degrees from the screen.",
30
+ SWAP_XY: "Touch runs across the screen's diagonal: x and y are swapped.",
31
+ SWAP_XY | REVERSE_X: "Touch is turned 90 degrees from the screen: x and y are swapped, and x is mirrored.",
32
+ SWAP_XY | REVERSE_Y: "Touch is turned 90 degrees from the screen: x and y are swapped, and y is mirrored.",
33
+ SWAP_XY | REVERSE_X | REVERSE_Y: "Touch runs across the screen's other diagonal: x and y are swapped, and both are mirrored.",
34
+ }
35
+
36
+
37
+ def mask_name(mask):
38
+ """``mask`` as the touch devices' constant names, such as ``"SWAP_XY | REVERSE_X"``."""
39
+ names = [n for bit, n in ((SWAP_XY, "SWAP_XY"), (REVERSE_X, "REVERSE_X"), (REVERSE_Y, "REVERSE_Y")) if mask & bit]
40
+ return " | ".join(names) if names else "0"
41
+
42
+
43
+ def apply_mask(point, mask, width, height):
44
+ """``point`` through ``mask`` as a touch device maps it, on a ``width`` x ``height`` display."""
45
+ x, y = point[0], point[1]
46
+ if mask & SWAP_XY:
47
+ x, y = y, x
48
+ if mask & REVERSE_X:
49
+ x = width - x - 1
50
+ if mask & REVERSE_Y:
51
+ y = height - y - 1
52
+ return (x, y)
53
+
54
+
55
+ def tolerance(width, height):
56
+ """A fingertip's width on this screen, in pixels: 8% of the short side, at least 20.
57
+
58
+ A tap this far from its target is on it, and a pattern (an offset, say)
59
+ smaller than this is a finger's aim, not a mapping fault.
60
+ """
61
+ return max(20, min(width, height) * 8 // 100)
62
+
63
+
64
+ def _pattern_right(seen, width, height, tol):
65
+ """Whether fit `seen` is the identity: no swap or mirror, the right scale, and no offset past ``tol``."""
66
+ if seen is None or seen["mask"]:
67
+ return False
68
+ for axis, size in (("x", width), ("y", height)):
69
+ a, b = seen[axis]
70
+ mid = (size - 1) / 2
71
+ if abs(a - 1) > SCALE_OK or abs((mid - b) / a - mid) > tol:
72
+ return False
73
+ return True
74
+
75
+
76
+ def _mean(values):
77
+ return sum(values) / len(values)
78
+
79
+
80
+ def _line(src, dst):
81
+ """``(a, b)`` for the least-squares line ``dst = a * src + b``, or None when ``src`` doesn't vary."""
82
+ ms, md = _mean(src), _mean(dst)
83
+ var = sum((s - ms) * (s - ms) for s in src)
84
+ if var < 1e-9:
85
+ return None
86
+ a = sum((s - ms) * (d - md) for s, d in zip(src, dst)) / var
87
+ return (a, md - a * ms)
88
+
89
+
90
+ def _corr(src, dst):
91
+ ms, md = _mean(src), _mean(dst)
92
+ vs = sum((s - ms) * (s - ms) for s in src)
93
+ vd = sum((d - md) * (d - md) for d in dst)
94
+ if vs < 1e-9 or vd < 1e-9:
95
+ return 0.0
96
+ return sum((s - ms) * (d - md) for s, d in zip(src, dst)) / (vs * vd) ** 0.5
97
+
98
+
99
+ def fit(points, targets):
100
+ """Fit ``targets`` from ``points``, one axis at a time.
101
+
102
+ Args:
103
+ points (list): ``(x, y)`` as read.
104
+ targets (list): ``(x, y)`` where each one should have been.
105
+
106
+ Returns:
107
+ dict: ``swap`` (target x follows the points' y), ``x`` and ``y``, each
108
+ the ``(a, b)`` of ``target = a * point + b`` for that target axis, and
109
+ ``mask``, the transform the fit amounts to. None when the points don't
110
+ spread out on either axis.
111
+ """
112
+ px = [p[0] for p in points]
113
+ py = [p[1] for p in points]
114
+ tx = [t[0] for t in targets]
115
+ ty = [t[1] for t in targets]
116
+ straight = abs(_corr(px, tx)) + abs(_corr(py, ty))
117
+ crossed = abs(_corr(py, tx)) + abs(_corr(px, ty))
118
+ swap = crossed > straight
119
+ lx = _line(py if swap else px, tx)
120
+ ly = _line(px if swap else py, ty)
121
+ if lx is None or ly is None:
122
+ return None
123
+ mask = (SWAP_XY if swap else 0) | (REVERSE_X if lx[0] < 0 else 0) | (REVERSE_Y if ly[0] < 0 else 0)
124
+ return {"swap": swap, "x": lx, "y": ly, "mask": mask}
125
+
126
+
127
+ def _edges(line, size):
128
+ """Where a fitted axis reads at the screen's two edges: ``(at 0, at size - 1)``."""
129
+ a, b = line
130
+ return (round((0 - b) / a), round((size - 1 - b) / a))
131
+
132
+
133
+ def analyse(taps, width, height, mask=None, rotation=0, table=None):
134
+ """What's wrong with a touch mapping, from taps on targets.
135
+
136
+ Args:
137
+ taps (list): dicts with ``target`` and ``mapped``, each ``(x, y)``,
138
+ and ``raw``, the ``touch_read`` point, or None where the board's
139
+ touch device couldn't be reached.
140
+ width (int): The display's width, as the touch device maps it.
141
+ height (int): Its height.
142
+ mask (int): The rotation table entry in use, or None if unknown.
143
+ rotation (int): The touch device's rotation, in degrees.
144
+ table (tuple): The board's ``touch_rotation_table``, or None.
145
+
146
+ Returns:
147
+ dict: ``ok`` (the taps follow the targets, each within twice
148
+ `tolerance`, and no press started on the last one), ``off`` (each tap's distance
149
+ from its target, in pixels), ``worst``, ``tolerance``, ``seen`` (the
150
+ `fit` of the mapped points), ``words`` (what's wrong, in plain
151
+ sentences), ``stale`` (how many presses began on the tap before's
152
+ point, when any did) and ``fix`` (the table entry and calibration that would be
153
+ right, or None without raw points).
154
+ """
155
+ tol = tolerance(width, height)
156
+ targets = [tuple(t["target"]) for t in taps]
157
+ mapped = [tuple(t["mapped"]) for t in taps]
158
+ off = [round(((m[0] - t[0]) ** 2 + (m[1] - t[1]) ** 2) ** 0.5) for m, t in zip(mapped, targets)]
159
+ worst = max(off) if off else 0
160
+ seen = fit(mapped, targets)
161
+ # Right when the taps follow the targets with no pattern to their misses
162
+ # and none is far out: a fingertip's aim, not the mapping.
163
+ ok = _pattern_right(seen, width, height, tol) and worst <= 2 * tol
164
+ out = {"ok": ok, "off": off, "worst": worst, "tolerance": tol, "seen": seen, "words": [], "fix": None}
165
+ words = out["words"]
166
+ stale = _stale(taps, tol)
167
+ if stale:
168
+ # A press that read the last tap's point throws the fit off, so the
169
+ # fault is that, not a pattern.
170
+ out["ok"] = False
171
+ out["stale"] = stale
172
+ words.append(
173
+ "Each press starts where the last one ended: the touch controller's first read of %d of the taps "
174
+ "was the tap before's point, so a tap can press what you touched last." % stale
175
+ )
176
+ return out
177
+ if ok:
178
+ words.append("Taps land where you touch: %d px off on average, the worst %d px." % (round(sum(off) / len(off)), worst))
179
+ if seen is None:
180
+ words.append("The taps all read the same place: the touch controller isn't reporting where you touch.")
181
+ return out
182
+ if not out["ok"]:
183
+ if seen["mask"]:
184
+ words.append(_WORDS[seen["mask"]])
185
+ words.extend(_scale_words(seen, width, height, tol))
186
+ if len(words) == 0:
187
+ far = [i + 1 for i, d in enumerate(off) if d > 2 * tol]
188
+ if far:
189
+ words.append("Tap %s landed %d px from its target, far past the others." % (" and ".join(str(n) for n in far), worst))
190
+ else:
191
+ words.append("Taps land up to %d px from their targets." % worst)
192
+
193
+ raws = [t.get("raw") for t in taps]
194
+ if None in raws:
195
+ return out
196
+ raws = [tuple(r[:2]) for r in raws]
197
+ need = fit(raws, targets)
198
+ if need is None:
199
+ return out
200
+ good = need["mask"]
201
+ after = fit([apply_mask(r, good, width, height) for r in raws], targets)
202
+ fix = {"mask": good, "mask_name": mask_name(good), "rotation": rotation, "table": None, "calibration": None}
203
+ if table is not None:
204
+ new = list(table)
205
+ new[(rotation // 90) % 4] = good
206
+ fix["table"] = new
207
+ if after is not None:
208
+ cal = {}
209
+ for axis, size in (("x", width), ("y", height)):
210
+ a, b = after[axis]
211
+ if abs(a - 1) > SCALE_OK or abs(b) > tol:
212
+ # Where touch_read's own axis reads at the screen's edges, and
213
+ # where it should: the mask may swap and reverse it on the way.
214
+ raw_axis = ("y" if axis == "x" else "x") if need["swap"] else axis
215
+ rev = good & (REVERSE_X if axis == "x" else REVERSE_Y)
216
+ should = (size - 1, 0) if rev else (0, size - 1)
217
+ cal[axis] = {"raw_axis": raw_axis, "reads": _edges(need[axis], size), "should": should}
218
+ fix["calibration"] = cal or None
219
+ out["fix"] = fix
220
+ if not out["ok"]:
221
+ if mask is not None and good != mask:
222
+ words.append("Fix: the rotation table's entry for rotation %d should be %s, not %s." % (rotation, mask_name(good), mask_name(mask)))
223
+ for axis in ("x", "y"):
224
+ c = (fix["calibration"] or {}).get(axis)
225
+ if c:
226
+ words.append(
227
+ "Fix: touch_read's %s reads %d at the %s edge and %d at the %s; it should read %d and %d."
228
+ % (c["raw_axis"], c["reads"][0], "left" if axis == "x" else "top", c["reads"][1],
229
+ "right" if axis == "x" else "bottom", c["should"][0], c["should"][1])
230
+ )
231
+ return out
232
+
233
+
234
+ def _stale(taps, tol):
235
+ """How many taps began with a first read on the tap before them, far from where they settled."""
236
+ count = 0
237
+ for before, tap in zip(taps, taps[1:]):
238
+ first = tap.get("first")
239
+ if first is None or before.get("raw") is None:
240
+ continue
241
+ on_before = abs(first[0] - before["raw"][0]) + abs(first[1] - before["raw"][1]) <= 2 * tol
242
+ settled_far = abs(first[0] - tap["raw"][0]) + abs(first[1] - tap["raw"][1]) > 4 * tol
243
+ if on_before and settled_far:
244
+ count += 1
245
+ return count
246
+
247
+
248
+ def _scale_words(seen, width, height, tol):
249
+ """Sentences for a fitted scale or offset that's wrong."""
250
+ out = []
251
+ for axis, size, across, lo, hi in (("x", width, "width", "left", "right"), ("y", height, "height", "up", "down")):
252
+ a, b = seen[axis]
253
+ if abs(abs(a) - 1) > SCALE_OK:
254
+ out.append("Taps spread over %d%% of the screen's %s instead of all of it." % (round(100 / abs(a)), across))
255
+ if seen["swap"]:
256
+ continue # an offset along a swapped axis has no plain direction
257
+ # Where a touch at the middle of this axis lands, against the middle.
258
+ mid = (size - 1) / 2
259
+ shift = (mid - b) / a - mid
260
+ if abs(shift) > tol:
261
+ out.append("Taps land %d px too far %s." % (abs(round(shift)), hi if shift > 0 else lo))
262
+ return out