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/roku/__init__.py ADDED
@@ -0,0 +1,156 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Roku remote: a TV remote on the touch screen, over the LAN (Roku's ECP).
5
+
6
+ It finds the Rokus on the network (SSDP), lists them by name, and sends
7
+ keys to the one picked: the d-pad and OK, Back, the TV's Home, play/pause,
8
+ rewind and fast forward, volume and mute, and power.
9
+
10
+ Every request runs on ``scope.worker`` and closes its socket when it's
11
+ done, so a TV that doesn't answer never holds the screen up, and nothing is
12
+ left open when the app closes.
13
+
14
+ ``config.json`` in its data folder: ``{"prefer": "Bedroom"}`` picks the TV
15
+ whose name or model holds that text when there are several. The one last
16
+ picked wins while the board stays up.
17
+ """
18
+
19
+ import lvgl as lv
20
+ from appkit import net as appnet
21
+
22
+ from touchkit import ecp, net, settings, ui
23
+
24
+ DEFAULTS = {"prefer": None}
25
+
26
+ state = None # the open remote's state, for the console
27
+
28
+
29
+ def main(scope):
30
+ global state
31
+ try:
32
+ appnet.sockets()
33
+ except ImportError as e:
34
+ # A browser page: no UDP for SSDP, and no TCP to a TV.
35
+ ui.missing(scope, "no sockets", "Roku remote finds TVs with SSDP and talks to them over the network, "
36
+ "and this Python has no sockets: %s. Run it on a board with Wi-Fi or on a computer." % (e,))
37
+ return
38
+ conf = settings.config(scope, DEFAULTS)
39
+ root = scope.root
40
+ status = ui.status(scope)
41
+ W, H = scope.size
42
+ st = state = {"tvs": [], "host": None, "phase": "wifi", "last": None, "sent": 0, "errors": 0}
43
+ kept = scope.store() # the TV last picked, while the board stays up
44
+ w = scope.worker
45
+
46
+ tv_dd = lv.dropdown(root)
47
+ tv_dd.set_width(330)
48
+ tv_dd.set_pos(W - 16 - 330, 10)
49
+ tv_dd.set_options("looking for Rokus...")
50
+ msg = ui.label(root, "", ui.font, ui.MUTED)
51
+ msg.set_pos(W - 16 - 330, 60)
52
+ msg.set_width(330)
53
+
54
+ def send(key, label):
55
+ host = st["host"]
56
+ if host is None:
57
+ msg.set_text("no TV yet")
58
+ return
59
+
60
+ def done(value, error):
61
+ if error is not None:
62
+ st["errors"] += 1
63
+ msg.set_text("%s: %s" % (label, error))
64
+ else:
65
+ st["sent"] += 1
66
+ msg.set_text("sent %s" % label)
67
+
68
+ msg.set_text("%s..." % label)
69
+ st["last"] = key
70
+ w.submit(lambda: ecp.keypress(host, key), done, key="key:" + key)
71
+
72
+ def btn(text, key, x, y, bw=104, bh=72, label=None, color=None):
73
+ b, _ = ui.button(root, text, lambda: send(key, label or key), w=bw, h=bh, font=ui.big, color=color)
74
+ b.set_pos(x, y)
75
+ return b
76
+
77
+ # The d-pad, OK in the middle, Back and the TV's Home above it.
78
+ cx, cy = 190, 210
79
+ btn(lv.SYMBOL.UP, "Up", cx - 52, cy - 128)
80
+ btn(lv.SYMBOL.LEFT, "Left", cx - 52 - 116, cy - 36)
81
+ btn("OK", "Select", cx - 52, cy - 36, label="OK", color=ui.WARN)
82
+ btn(lv.SYMBOL.RIGHT, "Right", cx - 52 + 116, cy - 36)
83
+ btn(lv.SYMBOL.DOWN, "Down", cx - 52, cy + 56)
84
+ btn(lv.SYMBOL.LEFT + " Back", "Back", 24, 12, bw=150, bh=60, label="Back")
85
+ btn(lv.SYMBOL.HOME + " TV", "Home", 206, 12, bw=150, bh=60, label="TV Home")
86
+ # Playback, volume and power on the right.
87
+ rx, ry = 420, 110
88
+ btn(lv.SYMBOL.PREV, "Rev", rx, ry, bw=110, label="Rewind")
89
+ btn(lv.SYMBOL.PLAY + lv.SYMBOL.PAUSE, "Play", rx + 122, ry, bw=110, label="Play/Pause")
90
+ btn(lv.SYMBOL.NEXT, "Fwd", rx + 244, ry, bw=110, label="Fast forward")
91
+ ry += 86
92
+ btn(lv.SYMBOL.VOLUME_MID, "VolumeDown", rx, ry, bw=110, label="Volume down")
93
+ btn(lv.SYMBOL.MUTE, "VolumeMute", rx + 122, ry, bw=110, label="Mute")
94
+ btn(lv.SYMBOL.VOLUME_MAX, "VolumeUp", rx + 244, ry, bw=110, label="Volume up")
95
+ ry += 86
96
+ btn(lv.SYMBOL.POWER + " Power", "Power", rx, ry, bw=232, label="Power", color=ui.BAD)
97
+ btn(lv.SYMBOL.REFRESH, "InstantReplay", rx + 244, ry, bw=110, label="Replay")
98
+
99
+ def show_tvs():
100
+ tvs = st["tvs"]
101
+ if not tvs:
102
+ tv_dd.set_options("no Roku found: tap to look again")
103
+ return
104
+ tv_dd.set_options("\n".join("%s (%s)" % (t["name"], t["host"].rsplit(".", 1)[-1]) for t in tvs))
105
+ i = ecp.pick(tvs, conf.get("prefer"), kept.get("host"))
106
+ if i is None:
107
+ # A preferred TV that isn't on the network: send nothing rather
108
+ # than drive whichever set answered first.
109
+ st["host"] = None
110
+ status.set_text("no TV matches %r: tap to choose" % (conf.get("prefer"),))
111
+ return
112
+ tv_dd.set_selected(i)
113
+ choose(i)
114
+
115
+ def choose(i):
116
+ t = st["tvs"][i]
117
+ st["host"] = t["host"]
118
+ kept["host"] = t["host"]
119
+ status.set_text("%s, %s" % (t["model"] or "Roku", t["power"] or "?"))
120
+
121
+ def dd_changed(e):
122
+ if st["tvs"]:
123
+ choose(tv_dd.get_selected())
124
+ else:
125
+ look()
126
+
127
+ tv_dd.add_event_cb(dd_changed, lv.EVENT.VALUE_CHANGED, None)
128
+
129
+ def look():
130
+ st["phase"] = "discovering"
131
+ msg.set_text("looking for Rokus on the network...")
132
+
133
+ def done(value, error):
134
+ st["phase"] = "ready"
135
+ if error is not None:
136
+ msg.set_text("discovery failed: %s" % (error,))
137
+ st["tvs"] = []
138
+ else:
139
+ st["tvs"] = value
140
+ msg.set_text("%d Roku%s found" % (len(value), "" if len(value) == 1 else "s"))
141
+ show_tvs()
142
+
143
+ w.submit(ecp.discover, done, key="discover")
144
+
145
+ def setup(t):
146
+ if st["phase"] != "wifi":
147
+ t.deinit()
148
+ return
149
+ if net.up():
150
+ rssi = net.rssi()
151
+ status.set_text("Wi-Fi %s dBm" % rssi if rssi else "Wi-Fi up")
152
+ look()
153
+ else:
154
+ msg.set_text("joining Wi-Fi...")
155
+
156
+ scope.every(500, setup)
apps/roku/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "roku", "title": "Roku remote", "category": "Video", "icon": "settings_remote", "color": "#4A3D8C"}
@@ -0,0 +1,362 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Roku camera: the camera live on a Roku TV, cast by Miracast.
5
+
6
+ Pick the TV and press Start. The TV shows the camera full screen and
7
+ nothing else; the board's screen shows only the controls: Start and Stop,
8
+ the TV, the picture's size and frame rate, brightness, night and which way
9
+ up, and how the link is doing. The picture never passes through the screen
10
+ or through Python on the way: on an ESP32-P4 the camera's scaler writes each
11
+ frame straight into the frame the H.264 encoder reads next, and on a
12
+ computer ffmpeg reads the camera (the one chosen in Connections) and encodes
13
+ it. It casts with ``castdisplay.camcast``.
14
+
15
+ The TV needs Screen mirroring on (Settings > System > Screen mirroring), and
16
+ the first cast may ask on the TV to allow it. A TV in standby is woken.
17
+
18
+ ``config.json`` in its data folder holds the settings, and ``tv``, the
19
+ address of the TV picked last: Start casts only to a TV someone picked.
20
+ """
21
+
22
+ import json
23
+ import sys
24
+
25
+ import lvgl as lv
26
+
27
+ from appkit import data
28
+ from appkit import net as appnet
29
+ from touchkit import cam, ecp, net, settings, ui
30
+ from touchkit.clock import sleep_ms
31
+
32
+ CONFIG = "config.json"
33
+ SIZES = ((1280, 720), (960, 544), (640, 352))
34
+ RATES = (15, 20, 25, 30)
35
+ BITRATE = {1280: 4_000_000, 960: 3_000_000, 640: 1_500_000}
36
+ DEFAULTS = {"tv": None, "tv_name": None, "size": 0, "fps": 3, "bright": cam.BRIGHT, "light": 0,
37
+ "flip": False, "mirror": False, "name": None}
38
+
39
+ state = None # the open app's state, for the console
40
+
41
+
42
+ def _encoder_room(width):
43
+ """(largest free block of internal RAM, what the encoder needs), in bytes.
44
+
45
+ The P4's H.264 encoder keeps its reference rows in one block of internal
46
+ RAM, 1,152 bytes for each 16 pixels of width, and nothing else will do.
47
+ On a computer there's no such limit."""
48
+ need = 3 * 16 * 24 * (width // 16) + 128
49
+ try:
50
+ import esp32
51
+
52
+ heaps = esp32.idf_heap_info(esp32.HEAP_DATA)
53
+ except (ImportError, AttributeError):
54
+ return need, need
55
+ return max((h[2] for h in heaps if h[0] < 1_000_000), default=0), need
56
+
57
+
58
+ def _save(scope, conf):
59
+ """Keep the settings for next time. Not while a cast runs: a flash write
60
+ stalls the stream on a board."""
61
+ try:
62
+ with open(data.join(scope.data_dir, CONFIG), "w") as f:
63
+ json.dump({k: conf[k] for k in DEFAULTS}, f)
64
+ except OSError as e:
65
+ print("rokucam: couldn't keep the settings:", e)
66
+
67
+
68
+ def main(scope):
69
+ global state
70
+ board = sys.implementation.name != "cpython"
71
+ try:
72
+ appnet.sockets()
73
+ except ImportError as e:
74
+ # A browser page: no SSDP to find a TV, and no server for it to read the stream from.
75
+ ui.missing(scope, "no sockets", "Roku camera finds TVs with SSDP and serves them the picture over the "
76
+ "network, and this Python has no sockets: %s. Run it on a board with Wi-Fi or on a computer." % (e,))
77
+ return
78
+ try:
79
+ from castdisplay import camcast
80
+ except (ImportError, RuntimeError) as e:
81
+ # On a board the cast is castif's (an ESP32-P4's encoder); on a
82
+ # computer, ffmpeg's. Anything else has no stream to send.
83
+ ui.missing(scope, "missing: castif", "Roku camera needs an ESP32-P4 with castif, or a computer "
84
+ "with ffmpeg, to encode the picture: %s." % (e,))
85
+ return
86
+
87
+ conf = settings.config(scope, DEFAULTS)
88
+ status = ui.status(scope)
89
+ root = scope.root
90
+ W, H = scope.size
91
+ narrow = W < 400
92
+ f_body = ui.small if narrow else ui.font
93
+ f_big = ui.font if narrow else ui.big
94
+ pad = 4 if narrow else 12
95
+ st = state = {"tvs": [], "cast": None, "camera": None, "dirty": False, "error": None}
96
+
97
+ col = lv.obj(root)
98
+ ui.plain(col)
99
+ col.add_flag(lv.obj.FLAG.SCROLLABLE)
100
+ col.set_size(W, H)
101
+ col.set_pos(0, 0)
102
+ col.set_flex_flow(lv.FLEX_FLOW.COLUMN)
103
+ col.set_style_pad_all(pad, 0)
104
+ col.set_style_pad_row(pad, 0)
105
+
106
+ go_b, go_l = ui.button(col, lv.SYMBOL.PLAY + " Start", lambda: toggle(), h=40 if narrow else 72, font=f_big,
107
+ color=ui.ACCENT)
108
+ go_b.set_width(lv.pct(100))
109
+ link = ui.label(col, "", f_body, ui.WHITE)
110
+ link.set_width(lv.pct(100))
111
+ link.set_long_mode(lv.label.LONG_MODE.WRAP)
112
+
113
+ def row(text):
114
+ r = lv.obj(col)
115
+ ui.plain(r)
116
+ r.set_size(lv.pct(100), lv.SIZE_CONTENT)
117
+ r.set_flex_flow(lv.FLEX_FLOW.ROW)
118
+ r.set_flex_align(lv.FLEX_ALIGN.SPACE_BETWEEN, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
119
+ ui.label(r, text, f_body, ui.MUTED)
120
+ return r
121
+
122
+ def dropdown(text, options, selected, changed):
123
+ dd = lv.dropdown(row(text))
124
+ dd.set_style_text_font(f_body, 0)
125
+ dd.set_width(lv.pct(62))
126
+ dd.set_options(options)
127
+ if selected is not None:
128
+ dd.set_selected(selected)
129
+ dd.add_event_cb(lambda e: changed(dd.get_selected()), lv.EVENT.VALUE_CHANGED, None)
130
+ return dd
131
+
132
+ def switch(text, on, changed):
133
+ sw = lv.switch(row(text))
134
+ if on:
135
+ sw.add_state(lv.STATE.CHECKED)
136
+ sw.add_event_cb(lambda e: changed(sw.has_state(lv.STATE.CHECKED)), lv.EVENT.VALUE_CHANGED, None)
137
+ return sw
138
+
139
+ # -- the settings ------------------------------------------------------------
140
+
141
+ def changed(key, value, restart=False, live=None):
142
+ conf[key] = value
143
+ st["dirty"] = True
144
+ if st["cast"] is None:
145
+ keep()
146
+ elif restart:
147
+ stop(quiet=True)
148
+ start()
149
+ elif live is not None:
150
+ live()
151
+
152
+ def keep():
153
+ if st["dirty"] and st["cast"] is None:
154
+ _save(scope, conf)
155
+ st["dirty"] = False
156
+
157
+ def pick_tv(i):
158
+ if not st["tvs"]:
159
+ look()
160
+ return
161
+ if tv_dd.get_option_count() > len(st["tvs"]):
162
+ i -= 1 # the list leads with "Choose a TV" until one is picked
163
+ if i < 0:
164
+ return
165
+ choose(st["tvs"][i])
166
+
167
+ def choose(t):
168
+ conf["tv_name"] = t["name"]
169
+ changed("tv", t["host"], restart=st["cast"] is not None)
170
+ show_tvs()
171
+
172
+ def choose_host(host):
173
+ for t in st["tvs"]:
174
+ if t["host"] == host:
175
+ choose(t)
176
+ return True
177
+ return False
178
+
179
+ tv_dd = dropdown("TV", "looking for Rokus...", None, pick_tv)
180
+ dropdown("Size", "\n".join("%d x %d" % s for s in SIZES), conf["size"] % len(SIZES),
181
+ lambda i: changed("size", i, restart=True))
182
+ dropdown("Frame rate", "\n".join("%d fps" % r for r in RATES), conf["fps"] % len(RATES),
183
+ lambda i: changed("fps", i, restart=True))
184
+
185
+ def controls():
186
+ c = st["camera"]
187
+ if c is not None:
188
+ cam.apply_controls(c, conf)
189
+
190
+ def turned():
191
+ c = st["cast"]
192
+ if c is None:
193
+ return
194
+ if board:
195
+ c.set_turn(conf["flip"], conf["mirror"])
196
+ else: # ffmpeg turns the picture: a new session
197
+ stop(quiet=True)
198
+ start()
199
+
200
+ if board:
201
+ bright = lv.slider(row("Brightness"))
202
+ bright.set_width(lv.pct(55))
203
+ bright.set_range(10, 90)
204
+ bright.set_value(conf["bright"], False)
205
+ bright.add_event_cb(lambda e: changed("bright", bright.get_value(), live=controls), lv.EVENT.RELEASED, None)
206
+ switch("Night", conf["light"], lambda on: changed("light", 1 if on else 0, live=controls))
207
+ switch("Upside down", conf["flip"], lambda on: changed("flip", on, live=turned))
208
+ switch("Mirror", conf["mirror"], lambda on: changed("mirror", on, live=turned))
209
+
210
+ # -- the TVs -----------------------------------------------------------------
211
+
212
+ def show_tvs():
213
+ tvs = st["tvs"]
214
+ if not tvs:
215
+ tv_dd.set_options("no Roku found: tap to look again")
216
+ return
217
+ names = ["%s (%s)" % (t["name"], t["host"].rsplit(".", 1)[-1]) for t in tvs]
218
+ i = ecp.pick(tvs, None, conf["tv"]) if conf["tv"] else None
219
+ if i is not None and tvs[i]["host"] != conf["tv"]:
220
+ i = None
221
+ if i is None:
222
+ # Nothing is cast to a TV nobody picked.
223
+ tv_dd.set_options("\n".join(["Choose a TV"] + names))
224
+ tv_dd.set_selected(0)
225
+ return
226
+ tv_dd.set_options("\n".join(names))
227
+ tv_dd.set_selected(i)
228
+
229
+ def look():
230
+ link.set_text("Looking for Rokus...")
231
+
232
+ def done(value, error):
233
+ if error is not None:
234
+ link.set_text("Couldn't look for Rokus: %s" % (error,))
235
+ st["tvs"] = []
236
+ else:
237
+ st["tvs"] = value
238
+ say()
239
+ show_tvs()
240
+
241
+ scope.worker.submit(ecp.discover, done, key="discover")
242
+
243
+ # -- the cast ----------------------------------------------------------------
244
+
245
+ def open_camera():
246
+ if board:
247
+ c = cam.open_camera({"flip": False, "mirror": False}) # turned in the scaler instead
248
+ cam.apply_controls(c, conf)
249
+ st["camera"] = c
250
+ return c
251
+ import hostcamera
252
+
253
+ return hostcamera.ffmpeg_input(hostcamera.device(), size=(1280, 720), fps=RATES[conf["fps"] % len(RATES)])
254
+
255
+ def start():
256
+ host = conf["tv"]
257
+ if not host:
258
+ link.set_text("Choose a TV first.")
259
+ return
260
+ w, h = SIZES[conf["size"] % len(SIZES)]
261
+ have, need = _encoder_room(w)
262
+ if have < need:
263
+ link.set_text("The video encoder needs %d KB of internal memory in one piece, and the biggest "
264
+ "free piece is %d KB. Restart the board, then start." % (need // 1024, have // 1024))
265
+ return
266
+ keep()
267
+ try:
268
+ ecp.keypress(host, "PowerOn") # wakes one in standby; one that's on ignores it
269
+ except OSError:
270
+ pass
271
+ try:
272
+ camera = open_camera()
273
+ st["cast"] = camcast.CameraCast(host, camera, w, h, fps=RATES[conf["fps"] % len(RATES)],
274
+ bitrate=BITRATE[w], flip=conf["flip"], mirror=conf["mirror"],
275
+ name=conf["name"] or None, log=print)
276
+ except Exception as e:
277
+ close_camera()
278
+ st["error"] = str(e)
279
+ link.set_text("Couldn't start: %s" % (e,))
280
+ return
281
+ st["error"] = None
282
+ go_l.set_text(lv.SYMBOL.STOP + " Stop")
283
+ go_b.set_style_bg_color(ui.BAD, 0)
284
+ if board:
285
+ scope.foreground(frame)
286
+ say()
287
+
288
+ def frame():
289
+ c = st["cast"]
290
+ if c is not None and not c.step(timeout=60):
291
+ sleep_ms(3) # the encoder still has the last frame, or the TV isn't playing yet
292
+
293
+ def close_camera():
294
+ c, st["camera"] = st["camera"], None
295
+ if c is not None:
296
+ c.deinit()
297
+
298
+ def stop(quiet=False):
299
+ if board:
300
+ scope.foreground(None)
301
+ c, st["cast"] = st["cast"], None
302
+ if c is not None:
303
+ try:
304
+ c.close()
305
+ except Exception as e:
306
+ print("rokucam: close:", e)
307
+ close_camera()
308
+ go_l.set_text(lv.SYMBOL.PLAY + " Start")
309
+ go_b.set_style_bg_color(ui.ACCENT, 0)
310
+ if not quiet:
311
+ keep()
312
+ link.set_text("Stopped.")
313
+
314
+ def toggle():
315
+ if st["cast"] is not None:
316
+ stop()
317
+ else:
318
+ start()
319
+
320
+ def tv_name():
321
+ return conf["tv_name"] or conf["tv"] or "the TV"
322
+
323
+ def say(t=None):
324
+ c = st["cast"]
325
+ if c is None:
326
+ if st["error"] is None and t is None:
327
+ n = len(st["tvs"])
328
+ link.set_text("%d Roku%s found. %s" % (n, "" if n == 1 else "s",
329
+ "Press Start." if conf["tv"] else "Choose a TV, then press Start."))
330
+ return
331
+ s = c.stats()
332
+ w, h = c.width, c.height
333
+ if s["playing"]:
334
+ link.set_text("Casting to %s\n%d x %d, %.1f fps" % (tv_name(), w, h, s["fps"]))
335
+ status.set_text("%.0f fps" % s["fps"])
336
+ elif s["sessions"] > 1 and c.result:
337
+ link.set_text("%s didn't answer (%s): trying again..." % (tv_name(), c.result))
338
+ else:
339
+ link.set_text("Connecting to %s..." % tv_name())
340
+
341
+ def close():
342
+ if st["cast"] is not None:
343
+ stop()
344
+
345
+ st["start"], st["stop"], st["choose"] = start, stop, choose_host # for the console
346
+ scope.on_close(close)
347
+ scope.every(1000, say)
348
+
349
+ def setup(t):
350
+ if not net.up():
351
+ link.set_text("Waiting for Wi-Fi (set it up in System if it never comes).")
352
+ return
353
+ if t is not None:
354
+ t.deinit()
355
+ rssi = net.rssi()
356
+ status.set_text("Wi-Fi %s dBm" % rssi if rssi else "Wi-Fi up")
357
+ look()
358
+
359
+ if net.up():
360
+ setup(None)
361
+ else:
362
+ scope.every(500, setup)
apps/rokucam/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "rokucam", "title": "Roku camera", "category": "Video", "icon": "live_tv", "color": "#5B2C8C", "needs": ["castif|subprocess"]}