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,289 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Video doorbell: see who is at the door, and talk to them.
|
|
5
|
+
|
|
6
|
+
Open it on two devices. On the one at the door, with a camera, leave
|
|
7
|
+
DOOR = True: it advertises itself on the network and sends a picture of
|
|
8
|
+
what its camera sees a few times a second. On the one inside, with a
|
|
9
|
+
screen, set DOOR = False: it finds the door by name and shows its pictures.
|
|
10
|
+
Each side has a button to hold while talking, as on a walkie-talkie: the
|
|
11
|
+
visitor presses the doorbell to speak, and you press yours to answer.
|
|
12
|
+
|
|
13
|
+
The inside side says when the visitor starts talking, and every few
|
|
14
|
+
seconds how many pictures it has shown and how late they were.
|
|
15
|
+
|
|
16
|
+
Needs: at the door, a camera that board_peripherals.camera() opens (an
|
|
17
|
+
ESP32-P4 with cameraif), or on a computer a camera that ffmpeg can open (hostcamera), or a folder of JPEG files named by
|
|
18
|
+
PICTURES. Inside, a display and a JPEG decoder (jpegio, built into the
|
|
19
|
+
firmware, or Pillow on CPython). On both, a microphone and a speaker
|
|
20
|
+
boarddev reaches, and a network with mdns. On a computer, hold the space
|
|
21
|
+
bar or a mouse button in the window that opens. Whatever is missing shows
|
|
22
|
+
on the screen, and the app keeps running with the rest.
|
|
23
|
+
|
|
24
|
+
On a computer it uses the sound input and output chosen in the picker, and
|
|
25
|
+
moves to another when you choose one while it runs.
|
|
26
|
+
|
|
27
|
+
Open it from the appkit launcher: choose "Video doorbell" on its home
|
|
28
|
+
screen.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
import events
|
|
32
|
+
import lvgl as lv
|
|
33
|
+
from appkit import connections
|
|
34
|
+
from touchkit import ui
|
|
35
|
+
from multimer import sleep_ms, ticks_diff, ticks_ms
|
|
36
|
+
|
|
37
|
+
# Change these to match your setup.
|
|
38
|
+
DOOR = True # True at the door, False inside
|
|
39
|
+
DOOR_NAME = "front-door" # the door's name on the network
|
|
40
|
+
DOOR_ADDRESS = None # or ("192.168.1.20", PORT) to skip the search
|
|
41
|
+
PORT = 47800
|
|
42
|
+
BUTTON_PIN = None # a board's button GPIO, pressed when low; None for the window
|
|
43
|
+
PICTURES = None # a folder of JPEG files to send in place of a camera
|
|
44
|
+
FPS = 10 # pictures a second, at most
|
|
45
|
+
SIZE = "VGA" # a camera's picture size: "QVGA" is 320x240, "VGA" 640x480
|
|
46
|
+
QUALITY = 60 # 1 to 100; lower is smaller and quicker to send
|
|
47
|
+
RATE = 16000 # sound samples a second
|
|
48
|
+
|
|
49
|
+
FIND_MS = 3000 # how often, inside, to look for the door while it's not linked
|
|
50
|
+
REPORT_MS = 5000 # how often, inside, to say how the pictures are coming
|
|
51
|
+
TICK_MS = 10 # how often the loop checks the button and the link
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def main(scope):
|
|
55
|
+
# Pictures and sound need medialink; without it there is nothing to run.
|
|
56
|
+
try:
|
|
57
|
+
import medialink
|
|
58
|
+
from audiodev import AudioFormat
|
|
59
|
+
except ImportError as e:
|
|
60
|
+
ui.missing(
|
|
61
|
+
scope,
|
|
62
|
+
"missing: medialink",
|
|
63
|
+
"The video doorbell needs medialink, boarddev and audiodev: %s." % (e,),
|
|
64
|
+
)
|
|
65
|
+
return
|
|
66
|
+
|
|
67
|
+
scr = scope.root
|
|
68
|
+
status = ui.status(scope)
|
|
69
|
+
status.set_text("door" if DOOR else "inside")
|
|
70
|
+
|
|
71
|
+
body = ui.label(scr, "", ui.big, ui.WHITE)
|
|
72
|
+
body.set_width(scope.size[0] - 64)
|
|
73
|
+
body.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
|
|
74
|
+
body.align(lv.ALIGN.TOP_MID, 0, 80)
|
|
75
|
+
stats = ui.label(scr, "", ui.font, ui.MUTED)
|
|
76
|
+
stats.align(lv.ALIGN.BOTTOM_MID, 0, -16)
|
|
77
|
+
# Inside, the door's pictures go here.
|
|
78
|
+
picture = lv.image(scr)
|
|
79
|
+
picture.align(lv.ALIGN.CENTER, 0, 30)
|
|
80
|
+
|
|
81
|
+
s = {
|
|
82
|
+
"held": False, # the window's button, while a key or mouse button is down
|
|
83
|
+
"refused": False,
|
|
84
|
+
"floor": None,
|
|
85
|
+
"looked": ticks_ms(),
|
|
86
|
+
"taken": ticks_ms(),
|
|
87
|
+
"reported": ticks_ms(),
|
|
88
|
+
"finding": False,
|
|
89
|
+
"bad": 0,
|
|
90
|
+
"keep": {},
|
|
91
|
+
"checked": ticks_ms(),
|
|
92
|
+
"found": None,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
def say(text):
|
|
96
|
+
body.set_text(text)
|
|
97
|
+
print(text)
|
|
98
|
+
|
|
99
|
+
# The door's camera. Without one the door can't send pictures, and says so.
|
|
100
|
+
camera = None
|
|
101
|
+
if DOOR:
|
|
102
|
+
try:
|
|
103
|
+
if PICTURES:
|
|
104
|
+
camera = medialink.FileCamera(PICTURES)
|
|
105
|
+
else:
|
|
106
|
+
try:
|
|
107
|
+
import board_peripherals
|
|
108
|
+
import cameraif
|
|
109
|
+
except ImportError:
|
|
110
|
+
# A computer: the camera chosen in the picker, moving when it changes.
|
|
111
|
+
camera = connections.HostCamera(scope, size=SIZE, quality=QUALITY, on_error=say)
|
|
112
|
+
else:
|
|
113
|
+
# Have the chip scale each picture down and encode it, so it is
|
|
114
|
+
# quick to send and the other side decodes it in hardware.
|
|
115
|
+
camera = board_peripherals.camera()
|
|
116
|
+
camera.reconfigure(pixel_format=cameraif.PixelFormat.JPEG, frame_size=getattr(cameraif.FrameSize, SIZE))
|
|
117
|
+
camera.quality = QUALITY
|
|
118
|
+
except Exception as e:
|
|
119
|
+
print("video doorbell: camera:", e)
|
|
120
|
+
say("No camera here, so the door can't send pictures.")
|
|
121
|
+
|
|
122
|
+
# The link, its video, and the sound both ways.
|
|
123
|
+
link = video = intercom = finder = None
|
|
124
|
+
mic = speaker = None
|
|
125
|
+
try:
|
|
126
|
+
link = medialink.Link(medialink.UDPTransport(PORT), retries=2)
|
|
127
|
+
video = medialink.Video(link)
|
|
128
|
+
except Exception as e:
|
|
129
|
+
link = video = None
|
|
130
|
+
say("No network for the link: %s" % (e,))
|
|
131
|
+
|
|
132
|
+
audio = AudioFormat(RATE, 1, 16)
|
|
133
|
+
sound = connections.Sound(scope, audio, on_error=say)
|
|
134
|
+
try:
|
|
135
|
+
mic = sound.open_mic()
|
|
136
|
+
speaker = sound.open_speaker()
|
|
137
|
+
if link is not None:
|
|
138
|
+
intercom = medialink.Intercom(link, mic, speaker)
|
|
139
|
+
sound.attach(intercom)
|
|
140
|
+
except Exception as e:
|
|
141
|
+
for dev in (mic, speaker):
|
|
142
|
+
if dev is not None:
|
|
143
|
+
try:
|
|
144
|
+
dev.close()
|
|
145
|
+
except Exception:
|
|
146
|
+
pass
|
|
147
|
+
mic = speaker = intercom = None
|
|
148
|
+
say("No microphone or speaker: %s. Pictures only." % (e,))
|
|
149
|
+
|
|
150
|
+
try:
|
|
151
|
+
finder = medialink.MDNS()
|
|
152
|
+
except Exception as e:
|
|
153
|
+
say("No name search on this network: %s" % (e,))
|
|
154
|
+
|
|
155
|
+
if link is not None and DOOR and camera is not None and finder is not None:
|
|
156
|
+
finder.advertise(DOOR_NAME, PORT)
|
|
157
|
+
say("This is %r" % DOOR_NAME)
|
|
158
|
+
elif not DOOR and link is not None and finder is not None:
|
|
159
|
+
say("Looking for %r" % DOOR_NAME)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def close():
|
|
163
|
+
if intercom is not None:
|
|
164
|
+
intercom.close()
|
|
165
|
+
if link is not None:
|
|
166
|
+
link.close()
|
|
167
|
+
if camera is not None:
|
|
168
|
+
camera.deinit()
|
|
169
|
+
|
|
170
|
+
scope.on_close(close)
|
|
171
|
+
if finder is not None:
|
|
172
|
+
scope.on_close(finder.deinit)
|
|
173
|
+
|
|
174
|
+
# The button is a pin on a board, or a key or mouse button in the window.
|
|
175
|
+
pin = None
|
|
176
|
+
if BUTTON_PIN is not None:
|
|
177
|
+
from machine import Pin
|
|
178
|
+
|
|
179
|
+
pin = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
|
|
180
|
+
|
|
181
|
+
def on_down(event):
|
|
182
|
+
s["held"] = True
|
|
183
|
+
|
|
184
|
+
def on_up(event):
|
|
185
|
+
s["held"] = False
|
|
186
|
+
|
|
187
|
+
if pin is None:
|
|
188
|
+
scope.on([events.KEYDOWN, events.MOUSEBUTTONDOWN], on_down)
|
|
189
|
+
scope.on([events.KEYUP, events.MOUSEBUTTONUP], on_up)
|
|
190
|
+
|
|
191
|
+
def held():
|
|
192
|
+
return (not pin.value()) if pin is not None else s["held"]
|
|
193
|
+
|
|
194
|
+
def connect(address):
|
|
195
|
+
try:
|
|
196
|
+
link.connect(address)
|
|
197
|
+
say("Watching the door at %s" % (address,))
|
|
198
|
+
except medialink.PeerUnreachable:
|
|
199
|
+
pass
|
|
200
|
+
|
|
201
|
+
def found(value, error):
|
|
202
|
+
# The search's answer comes at a safe point, which can be in the
|
|
203
|
+
# middle of a step; the step connects to it, so the link is only
|
|
204
|
+
# ever used from one place.
|
|
205
|
+
s["finding"] = False
|
|
206
|
+
if error is not None:
|
|
207
|
+
print("door search failed: %s" % (error,))
|
|
208
|
+
return
|
|
209
|
+
s["found"] = value or ()
|
|
210
|
+
|
|
211
|
+
def show(jpeg):
|
|
212
|
+
try:
|
|
213
|
+
px, w, h = ui.decode_jpeg(jpeg, scope.size[0] - 32, scope.size[1] - 120)
|
|
214
|
+
ui.rgb565_image(picture, px, w, h, s["keep"])
|
|
215
|
+
except Exception as e:
|
|
216
|
+
s["bad"] += 1
|
|
217
|
+
print("picture not shown: %s" % (e,))
|
|
218
|
+
|
|
219
|
+
def control(now):
|
|
220
|
+
# Inside, call the door while it isn't there. Both sides start and end
|
|
221
|
+
# talking with the button, and say when the other side is talking.
|
|
222
|
+
# The step calls this, never a timer: a timer can fire in the middle
|
|
223
|
+
# of the intercom's work, and a release there closes the microphone
|
|
224
|
+
# while it is being read.
|
|
225
|
+
if s["found"] is not None:
|
|
226
|
+
for _name, address in s["found"]:
|
|
227
|
+
connect(address)
|
|
228
|
+
s["found"] = None
|
|
229
|
+
if not DOOR and not link.alive and ticks_diff(now, s["looked"]) > FIND_MS:
|
|
230
|
+
s["looked"] = now
|
|
231
|
+
if DOOR_ADDRESS:
|
|
232
|
+
connect(DOOR_ADDRESS)
|
|
233
|
+
elif not s["finding"]:
|
|
234
|
+
s["finding"] = True
|
|
235
|
+
scope.worker.submit(lambda: finder.find(DOOR_NAME, timeout=1), found, key="find")
|
|
236
|
+
|
|
237
|
+
if intercom is not None:
|
|
238
|
+
pressed = held()
|
|
239
|
+
if pressed and not intercom.talking and link.alive and not s["refused"]:
|
|
240
|
+
try:
|
|
241
|
+
s["refused"] = not intercom.press()
|
|
242
|
+
say("The other side is talking" if s["refused"] else "Talking...")
|
|
243
|
+
except medialink.PeerUnreachable:
|
|
244
|
+
s["refused"] = True
|
|
245
|
+
say("No answer from the other side")
|
|
246
|
+
elif not pressed:
|
|
247
|
+
s["refused"] = False
|
|
248
|
+
if intercom.talking:
|
|
249
|
+
intercom.release()
|
|
250
|
+
|
|
251
|
+
if link.floor != s["floor"]:
|
|
252
|
+
s["floor"] = link.floor
|
|
253
|
+
if link.floor == medialink.LISTENING:
|
|
254
|
+
say("Someone at the door is talking" if not DOOR else "Inside is answering")
|
|
255
|
+
|
|
256
|
+
# Inside, every few seconds, say how the pictures are coming.
|
|
257
|
+
if not DOOR and video is not None and ticks_diff(now, s["reported"]) >= REPORT_MS:
|
|
258
|
+
s["reported"] = now
|
|
259
|
+
sound = (intercom.stats["latency_ms"] or 0) if intercom is not None else "no"
|
|
260
|
+
underruns = intercom.stats["underruns"] if intercom is not None else 0
|
|
261
|
+
stats.set_text("%d pictures shown, %s ms late at most; sound %s ms late, %d underruns"
|
|
262
|
+
% (video.stats["received"], (video.stats["max_latency_ms"] or 0), sound, underruns))
|
|
263
|
+
video.stats["max_latency_ms"] = None
|
|
264
|
+
|
|
265
|
+
def step():
|
|
266
|
+
# The main loop calls this over and over: the sound, and the pictures
|
|
267
|
+
# either way. A camera frame waits for the chip, so it can't be a timer.
|
|
268
|
+
now = ticks_ms()
|
|
269
|
+
if link is not None and ticks_diff(now, s["checked"]) >= TICK_MS:
|
|
270
|
+
s["checked"] = now
|
|
271
|
+
control(now)
|
|
272
|
+
if intercom is not None:
|
|
273
|
+
intercom.service()
|
|
274
|
+
if video is not None:
|
|
275
|
+
if DOOR:
|
|
276
|
+
if camera is not None and link.alive and video.ready and ticks_diff(now, s["taken"]) >= 1000 // FPS:
|
|
277
|
+
s["taken"] = now
|
|
278
|
+
jpeg = camera.take()
|
|
279
|
+
if jpeg is not None:
|
|
280
|
+
video.send(bytes(jpeg), now)
|
|
281
|
+
video.service()
|
|
282
|
+
else:
|
|
283
|
+
frame = video.receive()
|
|
284
|
+
if frame is not None:
|
|
285
|
+
show(frame.payload)
|
|
286
|
+
if intercom is None or not intercom.talking:
|
|
287
|
+
sleep_ms(1)
|
|
288
|
+
|
|
289
|
+
scope.foreground(step)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "medialink_video_doorbell", "title": "Video doorbell", "category": "Link", "icon": "videocam", "color": "#4A3D8C"}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""See each other, and talk while you hold a button.
|
|
5
|
+
|
|
6
|
+
Open it from the appkit launcher on two devices, each with a camera, a
|
|
7
|
+
screen, a microphone and a speaker, each with its own NAME and the other's
|
|
8
|
+
as PEER. They find each other by name, and each shows what the other's
|
|
9
|
+
camera sees. Hold the button to talk; one side talks at a time, as on a
|
|
10
|
+
walkie-talkie, and the pictures keep coming both ways while you do.
|
|
11
|
+
|
|
12
|
+
The panel on the right says how many pictures went and came, how late they
|
|
13
|
+
were, and how the sound went. The same lines go to the console.
|
|
14
|
+
|
|
15
|
+
Needs: on each side, a camera that board_peripherals.camera() opens (an
|
|
16
|
+
ESP32-P4 with cameraif), or on a computer a camera that ffmpeg can open (hostcamera), or a folder of JPEG files named by
|
|
17
|
+
PICTURES; a display in board_config with jpegio (built into the firmware,
|
|
18
|
+
or Pillow on CPython); a microphone and a speaker boarddev reaches; and a
|
|
19
|
+
network with mdns. On a computer, hold the space bar or a mouse button in
|
|
20
|
+
the window that opens.
|
|
21
|
+
|
|
22
|
+
On a computer it uses the sound input and output chosen in the picker, and
|
|
23
|
+
moves to another when you choose one while it runs.
|
|
24
|
+
|
|
25
|
+
What is missing is named on screen, and the rest of the call keeps running.
|
|
26
|
+
Change the settings at the top of this file and open the app again.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from appkit import connections
|
|
30
|
+
from touchkit import ui
|
|
31
|
+
|
|
32
|
+
# Change these to match your setup.
|
|
33
|
+
NAME = "phone-1" # this side's name on the network
|
|
34
|
+
PEER = "phone-2" # the other side's
|
|
35
|
+
PEER_ADDRESS = None # or ("192.168.1.20", PORT) to skip the search
|
|
36
|
+
PORT = 47800
|
|
37
|
+
BUTTON_PIN = None # a board's button GPIO, pressed when low; None for the window
|
|
38
|
+
PICTURES = None # a folder of JPEG files to send in place of a camera
|
|
39
|
+
FPS = 10 # pictures a second, at most
|
|
40
|
+
SIZE = "QVGA" # a camera's picture size: "QVGA" is 320x240, "VGA" 640x480
|
|
41
|
+
QUALITY = 60 # 1 to 100; lower is smaller and quicker to send
|
|
42
|
+
RATE = 16000 # sound samples a second
|
|
43
|
+
LOOK_MS = 3000 # how often a side that isn't connected looks for its peer
|
|
44
|
+
STATS_MS = 5000 # how often the numbers are refreshed
|
|
45
|
+
PICTURE_SHARE = 2 / 3 # the part of the app's area the pictures take
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def main(scope):
|
|
49
|
+
# The libraries come in here, not at import time, so a missing one is
|
|
50
|
+
# shown on screen rather than failing the launcher.
|
|
51
|
+
try:
|
|
52
|
+
import events
|
|
53
|
+
import medialink
|
|
54
|
+
import multimer
|
|
55
|
+
from audiodev import AudioFormat
|
|
56
|
+
except ImportError as e:
|
|
57
|
+
ui.missing(scope, "missing: medialink", "The videophone needs medialink, boarddev, multimer, audiodev and events: %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
|
+
notes_text = []
|
|
65
|
+
|
|
66
|
+
# The camera: a camera on a board, or a folder of pictures on a computer.
|
|
67
|
+
# Without one, this side still shows the other's pictures and talks.
|
|
68
|
+
camera = None
|
|
69
|
+
try:
|
|
70
|
+
if PICTURES:
|
|
71
|
+
camera = medialink.FileCamera(PICTURES)
|
|
72
|
+
else:
|
|
73
|
+
try:
|
|
74
|
+
import board_peripherals
|
|
75
|
+
import cameraif
|
|
76
|
+
except ImportError:
|
|
77
|
+
# A computer: the camera chosen in the picker, moving when it changes.
|
|
78
|
+
camera = connections.HostCamera(scope, size=SIZE, quality=QUALITY, on_error=status.set_text)
|
|
79
|
+
else:
|
|
80
|
+
camera = board_peripherals.camera()
|
|
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.reconfigure(pixel_format=cameraif.PixelFormat.JPEG, frame_size=getattr(cameraif.FrameSize, SIZE))
|
|
84
|
+
camera.quality = QUALITY
|
|
85
|
+
except Exception as e:
|
|
86
|
+
if camera is not None:
|
|
87
|
+
camera.deinit()
|
|
88
|
+
camera = None
|
|
89
|
+
notes_text.append("No camera here, so pictures aren't sent.")
|
|
90
|
+
print("videophone: camera:", e)
|
|
91
|
+
|
|
92
|
+
# The screen: the other side's pictures, on the left of the app's area.
|
|
93
|
+
screen = None
|
|
94
|
+
picture_w = int(rect[2] * PICTURE_SHARE)
|
|
95
|
+
try:
|
|
96
|
+
from board_config import display_drv
|
|
97
|
+
|
|
98
|
+
screen = medialink.Screen(display_drv, rect=(rect[0], rect[1], picture_w, rect[3]))
|
|
99
|
+
except Exception as e:
|
|
100
|
+
notes_text.append("no display: pictures aren't shown (%s)" % (e,))
|
|
101
|
+
print("videophone: screen:", e)
|
|
102
|
+
|
|
103
|
+
# The button is a pin on a board, or a key or mouse button in the window.
|
|
104
|
+
st = {"held": False, "refused": False, "searching": False, "taken": 0, "counted": 0, "reported": 0}
|
|
105
|
+
pin = None
|
|
106
|
+
if BUTTON_PIN is not None:
|
|
107
|
+
from machine import Pin
|
|
108
|
+
|
|
109
|
+
pin = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
|
|
110
|
+
|
|
111
|
+
def held():
|
|
112
|
+
return not pin.value()
|
|
113
|
+
|
|
114
|
+
else:
|
|
115
|
+
def down(event):
|
|
116
|
+
st["held"] = True
|
|
117
|
+
|
|
118
|
+
def up(event):
|
|
119
|
+
st["held"] = False
|
|
120
|
+
|
|
121
|
+
scope.on([events.KEYDOWN, events.MOUSEBUTTONDOWN], down)
|
|
122
|
+
scope.on([events.KEYUP, events.MOUSEBUTTONUP], up)
|
|
123
|
+
|
|
124
|
+
def held():
|
|
125
|
+
return st["held"]
|
|
126
|
+
|
|
127
|
+
# Listen on PORT with sound and pictures on one link. Without a network
|
|
128
|
+
# there is no call, so this one stops the app, and says why.
|
|
129
|
+
try:
|
|
130
|
+
link = medialink.Link(medialink.UDPTransport(PORT), retries=2)
|
|
131
|
+
except Exception as e:
|
|
132
|
+
if camera is not None:
|
|
133
|
+
camera.deinit()
|
|
134
|
+
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))
|
|
135
|
+
return
|
|
136
|
+
|
|
137
|
+
# The sound: the microphone the button talks into, the speaker the other
|
|
138
|
+
# side's voice comes out of. Either may be missing.
|
|
139
|
+
audio = AudioFormat(RATE, 1, 16)
|
|
140
|
+
sound = connections.Sound(scope, audio, on_error=status.set_text)
|
|
141
|
+
mic = speaker = None
|
|
142
|
+
try:
|
|
143
|
+
mic = sound.open_mic()
|
|
144
|
+
except Exception as e:
|
|
145
|
+
notes_text.append("no microphone: can't talk (%s)" % (e,))
|
|
146
|
+
print("videophone: microphone:", e)
|
|
147
|
+
try:
|
|
148
|
+
speaker = sound.open_speaker()
|
|
149
|
+
except Exception as e:
|
|
150
|
+
notes_text.append("no speaker: can't hear (%s)" % (e,))
|
|
151
|
+
print("videophone: speaker:", e)
|
|
152
|
+
intercom = None
|
|
153
|
+
if mic is not None or speaker is not None:
|
|
154
|
+
intercom = medialink.Intercom(link, mic, speaker)
|
|
155
|
+
sound.attach(intercom)
|
|
156
|
+
video = medialink.Video(link)
|
|
157
|
+
|
|
158
|
+
# Announce this side by name, and look for the peer by name.
|
|
159
|
+
finder = None
|
|
160
|
+
try:
|
|
161
|
+
finder = medialink.MDNS()
|
|
162
|
+
finder.advertise(NAME, PORT)
|
|
163
|
+
except Exception as e:
|
|
164
|
+
finder = None
|
|
165
|
+
notes_text.append("no mdns, so no search: %s" % (e,))
|
|
166
|
+
print("videophone: mdns:", e)
|
|
167
|
+
|
|
168
|
+
# The panel on the right: the numbers, as the console gets them.
|
|
169
|
+
left = picture_w + 16
|
|
170
|
+
info = ui.label(scr, "", ui.small, ui.WHITE)
|
|
171
|
+
info.set_pos(left, 16)
|
|
172
|
+
info.set_width(max(120, rect[2] - left - 16))
|
|
173
|
+
notes = ui.label(scr, "\n".join(notes_text), ui.tiny, ui.MUTED)
|
|
174
|
+
notes.set_pos(left, rect[3] - 80)
|
|
175
|
+
notes.set_width(max(120, rect[2] - left - 16))
|
|
176
|
+
|
|
177
|
+
def say(line):
|
|
178
|
+
info.set_text(line)
|
|
179
|
+
print(line)
|
|
180
|
+
|
|
181
|
+
say("This is %r; looking for %r" % (NAME, PEER))
|
|
182
|
+
status.set_text("looking for %s" % PEER)
|
|
183
|
+
for line in notes_text:
|
|
184
|
+
print("videophone:", line)
|
|
185
|
+
|
|
186
|
+
def connect(found):
|
|
187
|
+
for _name, address in found:
|
|
188
|
+
try:
|
|
189
|
+
link.connect(address)
|
|
190
|
+
status.set_text("connected")
|
|
191
|
+
say("Connected to %r at %s" % (PEER, address))
|
|
192
|
+
except medialink.PeerUnreachable:
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
def found(value, error):
|
|
196
|
+
st["searching"] = False
|
|
197
|
+
if error is not None:
|
|
198
|
+
print("videophone: search:", error)
|
|
199
|
+
return
|
|
200
|
+
connect(value)
|
|
201
|
+
|
|
202
|
+
def look(t):
|
|
203
|
+
# Call the peer while it isn't there. The search can take a second,
|
|
204
|
+
# so it runs on the worker and the connect happens on this thread.
|
|
205
|
+
if link.alive or st["searching"]:
|
|
206
|
+
return
|
|
207
|
+
if PEER_ADDRESS:
|
|
208
|
+
connect([(PEER, PEER_ADDRESS)])
|
|
209
|
+
elif finder is not None:
|
|
210
|
+
st["searching"] = True
|
|
211
|
+
scope.worker.submit(lambda: finder.find(PEER, timeout=1), found, key="videophone-find")
|
|
212
|
+
|
|
213
|
+
def report(t):
|
|
214
|
+
# Every few seconds, say how it is going.
|
|
215
|
+
total_shown = video.stats["received"]
|
|
216
|
+
sent = video.stats["sent"]
|
|
217
|
+
latency = (intercom.stats["latency_ms"] or 0) if intercom is not None else 0
|
|
218
|
+
underruns = intercom.stats["underruns"] if intercom is not None else 0
|
|
219
|
+
say("Pictures: %d sent, %d shown, %s ms late at most. Sound: %s ms late, %d underruns"
|
|
220
|
+
% (sent, total_shown, (video.stats["max_latency_ms"] or 0), latency, underruns))
|
|
221
|
+
video.stats["max_latency_ms"] = None
|
|
222
|
+
|
|
223
|
+
def close():
|
|
224
|
+
# Release what the app opened, in the order the example does.
|
|
225
|
+
if intercom is not None:
|
|
226
|
+
intercom.close()
|
|
227
|
+
link.close()
|
|
228
|
+
if camera is not None:
|
|
229
|
+
camera.deinit()
|
|
230
|
+
|
|
231
|
+
scope.on_close(close)
|
|
232
|
+
if finder is not None:
|
|
233
|
+
scope.on_close(finder.deinit)
|
|
234
|
+
scope.every(LOOK_MS, look)
|
|
235
|
+
scope.every(STATS_MS, report)
|
|
236
|
+
|
|
237
|
+
def step():
|
|
238
|
+
# One pass of the example's loop, run by the program's main loop.
|
|
239
|
+
now = multimer.ticks_ms()
|
|
240
|
+
pressed = held()
|
|
241
|
+
if intercom is not None:
|
|
242
|
+
# Call the peer while it isn't there; talk while the button is held.
|
|
243
|
+
if pressed and mic is not None and not intercom.talking and link.alive and not st["refused"]:
|
|
244
|
+
try:
|
|
245
|
+
st["refused"] = not intercom.press()
|
|
246
|
+
status.set_text("the other side is talking" if st["refused"] else "talking")
|
|
247
|
+
print("The other side is talking" if st["refused"] else "Talking...")
|
|
248
|
+
except medialink.PeerUnreachable:
|
|
249
|
+
st["refused"] = True
|
|
250
|
+
status.set_text("no answer")
|
|
251
|
+
print("No answer from the other side")
|
|
252
|
+
elif not pressed:
|
|
253
|
+
st["refused"] = False
|
|
254
|
+
if intercom.talking:
|
|
255
|
+
intercom.release()
|
|
256
|
+
intercom.service()
|
|
257
|
+
|
|
258
|
+
# Send a picture whenever the last has gone and the next is due.
|
|
259
|
+
if camera is not None and link.alive and video.ready and multimer.ticks_diff(now, st["taken"]) >= 1000 // FPS:
|
|
260
|
+
st["taken"] = now
|
|
261
|
+
jpeg = camera.take()
|
|
262
|
+
if jpeg is not None:
|
|
263
|
+
video.send(bytes(jpeg), now)
|
|
264
|
+
video.service()
|
|
265
|
+
|
|
266
|
+
# Show each one that arrives.
|
|
267
|
+
frame = video.receive()
|
|
268
|
+
if frame is not None and screen is not None:
|
|
269
|
+
screen.show(frame.payload)
|
|
270
|
+
if intercom is None or not intercom.talking:
|
|
271
|
+
multimer.sleep_ms(1)
|
|
272
|
+
|
|
273
|
+
scope.foreground(step)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "medialink_videophone", "title": "Videophone", "category": "Link", "icon": "video_call", "color": "#8C1E4E"}
|