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,213 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Home Assistant: a wall panel for the lights, switches and sensors in it.
5
+
6
+ Each entity is a tile with its name and state. A tap toggles a light,
7
+ switch, fan, cover or input boolean, plays or pauses a media player,
8
+ and runs a scene, script or button; a sensor's tile shows its value. Lit
9
+ tiles are teal, ones that are off are grey, and ones Home Assistant
10
+ can't reach say so. The tiles refresh every few seconds, and right
11
+ after a tap.
12
+
13
+ Every request runs on ``scope.worker``, so a slow answer never holds the
14
+ screen up.
15
+
16
+ Two files in its data folder (``/data/homeassistant`` on the board) say
17
+ where Home Assistant is and how to sign in:
18
+
19
+ ``config.json``
20
+ ``{"url": "http://homeassistant.local:8123"}``. Two more keys are
21
+ optional: ``"entities"``, a list of entity ids to show in that order
22
+ (without it, every entity a tap can act on and every sensor with a
23
+ unit, by name), and ``"every"``, the seconds between refreshes (5).
24
+ ``token``
25
+ A long-lived access token and nothing else, made in Home Assistant
26
+ under your profile, Security. It has a file of its own so it can be
27
+ copied on as it is, and so ``config.json`` can be shared without it.
28
+ """
29
+
30
+ import lvgl as lv
31
+
32
+ from appkit import data
33
+ from touchkit import net, settings, ui
34
+
35
+ from . import api
36
+
37
+ DEFAULTS = {"url": None, "entities": None, "every": 5}
38
+
39
+ GAP = 12 # between tiles, and round the grid
40
+ TILE_W = 220 # the narrowest a tile gets before the grid drops a column
41
+ TILE_H = 104
42
+
43
+ ON = ("on", "open", "opening", "playing", "home")
44
+ GONE = ("unavailable", "unknown")
45
+
46
+ state = None # the open panel's state, for the console
47
+
48
+
49
+ def _shown(entity_id, value, unit):
50
+ """What a tile says under the name."""
51
+ if value in GONE:
52
+ return value
53
+ if api.action(entity_id) in ("turn_on", "press"):
54
+ return "Tap to run"
55
+ if unit:
56
+ dot = value.find(".")
57
+ if dot >= 0 and len(value) - dot > 2:
58
+ # A rate or a reading with every digit the sensor sent: one decimal is plenty on a tile.
59
+ try:
60
+ value = "%.1f" % float(value)
61
+ except ValueError:
62
+ pass
63
+ return "%s %s" % (value, unit)
64
+ return value[:1].upper() + value[1:].replace("_", " ")
65
+
66
+
67
+ def _token(folder):
68
+ """The token in *folder*'s ``token`` file, or None when there's none."""
69
+ try:
70
+ with open(data.join(folder, "token")) as f:
71
+ return f.read().strip() or None
72
+ except OSError:
73
+ return None
74
+
75
+
76
+ def main(scope):
77
+ global state
78
+ conf = settings.config(scope, DEFAULTS)
79
+ conf["token"] = _token(scope.data_dir)
80
+ root = scope.root
81
+ status = ui.status(scope)
82
+ W, H = scope.size
83
+ st = state = {
84
+ "phase": "wifi",
85
+ "rows": [],
86
+ "tiles": {},
87
+ "taps": 0,
88
+ "errors": 0,
89
+ "error": None,
90
+ }
91
+ w = scope.worker
92
+
93
+ if not conf["url"] or not conf["token"]:
94
+ status.set_text("Not set up")
95
+ msg = ui.label(root, "Put Home Assistant's address in config.json in %s:\n\n"
96
+ '{"url": "http://homeassistant.local:8123"}\n\n'
97
+ "and an access token in a file named token beside it. Make the token in "
98
+ "Home Assistant under your profile, Security." % scope.data_dir, ui.font, ui.WHITE) # fmt: skip
99
+ msg.set_width(W - 48)
100
+ msg.set_long_mode(lv.label.LONG_MODE.WRAP)
101
+ msg.align(lv.ALIGN.CENTER, 0, 0)
102
+ st["phase"] = "setup"
103
+ return
104
+
105
+ cols = max(1, (W - GAP) // (TILE_W + GAP))
106
+ tile_w = (W - GAP * (cols + 1)) // cols
107
+
108
+ grid = lv.obj(root)
109
+ grid.remove_style_all()
110
+ grid.set_size(W, H)
111
+ grid.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
112
+ grid.set_style_pad_all(GAP, 0)
113
+ grid.set_style_pad_row(GAP, 0)
114
+ grid.set_style_pad_column(GAP, 0)
115
+ grid.set_scroll_dir(lv.DIR.VER)
116
+
117
+ msg = ui.label(root, "joining Wi-Fi...", ui.font, ui.MUTED)
118
+ msg.align(lv.ALIGN.CENTER, 0, 0)
119
+
120
+ def tile(entity_id):
121
+ b = lv.button(grid)
122
+ b.set_size(tile_w, TILE_H)
123
+ b.set_style_radius(14, 0)
124
+ b.set_style_pad_all(12, 0)
125
+ name = ui.label(b, "", ui.small, ui.WHITE)
126
+ name.set_size(tile_w - 24, ui.small.line_height)
127
+ name.set_long_mode(lv.label.LONG_MODE.DOTS)
128
+ name.align(lv.ALIGN.TOP_LEFT, 0, 0)
129
+ value = ui.label(b, "", ui.big, ui.WHITE)
130
+ value.set_size(tile_w - 24, ui.big.line_height)
131
+ value.set_long_mode(lv.label.LONG_MODE.DOTS)
132
+ value.align(lv.ALIGN.BOTTOM_LEFT, 0, 0)
133
+ b.add_event_cb(lambda e: tap(entity_id), lv.EVENT.CLICKED, None)
134
+ return b, name, value
135
+
136
+ def paint(row):
137
+ entity_id, name, value, unit = row
138
+ b, name_l, value_l = st["tiles"][entity_id]
139
+ name_l.set_text(name)
140
+ value_l.set_text(_shown(entity_id, value, unit))
141
+ if value in GONE:
142
+ color, text = ui.PANEL, ui.MUTED
143
+ elif api.action(entity_id) is None:
144
+ color, text = ui.PANEL, ui.WHITE
145
+ elif value in ON:
146
+ color, text = ui.ACCENT, ui.WHITE
147
+ else:
148
+ color, text = ui.KEY, ui.WHITE
149
+ b.set_style_bg_color(color, 0)
150
+ value_l.set_style_text_color(text, 0)
151
+
152
+ def show(rows):
153
+ ids = [r[0] for r in rows]
154
+ if ids != [r[0] for r in st["rows"]]:
155
+ # The set of entities changed: build the tiles again, in order.
156
+ grid.clean()
157
+ st["tiles"] = {}
158
+ for entity_id in ids:
159
+ st["tiles"][entity_id] = tile(entity_id)
160
+ st["rows"] = rows
161
+ for row in rows:
162
+ paint(row)
163
+ if rows:
164
+ msg.add_flag(lv.obj.FLAG.HIDDEN)
165
+ else:
166
+ msg.set_text("Home Assistant has nothing to show yet")
167
+ msg.remove_flag(lv.obj.FLAG.HIDDEN)
168
+
169
+ def refresh():
170
+ if w.pending("states"):
171
+ return
172
+
173
+ def done(rows, error):
174
+ if error is not None:
175
+ st["errors"] += 1
176
+ st["error"] = str(error)
177
+ status.set_text("Can't reach Home Assistant: %s" % (error,))
178
+ if not st["rows"]:
179
+ msg.set_text("Can't reach Home Assistant:\n%s" % (error,))
180
+ return
181
+ st["error"] = None
182
+ status.set_text("%d in Home Assistant" % len(rows))
183
+ show(rows)
184
+
185
+ w.submit(lambda: api.states(conf), done, key="states")
186
+
187
+ def tap(entity_id):
188
+ tiles = st["tiles"]
189
+ if api.action(entity_id) is None or entity_id not in tiles:
190
+ refresh()
191
+ return
192
+ tiles[entity_id][2].set_text("...")
193
+
194
+ def done(service, error):
195
+ if error is not None:
196
+ st["errors"] += 1
197
+ status.set_text("%s: %s" % (entity_id, error))
198
+ else:
199
+ st["taps"] += 1
200
+ refresh()
201
+
202
+ w.submit(lambda: api.tap(conf, entity_id), done, key="tap:" + entity_id)
203
+
204
+ def tick(t):
205
+ if st["phase"] == "wifi":
206
+ if not net.up():
207
+ return
208
+ st["phase"] = "ready"
209
+ msg.set_text("asking Home Assistant...")
210
+ refresh()
211
+
212
+ scope.every(max(1, int(conf["every"])) * 1000, tick)
213
+ tick(None)
@@ -0,0 +1,104 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Read and toggle Home Assistant entities over its REST API.
5
+
6
+ Every call blocks on the network, so run it on ``scope.worker``. Each call
7
+ makes one request and closes it.
8
+
9
+ `states` asks Home Assistant to render a template that lists only the four
10
+ fields the panel shows, so a board never parses the whole ``/api/states``
11
+ answer, which runs to tens of kilobytes on a busy house.
12
+ """
13
+
14
+ import json
15
+
16
+ from appkit import net
17
+
18
+ TIMEOUT = 8 # seconds for one request
19
+
20
+ #: What a tap does, by domain: the service it calls on the entity.
21
+ ACTIONS = {
22
+ "light": "toggle",
23
+ "switch": "toggle",
24
+ "fan": "toggle",
25
+ "input_boolean": "toggle",
26
+ "cover": "toggle",
27
+ "media_player": "media_play_pause",
28
+ "scene": "turn_on",
29
+ "script": "turn_on",
30
+ "button": "press",
31
+ "input_button": "press",
32
+ }
33
+
34
+ # Every entity in a domain above, and every sensor with a unit (a
35
+ # temperature, say, but not the time of the next sunrise).
36
+ _ALL = "s.domain in %s or (s.domain == 'sensor' and s.attributes.unit_of_measurement is defined)"
37
+ _ROWS = (
38
+ "[{%%- for s in states if %s -%%}"
39
+ "{{- ',' if not loop.first -}}"
40
+ "[{{ s.entity_id|tojson }},{{ s.name|tojson }},{{ s.state|tojson }},"
41
+ "{{ s.attributes.unit_of_measurement|default('')|tojson }}]"
42
+ "{%%- endfor %%}]"
43
+ )
44
+
45
+
46
+ def _post(conf, path, body):
47
+ """The body of one POST to *path*, as bytes; raises ``OSError`` on any answer but 2xx."""
48
+ r = net.requests().post(
49
+ conf["url"].rstrip("/") + path,
50
+ data=json.dumps(body),
51
+ headers={
52
+ "Authorization": "Bearer " + conf["token"],
53
+ "Content-Type": "application/json",
54
+ },
55
+ timeout=TIMEOUT,
56
+ )
57
+ try:
58
+ code = r.status_code
59
+ if code == 401:
60
+ raise OSError("Home Assistant refused the token")
61
+ if not 200 <= code < 300:
62
+ raise OSError("Home Assistant answered %d" % code)
63
+ return r.content
64
+ finally:
65
+ r.close()
66
+
67
+
68
+ def states(conf):
69
+ """The entities to show, as ``[entity_id, name, state, unit]`` lists.
70
+
71
+ With ``conf["entities"]``, a list of entity ids, those in that order;
72
+ one Home Assistant doesn't know is left out. Without it, every entity
73
+ a tap can act on (`ACTIONS`) and every sensor with a unit, by name.
74
+
75
+ Args:
76
+ conf (dict): ``url`` and ``token``, and ``entities`` if set.
77
+ """
78
+ want = conf.get("entities")
79
+ if want:
80
+ cond = "s.entity_id in %s" % json.dumps(list(want))
81
+ else:
82
+ cond = _ALL % json.dumps(sorted(ACTIONS))
83
+ rows = json.loads(_post(conf, "/api/template", {"template": _ROWS % cond}))
84
+ if want:
85
+ order = {e: i for i, e in enumerate(want)}
86
+ rows.sort(key=lambda r: order[r[0]])
87
+ else:
88
+ rows.sort(key=lambda r: r[1].lower())
89
+ return rows
90
+
91
+
92
+ def action(entity_id):
93
+ """The service a tap on *entity_id* calls, or None when a tap does nothing (a sensor)."""
94
+ return ACTIONS.get(entity_id.split(".", 1)[0])
95
+
96
+
97
+ def tap(conf, entity_id):
98
+ """Call `action`'s service on *entity_id*. Returns the service's name, or None for a sensor."""
99
+ service = action(entity_id)
100
+ if service is None:
101
+ return None
102
+ domain = entity_id.split(".", 1)[0]
103
+ _post(conf, "/api/services/%s/%s" % (domain, service), {"entity_id": entity_id})
104
+ return service
@@ -0,0 +1 @@
1
+ {"name": "homeassistant", "title": "Home Assistant", "category": "Desk", "icon": "lightbulb", "color": "#1F7A8C"}
apps/huey/__init__.py ADDED
@@ -0,0 +1,287 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Huey: hold the chameleon, ask him something out loud, and he answers.
5
+
6
+ While you hold Huey he turns to you and the board's microphone records.
7
+ Let go, and the question goes to an OpenAI-compatible server three times
8
+ over: for its words, for a reply, and for the reply as speech, which plays
9
+ through the board's speaker and shows in a speech bubble. The reply also
10
+ says how Huey acts it out: a mood, colours, something to hold, a place.
11
+
12
+ Which server, which models and which key file are in one settings file,
13
+ ``settings.json`` in the app's data folder; see ``settings.py``.
14
+ """
15
+
16
+ import lvgl as lv
17
+
18
+ from .settings import DEFAULTS
19
+ from .actor import Actor
20
+ from .turn import Turn
21
+ from ..perch.stage import TICK_MS
22
+
23
+ _GLASS = 0x0B2A4A
24
+ _LIME = 0xC4DC4C
25
+ _PAPER = 0xFFFDF4
26
+ _AMBER = 0xFFE9B0
27
+ _INK = 0x14283C
28
+ _BUBBLE_TICKS = 6000 // TICK_MS # an answer stays this long after its last word
29
+ _NOTE_TICKS = 5000 // TICK_MS
30
+
31
+
32
+ def _font(size):
33
+ """The Montserrat nearest *size* that this LVGL was built with."""
34
+ for wanted in (size, 16, 14):
35
+ font = getattr(lv, "font_montserrat_%d" % wanted, None)
36
+ if font is not None:
37
+ return font
38
+ return lv.font_get_default()
39
+
40
+
41
+ class _Show:
42
+ """The words over the picture: a speech bubble, a line of what is happening, a ring that hears."""
43
+
44
+ def __init__(self, root, size, stage):
45
+ w, h = size
46
+ self.w, self.h = w, h
47
+ self.stage = stage
48
+ big = h >= 400
49
+ self.ring_obj = lv.obj(root)
50
+ self.ring_obj.remove_style_all()
51
+ self.ring_obj.set_style_radius(lv.RADIUS_CIRCLE, 0)
52
+ self.ring_obj.set_style_border_color(lv.color_hex(_LIME), 0)
53
+ self.ring_obj.set_style_border_width(max(3, h // 100), 0)
54
+ self.ring_obj.set_style_border_opa(200, 0)
55
+ self.ring_obj.remove_flag(lv.obj.FLAG.CLICKABLE)
56
+ self.ring_obj.add_flag(lv.obj.FLAG.HIDDEN)
57
+ self._ring = 0.0
58
+
59
+ self.bubble = lv.obj(root)
60
+ self.bubble.remove_style_all()
61
+ self.bubble.set_style_radius(max(10, h // 24), 0)
62
+ self.bubble.set_style_bg_color(lv.color_hex(_PAPER), 0)
63
+ self.bubble.set_style_bg_opa(240, 0)
64
+ self.bubble.set_style_pad_all(max(8, h // 34), 0)
65
+ self.bubble.set_width(lv.SIZE_CONTENT)
66
+ self.bubble.set_height(lv.SIZE_CONTENT)
67
+ self.bubble.remove_flag(lv.obj.FLAG.CLICKABLE)
68
+ self.bubble.remove_flag(lv.obj.FLAG.SCROLLABLE)
69
+ self.words = lv.label(self.bubble)
70
+ self.words.set_style_text_font(_font(24 if big else 16), 0)
71
+ self.words.set_style_text_color(lv.color_hex(_INK), 0)
72
+ self.words.set_long_mode(lv.label.LONG_MODE.WRAP)
73
+ self._words_most = w * 62 // 100
74
+ self.tail = lv.obj(root)
75
+ self.tail.remove_style_all()
76
+ self.tail.set_style_bg_color(lv.color_hex(_PAPER), 0)
77
+ self.tail.set_style_bg_opa(240, 0)
78
+ self.tail.set_style_radius(lv.RADIUS_CIRCLE, 0)
79
+ self.tail.remove_flag(lv.obj.FLAG.CLICKABLE)
80
+ self.bubble.add_flag(lv.obj.FLAG.HIDDEN)
81
+ self.tail.add_flag(lv.obj.FLAG.HIDDEN)
82
+ self._bubble_left = 0
83
+
84
+ self.line = lv.obj(root)
85
+ self.line.remove_style_all()
86
+ self.line.set_style_radius(lv.RADIUS_CIRCLE, 0)
87
+ self.line.set_style_bg_color(lv.color_hex(_GLASS), 0)
88
+ self.line.set_style_bg_opa(190, 0)
89
+ self.line.set_style_pad_hor(max(10, h // 28), 0)
90
+ self.line.set_style_pad_ver(max(4, h // 80), 0)
91
+ self.line.set_width(lv.SIZE_CONTENT)
92
+ self.line.set_height(lv.SIZE_CONTENT)
93
+ self.line.remove_flag(lv.obj.FLAG.CLICKABLE)
94
+ self.line.remove_flag(lv.obj.FLAG.SCROLLABLE)
95
+ self.line_text = lv.label(self.line)
96
+ self.line_text.set_style_text_font(_font(16 if big else 14), 0)
97
+ self.line_text.set_style_text_color(lv.color_white(), 0)
98
+ self.line_text.set_long_mode(lv.label.LONG_MODE.DOTS)
99
+ self.line.add_flag(lv.obj.FLAG.HIDDEN)
100
+ self._note = ""
101
+ self._heard = ""
102
+ self._note_left = 0
103
+ #: What the bubble and the line say now, for whoever is watching from outside.
104
+ self.said = None
105
+ self.noted = ""
106
+
107
+ def _head(self):
108
+ """Where Huey's head is on the screen, near enough."""
109
+ if self.stage.place is None:
110
+ # his first place is still being drawn
111
+ return self.w // 2, self.h // 3
112
+ ox, oy = self.stage._origin()
113
+ k = self.stage._shown[0].header.h / 250 if self.stage._shown else self.h / 800
114
+ return int(ox + 60 * k), int(oy - 150 * k)
115
+
116
+ def _line(self):
117
+ text = self._note or self._heard
118
+ if self._note and self._heard:
119
+ text = "%s %s" % (self._heard, self._note)
120
+ self.noted = text
121
+ if not text:
122
+ self.line.add_flag(lv.obj.FLAG.HIDDEN)
123
+ return
124
+ self.line_text.set_width(lv.SIZE_CONTENT)
125
+ self.line_text.set_text(text)
126
+ self.line_text.update_layout()
127
+ most = self.w * 86 // 100
128
+ if self.line_text.get_width() > most:
129
+ self.line_text.set_width(most)
130
+ self.line.remove_flag(lv.obj.FLAG.HIDDEN)
131
+ self.line.align(lv.ALIGN.BOTTOM_MID, 0, -max(6, self.h // 40))
132
+
133
+ def note(self, text):
134
+ """What is happening now, on the line at the bottom; "" for nothing."""
135
+ if text != self._note:
136
+ self._note = text
137
+ self._note_left = _NOTE_TICKS if text else 0
138
+ self._line()
139
+
140
+ def heard(self, text):
141
+ """The question as it was understood, on the line whenever nothing is happening."""
142
+ self._heard = '"%s"' % text if text else ""
143
+ self._line()
144
+
145
+ def say(self, text, trouble):
146
+ """Put *text* in the speech bubble, in amber when it is *trouble*; None takes the bubble away."""
147
+ self.said = text
148
+ if text is None:
149
+ self.bubble.add_flag(lv.obj.FLAG.HIDDEN)
150
+ self.tail.add_flag(lv.obj.FLAG.HIDDEN)
151
+ self._bubble_left = 0
152
+ self.heard("")
153
+ return
154
+ color = lv.color_hex(_AMBER if trouble else _PAPER)
155
+ self.bubble.set_style_bg_color(color, 0)
156
+ self.tail.set_style_bg_color(color, 0)
157
+ self.words.set_width(lv.SIZE_CONTENT)
158
+ self.words.set_text(text)
159
+ self.words.update_layout()
160
+ if self.words.get_width() > self._words_most:
161
+ self.words.set_width(self._words_most)
162
+ self.bubble.remove_flag(lv.obj.FLAG.HIDDEN)
163
+ self.bubble.update_layout()
164
+ self._place_bubble()
165
+ # an answer stays while it is spoken; trouble goes after a while by itself
166
+ self._bubble_left = _BUBBLE_TICKS * 2 if trouble else -1
167
+
168
+ def _place_bubble(self):
169
+ hx, hy = self._head()
170
+ bw, bh = self.bubble.get_width(), self.bubble.get_height()
171
+ gap = max(6, self.h // 60)
172
+ x = min(max(hx + self.w // 16, gap), self.w - bw - gap)
173
+ y = min(max(hy - bh - self.h // 8, gap), self.h - bh - gap)
174
+ self.bubble.set_pos(x, y)
175
+ dot = max(8, self.h // 34)
176
+ self.tail.set_size(dot, dot)
177
+ self.tail.set_pos(x + bw // 8, y + bh + dot // 4)
178
+ self.tail.remove_flag(lv.obj.FLAG.HIDDEN)
179
+
180
+ def rest(self):
181
+ """The answer has been spoken: its bubble stays a while longer, then goes."""
182
+ if self._bubble_left < 0:
183
+ self._bubble_left = _BUBBLE_TICKS
184
+
185
+ def ring(self, level):
186
+ """How loud the microphone hears, 0 to 1, as a ring round Huey's head; None takes it away."""
187
+ if level is None:
188
+ self.ring_obj.add_flag(lv.obj.FLAG.HIDDEN)
189
+ self._ring = 0.0
190
+ return
191
+ # out fast, back slowly: a voice reads as a pulse
192
+ self._ring = level if level > self._ring else self._ring * 0.8
193
+ hx, hy = self._head()
194
+ size = int(self.h * (0.2 + 0.22 * min(self._ring * 2.5, 1.0)))
195
+ self.ring_obj.set_size(size, size)
196
+ self.ring_obj.set_pos(hx - size // 2, hy - size // 2)
197
+ self.ring_obj.remove_flag(lv.obj.FLAG.HIDDEN)
198
+
199
+ def tick(self):
200
+ if self._bubble_left > 0:
201
+ self._bubble_left -= 1
202
+ if self._bubble_left == 0:
203
+ self.say(None, False)
204
+ if self._note_left > 0 and self._note in ("Hold me to talk", "Hold me while you talk", "No speaker here: read my words"):
205
+ self._note_left -= 1
206
+ if self._note_left == 0:
207
+ self.note("")
208
+
209
+
210
+ def start(scope, post=None, microphone=None, speaker=None):
211
+ """Open the app on *scope*, as `main` does.
212
+
213
+ Args:
214
+ scope: The app's scope.
215
+ post: What sends a request to the server, in place of the
216
+ connection the app keeps open to it.
217
+ microphone: What opens a microphone at a rate, in place of the board's.
218
+ speaker: What opens a speaker at a rate and a count of channels, in place of the board's.
219
+ """
220
+ root = scope.root
221
+ root.remove_flag(lv.obj.FLAG.SCROLLABLE)
222
+ root.set_style_pad_all(0, 0)
223
+ root.set_style_border_width(0, 0)
224
+ root.set_style_bg_color(lv.color_hex(_GLASS), 0)
225
+ root.set_style_bg_opa(255, 0)
226
+
227
+ path = scope.data_dir + "/settings.json"
228
+ stage = Actor(root, scope.size)
229
+ show = _Show(root, scope.size, stage)
230
+ turn = Turn(stage, show, scope.worker, path, post, microphone, speaker)
231
+ why = turn.ready()
232
+ place = turn.settings["place"] if turn.settings else DEFAULTS["place"]
233
+ if place != stage.place:
234
+ stage.go(place)
235
+ if why:
236
+ show.say(why, True)
237
+ else:
238
+ show.note("Hold me to talk")
239
+
240
+ point = lv.point_t()
241
+ held = [False]
242
+
243
+ def pressed(_event):
244
+ indev = lv.indev_active()
245
+ if indev is None:
246
+ return
247
+ indev.get_point(point)
248
+ if stage.hit(point.x, point.y):
249
+ held[0] = True
250
+ turn.press()
251
+ elif turn.state == "idle":
252
+ show.note("Hold me to talk")
253
+
254
+ def released(_event):
255
+ if held[0]:
256
+ held[0] = False
257
+ turn.release()
258
+
259
+ root.add_flag(lv.obj.FLAG.CLICKABLE)
260
+ root.add_event_cb(pressed, lv.EVENT.PRESSED, None)
261
+ root.add_event_cb(released, lv.EVENT.RELEASED, None)
262
+ root.add_event_cb(released, lv.EVENT.PRESS_LOST, None)
263
+
264
+ was = [turn.state]
265
+
266
+ def tick(_timer=None):
267
+ if turn.astray():
268
+ return
269
+ stage.tick()
270
+ turn.tick()
271
+ if was[0] == "speak" and turn.state == "idle":
272
+ show.rest()
273
+ was[0] = turn.state
274
+ show.tick()
275
+
276
+ def close():
277
+ turn.close()
278
+ stage.close()
279
+
280
+ scope.every(TICK_MS, tick)
281
+ scope.on_close(close)
282
+ # for whoever drives the app from outside: a test, a screenshot script
283
+ scope.huey = (stage, show, turn)
284
+
285
+
286
+ def main(scope):
287
+ start(scope)