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.
- apps/__init__.py +5 -0
- apps/about/__init__.py +257 -0
- apps/about/app.json +1 -0
- apps/about/credits.py +12 -0
- apps/about/facts.py +398 -0
- apps/about/fonts/OFL.txt +93 -0
- apps/about/fonts/latin1_14.bin +0 -0
- apps/about/fonts/latin1_16.bin +0 -0
- apps/about/fonts/latin1_24.bin +0 -0
- apps/about/fonts/latin1_32.bin +0 -0
- apps/about/fonts/latin1_40.bin +0 -0
- apps/backline/__init__.py +58 -0
- apps/backline/app.json +1 -0
- apps/backline/app.py +475 -0
- apps/backline/art.py +717 -0
- apps/backline/ask.py +781 -0
- apps/backline/box.py +502 -0
- apps/backline/cast.py +137 -0
- apps/backline/desk/__init__.py +329 -0
- apps/backline/desk/editor.py +331 -0
- apps/backline/desk/grid.py +154 -0
- apps/backline/desk/look.py +169 -0
- apps/backline/desk/pedals.py +102 -0
- apps/backline/desk/perform.py +157 -0
- apps/backline/desk/prompt.py +210 -0
- apps/backline/desk/strip.py +159 -0
- apps/backline/desk_main.py +226 -0
- apps/backline/engine.py +622 -0
- apps/backline/floor.py +509 -0
- apps/backline/footswitch.py +119 -0
- apps/backline/glyphs.py +82 -0
- apps/backline/kits.py +428 -0
- apps/backline/midipedal.py +362 -0
- apps/backline/page_data.py +413 -0
- apps/backline/parts.py +145 -0
- apps/backline/pedal.py +107 -0
- apps/backline/remote.py +134 -0
- apps/backline/songs/ballad_68.json +1 -0
- apps/backline/songs/bossa.json +1 -0
- apps/backline/songs/country_train.json +1 -0
- apps/backline/songs/funk.json +1 -0
- apps/backline/songs/metal.json +1 -0
- apps/backline/songs/pop.json +1 -0
- apps/backline/songs/rock.json +1 -0
- apps/backline/songs/shuffle.json +1 -0
- apps/battery/__init__.py +101 -0
- apps/battery/app.json +1 -0
- apps/bit_world/act_deck.py +683 -0
- apps/bit_world/act_game.py +762 -0
- apps/bit_world/act_pet.py +1032 -0
- apps/bit_world/app.json +1 -0
- apps/bit_world/bit_output.py +45 -0
- apps/bit_world/bit_palette.py +503 -0
- apps/bit_world/bit_sound.py +431 -0
- apps/bit_world/bit_sprites.py +632 -0
- apps/bit_world/bit_world.py +534 -0
- apps/btreceiver/__init__.py +690 -0
- apps/btreceiver/app.json +1 -0
- apps/btreceiver/chime.py +155 -0
- apps/btreceiver/demo.py +529 -0
- apps/btreceiver/screen.py +633 -0
- apps/camera/__init__.py +316 -0
- apps/camera/app.json +1 -0
- apps/card/__init__.py +113 -0
- apps/card/app.json +1 -0
- apps/cast/__init__.py +631 -0
- apps/cast/app.json +1 -0
- apps/cast/receivers.py +193 -0
- apps/chess/__init__.py +18 -0
- apps/chess/app.json +1 -0
- apps/chess/app.py +922 -0
- apps/chess/art.py +323 -0
- apps/chess/ble.py +411 -0
- apps/chess/engine.py +925 -0
- apps/chess/game.py +327 -0
- apps/chess/net.py +261 -0
- apps/clock/__init__.py +38 -0
- apps/clock/app.json +1 -0
- apps/detect/__init__.py +214 -0
- apps/detect/app.json +1 -0
- apps/fmsynth/__init__.py +41 -0
- apps/fmsynth/algorithms.py +61 -0
- apps/fmsynth/app.json +1 -0
- apps/fmsynth/app.py +355 -0
- apps/fmsynth/editor.py +206 -0
- apps/fmsynth/engine.py +257 -0
- apps/fmsynth/meters.py +262 -0
- apps/fmsynth/midi.py +375 -0
- apps/fmsynth/panel_view.py +1071 -0
- apps/fmsynth/patches/fmsynth-01.syx +0 -0
- apps/fmsynth/settings.py +138 -0
- apps/fmsynth/voice.py +398 -0
- apps/gamepad/__init__.py +685 -0
- apps/gamepad/app.json +1 -0
- apps/gamepad/pad.py +94 -0
- apps/gamepad/watch.py +352 -0
- apps/hidden/__init__.py +72 -0
- apps/hidden/app.json +1 -0
- apps/home/__init__.py +483 -0
- apps/home/app.json +1 -0
- apps/home/main.py +36 -0
- apps/homeassistant/__init__.py +213 -0
- apps/homeassistant/api.py +104 -0
- apps/homeassistant/app.json +1 -0
- apps/huey/__init__.py +287 -0
- apps/huey/acting.py +184 -0
- apps/huey/actor.py +194 -0
- apps/huey/app.json +1 -0
- apps/huey/client.py +125 -0
- apps/huey/servers.py +121 -0
- apps/huey/settings.example.json +8 -0
- apps/huey/settings.py +97 -0
- apps/huey/turn.py +504 -0
- apps/huey/voice.py +217 -0
- apps/info/__init__.py +810 -0
- apps/info/app.json +1 -0
- apps/lora/__init__.py +62 -0
- apps/lora/app.json +1 -0
- apps/manifest.py +71 -0
- apps/medialink_baby_monitor/__init__.py +222 -0
- apps/medialink_baby_monitor/app.json +1 -0
- apps/medialink_doorbell/__init__.py +251 -0
- apps/medialink_doorbell/app.json +1 -0
- apps/medialink_hands_free/__init__.py +217 -0
- apps/medialink_hands_free/app.json +1 -0
- apps/medialink_surveillance_camera/__init__.py +258 -0
- apps/medialink_surveillance_camera/app.json +1 -0
- apps/medialink_video_doorbell/__init__.py +289 -0
- apps/medialink_video_doorbell/app.json +1 -0
- apps/medialink_videophone/__init__.py +273 -0
- apps/medialink_videophone/app.json +1 -0
- apps/medialink_walkie_talkie/__init__.py +228 -0
- apps/medialink_walkie_talkie/app.json +1 -0
- apps/midicontroller/__init__.py +774 -0
- apps/midicontroller/app.json +1 -0
- apps/midicontroller/links.py +421 -0
- apps/midicontroller/surface.py +343 -0
- apps/midiengine/__init__.py +977 -0
- apps/midiengine/app.json +1 -0
- apps/midiengine/banks.py +85 -0
- apps/midiengine/midiin.py +165 -0
- apps/midiengine/player.py +392 -0
- apps/midiengine/umidiparser.py +1413 -0
- apps/mp3/__init__.py +240 -0
- apps/mp3/app.json +1 -0
- apps/perch/__init__.py +119 -0
- apps/perch/app.json +1 -0
- apps/perch/huey.py +481 -0
- apps/perch/huey_props.py +386 -0
- apps/perch/huey_shift.py +42 -0
- apps/perch/huey_views.py +296 -0
- apps/perch/scene_bench.py +680 -0
- apps/perch/scene_coast.py +479 -0
- apps/perch/scene_desk.py +492 -0
- apps/perch/stage.py +1274 -0
- apps/perch/vec.py +366 -0
- apps/repl/__init__.py +325 -0
- apps/repl/app.json +1 -0
- apps/roku/__init__.py +156 -0
- apps/roku/app.json +1 -0
- apps/rokucam/__init__.py +362 -0
- apps/rokucam/app.json +1 -0
- apps/security/__init__.py +261 -0
- apps/security/app.json +1 -0
- apps/settings/__init__.py +221 -0
- apps/settings/app.json +1 -0
- apps/shell/__init__.py +33 -0
- apps/shell/app.json +1 -0
- apps/spotify/__init__.py +669 -0
- apps/spotify/app.json +1 -0
- apps/spotify/screen.py +82 -0
- apps/steps/__init__.py +58 -0
- apps/steps/app.json +1 -0
- apps/stompbox/__init__.py +31 -0
- apps/stompbox/app.json +1 -0
- apps/stompbox/app.py +668 -0
- apps/stompbox/boards.json +20 -0
- apps/stompbox/effects.py +241 -0
- apps/stompbox/engine.py +340 -0
- apps/stompbox/footswitch.py +331 -0
- apps/stompbox/inputs.py +449 -0
- apps/stompbox/meter.py +72 -0
- apps/stompbox/outputs.py +135 -0
- apps/stompbox/pedals.txt +45 -0
- apps/stompbox/presets.py +106 -0
- apps/stompbox/styles.py +135 -0
- apps/stompbox/ui.py +817 -0
- apps/stompbox/wavfile.py +73 -0
- apps/synthstation/__init__.py +20 -0
- apps/synthstation/app.json +1 -0
- apps/synthstation/audio.py +397 -0
- apps/synthstation/catalog.py +50 -0
- apps/synthstation/meters.py +136 -0
- apps/synthstation/midi.py +380 -0
- apps/synthstation/pads.py +177 -0
- apps/synthstation/parts.py +128 -0
- apps/synthstation/station.py +1069 -0
- apps/system/__init__.py +11 -0
- apps/system/app.json +1 -0
- apps/touchcal/__init__.py +324 -0
- apps/touchcal/app.json +1 -0
- apps/touchcal/fit.py +262 -0
- apps/wake/__init__.py +268 -0
- apps/wake/app.json +1 -0
- apps/wake/feed.py +155 -0
- apps/watch/__init__.py +355 -0
- apps/watch/app.json +1 -0
- apps/watch/faces.py +35 -0
- apps/watch/wheel.py +157 -0
- apps/weather/__init__.py +278 -0
- apps/weather/app.json +1 -0
- apps/weather/icons.py +172 -0
- apps/weather/sizing.py +71 -0
- apps/weather/zippad.py +172 -0
- omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
- omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
- omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
- omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Hands free: talk to another device without a button.
|
|
5
|
+
|
|
6
|
+
Each side listens to its own microphone. When someone starts to speak, that
|
|
7
|
+
side takes the turn and sends, from a moment before the first word so the
|
|
8
|
+
word isn't clipped. When they stop, the turn goes back. One side talks at a
|
|
9
|
+
time: while the other is talking, this one plays and doesn't listen, so it
|
|
10
|
+
doesn't send the other side's voice back.
|
|
11
|
+
|
|
12
|
+
The screen shows whose turn it is and the state of the link to the peer.
|
|
13
|
+
medialink.VoiceActivity decides what counts as speech: audiodsp's speech
|
|
14
|
+
detector where the firmware has it, and a level gate over the room's own
|
|
15
|
+
noise elsewhere. Set WAKE_WORD to a microWakeWord model's manifest and a turn
|
|
16
|
+
starts only with the word, so the room's talk stays home. That needs
|
|
17
|
+
firmware with tflite and audiodsp. The model is looked for in a ``models``
|
|
18
|
+
folder, as the Wake app does: ``/sd/models`` on the SD card (mounted just
|
|
19
|
+
while it loads), ``/models`` on flash, then ``/data/models``. The model files, and where to download them, are at
|
|
20
|
+
https://github.com/PyDevices/apps/blob/main/docs/reference/models.md
|
|
21
|
+
|
|
22
|
+
On a computer it uses the sound input and output chosen in the picker, and moves
|
|
23
|
+
to another when you choose one while it runs.
|
|
24
|
+
|
|
25
|
+
Needs: a microphone and a speaker that boarddev.pcm_in() and pcm_out()
|
|
26
|
+
reach, which an OmniPython board package or omnipython-desktop provides, and a
|
|
27
|
+
network: a board already on Wi-Fi with mdns in its firmware, or
|
|
28
|
+
python-zeroconf on CPython. Headphones on a computer keep its speaker out of
|
|
29
|
+
its microphone. A piece that is missing is named on the screen, and the rest
|
|
30
|
+
of the app keeps running.
|
|
31
|
+
|
|
32
|
+
To open it, put this app in the apps folder of an app set, run appkit.run()
|
|
33
|
+
from its main.py, and choose "Hands free" on the home screen.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
import medialink
|
|
37
|
+
import multimer
|
|
38
|
+
from appkit import connections, places
|
|
39
|
+
from audiodev import AudioFormat
|
|
40
|
+
from touchkit import ui
|
|
41
|
+
|
|
42
|
+
# Change these to match your setup.
|
|
43
|
+
NAME = "hands-free-1" # this side's name on the network
|
|
44
|
+
PEER = "hands-free-2" # the other side's
|
|
45
|
+
PEER_ADDRESS = None # or ("192.168.1.20", 47830) to skip the search
|
|
46
|
+
PORT = 47830 # this side's port
|
|
47
|
+
RATE = 16000 # samples a second
|
|
48
|
+
HOLD_MS = 800 # how long a turn lasts after the last word
|
|
49
|
+
WAKE_WORD = None # or "hey_jarvis.json", from a models folder, to start a turn by name
|
|
50
|
+
|
|
51
|
+
TICK_MS = 100 # how often the screen and the link are checked
|
|
52
|
+
LOOK_EVERY_MS = 3000 # between searches for the peer while it's gone
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _wake_word(voice):
|
|
56
|
+
"""The wake word in front of *voice*, its model from a ``models`` folder."""
|
|
57
|
+
card = places.mount_card()
|
|
58
|
+
try:
|
|
59
|
+
path = places.find("models", WAKE_WORD)
|
|
60
|
+
if path is None:
|
|
61
|
+
raise OSError("%s is in none of %s" % (WAKE_WORD, places.where_text("models")))
|
|
62
|
+
return medialink.WakeWord(path, RATE, voice=voice)
|
|
63
|
+
finally:
|
|
64
|
+
places.unmount_card(card)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def main(scope):
|
|
68
|
+
scr = scope.root
|
|
69
|
+
status = ui.status(scope)
|
|
70
|
+
status.set_text("starting")
|
|
71
|
+
|
|
72
|
+
# The audio and the network. Each piece that fails is named on the screen
|
|
73
|
+
# and the app runs without it.
|
|
74
|
+
intercom = link = finder = None
|
|
75
|
+
try:
|
|
76
|
+
audio = AudioFormat(RATE, 1, 16)
|
|
77
|
+
sound = connections.Sound(scope, audio, on_error=status.set_text)
|
|
78
|
+
voice = medialink.VoiceActivity(RATE, hold_ms=HOLD_MS)
|
|
79
|
+
if WAKE_WORD:
|
|
80
|
+
voice = _wake_word(voice)
|
|
81
|
+
link = medialink.Link(medialink.UDPTransport(PORT), retries=2)
|
|
82
|
+
intercom = medialink.Intercom(link, sound.open_mic(), sound.open_speaker(), voice=voice)
|
|
83
|
+
sound.attach(intercom)
|
|
84
|
+
except Exception as e:
|
|
85
|
+
ui.missing(
|
|
86
|
+
scope,
|
|
87
|
+
"no microphone, speaker or network port",
|
|
88
|
+
"Hands free needs a microphone and a speaker that boarddev reaches, "
|
|
89
|
+
"and a UDP port %d: %s." % (PORT, e),
|
|
90
|
+
)
|
|
91
|
+
print("hands free: audio or network:", e)
|
|
92
|
+
if link is not None:
|
|
93
|
+
link.close()
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
heading = ui.label(scr, "Hands free", ui.big, ui.WHITE)
|
|
97
|
+
heading.set_pos(40, 40)
|
|
98
|
+
who = ui.label(scr, "", ui.font, ui.MUTED)
|
|
99
|
+
who.set_pos(40, 110)
|
|
100
|
+
turn = ui.label(scr, "Quiet", ui.huge, ui.ACCENT)
|
|
101
|
+
turn.set_pos(40, 170)
|
|
102
|
+
peer = ui.label(scr, "", ui.big, ui.WHITE)
|
|
103
|
+
peer.set_pos(40, 290)
|
|
104
|
+
note = ui.label(scr, "", ui.small, ui.MUTED)
|
|
105
|
+
note.set_pos(40, 350)
|
|
106
|
+
note.set_width(scope.size[0] - 80)
|
|
107
|
+
|
|
108
|
+
try:
|
|
109
|
+
finder = medialink.MDNS()
|
|
110
|
+
finder.advertise(NAME, PORT)
|
|
111
|
+
finder_note = ""
|
|
112
|
+
except Exception as e:
|
|
113
|
+
if finder is not None:
|
|
114
|
+
finder.deinit()
|
|
115
|
+
finder = None
|
|
116
|
+
finder_note = "No mDNS here (%s), so the peer is found only at PEER_ADDRESS." % (e,)
|
|
117
|
+
print("hands free: mdns:", e)
|
|
118
|
+
|
|
119
|
+
who.set_text("This is %r, deciding by %s" % (NAME, voice.detector))
|
|
120
|
+
note.set_text(finder_note)
|
|
121
|
+
print("This is %r, deciding by %s; looking for %r" % (NAME, voice.detector, PEER))
|
|
122
|
+
|
|
123
|
+
# The search runs on the worker, so the link check doesn't wait on it. The
|
|
124
|
+
find_key = "hands-free-find"
|
|
125
|
+
searching = {"on": False}
|
|
126
|
+
|
|
127
|
+
def close():
|
|
128
|
+
try:
|
|
129
|
+
intercom.close()
|
|
130
|
+
except Exception as e:
|
|
131
|
+
print("hands free: intercom close:", e)
|
|
132
|
+
link.close()
|
|
133
|
+
|
|
134
|
+
scope.on_close(close)
|
|
135
|
+
if finder is not None:
|
|
136
|
+
scope.on_close(finder.deinit)
|
|
137
|
+
|
|
138
|
+
st = {"looked": multimer.ticks_add(multimer.ticks_ms(), -LOOK_EVERY_MS - 1),
|
|
139
|
+
"was_alive": False, "was_floor": medialink.IDLE}
|
|
140
|
+
|
|
141
|
+
def connect(peers):
|
|
142
|
+
for _name, address in peers:
|
|
143
|
+
try:
|
|
144
|
+
link.connect(address)
|
|
145
|
+
except medialink.PeerUnreachable:
|
|
146
|
+
pass
|
|
147
|
+
|
|
148
|
+
def found(value, error):
|
|
149
|
+
searching["on"] = False
|
|
150
|
+
if error is not None:
|
|
151
|
+
print("hands free: search:", error)
|
|
152
|
+
return
|
|
153
|
+
connect(value)
|
|
154
|
+
|
|
155
|
+
def look_for_peer():
|
|
156
|
+
"""Find the peer and say hello. Either side may find the other first."""
|
|
157
|
+
if PEER_ADDRESS:
|
|
158
|
+
connect([(PEER, PEER_ADDRESS)])
|
|
159
|
+
elif finder is not None and not searching["on"]:
|
|
160
|
+
searching["on"] = True
|
|
161
|
+
scope.worker.submit(lambda: finder.find(PEER, timeout=1), found, key=find_key)
|
|
162
|
+
|
|
163
|
+
def check(t):
|
|
164
|
+
# The link and the floor, shown on the screen and printed as they change.
|
|
165
|
+
if not link.alive and multimer.ticks_diff(multimer.ticks_ms(), st["looked"]) > LOOK_EVERY_MS:
|
|
166
|
+
look_for_peer()
|
|
167
|
+
st["looked"] = multimer.ticks_ms()
|
|
168
|
+
if link.alive != st["was_alive"]:
|
|
169
|
+
st["was_alive"] = link.alive
|
|
170
|
+
print("Peer", "here" if link.alive else "gone: waiting for it")
|
|
171
|
+
if link.alive:
|
|
172
|
+
peer.set_text("Peer %s is here" % (PEER,))
|
|
173
|
+
status.set_text("connected to %s" % (PEER,))
|
|
174
|
+
else:
|
|
175
|
+
peer.set_text("Waiting for %s" % (PEER,))
|
|
176
|
+
status.set_text("waiting for %s" % (PEER,))
|
|
177
|
+
|
|
178
|
+
if link.floor != st["was_floor"] and shown["mic"] is None:
|
|
179
|
+
st["was_floor"] = link.floor
|
|
180
|
+
if st["was_floor"] == medialink.TALKING:
|
|
181
|
+
print("You're talking")
|
|
182
|
+
turn.set_text("You're talking")
|
|
183
|
+
elif st["was_floor"] == medialink.LISTENING:
|
|
184
|
+
print("The other side is talking")
|
|
185
|
+
turn.set_text("They're talking")
|
|
186
|
+
else:
|
|
187
|
+
print("Quiet")
|
|
188
|
+
turn.set_text("Quiet")
|
|
189
|
+
|
|
190
|
+
shown = {"mic": None}
|
|
191
|
+
|
|
192
|
+
def no_mic(why):
|
|
193
|
+
# The microphone wouldn't open (none here, or the browser was told no):
|
|
194
|
+
# this side still hears the other, and says so.
|
|
195
|
+
if shown["mic"] is None:
|
|
196
|
+
shown["mic"] = why
|
|
197
|
+
print("hands free: no microphone:", why)
|
|
198
|
+
turn.set_text("No microphone")
|
|
199
|
+
turn.set_style_text_color(ui.BAD, 0)
|
|
200
|
+
note.set_text("The microphone wouldn't open (%s), so this side hears the other but can't talk." % (why,))
|
|
201
|
+
|
|
202
|
+
def step():
|
|
203
|
+
# The audio loop: move one step, then give the others a moment when
|
|
204
|
+
# nobody is talking. The program's main loop calls it over and over.
|
|
205
|
+
try:
|
|
206
|
+
intercom.service()
|
|
207
|
+
except OSError as e:
|
|
208
|
+
# An Intercom without mic_error raises instead: stop it listening.
|
|
209
|
+
intercom.voice = None
|
|
210
|
+
no_mic(str(e))
|
|
211
|
+
if getattr(intercom, "mic_error", None):
|
|
212
|
+
no_mic(intercom.mic_error)
|
|
213
|
+
if not intercom.talking:
|
|
214
|
+
multimer.sleep_ms(2)
|
|
215
|
+
|
|
216
|
+
scope.every(TICK_MS, check)
|
|
217
|
+
scope.foreground(step)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "medialink_hands_free", "title": "Hands free", "category": "Link", "icon": "phone_in_talk", "color": "#3B6E2A"}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Watch and listen to a camera in another room.
|
|
5
|
+
|
|
6
|
+
Open it from the appkit launcher on two devices. The one with the camera
|
|
7
|
+
keeps CAMERA = True: it sends what it sees and hears for as long as the
|
|
8
|
+
other side watches. The other keeps CAMERA = False: it finds the camera by
|
|
9
|
+
name, shows the pictures on its left-hand side and plays the sound. Sound
|
|
10
|
+
goes out between a picture's packets, so a big picture doesn't hold it up.
|
|
11
|
+
|
|
12
|
+
The status line and the panel say what each side is doing: how many
|
|
13
|
+
pictures went or came, how fast, how late they were and, on the watching
|
|
14
|
+
side, how often the speaker ran dry. The same lines go to the console.
|
|
15
|
+
|
|
16
|
+
Needs: on the camera side, a camera that board_peripherals.camera() opens
|
|
17
|
+
(an ESP32-P4 with cameraif), or on a computer a camera that ffmpeg can open (hostcamera), or a folder of JPEG files named
|
|
18
|
+
by PICTURES, and a microphone boarddev.pcm_in() reaches. On the watching
|
|
19
|
+
side, a display in board_config with jpegio (built into the firmware, or
|
|
20
|
+
Pillow on CPython) and a speaker. Both on one network, with mdns: a board
|
|
21
|
+
already on Wi-Fi, or a computer with python-zeroconf.
|
|
22
|
+
|
|
23
|
+
On a computer the camera side's microphone and the watching side's speaker are
|
|
24
|
+
the sound input and output chosen in the picker, and move when you choose
|
|
25
|
+
another while the app runs.
|
|
26
|
+
|
|
27
|
+
What is missing is named on screen, and the rest of the app keeps running.
|
|
28
|
+
Change the settings at the top of this file and open the app again.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from appkit import connections
|
|
32
|
+
from touchkit import ui
|
|
33
|
+
|
|
34
|
+
# Change these to match your setup.
|
|
35
|
+
CAMERA = True # True on the side with the camera, False on the side that watches
|
|
36
|
+
CAMERA_NAME = "porch-camera" # the camera side's name on the network
|
|
37
|
+
CAMERA_ADDRESS = None # or ("192.168.1.20", PORT) to skip the search
|
|
38
|
+
PORT = 47800
|
|
39
|
+
PICTURES = None # a folder of JPEG files to send in place of a camera
|
|
40
|
+
FPS = 10 # pictures a second, at most
|
|
41
|
+
SIZE = "VGA" # a camera's picture size: "QVGA" is 320x240, "VGA" 640x480
|
|
42
|
+
QUALITY = 60 # 1 to 100; lower is smaller and quicker to send
|
|
43
|
+
RATE = 16000 # sound samples a second
|
|
44
|
+
STATS_MS = 5000 # how often the numbers are refreshed
|
|
45
|
+
LOOK_MS = 3000 # how often the watching side looks for the camera
|
|
46
|
+
PICTURE_SHARE = 2 / 3 # the part of the app's area the pictures take
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def main(scope):
|
|
50
|
+
# The libraries come in here, not at import time, so a missing one is
|
|
51
|
+
# shown on screen rather than failing the launcher.
|
|
52
|
+
try:
|
|
53
|
+
import medialink
|
|
54
|
+
import multimer
|
|
55
|
+
from audiodev import AudioFormat
|
|
56
|
+
except ImportError as e:
|
|
57
|
+
ui.missing(scope, "missing: medialink", "Surveillance camera needs medialink, boarddev, multimer and audiodev: %s." % (e,))
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
scr = scope.root
|
|
61
|
+
status = ui.status(scope)
|
|
62
|
+
rect = scope.rect or (0, 0, scope.size[0], scope.size[1])
|
|
63
|
+
status.set_text("starting")
|
|
64
|
+
|
|
65
|
+
# A camera side needs a camera; a watching side needs a display.
|
|
66
|
+
camera = None
|
|
67
|
+
if CAMERA:
|
|
68
|
+
if PICTURES:
|
|
69
|
+
camera = medialink.FileCamera(PICTURES)
|
|
70
|
+
else:
|
|
71
|
+
# A board can have cameraif and a board_peripherals with no camera
|
|
72
|
+
# in it; that is "no camera" too.
|
|
73
|
+
try:
|
|
74
|
+
try:
|
|
75
|
+
import board_peripherals
|
|
76
|
+
import cameraif
|
|
77
|
+
except ImportError:
|
|
78
|
+
# A computer: the camera chosen in the picker, moving when it changes.
|
|
79
|
+
camera = connections.HostCamera(scope, size=SIZE, quality=QUALITY, on_error=status.set_text)
|
|
80
|
+
else:
|
|
81
|
+
# Have the chip scale each picture down and encode it, so it is
|
|
82
|
+
# quick to send and the other side decodes it in hardware.
|
|
83
|
+
camera = board_peripherals.camera()
|
|
84
|
+
camera.reconfigure(pixel_format=cameraif.PixelFormat.JPEG, frame_size=getattr(cameraif.FrameSize, SIZE))
|
|
85
|
+
camera.quality = QUALITY
|
|
86
|
+
except Exception as e:
|
|
87
|
+
print("surveillance camera: camera:", e)
|
|
88
|
+
if camera is not None:
|
|
89
|
+
camera.deinit()
|
|
90
|
+
ui.missing(scope, "no camera", "No camera here. The camera side needs an ESP32-P4 board with a camera, a computer with ffmpeg and a camera, or a folder of JPEG files set as PICTURES.")
|
|
91
|
+
return
|
|
92
|
+
else:
|
|
93
|
+
try:
|
|
94
|
+
from board_config import display_drv
|
|
95
|
+
except ImportError as e:
|
|
96
|
+
ui.missing(scope, "no display", "No display here: the watching side needs a display in board_config. %s" % (e,))
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
# Open the link, with sound on it one way and pictures the same way.
|
|
100
|
+
link = None
|
|
101
|
+
video = None
|
|
102
|
+
try:
|
|
103
|
+
link = medialink.Link(medialink.UDPTransport(PORT), retries=2)
|
|
104
|
+
video = medialink.Video(link)
|
|
105
|
+
except Exception as e:
|
|
106
|
+
ui.missing(scope, "no network", "No network port here: %s. Connect to Wi-Fi, or use a computer with a free UDP port %d." % (e, PORT))
|
|
107
|
+
if link is not None:
|
|
108
|
+
link.close()
|
|
109
|
+
return
|
|
110
|
+
|
|
111
|
+
# Each thing that's missing is named in the notes; the rest still runs.
|
|
112
|
+
notes_text = []
|
|
113
|
+
|
|
114
|
+
# Find and be found by name. Without mdns, CAMERA_ADDRESS still works.
|
|
115
|
+
finder = None
|
|
116
|
+
try:
|
|
117
|
+
finder = medialink.MDNS()
|
|
118
|
+
except Exception as e:
|
|
119
|
+
notes_text.append("no mdns, so no search: %s" % (e,))
|
|
120
|
+
print("surveillance camera: mdns:", e)
|
|
121
|
+
|
|
122
|
+
# The sound: the camera side's microphone, or the watching side's speaker.
|
|
123
|
+
intercom = None
|
|
124
|
+
try:
|
|
125
|
+
audio = AudioFormat(RATE, 1, 16)
|
|
126
|
+
sound = connections.Sound(scope, audio, mic=CAMERA, speaker=not CAMERA, on_error=status.set_text)
|
|
127
|
+
if CAMERA:
|
|
128
|
+
intercom = medialink.Intercom(link, sound.open_mic(), None)
|
|
129
|
+
else:
|
|
130
|
+
intercom = medialink.Intercom(link, None, sound.open_speaker())
|
|
131
|
+
sound.attach(intercom)
|
|
132
|
+
except Exception as e:
|
|
133
|
+
notes_text.append("no sound: %s" % (e,))
|
|
134
|
+
print("surveillance camera: sound:", e)
|
|
135
|
+
|
|
136
|
+
# The pictures take the left part of the area; the status is on the right.
|
|
137
|
+
picture_w = int(rect[2] * PICTURE_SHARE)
|
|
138
|
+
screen = None
|
|
139
|
+
if not CAMERA:
|
|
140
|
+
try:
|
|
141
|
+
screen = medialink.Screen(display_drv, rect=(rect[0], rect[1], picture_w, rect[3]))
|
|
142
|
+
except Exception as e:
|
|
143
|
+
notes_text.append("no picture decoder (jpegio): %s" % (e,))
|
|
144
|
+
print("surveillance camera: screen:", e)
|
|
145
|
+
|
|
146
|
+
# The status on the right: the numbers, as the console gets them.
|
|
147
|
+
left = picture_w + 16 if not CAMERA else 16
|
|
148
|
+
info = ui.label(scr, "", ui.small, ui.WHITE)
|
|
149
|
+
info.set_pos(left, 16)
|
|
150
|
+
info.set_width(max(120, rect[2] - left - 16))
|
|
151
|
+
notes = ui.label(scr, "\n".join(notes_text), ui.tiny, ui.MUTED)
|
|
152
|
+
notes.set_pos(left, rect[3] - 80)
|
|
153
|
+
notes.set_width(max(120, rect[2] - left - 16))
|
|
154
|
+
|
|
155
|
+
def say(line):
|
|
156
|
+
info.set_text(line)
|
|
157
|
+
print("surveillance camera:", line)
|
|
158
|
+
|
|
159
|
+
if CAMERA:
|
|
160
|
+
if finder is not None:
|
|
161
|
+
finder.advertise(CAMERA_NAME, PORT)
|
|
162
|
+
status.set_text("waiting to be watched")
|
|
163
|
+
say("This is %r, waiting to be watched" % CAMERA_NAME)
|
|
164
|
+
else:
|
|
165
|
+
status.set_text("looking for the camera")
|
|
166
|
+
say("Looking for %r" % CAMERA_NAME)
|
|
167
|
+
|
|
168
|
+
# The search runs on the worker, so the picture doesn't wait on it. The
|
|
169
|
+
find_key = "surveillance-find"
|
|
170
|
+
searching = {"on": False}
|
|
171
|
+
|
|
172
|
+
def connect(peers):
|
|
173
|
+
for _name, address in peers:
|
|
174
|
+
try:
|
|
175
|
+
link.connect(address)
|
|
176
|
+
status.set_text("watching")
|
|
177
|
+
say("Watching %r at %s" % (CAMERA_NAME, address))
|
|
178
|
+
except medialink.PeerUnreachable:
|
|
179
|
+
pass
|
|
180
|
+
|
|
181
|
+
def found(value, error):
|
|
182
|
+
searching["on"] = False
|
|
183
|
+
if error is not None:
|
|
184
|
+
print("surveillance camera: search:", error)
|
|
185
|
+
return
|
|
186
|
+
connect(value)
|
|
187
|
+
|
|
188
|
+
def look_for_camera():
|
|
189
|
+
"""Find the camera by name and say hello to it."""
|
|
190
|
+
if CAMERA_ADDRESS:
|
|
191
|
+
connect([(CAMERA_NAME, CAMERA_ADDRESS)])
|
|
192
|
+
elif finder is not None and not searching["on"]:
|
|
193
|
+
searching["on"] = True
|
|
194
|
+
scope.worker.submit(lambda: finder.find(CAMERA_NAME, timeout=1), found, key=find_key)
|
|
195
|
+
|
|
196
|
+
def close():
|
|
197
|
+
# Release what the app opened, in the order the example does.
|
|
198
|
+
if intercom is not None:
|
|
199
|
+
intercom.close()
|
|
200
|
+
link.close()
|
|
201
|
+
if camera is not None:
|
|
202
|
+
camera.deinit()
|
|
203
|
+
|
|
204
|
+
scope.on_close(close)
|
|
205
|
+
if finder is not None:
|
|
206
|
+
scope.on_close(finder.deinit)
|
|
207
|
+
|
|
208
|
+
now = multimer.ticks_ms()
|
|
209
|
+
st = {"looked": now, "taken": now, "reported": now, "counted": 0}
|
|
210
|
+
|
|
211
|
+
def step():
|
|
212
|
+
# One pass of the example's loop, run by the program's main loop.
|
|
213
|
+
now = multimer.ticks_ms()
|
|
214
|
+
if CAMERA:
|
|
215
|
+
# Talk whenever someone watches, and take a picture whenever the
|
|
216
|
+
# last one has gone and the next is due.
|
|
217
|
+
if intercom is not None:
|
|
218
|
+
if link.alive and not intercom.talking:
|
|
219
|
+
try:
|
|
220
|
+
intercom.press()
|
|
221
|
+
except medialink.PeerUnreachable:
|
|
222
|
+
pass
|
|
223
|
+
elif intercom.talking and not link.alive:
|
|
224
|
+
intercom.release()
|
|
225
|
+
if link.alive and video.ready and multimer.ticks_diff(now, st["taken"]) >= 1000 // FPS:
|
|
226
|
+
st["taken"] = now
|
|
227
|
+
jpeg = camera.take()
|
|
228
|
+
if jpeg is not None:
|
|
229
|
+
video.send(bytes(jpeg), st["taken"])
|
|
230
|
+
video.service()
|
|
231
|
+
else:
|
|
232
|
+
# Look for the camera while it isn't there, and show each picture.
|
|
233
|
+
if not link.alive and multimer.ticks_diff(now, st["looked"]) > LOOK_MS:
|
|
234
|
+
look_for_camera()
|
|
235
|
+
st["looked"] = multimer.ticks_ms()
|
|
236
|
+
frame = video.receive()
|
|
237
|
+
if frame is not None and screen is not None:
|
|
238
|
+
screen.show(frame.payload)
|
|
239
|
+
if intercom is not None:
|
|
240
|
+
intercom.service()
|
|
241
|
+
|
|
242
|
+
# Every few seconds, say how it is going.
|
|
243
|
+
if multimer.ticks_diff(now, st["reported"]) >= STATS_MS:
|
|
244
|
+
total = video.stats["sent" if CAMERA else "received"]
|
|
245
|
+
rate = (total - st["counted"]) * 1000 / multimer.ticks_diff(now, st["reported"])
|
|
246
|
+
if CAMERA:
|
|
247
|
+
say("%d pictures sent, %.1f a second" % (total, rate))
|
|
248
|
+
else:
|
|
249
|
+
latency = (intercom.stats["latency_ms"] or 0) if intercom is not None else 0
|
|
250
|
+
underruns = intercom.stats["underruns"] if intercom is not None else 0
|
|
251
|
+
say("%d pictures shown, %.1f a second, %s ms late at most; sound %s ms late, %d underruns"
|
|
252
|
+
% (total, rate, (video.stats["max_latency_ms"] or 0), latency, underruns))
|
|
253
|
+
video.stats["max_latency_ms"] = None
|
|
254
|
+
st["counted"], st["reported"] = total, now
|
|
255
|
+
if intercom is None or not intercom.talking:
|
|
256
|
+
multimer.sleep_ms(1)
|
|
257
|
+
|
|
258
|
+
scope.foreground(step)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "medialink_surveillance_camera", "title": "Surveillance camera", "category": "Link", "icon": "camera_outdoor", "color": "#6A2C8C"}
|