omnipython-apps 0.1.4.dev8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. apps/__init__.py +5 -0
  2. apps/about/__init__.py +257 -0
  3. apps/about/app.json +1 -0
  4. apps/about/credits.py +12 -0
  5. apps/about/facts.py +398 -0
  6. apps/about/fonts/OFL.txt +93 -0
  7. apps/about/fonts/latin1_14.bin +0 -0
  8. apps/about/fonts/latin1_16.bin +0 -0
  9. apps/about/fonts/latin1_24.bin +0 -0
  10. apps/about/fonts/latin1_32.bin +0 -0
  11. apps/about/fonts/latin1_40.bin +0 -0
  12. apps/backline/__init__.py +58 -0
  13. apps/backline/app.json +1 -0
  14. apps/backline/app.py +475 -0
  15. apps/backline/art.py +717 -0
  16. apps/backline/ask.py +781 -0
  17. apps/backline/box.py +502 -0
  18. apps/backline/cast.py +137 -0
  19. apps/backline/desk/__init__.py +329 -0
  20. apps/backline/desk/editor.py +331 -0
  21. apps/backline/desk/grid.py +154 -0
  22. apps/backline/desk/look.py +169 -0
  23. apps/backline/desk/pedals.py +102 -0
  24. apps/backline/desk/perform.py +157 -0
  25. apps/backline/desk/prompt.py +210 -0
  26. apps/backline/desk/strip.py +159 -0
  27. apps/backline/desk_main.py +226 -0
  28. apps/backline/engine.py +622 -0
  29. apps/backline/floor.py +509 -0
  30. apps/backline/footswitch.py +119 -0
  31. apps/backline/glyphs.py +82 -0
  32. apps/backline/kits.py +428 -0
  33. apps/backline/midipedal.py +362 -0
  34. apps/backline/page_data.py +413 -0
  35. apps/backline/parts.py +145 -0
  36. apps/backline/pedal.py +107 -0
  37. apps/backline/remote.py +134 -0
  38. apps/backline/songs/ballad_68.json +1 -0
  39. apps/backline/songs/bossa.json +1 -0
  40. apps/backline/songs/country_train.json +1 -0
  41. apps/backline/songs/funk.json +1 -0
  42. apps/backline/songs/metal.json +1 -0
  43. apps/backline/songs/pop.json +1 -0
  44. apps/backline/songs/rock.json +1 -0
  45. apps/backline/songs/shuffle.json +1 -0
  46. apps/battery/__init__.py +101 -0
  47. apps/battery/app.json +1 -0
  48. apps/bit_world/act_deck.py +683 -0
  49. apps/bit_world/act_game.py +762 -0
  50. apps/bit_world/act_pet.py +1032 -0
  51. apps/bit_world/app.json +1 -0
  52. apps/bit_world/bit_output.py +45 -0
  53. apps/bit_world/bit_palette.py +503 -0
  54. apps/bit_world/bit_sound.py +431 -0
  55. apps/bit_world/bit_sprites.py +632 -0
  56. apps/bit_world/bit_world.py +534 -0
  57. apps/btreceiver/__init__.py +690 -0
  58. apps/btreceiver/app.json +1 -0
  59. apps/btreceiver/chime.py +155 -0
  60. apps/btreceiver/demo.py +529 -0
  61. apps/btreceiver/screen.py +633 -0
  62. apps/camera/__init__.py +316 -0
  63. apps/camera/app.json +1 -0
  64. apps/card/__init__.py +113 -0
  65. apps/card/app.json +1 -0
  66. apps/cast/__init__.py +631 -0
  67. apps/cast/app.json +1 -0
  68. apps/cast/receivers.py +193 -0
  69. apps/chess/__init__.py +18 -0
  70. apps/chess/app.json +1 -0
  71. apps/chess/app.py +922 -0
  72. apps/chess/art.py +323 -0
  73. apps/chess/ble.py +411 -0
  74. apps/chess/engine.py +925 -0
  75. apps/chess/game.py +327 -0
  76. apps/chess/net.py +261 -0
  77. apps/clock/__init__.py +38 -0
  78. apps/clock/app.json +1 -0
  79. apps/detect/__init__.py +214 -0
  80. apps/detect/app.json +1 -0
  81. apps/fmsynth/__init__.py +41 -0
  82. apps/fmsynth/algorithms.py +61 -0
  83. apps/fmsynth/app.json +1 -0
  84. apps/fmsynth/app.py +355 -0
  85. apps/fmsynth/editor.py +206 -0
  86. apps/fmsynth/engine.py +257 -0
  87. apps/fmsynth/meters.py +262 -0
  88. apps/fmsynth/midi.py +375 -0
  89. apps/fmsynth/panel_view.py +1071 -0
  90. apps/fmsynth/patches/fmsynth-01.syx +0 -0
  91. apps/fmsynth/settings.py +138 -0
  92. apps/fmsynth/voice.py +398 -0
  93. apps/gamepad/__init__.py +685 -0
  94. apps/gamepad/app.json +1 -0
  95. apps/gamepad/pad.py +94 -0
  96. apps/gamepad/watch.py +352 -0
  97. apps/hidden/__init__.py +72 -0
  98. apps/hidden/app.json +1 -0
  99. apps/home/__init__.py +483 -0
  100. apps/home/app.json +1 -0
  101. apps/home/main.py +36 -0
  102. apps/homeassistant/__init__.py +213 -0
  103. apps/homeassistant/api.py +104 -0
  104. apps/homeassistant/app.json +1 -0
  105. apps/huey/__init__.py +287 -0
  106. apps/huey/acting.py +184 -0
  107. apps/huey/actor.py +194 -0
  108. apps/huey/app.json +1 -0
  109. apps/huey/client.py +125 -0
  110. apps/huey/servers.py +121 -0
  111. apps/huey/settings.example.json +8 -0
  112. apps/huey/settings.py +97 -0
  113. apps/huey/turn.py +504 -0
  114. apps/huey/voice.py +217 -0
  115. apps/info/__init__.py +810 -0
  116. apps/info/app.json +1 -0
  117. apps/lora/__init__.py +62 -0
  118. apps/lora/app.json +1 -0
  119. apps/manifest.py +71 -0
  120. apps/medialink_baby_monitor/__init__.py +222 -0
  121. apps/medialink_baby_monitor/app.json +1 -0
  122. apps/medialink_doorbell/__init__.py +251 -0
  123. apps/medialink_doorbell/app.json +1 -0
  124. apps/medialink_hands_free/__init__.py +217 -0
  125. apps/medialink_hands_free/app.json +1 -0
  126. apps/medialink_surveillance_camera/__init__.py +258 -0
  127. apps/medialink_surveillance_camera/app.json +1 -0
  128. apps/medialink_video_doorbell/__init__.py +289 -0
  129. apps/medialink_video_doorbell/app.json +1 -0
  130. apps/medialink_videophone/__init__.py +273 -0
  131. apps/medialink_videophone/app.json +1 -0
  132. apps/medialink_walkie_talkie/__init__.py +228 -0
  133. apps/medialink_walkie_talkie/app.json +1 -0
  134. apps/midicontroller/__init__.py +774 -0
  135. apps/midicontroller/app.json +1 -0
  136. apps/midicontroller/links.py +421 -0
  137. apps/midicontroller/surface.py +343 -0
  138. apps/midiengine/__init__.py +977 -0
  139. apps/midiengine/app.json +1 -0
  140. apps/midiengine/banks.py +85 -0
  141. apps/midiengine/midiin.py +165 -0
  142. apps/midiengine/player.py +392 -0
  143. apps/midiengine/umidiparser.py +1413 -0
  144. apps/mp3/__init__.py +240 -0
  145. apps/mp3/app.json +1 -0
  146. apps/perch/__init__.py +119 -0
  147. apps/perch/app.json +1 -0
  148. apps/perch/huey.py +481 -0
  149. apps/perch/huey_props.py +386 -0
  150. apps/perch/huey_shift.py +42 -0
  151. apps/perch/huey_views.py +296 -0
  152. apps/perch/scene_bench.py +680 -0
  153. apps/perch/scene_coast.py +479 -0
  154. apps/perch/scene_desk.py +492 -0
  155. apps/perch/stage.py +1274 -0
  156. apps/perch/vec.py +366 -0
  157. apps/repl/__init__.py +325 -0
  158. apps/repl/app.json +1 -0
  159. apps/roku/__init__.py +156 -0
  160. apps/roku/app.json +1 -0
  161. apps/rokucam/__init__.py +362 -0
  162. apps/rokucam/app.json +1 -0
  163. apps/security/__init__.py +261 -0
  164. apps/security/app.json +1 -0
  165. apps/settings/__init__.py +221 -0
  166. apps/settings/app.json +1 -0
  167. apps/shell/__init__.py +33 -0
  168. apps/shell/app.json +1 -0
  169. apps/spotify/__init__.py +669 -0
  170. apps/spotify/app.json +1 -0
  171. apps/spotify/screen.py +82 -0
  172. apps/steps/__init__.py +58 -0
  173. apps/steps/app.json +1 -0
  174. apps/stompbox/__init__.py +31 -0
  175. apps/stompbox/app.json +1 -0
  176. apps/stompbox/app.py +668 -0
  177. apps/stompbox/boards.json +20 -0
  178. apps/stompbox/effects.py +241 -0
  179. apps/stompbox/engine.py +340 -0
  180. apps/stompbox/footswitch.py +331 -0
  181. apps/stompbox/inputs.py +449 -0
  182. apps/stompbox/meter.py +72 -0
  183. apps/stompbox/outputs.py +135 -0
  184. apps/stompbox/pedals.txt +45 -0
  185. apps/stompbox/presets.py +106 -0
  186. apps/stompbox/styles.py +135 -0
  187. apps/stompbox/ui.py +817 -0
  188. apps/stompbox/wavfile.py +73 -0
  189. apps/synthstation/__init__.py +20 -0
  190. apps/synthstation/app.json +1 -0
  191. apps/synthstation/audio.py +397 -0
  192. apps/synthstation/catalog.py +50 -0
  193. apps/synthstation/meters.py +136 -0
  194. apps/synthstation/midi.py +380 -0
  195. apps/synthstation/pads.py +177 -0
  196. apps/synthstation/parts.py +128 -0
  197. apps/synthstation/station.py +1069 -0
  198. apps/system/__init__.py +11 -0
  199. apps/system/app.json +1 -0
  200. apps/touchcal/__init__.py +324 -0
  201. apps/touchcal/app.json +1 -0
  202. apps/touchcal/fit.py +262 -0
  203. apps/wake/__init__.py +268 -0
  204. apps/wake/app.json +1 -0
  205. apps/wake/feed.py +155 -0
  206. apps/watch/__init__.py +355 -0
  207. apps/watch/app.json +1 -0
  208. apps/watch/faces.py +35 -0
  209. apps/watch/wheel.py +157 -0
  210. apps/weather/__init__.py +278 -0
  211. apps/weather/app.json +1 -0
  212. apps/weather/icons.py +172 -0
  213. apps/weather/sizing.py +71 -0
  214. apps/weather/zippad.py +172 -0
  215. omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
  216. omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
  217. omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
  218. omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
