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/info/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "info", "title": "Info", "category": "System", "icon": "info", "color": "#3E6B5A"}
apps/lora/__init__.py ADDED
@@ -0,0 +1,62 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """LoRa: the radio's identity, a test packet, and listening for activity.
5
+
6
+ It needs a board with a ``lora`` role (an SX1262, as on the T-Watch S3). Set
7
+ ``LORA_MHZ`` to a frequency your region allows before you send: 915 MHz
8
+ suits the Americas, Europe uses 868 and much of Asia 433. The radio has
9
+ power while the app is open, where the board can switch it.
10
+ """
11
+
12
+ import sys
13
+
14
+ import lvgl as lv
15
+
16
+ from touchkit import ui
17
+
18
+ LORA_MHZ = 915.0
19
+ LORA_DBM = 10
20
+
21
+
22
+ def main(scope):
23
+ board = sys.modules.get("board_config")
24
+ if board is None or "lora" not in getattr(board, "PERIPHERALS", ()):
25
+ ui.missing(scope, "No LoRa radio", "This board has no LoRa radio.")
26
+ return
27
+ peripherals = sys.modules.get("board_peripherals")
28
+ release = getattr(peripherals, "release", None)
29
+ if release is not None:
30
+ scope.on_close(lambda: release("lora")) # its power off again when you leave
31
+ try:
32
+ radio = board.lora
33
+ radio.configure(LORA_MHZ, sf=9, power_dbm=LORA_DBM)
34
+ except Exception as e:
35
+ ui.missing(scope, "LoRa radio not answering", "The radio didn't answer: %r" % (e,))
36
+ return
37
+ root = scope.root
38
+ root.set_flex_flow(lv.FLEX_FLOW.COLUMN)
39
+ root.set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
40
+ root.set_style_pad_row(8, 0)
41
+ root.remove_flag(lv.obj.FLAG.SCROLLABLE)
42
+ text = ui.label(root, "%s\nat %.1f MHz" % (radio.version, LORA_MHZ), ui.small, ui.WHITE)
43
+ text.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
44
+ sent = [0]
45
+
46
+ def send():
47
+ sent[0] += 1
48
+ try:
49
+ ms = radio.send(b"PyDevices %d" % sent[0])
50
+ text.set_text("%s\nsent #%d, %d ms on air\nat %.1f MHz" % (radio.version, sent[0], ms, LORA_MHZ))
51
+ except Exception as e:
52
+ text.set_text("send failed: %r" % (e,))
53
+
54
+ def listen():
55
+ try:
56
+ busy = radio.channel_active()
57
+ text.set_text("%s\nthe channel is %s\nat %.1f MHz" % (radio.version, "busy" if busy else "quiet", LORA_MHZ))
58
+ except Exception as e:
59
+ text.set_text("listening failed: %r" % (e,))
60
+
61
+ ui.button(root, "Send", send, color=ui.PANEL)
62
+ ui.button(root, "Listen", listen, color=ui.PANEL)
apps/lora/app.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "lora", "title": "LoRa", "category": "Link", "icon": "cell_tower", "color": "#6A4C93"}
apps/manifest.py ADDED
@@ -0,0 +1,71 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """The apps as a MicroPython manifest: freeze every app, or the ones you choose.
5
+
6
+ From a firmware's manifest::
7
+
8
+ include("path/to/apps/apps") # every app
9
+ include("path/to/apps/apps", apps="home,clock") # these, and the apps they import from
10
+
11
+ or name this folder in omnipython-builds' ``build_mp.py --modules all,path/to/apps/apps``.
12
+
13
+ The shared libraries in ``lib/`` (appkit and the rest) are frozen with them,
14
+ from the repository's own ``manifest.py``; ``libs=False`` leaves them out for a
15
+ build that brings its own. The apps are frozen as the package ``frozen_apps``,
16
+ and Home lists them beside any in ``/apps``. What they need from elsewhere,
17
+ LVGL, icons and ttfio, the instruments, comes from the firmware's other
18
+ modules: an app whose module is missing says so when it starts.
19
+
20
+ Each app's ``app.json`` is checked here, and its icon against the icons
21
+ package's names, which this finds in ``$OMNIPYTHON_LIBRARY`` (build_mp.py
22
+ sets it) or on the build's import path. ``index_dir=`` says where the index is
23
+ written (default: a temporary folder). ``native=False`` takes the
24
+ ``@micropython.native`` decorators out of the frozen copies, for a port with
25
+ no native code emitter; it's the default for the Windows and webassembly
26
+ ports when build_mp.py names the target.
27
+ """
28
+
29
+ import importlib.util
30
+ import os
31
+ import sys
32
+
33
+ if 0:
34
+
35
+ def include(*args, **kwargs):
36
+ pass
37
+
38
+ def module(*args, **kwargs):
39
+ pass
40
+
41
+ def package(*args, **kwargs):
42
+ pass
43
+
44
+ class options: # noqa: N801
45
+ apps = libs = index_dir = native = None
46
+
47
+ def defaults(**kwargs):
48
+ pass
49
+
50
+
51
+ _target = os.environ.get("OMNIPYTHON_TARGET", "")
52
+ options.defaults(apps=None, libs=True, index_dir=None, native=_target.split("/")[0] not in ("windows", "webassembly"))
53
+
54
+ _here = os.getcwd() # include() runs a manifest from its own folder
55
+ if options.libs:
56
+ include(os.path.dirname(_here))
57
+
58
+ _spec = importlib.util.spec_from_file_location(
59
+ "apps_freeze_index", os.path.join(os.path.dirname(_here), "lib", "appkit", "index.py")
60
+ )
61
+ _index = importlib.util.module_from_spec(_spec)
62
+ _spec.loader.exec_module(_index)
63
+ _tree = os.environ.get("OMNIPYTHON_LIBRARY")
64
+ _icons = os.path.join(_tree, "icons") if _tree else None
65
+ if _icons and os.path.isdir(_icons):
66
+ sys.path.insert(0, _icons)
67
+ try:
68
+ _index.freeze_apps(_here, module, package, options.index_dir, options.apps, options.native)
69
+ finally:
70
+ if _icons in sys.path:
71
+ sys.path.remove(_icons)
@@ -0,0 +1,222 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Listen to a room from another one, like a baby monitor.
5
+
6
+ Open it on two devices. On the monitor beside the cot it listens all the
7
+ time but sends only while the room makes a sound, a cry, a voice, a thud
8
+ or a bump, and goes quiet again a moment after it stops. On the listener, where you
9
+ are, it plays what comes and shows "Sound" while it plays and "Quiet" when
10
+ it stops, so you can see at a glance which it is.
11
+
12
+ The monitor decides when the room makes a sound with
13
+ medialink.VoiceActivity: audiodsp's speech detector where the firmware has
14
+ it, and a level gate over the room's own noise elsewhere. It sends every
15
+ sound, not just voices, since a thud from the cot matters as much as a cry.
16
+
17
+ On a computer it uses the sound input (the monitor) or output (the listener)
18
+ chosen in the picker, and moves to another when you choose one while it runs.
19
+
20
+ Needs: on the monitor, a microphone that boarddev.pcm_in() reaches; on the
21
+ listener, a speaker that boarddev.pcm_out() reaches. An OmniPython board
22
+ package or omnipython-desktop provides both. Both need a network: a board
23
+ already on Wi-Fi with mdns in its firmware, or python-zeroconf on CPython.
24
+ Set PEER_ADDRESS on the listener to skip the search. A device that lacks
25
+ any of these says so on screen and the app stays open.
26
+
27
+ Open it from the appkit launcher: pick "Baby monitor" on the home screen.
28
+ Set ROLE on each device first, since the same app runs on both.
29
+ """
30
+
31
+ from appkit import connections
32
+ from touchkit import ui
33
+
34
+ # Change these to match your setup.
35
+ ROLE = "monitor" # "monitor" in the room to hear, "listener" where you are
36
+ NAME = "baby-monitor" # the monitor's name on the network
37
+ PEER_ADDRESS = None # on the listener, ("192.168.1.20", 47810) skips the search
38
+ PORT = 47810 # this side's port
39
+ RATE = 16000 # samples a second
40
+ HOLD_MS = 1500 # how long the monitor keeps sending after the last sound
41
+
42
+
43
+ def room_voice(medialink):
44
+ """Return the monitor's VoiceActivity, which starts a turn on every sound.
45
+
46
+ Hands-free calls start a turn only on a voice; here a thud or a bump is
47
+ worth hearing too. A medialink from before ``speech_only`` sends every
48
+ sound already.
49
+ """
50
+ try:
51
+ return medialink.VoiceActivity(RATE, hold_ms=HOLD_MS, speech_only=False)
52
+ except TypeError:
53
+ return medialink.VoiceActivity(RATE, hold_ms=HOLD_MS)
54
+
55
+
56
+ def main(scope):
57
+ try:
58
+ import medialink
59
+ import multimer
60
+ from audiodev import AudioFormat
61
+ except ImportError as e:
62
+ ui.missing(
63
+ scope,
64
+ "missing: medialink",
65
+ "The baby monitor needs medialink, multimer and audiodev: %s." % (e,),
66
+ )
67
+ return
68
+
69
+ scr = scope.root
70
+ status = ui.status(scope)
71
+ status.set_text("starting")
72
+
73
+ # The screen: what this device is doing, the link, and the sound.
74
+ if ROLE == "monitor":
75
+ role_text = "Monitor %r in the room" % NAME
76
+ else:
77
+ role_text = "Listening for %r" % NAME
78
+ role = ui.label(scr, role_text, ui.big, ui.WHITE)
79
+ role.set_pos(40, 40)
80
+ link_line = ui.label(scr, "Not connected: waiting", ui.font, ui.MUTED)
81
+ link_line.set_pos(40, 100)
82
+ sound_line = ui.label(scr, "Quiet", ui.huge, ui.ACCENT)
83
+ sound_line.set_pos(40, 170)
84
+ note = ui.label(scr, "", ui.font, ui.WARN)
85
+ note.set_pos(40, 300)
86
+ note.set_width(scope.size[0] - 80)
87
+
88
+ # The link and the search for the other side. Each may be missing here.
89
+ try:
90
+ link = medialink.Link(medialink.UDPTransport(PORT), retries=2)
91
+ except Exception as e:
92
+ note.set_text("No network port here: %s" % (e,))
93
+ return
94
+
95
+ finder = None
96
+ try:
97
+ finder = medialink.MDNS()
98
+ if ROLE == "monitor":
99
+ finder.advertise(NAME, PORT)
100
+ except Exception as e:
101
+ finder = None
102
+ note.set_text("No name search here, so set PEER_ADDRESS: %s" % (e,))
103
+
104
+ # The sound: a microphone on the monitor, a speaker on the listener.
105
+ # Without one, the screen says so and nothing else runs.
106
+ audio = AudioFormat(RATE, 1, 16)
107
+ sound = connections.Sound(scope, audio, mic=ROLE == "monitor", speaker=ROLE != "monitor", on_error=status.set_text)
108
+ intercom = None
109
+ try:
110
+ if ROLE == "monitor":
111
+ voice = room_voice(medialink)
112
+ intercom = medialink.Intercom(link, sound.open_mic(), None, voice=voice)
113
+ role.set_text("Monitor %r, deciding by %s" % (NAME, voice.detector))
114
+ else:
115
+ intercom = medialink.Intercom(link, None, sound.open_speaker())
116
+ sound.attach(intercom)
117
+ except Exception as e:
118
+ what = "microphone" if ROLE == "monitor" else "speaker"
119
+ sound_line.set_text("No " + what)
120
+ note.set_text("No %s here: %s" % (what, e))
121
+ sound_line.set_style_text_color(ui.BAD, 0)
122
+
123
+ def close():
124
+ if intercom is not None:
125
+ intercom.close()
126
+ link.close()
127
+
128
+ scope.on_close(close)
129
+ if finder is not None:
130
+ scope.on_close(finder.deinit)
131
+
132
+ if intercom is None:
133
+ status.set_text("no %s" % ("microphone" if ROLE == "monitor" else "speaker"))
134
+ return
135
+
136
+ # The search runs on the worker, so the sound doesn't wait on it. The
137
+ find_key = "baby-monitor-find"
138
+ searching = {"on": False}
139
+
140
+ def connect(peers):
141
+ for _name, address in peers:
142
+ try:
143
+ link.connect(address)
144
+ except medialink.PeerUnreachable:
145
+ pass
146
+
147
+ def found(value, error):
148
+ searching["on"] = False
149
+ if error is not None:
150
+ print("baby monitor: search:", error)
151
+ return
152
+ connect(value)
153
+
154
+ def look_for_monitor():
155
+ """Find the monitor and say hello to it."""
156
+ if PEER_ADDRESS:
157
+ connect([(NAME, PEER_ADDRESS)])
158
+ elif finder is not None and not searching["on"]:
159
+ searching["on"] = True
160
+ scope.worker.submit(lambda: finder.find(NAME, timeout=1), found, key=find_key)
161
+
162
+ # The link runs in the main loop, one pass at a time, so the screen
163
+ # stays responsive while audio moves.
164
+ st = {
165
+ "looked": multimer.ticks_add(multimer.ticks_ms(), -5000),
166
+ "alive": None,
167
+ "sound": False,
168
+ "started": multimer.ticks_ms(),
169
+ "mic": None,
170
+ }
171
+
172
+ def no_mic(why):
173
+ # The microphone wouldn't open (none here, or the browser was told no).
174
+ if st["mic"] is None:
175
+ st["mic"] = why
176
+ print("baby monitor: no microphone:", why)
177
+ sound_line.set_text("No microphone")
178
+ sound_line.set_style_text_color(ui.BAD, 0)
179
+ note.set_text("The microphone wouldn't open: %s" % (why,))
180
+
181
+ def step():
182
+ if ROLE != "monitor" and not link.alive:
183
+ if multimer.ticks_diff(multimer.ticks_ms(), st["looked"]) > 3000:
184
+ look_for_monitor()
185
+ st["looked"] = multimer.ticks_ms()
186
+ if link.alive != st["alive"]:
187
+ st["alive"] = link.alive
188
+ status.set_text("connected" if st["alive"] else "not connected: waiting")
189
+ link_line.set_text("Connected" if st["alive"] else "Not connected: waiting")
190
+ try:
191
+ intercom.service()
192
+ except OSError as e:
193
+ # An Intercom without mic_error raises instead: stop it listening.
194
+ intercom.voice = None
195
+ no_mic(str(e))
196
+ if getattr(intercom, "mic_error", None):
197
+ no_mic(intercom.mic_error)
198
+ if st["mic"] is not None:
199
+ multimer.sleep_ms(2)
200
+ return
201
+
202
+ # On the monitor a turn is its own sending; on the listener, the
203
+ # monitor's.
204
+ if ROLE == "monitor":
205
+ sound = intercom.talking
206
+ else:
207
+ sound = link.floor == medialink.LISTENING
208
+ if sound != st["sound"]:
209
+ st["sound"] = sound
210
+ if sound:
211
+ st["started"] = multimer.ticks_ms()
212
+ sound_line.set_text("Sound")
213
+ sound_line.set_style_text_color(ui.GOOD, 0)
214
+ else:
215
+ heard = multimer.ticks_diff(multimer.ticks_ms(), st["started"])
216
+ sound_line.set_text("Quiet")
217
+ sound_line.set_style_text_color(ui.ACCENT, 0)
218
+ note.set_text("Last sound lasted %d ms" % heard)
219
+ if not intercom.talking:
220
+ multimer.sleep_ms(2)
221
+
222
+ scope.foreground(step)
@@ -0,0 +1 @@
1
+ {"name": "medialink_baby_monitor", "title": "Baby monitor", "category": "Link", "icon": "child_care", "color": "#1E6F8C"}
@@ -0,0 +1,251 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Doorbell: ring a house over the network, then talk to whoever answers.
5
+
6
+ Open Doorbell from the launcher on each side. Set ROLE in this file first:
7
+ "door" outside, "house" inside. A press of the door's button rings a chime
8
+ in the house. For a minute after that the door is hands-free: the visitor
9
+ just speaks, and the door sends while they do. Inside, hold the house's
10
+ button to answer, like a walkie-talkie. One side talks at a time.
11
+
12
+ The ring travels as a medialink event, which the link sends again until the
13
+ house says it arrived, so a lost packet doesn't lose a visitor.
14
+
15
+ On a computer it uses the sound input and output chosen in the picker, and moves
16
+ to another when you choose one while it runs.
17
+
18
+ Needs: a microphone and a speaker that boarddev.pcm_in() and pcm_out() reach,
19
+ which an OmniPython board package or omnipython-desktop provides, and a
20
+ network: a board on Wi-Fi with mdns in its firmware, or python-zeroconf on CPython.
21
+ On a computer, hold any key or a mouse button in the window. Without a
22
+ microphone, speaker, network port or mDNS, the screen says which one is
23
+ missing and the app stays open.
24
+ """
25
+
26
+ import math
27
+
28
+ import events
29
+ import lvgl as lv
30
+ import medialink
31
+ import multimer
32
+ from appkit import connections
33
+ from audiodev import AudioFormat
34
+ from touchkit import ui
35
+
36
+ # Change these to match your setup.
37
+ ROLE = "door" # "door" outside, "house" inside
38
+ HOUSE = "doorbell-house" # the house's name on the network
39
+ HOUSE_ADDRESS = None # on the door, ("192.168.1.20", 47820) skips the search
40
+ PORT = 47820 # this side's port
41
+ BUTTON_PIN = None # a board's button GPIO, pressed when low; None for the window
42
+ RATE = 16000 # samples a second
43
+ TALK_S = 60 # how long the door stays hands-free after a ring
44
+
45
+
46
+ def chime():
47
+ """Two falling notes that fade, as 16-bit PCM."""
48
+ pcm = bytearray()
49
+ for freq, seconds in ((660, 0.35), (520, 0.6)):
50
+ count = int(RATE * seconds)
51
+ for n in range(count):
52
+ level = int(9000 * (1 - n / count) * math.sin(2 * math.pi * freq * n / RATE))
53
+ pcm += (level & 0xFFFF).to_bytes(2, "little")
54
+ return bytes(pcm)
55
+
56
+
57
+ def _close(thing):
58
+ """Close a device that may or may not be open, without raising."""
59
+ if thing is None:
60
+ return
61
+ for name in ("close", "deinit"):
62
+ method = getattr(thing, name, None)
63
+ if method is not None:
64
+ try:
65
+ method()
66
+ except Exception:
67
+ pass
68
+ return
69
+
70
+
71
+ def main(scope):
72
+ # Open the microphone and the speaker at one format: mono, 16-bit.
73
+ audio = AudioFormat(RATE, 1, 16)
74
+ sound = connections.Sound(scope, audio)
75
+ try:
76
+ mic = sound.open_mic()
77
+ except Exception as e:
78
+ ui.missing(scope, "no microphone", "Doorbell needs a microphone: %s." % (e,))
79
+ return
80
+ try:
81
+ speaker = sound.open_speaker()
82
+ except Exception as e:
83
+ _close(mic)
84
+ ui.missing(scope, "no speaker", "Doorbell needs a speaker: %s." % (e,))
85
+ return
86
+
87
+ try:
88
+ link = medialink.Link(medialink.UDPTransport(PORT), retries=3)
89
+ except Exception as e:
90
+ _close(speaker)
91
+ _close(mic)
92
+ ui.missing(scope, "no network port", "Doorbell couldn't use port %d: %s." % (PORT, e))
93
+ return
94
+
95
+ # The button is a pin on a board, or a key or mouse button in a window.
96
+ # The window's events arrive through the app's subscriptions.
97
+ pad = {"held": False}
98
+ if BUTTON_PIN is not None:
99
+ from machine import Pin
100
+
101
+ pin = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
102
+
103
+ def held():
104
+ return not pin.value()
105
+
106
+ else:
107
+ def down(event):
108
+ pad["held"] = True
109
+
110
+ def up(event):
111
+ pad["held"] = False
112
+
113
+ scope.on((events.KEYDOWN, events.MOUSEBUTTONDOWN), down)
114
+ scope.on((events.KEYUP, events.MOUSEBUTTONUP), up)
115
+
116
+ def held():
117
+ return pad["held"]
118
+
119
+ try:
120
+ finder = medialink.MDNS()
121
+ except Exception as e:
122
+ finder = None
123
+ print("doorbell: no mDNS: %s" % (e,))
124
+
125
+ voice = medialink.VoiceActivity(RATE) if ROLE == "door" else None
126
+ intercom = medialink.Intercom(link, mic, speaker)
127
+ sound.attach(intercom)
128
+ ding_dong = None
129
+ if ROLE == "house":
130
+ if finder is not None:
131
+ finder.advertise(HOUSE, PORT)
132
+ ding_dong = chime()
133
+
134
+ # The screen: the link's state on the status line, and what just
135
+ # happened in the middle.
136
+ scr = scope.root
137
+ status = ui.status(scope)
138
+ sound.on_error = status.set_text
139
+ status.set_text("This is the %s" % ROLE)
140
+ ui.label(scr, "Doorbell: %s" % ROLE, ui.big, ui.WHITE).align(lv.ALIGN.TOP_LEFT, 24, 80)
141
+ if ROLE == "door":
142
+ hint = "Press the button to ring the house."
143
+ else:
144
+ hint = "A ring plays a chime. Hold the button to answer."
145
+ if finder is None and HOUSE_ADDRESS is None:
146
+ hint = "No mDNS here: set HOUSE_ADDRESS to reach the house."
147
+ ui.label(scr, hint, ui.font, ui.MUTED).align(lv.ALIGN.TOP_LEFT, 24, 124)
148
+ msg = ui.label(scr, "", ui.big, ui.ACCENT)
149
+ msg.align(lv.ALIGN.CENTER, 0, 0)
150
+
151
+ def show(text):
152
+ msg.set_text(text)
153
+
154
+ # The search runs on the worker, so the screen doesn't wait on it. The
155
+ find_key = "doorbell-find"
156
+ searching = {"on": False}
157
+
158
+ def connect(peers):
159
+ for _name, address in peers:
160
+ try:
161
+ link.connect(address)
162
+ except medialink.PeerUnreachable:
163
+ pass
164
+
165
+ def found(value, error):
166
+ searching["on"] = False
167
+ if error is not None:
168
+ print("doorbell: search:", error)
169
+ return
170
+ connect(value)
171
+
172
+ def look_for_house():
173
+ """Find the house and say hello to it."""
174
+ if HOUSE_ADDRESS:
175
+ connect([(HOUSE, HOUSE_ADDRESS)])
176
+ elif finder is not None and not searching["on"]:
177
+ searching["on"] = True
178
+ scope.worker.submit(lambda: finder.find(HOUSE, timeout=1), found, key=find_key)
179
+
180
+ st = {
181
+ "looked": multimer.ticks_add(multimer.ticks_ms(), -5000),
182
+ "rang": None,
183
+ "held": False,
184
+ "alive": None,
185
+ "heard": 0,
186
+ }
187
+
188
+ def close():
189
+ intercom.close()
190
+ link.close()
191
+
192
+ scope.on_close(close)
193
+ if finder is not None:
194
+ scope.on_close(finder.deinit)
195
+
196
+ def step():
197
+ """One pass of the loop: the door's button, the house's chime and
198
+ button, and the audio copy. The program's main loop calls it."""
199
+ if scope.closed:
200
+ return
201
+ if ROLE == "door" and not link.alive:
202
+ if multimer.ticks_diff(multimer.ticks_ms(), st["looked"]) > 3000:
203
+ look_for_house()
204
+ st["looked"] = multimer.ticks_ms()
205
+ if link.alive != st["alive"]:
206
+ st["alive"] = link.alive
207
+ status.set_text("Connected" if st["alive"] else "Not connected: waiting")
208
+ pressed = held()
209
+
210
+ # At the door, a press rings, and the door listens for a while.
211
+ if ROLE == "door":
212
+ if pressed and not st["held"] and link.alive:
213
+ try:
214
+ link.send_event("ring")
215
+ show("Ring! Speak when you are ready")
216
+ st["rang"] = multimer.ticks_ms()
217
+ intercom.voice = voice
218
+ except medialink.PeerUnreachable:
219
+ show("The house didn't answer")
220
+ if st["rang"] is not None and multimer.ticks_diff(multimer.ticks_ms(), st["rang"]) > TALK_S * 1000:
221
+ st["rang"] = None
222
+ intercom.voice = None
223
+ voice.reset()
224
+
225
+ # In the house, a ring plays the chime, and the button talks back.
226
+ else:
227
+ for event in link.events():
228
+ if event == b"ring":
229
+ show("Ding dong: someone is at the door")
230
+ intercom.play(ding_dong)
231
+ if pressed and not st["held"] and link.alive and not intercom.talking:
232
+ try:
233
+ if intercom.press():
234
+ show("Talking to the door...")
235
+ else:
236
+ show("The door is talking")
237
+ except medialink.PeerUnreachable:
238
+ show("No answer from the door")
239
+ elif not pressed and intercom.talking:
240
+ intercom.release()
241
+ st["held"] = pressed
242
+ intercom.service()
243
+
244
+ # When the other side's turn ends, say so.
245
+ if intercom.stats["spurts"] != st["heard"] and link.floor == medialink.IDLE:
246
+ st["heard"] = intercom.stats["spurts"]
247
+ show("Heard %s" % ("the visitor" if ROLE == "house" else "the house"))
248
+ if not intercom.talking:
249
+ multimer.sleep_ms(2)
250
+
251
+ scope.foreground(step)
@@ -0,0 +1 @@
1
+ {"name": "medialink_doorbell", "title": "Doorbell", "category": "Link", "icon": "doorbell", "color": "#8C3B1E"}