apps/cast/__init__.py ADDED
@@ -0,0 +1,631 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Cast: a screen of its own on a display across the room, and the panel as its remote.
5
+
6
+ Pick a receiver and press Cast. The board draws a 1280 x 720 LVGL screen in
7
+ memory, a second LVGL display that only the receiver shows, and streams it
8
+ over Miracast, about a frame behind, so the remote feels direct: to a Roku
9
+ through its Screen mirroring, or to a Windows PC with its Wireless Display
10
+ app open. Or it serves the screen as HLS for VLC or any HLS player (ten
11
+ seconds behind; fine to watch, slow to drive). The panel shows the remote:
12
+ the d-pad and OK move round the cast screen, Back returns to its home.
13
+
14
+ On the cast screen: a clock, the photos the Camera app took, the camera
15
+ live, and what the board is. The list fills as receivers are found (see
16
+ ``receivers``). A Roku needs Screen mirroring on (Settings > System >
17
+ Screen mirroring), and the first cast may ask on the receiver to allow it.
18
+
19
+ It casts with the ``castdisplay`` package, which installs with it.
20
+ ``config.json`` in its data folder:
21
+ ``{"name": "Kitchen panel"}`` is the name the receiver shows, and
22
+ ``{"prefer": "Bedroom"}`` picks the receiver whose name holds that text
23
+ when there's no last one to go back to. The receivers cast to before are
24
+ kept in ``receivers.json`` there.
25
+
26
+ A Roku chosen for ``cast.target`` in the connections picker is picked here
27
+ too, and a cast that's running moves to it. On a board the picker lists
28
+ Rokus only while Cast is open, which is when the board looks for them.
29
+ """
30
+
31
+ import sys
32
+ import time
33
+
34
+ import lvgl as lv
35
+ from multimer import ticks_add, ticks_diff, ticks_ms
36
+
37
+ from appkit import places
38
+ from touchkit import cam, ecp, net, settings, ui
39
+ from touchkit.clock import sleep_ms
40
+
41
+ CAST_W, CAST_H = 1280, 720
42
+ FPS = 20
43
+ BITRATE = 2_000_000
44
+ DEFAULTS = {"name": "PyDevices", "prefer": None}
45
+ HLS = {"name": "VLC or any HLS player", "host": None, "kind": "hls"}
46
+
47
+ TILES = (
48
+ ("Clock", lv.SYMBOL.BELL, 0x1E6F8C),
49
+ ("Photos", lv.SYMBOL.IMAGE, 0x8C3B1E),
50
+ ("Camera", lv.SYMBOL.VIDEO, 0x3B6E2A),
51
+ ("About", lv.SYMBOL.LIST, 0x6A2C8C),
52
+ )
53
+
54
+ state = None # the open app's state, for the console
55
+
56
+
57
+ def _find_rokus(scope):
58
+ """Have castdisplay list the Rokus for ``cast.target`` while Cast is open.
59
+
60
+ A computer's connections registered them already, and they stay. On a
61
+ board Cast registers them, and stops the search when it closes, since it
62
+ sends one every 10 s.
63
+ """
64
+ try:
65
+ import endpointkit
66
+ from castdisplay import endpoints
67
+ except ImportError:
68
+ return # an older castdisplay, or no endpointkit: Cast finds its own
69
+ registry = endpointkit.registry
70
+ there = registry.providers("cast.target")
71
+ rokus = endpoints.register() # the same finder every time Cast opens
72
+ if rokus in there:
73
+ return
74
+ if rokus not in registry.providers("cast.target"):
75
+ registry.register(rokus) # back after an earlier close took it out
76
+ scope.on_close(lambda: registry.unregister(rokus))
77
+
78
+
79
+ def _encoder_room():
80
+ """(largest free block of internal RAM, what the encoder needs), in bytes.
81
+
82
+ The P4's H.264 encoder keeps its reference rows in one block of internal
83
+ RAM, 1,152 bytes for each 16 pixels of width (92 KB at 1280 wide), and
84
+ nothing else will do. Internal RAM is shared with Wi-Fi and every
85
+ thread's stack, so after enough use it can be free but in pieces.
86
+ """
87
+ need = 3 * 16 * 24 * (CAST_W // 16) + 128
88
+ try:
89
+ import esp32
90
+
91
+ heaps = esp32.idf_heap_info(esp32.HEAP_DATA)
92
+ except Exception:
93
+ return need, need
94
+ return max((h[2] for h in heaps if h[0] < 1_000_000), default=0), need
95
+
96
+
97
+ # -- the cast screen -------------------------------------------------------------
98
+
99
+
100
+ class CastScreen:
101
+ """A second LVGL display over the cast's framebuffer, and what's on it.
102
+
103
+ LVGL draws straight into the buffer the encoder reads (DIRECT mode); each
104
+ finished refresh tells the cast a frame changed.
105
+ """
106
+
107
+ def __init__(self, scope, sink, name):
108
+ self.scope = scope
109
+ self.sink = sink
110
+ self.name = name
111
+ self.disp = lv.display_create(CAST_W, CAST_H)
112
+ self.disp.set_color_format(lv.COLOR_FORMAT.RGB565)
113
+ buf = sink._buf
114
+ self.disp.set_buffers(buf, None, len(buf), lv.DISPLAY_RENDER_MODE.DIRECT)
115
+ self.disp.set_flush_cb(self._flush)
116
+ self.page = "home"
117
+ self.focus = 0
118
+ self.photos = []
119
+ self.photo_i = 0
120
+ self.keep = {}
121
+ self.cam = None
122
+ # The camera's scaler writes by DMA, which wants its buffer on a
123
+ # 64-byte boundary; a bytearray lands where it lands. So it gets a
124
+ # view that starts on one, a picture one row shorter (the view is
125
+ # short by those bytes) and x moved left by the pixels skipped.
126
+ import uctypes
127
+
128
+ off = (-uctypes.addressof(buf)) % 64
129
+ self.cam_fb = memoryview(buf)[off:]
130
+ self.cam_shift = off // 2
131
+ self.cam_h = CAST_H - 1 if off else CAST_H
132
+ self._build()
133
+
134
+ def _flush(self, disp, area, px):
135
+ if disp.flush_is_last():
136
+ self.sink.show()
137
+ disp.flush_ready()
138
+
139
+ def _screen(self):
140
+ prev = lv.display_get_default()
141
+ self.disp.set_default()
142
+ scr = lv.obj()
143
+ prev.set_default()
144
+ scr.set_style_bg_color(lv.color_hex(0x0B0E14), 0)
145
+ scr.set_style_bg_opa(lv.OPA.COVER, 0)
146
+ scr.set_style_text_color(lv.color_hex(0xFFFFFF), 0)
147
+ scr.set_style_text_font(ui.big, 0)
148
+ scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
149
+ return scr
150
+
151
+ def _label(self, parent, text, font=None, color=0xFFFFFF, scale=256):
152
+ lbl = lv.label(parent)
153
+ lbl.set_text(text)
154
+ lbl.set_style_text_font(font or ui.big, 0)
155
+ lbl.set_style_text_color(lv.color_hex(color), 0)
156
+ if scale != 256:
157
+ lbl.set_style_transform_scale(scale, 0)
158
+ return lbl
159
+
160
+ def _build(self):
161
+ # Home: a title, the clock, and a row of tiles.
162
+ self.home = h = self._screen()
163
+ t = self._label(h, self.name, ui.huge, 0x26A69A)
164
+ t.set_pos(80, 60)
165
+ self.home_clock = self._label(h, "", ui.huge, 0xB0B8C8)
166
+ self.home_clock.align(lv.ALIGN.TOP_RIGHT, -80, 60)
167
+ self.tiles = []
168
+ tw, th, gap = 250, 250, 40
169
+ x0 = (CAST_W - (len(TILES) * tw + (len(TILES) - 1) * gap)) // 2
170
+ for i, (text, sym, color) in enumerate(TILES):
171
+ b = lv.obj(h)
172
+ b.remove_style_all()
173
+ b.set_size(tw, th)
174
+ b.set_pos(x0 + i * (tw + gap), 230)
175
+ b.set_style_bg_color(lv.color_hex(color), 0)
176
+ b.set_style_bg_opa(lv.OPA.COVER, 0)
177
+ b.set_style_radius(28, 0)
178
+ b.set_style_border_color(lv.color_hex(0xFFFFFF), 0)
179
+ b.remove_flag(lv.obj.FLAG.SCROLLABLE)
180
+ icon = self._label(b, sym, ui.huge)
181
+ icon.align(lv.ALIGN.CENTER, 0, -30)
182
+ lbl = self._label(b, text, ui.big)
183
+ lbl.align(lv.ALIGN.CENTER, 0, 60)
184
+ self.tiles.append(b)
185
+ hint = self._label(h, "Use the board's screen as the remote", ui.font, 0x7A8494)
186
+ hint.align(lv.ALIGN.BOTTOM_MID, 0, -60)
187
+ self._show_focus()
188
+
189
+ # Clock: big, with the date.
190
+ self.clock = c = self._screen()
191
+ self.clock_time = self._label(c, "", ui.huge, 0xFFFFFF, scale=768)
192
+ self.clock_time.align(lv.ALIGN.CENTER, 0, -60)
193
+ self.clock_date = self._label(c, "", ui.big, 0xB0B8C8)
194
+ self.clock_date.align(lv.ALIGN.CENTER, 0, 120)
195
+
196
+ # Photos: one at a time, left and right to move, a new one every few seconds.
197
+ self.photo = p = self._screen()
198
+ self.photo_img = lv.image(p)
199
+ self.photo_img.align(lv.ALIGN.CENTER, 0, -20)
200
+ self.photo_cap = self._label(p, "", ui.font, 0xB0B8C8)
201
+ self.photo_cap.align(lv.ALIGN.BOTTOM_MID, 0, -24)
202
+
203
+ # Camera: the picture goes straight into the framebuffer, beside a caption.
204
+ self.camera = k = self._screen()
205
+ cap = self._label(k, lv.SYMBOL.VIDEO + "\nLive", ui.big, 0x7A8494)
206
+ cap.set_pos(24, 40)
207
+ self.cam_note = self._label(k, "", ui.font, 0x7A8494)
208
+ self.cam_note.set_pos(24, CAST_H - 60)
209
+
210
+ # About.
211
+ self.about = a = self._screen()
212
+ self.about_text = self._label(a, "", ui.big, 0xFFFFFF)
213
+ self.about_text.set_pos(120, 120)
214
+
215
+ lv.screen_load(self.home)
216
+
217
+ # -- the remote's keys ------------------------------------------------------
218
+
219
+ def key(self, k):
220
+ if self.page == "home":
221
+ if k == "left":
222
+ self.focus = (self.focus - 1) % len(TILES)
223
+ elif k == "right":
224
+ self.focus = (self.focus + 1) % len(TILES)
225
+ elif k == "ok":
226
+ self.open(TILES[self.focus][0].lower())
227
+ return
228
+ self._show_focus()
229
+ return
230
+ if k == "back":
231
+ self.open("home")
232
+ elif self.page == "photos" and k in ("left", "right"):
233
+ self.next_photo(-1 if k == "left" else 1)
234
+
235
+ def _show_focus(self):
236
+ for i, b in enumerate(self.tiles):
237
+ on = i == self.focus
238
+ b.set_style_border_width(8 if on else 0, 0)
239
+ b.set_style_transform_scale(282 if on else 256, 0)
240
+ b.set_style_transform_pivot_x(125, 0)
241
+ b.set_style_transform_pivot_y(125, 0)
242
+
243
+ def open(self, page):
244
+ if self.page == "camera":
245
+ self._camera_off()
246
+ self.page = page
247
+ scr = {"home": self.home, "clock": self.clock, "photos": self.photo, "camera": self.camera, "about": self.about}[
248
+ page
249
+ ]
250
+ lv.screen_load(scr)
251
+ if page == "photos":
252
+ self._load_photos()
253
+ elif page == "camera":
254
+ self._camera_on()
255
+ elif page == "about":
256
+ self._about()
257
+ self.tick()
258
+
259
+ # -- pages ------------------------------------------------------------------
260
+
261
+ def tick(self):
262
+ t = time.localtime()
263
+ hh = t[3] % 12 or 12
264
+ now = "%d:%02d" % (hh, t[4])
265
+ if self.page == "home":
266
+ self.home_clock.set_text(now)
267
+ elif self.page == "clock":
268
+ self.clock_time.set_text(now)
269
+ days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
270
+ months = ("January", "February", "March", "April", "May", "June", "July",
271
+ "August", "September", "October", "November", "December") # fmt: skip
272
+ self.clock_date.set_text(
273
+ "%s %s, %s %d" % ("AM" if t[3] < 12 else "PM", days[t[6]], months[t[1] - 1], t[2])
274
+ )
275
+ elif self.page == "camera" and self.cam is not None:
276
+ dt = ticks_diff(ticks_ms(), self.cam_t0) or 1
277
+ self.cam_note.set_text("%.0f fps" % (self.cam_frames * 1000 / dt))
278
+ self.cam_frames = 0
279
+ self.cam_t0 = ticks_ms()
280
+
281
+ def _load_photos(self):
282
+ if not getattr(self, "_card", None): # the card's photos, read-only, while the app is open
283
+ self._card = places.hold_card(self.scope)
284
+ self.photos = [p for k, p in cam.photos(self.scope)]
285
+ self.photo_i = 0
286
+ self.next_photo(0)
287
+
288
+ def next_photo(self, step):
289
+ if not self.photos:
290
+ self.photo_cap.set_text("No photos yet: take some with the Camera app.")
291
+ return
292
+ self.photo_i = (self.photo_i + step) % len(self.photos)
293
+ path = self.photos[self.photo_i]
294
+ try:
295
+ with open(path, "rb") as f:
296
+ data = f.read()
297
+ px, w, h = ui.decode_jpeg(data, CAST_W - 80, CAST_H - 100)
298
+ except Exception as e:
299
+ self.photo_cap.set_text("%s: %s" % (path, e))
300
+ return
301
+ ui.rgb565_image(self.photo_img, px, w, h, self.keep)
302
+ self.photo_img.align(lv.ALIGN.CENTER, 0, -20)
303
+ self.photo_cap.set_text(
304
+ "%s %d of %d " % (path.rsplit("/", 1)[1], self.photo_i + 1, len(self.photos))
305
+ + lv.SYMBOL.LEFT
306
+ + " "
307
+ + lv.SYMBOL.RIGHT
308
+ )
309
+
310
+ def _about(self):
311
+ name = net.hostname()
312
+ st = self.sink.stats()
313
+ self.about_text.set_text(
314
+ "ESP32-P4 WIFI6\n%s %s\n\nOn the network as %s (%s)\n"
315
+ "Streaming %d x %d at about %d frames a second"
316
+ % (
317
+ sys.implementation.name,
318
+ ".".join(str(v) for v in sys.implementation.version[:3]),
319
+ (name + ".local") if name else "?",
320
+ net.ip() or "no Wi-Fi",
321
+ CAST_W,
322
+ CAST_H,
323
+ st.get("fps", 0),
324
+ )
325
+ )
326
+
327
+ def _camera_on(self):
328
+ opts = cam.options(self.scope)
329
+ try:
330
+ self.cam = cam.open_camera(opts)
331
+ cam.apply_controls(self.cam, opts)
332
+ except Exception as e:
333
+ self.cam = None
334
+ self.cam_note.set_text("no camera: %s" % (e,))
335
+ return
336
+ cw, ch = self.cam.size()[:2]
337
+ w = self.cam_h * cw // ch
338
+ self.cam_box = (CAST_W - w - self.cam_shift, 0, w, self.cam_h)
339
+ self.cam_frames = 0
340
+ self.cam_t0 = ticks_ms()
341
+ self.scope.foreground(self._camera_loop)
342
+
343
+ def _camera_loop(self):
344
+ c = self.cam
345
+ if c is None:
346
+ return
347
+ x, y, w, h = self.cam_box
348
+ try:
349
+ if c.capture_scaled(self.cam_fb, CAST_W, self.cam_h, x=x, y=y, w=w, h=h, timeout=200) is not None:
350
+ self.sink.show()
351
+ self.cam_frames += 1
352
+ except Exception as e:
353
+ self._camera_off()
354
+ self.cam_note.set_text("camera: %s" % (e,))
355
+ return
356
+ sleep_ms(5)
357
+
358
+ def _camera_off(self):
359
+ self.scope.foreground(None)
360
+ c, self.cam = self.cam, None
361
+ if c is not None:
362
+ c.deinit()
363
+
364
+ def close(self):
365
+ self._camera_off()
366
+ try:
367
+ self.disp.delete() # and its screens
368
+ except Exception as e:
369
+ print("cast: display delete:", e)
370
+
371
+
372
+ # -- the panel: the remote ----------------------------------------------------------
373
+
374
+
375
+ def main(scope):
376
+ global state
377
+ try:
378
+ import castif # noqa: F401
379
+ except ImportError as e:
380
+ # The sender is the board's H.264 encoder and Miracast task. Nothing
381
+ # is scanned for here: a device without castif has no stream to send.
382
+ ui.missing(
383
+ scope,
384
+ "missing: castif",
385
+ "Cast needs castif, the board's H.264 encoder and Miracast sender, which this device lacks: %s." % (e,),
386
+ )
387
+ return
388
+ from . import receivers
389
+
390
+ conf = settings.config(scope, DEFAULTS)
391
+ folder = scope.data_dir
392
+ root = scope.root
393
+ status = ui.status(scope)
394
+ W, H = scope.size
395
+ st = state = {"targets": [], "sink": None, "screen": None, "busy": False, "looking": False}
396
+ kept = scope.store() # the receiver last cast to, while the board stays up
397
+ w = scope.worker
398
+
399
+ rx = W // 2 + 16
400
+ rw = W - rx - 24
401
+ dd = lv.dropdown(root)
402
+ dd.set_width(rw)
403
+ dd.set_pos(rx, 12)
404
+ dd.set_options("looking for receivers...")
405
+ go_b, go_l = ui.button(root, lv.SYMBOL.PLAY + " Cast", lambda: toggle(), w=rw, h=72, font=ui.big, color=ui.ACCENT)
406
+ go_b.set_pos(rx, 80)
407
+ msg = ui.label(root, "", ui.font, ui.WHITE)
408
+ msg.set_pos(rx, 170)
409
+ msg.set_width(rw)
410
+ info = ui.label(root, "", ui.small, ui.MUTED)
411
+ info.set_pos(rx, 330)
412
+ info.set_width(rw)
413
+
414
+ # The d-pad, OK in the middle, Back below.
415
+ def key(k):
416
+ screen = st["screen"]
417
+ if screen is None:
418
+ msg.set_text("Cast first, then the remote drives the cast screen.")
419
+ return
420
+ screen.key(k)
421
+
422
+ cx, cy = W // 4, 270
423
+
424
+ def pad(text, k, x, y, bw=120, bh=96, color=None):
425
+ b, _ = ui.button(root, text, lambda: key(k), w=bw, h=bh, font=ui.huge if len(text) < 3 else ui.big, color=color)
426
+ b.set_pos(x, y)
427
+
428
+ pad(lv.SYMBOL.UP, "up", cx - 60, cy - 216)
429
+ pad(lv.SYMBOL.LEFT, "left", cx - 60 - 136, cy - 108)
430
+ pad("OK", "ok", cx - 60, cy - 108, color=ui.WARN)
431
+ pad(lv.SYMBOL.RIGHT, "right", cx - 60 + 136, cy - 108)
432
+ pad(lv.SYMBOL.DOWN, "down", cx - 60, cy)
433
+ pad(lv.SYMBOL.LEFT + " Back", "back", cx - 60 - 136, cy + 120, bw=392, bh=72)
434
+
435
+ def show_targets():
436
+ t = st["targets"] + [HLS]
437
+ keep = dd.get_selected() if st.get("shown") else None
438
+ shown = st.get("shown") or []
439
+ picked = shown[keep]["host"] if keep is not None and keep < len(shown) else kept.get("host")
440
+ st["shown"] = t
441
+ dd.set_options("\n".join(r["name"] for r in t))
442
+ for i, r in enumerate(t):
443
+ if r["host"] == picked:
444
+ dd.set_selected(i)
445
+ return
446
+ prefer = conf.get("prefer")
447
+ if prefer:
448
+ for i, r in enumerate(t):
449
+ if prefer in r["name"]:
450
+ dd.set_selected(i)
451
+ return
452
+
453
+ def add(found):
454
+ # New ones join the list; one found again by a quicker route keeps
455
+ # its place, but takes a better name (an address gives way to a name).
456
+ have = {r["host"]: r for r in st["targets"]}
457
+ for r in found:
458
+ old = have.get(r["host"])
459
+ if old is None:
460
+ st["targets"].append(r)
461
+ have[r["host"]] = r
462
+ elif old["name"].endswith(" at " + old["host"]) and not r["name"].endswith(" at " + r["host"]):
463
+ old["name"] = r["name"]
464
+ show_targets()
465
+
466
+ def say_found(more):
467
+ n = len(st["targets"])
468
+ msg.set_text(
469
+ "%d receiver%s found%s"
470
+ % (n, "" if n == 1 else "s", "; still looking for computers..." if more else ". Pick one and press Cast.")
471
+ )
472
+
473
+ def look():
474
+ if st["looking"]:
475
+ return
476
+ st["looking"] = True
477
+ add(receivers.remembered(folder))
478
+ msg.set_text("Looking for receivers...")
479
+ rest = []
480
+
481
+ def swept(value, error):
482
+ if error is None:
483
+ add(value)
484
+ if rest:
485
+ part = rest[:36]
486
+ del rest[:36]
487
+ w.submit(lambda: receivers.sweep(part), swept, key="sweep")
488
+ say_found(True)
489
+ else:
490
+ st["looking"] = False
491
+ say_found(False)
492
+
493
+ def heard(value, error):
494
+ if error is None:
495
+ add(value)
496
+ else:
497
+ print("cast: mDNS:", error)
498
+ rest.extend(receivers.subnet_hosts(skip=[r["host"] for r in st["targets"]]))
499
+ swept([], None)
500
+
501
+ w.submit(receivers.announced, heard, key="announced")
502
+
503
+ def start():
504
+ r = st["shown"][dd.get_selected()]
505
+ kept["host"] = r["host"]
506
+ have, need = _encoder_room()
507
+ if have < need:
508
+ msg.set_text(
509
+ "The video encoder needs %d KB of the chip's internal memory in one piece, "
510
+ "and the biggest free piece is %d KB. Restart the board, then cast." % (need // 1024, have // 1024)
511
+ )
512
+ return
513
+ st["busy"] = True
514
+ try:
515
+ if r["kind"] == "hls":
516
+ from castdisplay.hls_display import HlsDisplay
517
+
518
+ sink = HlsDisplay(tv=None, width=CAST_W, height=CAST_H, fps=FPS, bitrate=BITRATE, log=print)
519
+ msg.set_text("Open this in VLC (Media > Open Network Stream):\n" + sink.url)
520
+ else:
521
+ from castdisplay.cast_display import CastDisplay
522
+
523
+ receivers.remember(folder, r) # now: no flash writes once the stream runs
524
+ if r["kind"] == "roku":
525
+ try:
526
+ ecp.keypress(r["host"], "PowerOn") # wakes one in standby; one that's on ignores it
527
+ except OSError:
528
+ pass
529
+ sink = CastDisplay(
530
+ r["host"], CAST_W, CAST_H, kind=r["kind"], fps=FPS, bitrate=BITRATE, name=conf["name"], log=print
531
+ )
532
+ msg.set_text("Casting to %s. It may ask once to allow it." % r["name"])
533
+ except Exception as e:
534
+ st["busy"] = False
535
+ msg.set_text("couldn't start: %s" % (e,))
536
+ return
537
+ st["sink"] = sink
538
+ st["screen"] = CastScreen(scope, sink, conf["name"])
539
+ go_l.set_text(lv.SYMBOL.STOP + " Stop")
540
+ go_b.set_style_bg_color(ui.BAD, 0)
541
+ st["busy"] = False
542
+
543
+ def stop():
544
+ screen, st["screen"] = st["screen"], None
545
+ sink, st["sink"] = st["sink"], None
546
+ if screen is not None:
547
+ screen.close()
548
+ if sink is not None:
549
+ try:
550
+ sink.close()
551
+ except Exception as e:
552
+ print("cast: close:", e)
553
+ go_l.set_text(lv.SYMBOL.PLAY + " Cast")
554
+ go_b.set_style_bg_color(ui.ACCENT, 0)
555
+ msg.set_text("Stopped.")
556
+ info.set_text("")
557
+
558
+ def toggle():
559
+ if st["busy"]:
560
+ return
561
+ if st["sink"] is not None:
562
+ stop()
563
+ elif st.get("shown"):
564
+ start()
565
+ else:
566
+ look()
567
+
568
+ def tick(t=None):
569
+ screen, sink = st["screen"], st["sink"]
570
+ if screen is not None:
571
+ screen.tick()
572
+ if screen.page == "photos" and ticks_diff(ticks_ms(), st.get("flip", 0)) > 0:
573
+ st["flip"] = ticks_add(ticks_ms(), 8000)
574
+ screen.next_photo(1)
575
+ if sink is not None:
576
+ try:
577
+ s = sink.stats()
578
+ info.set_text(
579
+ "%.0f frames a second, %s"
580
+ % (
581
+ s.get("fps", 0),
582
+ ("%d sessions, %d keyframes asked for" % (s.get("sessions", 0), s.get("idr_requests", 0)))
583
+ if "sessions" in s
584
+ else "serving HLS",
585
+ )
586
+ )
587
+ except Exception:
588
+ pass
589
+
590
+ def close():
591
+ if st["sink"] is not None:
592
+ stop()
593
+
594
+ def moved(endpoints):
595
+ # The receiver chosen for the whole device: listed, picked, and cast
596
+ # to now if a cast runs.
597
+ for ep in endpoints:
598
+ host = ep.caps.get("host")
599
+ if not host:
600
+ continue
601
+ kept["host"] = host
602
+ add([{"name": ep.label, "host": host, "kind": ep.caps.get("type") or "roku"}])
603
+ for i, r in enumerate(st["shown"]):
604
+ if r["host"] == host:
605
+ dd.set_selected(i)
606
+ if st["sink"] is not None and not st["busy"]:
607
+ stop()
608
+ start()
609
+ return
610
+
611
+ st["toggle"], st["key"], st["pick"] = toggle, key, dd.set_selected # for the console
612
+ scope.on_close(close)
613
+ _find_rokus(scope)
614
+ moved(scope.follow("cast.target", moved))
615
+ scope.every(1000, tick)
616
+
617
+ def setup(t):
618
+ # Look for receivers once Wi-Fi is up (it may still be joining after a boot).
619
+ if not net.up():
620
+ msg.set_text("Waiting for Wi-Fi (set it up in System if it never comes).")
621
+ return
622
+ if t is not None:
623
+ t.deinit()
624
+ rssi = net.rssi()
625
+ status.set_text("Wi-Fi %s dBm" % rssi if rssi else "Wi-Fi up")
626
+ look()
627
+
628
+ if net.up():
629
+ setup(None)
630
+ else:
631
+ scope.every(500, setup)
apps/cast/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "cast", "title": "Cast", "category": "Video", "icon": "cast", "color": "#6A2C8C"}