omnipython-applibs 0.1.2.dev16__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.
- appkit/__init__.py +52 -0
- appkit/__init__.pyi +17 -0
- appkit/data.py +149 -0
- appkit/data.pyi +25 -0
- appkit/foreground.py +357 -0
- appkit/foreground.pyi +45 -0
- appkit/home.py +19 -0
- appkit/home.pyi +8 -0
- appkit/hover.py +59 -0
- appkit/hover.pyi +13 -0
- appkit/index.py +199 -0
- appkit/index.pyi +30 -0
- appkit/launcher.py +1075 -0
- appkit/launcher.pyi +96 -0
- appkit/manifest.py +75 -0
- appkit/manifest.pyi +8 -0
- appkit/net.py +167 -0
- appkit/net.pyi +34 -0
- appkit/places.py +202 -0
- appkit/places.pyi +39 -0
- appkit/py.typed +0 -0
- appkit/scope.py +636 -0
- appkit/scope.pyi +110 -0
- appkit/shell.py +121 -0
- appkit/shell.pyi +32 -0
- appkit/worker.py +409 -0
- appkit/worker.pyi +83 -0
- controlkit/__init__.py +19 -0
- controlkit/__init__.pyi +10 -0
- controlkit/_draw.py +88 -0
- controlkit/_safe.py +29 -0
- controlkit/dotmatrix.py +101 -0
- controlkit/dotmatrix.pyi +23 -0
- controlkit/encoder.py +297 -0
- controlkit/encoder.pyi +55 -0
- controlkit/font5x7.py +22 -0
- controlkit/font5x7.pyi +11 -0
- controlkit/lamp_button.py +92 -0
- controlkit/lamp_button.pyi +40 -0
- controlkit/meter.py +86 -0
- controlkit/meter.pyi +30 -0
- controlkit/mock.py +116 -0
- controlkit/mock.pyi +35 -0
- controlkit/pad_grid.py +113 -0
- controlkit/pad_grid.pyi +41 -0
- controlkit/py.typed +0 -0
- controlkit/slider.py +185 -0
- controlkit/slider.pyi +49 -0
- controlkit/sprites.py +279 -0
- controlkit/sprites.pyi +22 -0
- controlkit/theme.py +120 -0
- controlkit/theme.pyi +15 -0
- deskkit/__init__.py +74 -0
- deskkit/__init__.pyi +18 -0
- deskkit/forecast.py +261 -0
- deskkit/forecast.pyi +40 -0
- deskkit/localtime.py +215 -0
- deskkit/localtime.pyi +33 -0
- deskkit/py.typed +0 -0
- deskkit/shell.py +113 -0
- deskkit/shell.pyi +33 -0
- omnipython_applibs-0.1.2.dev16.dist-info/METADATA +90 -0
- omnipython_applibs-0.1.2.dev16.dist-info/RECORD +143 -0
- omnipython_applibs-0.1.2.dev16.dist-info/WHEEL +5 -0
- omnipython_applibs-0.1.2.dev16.dist-info/licenses/LICENSE +21 -0
- omnipython_applibs-0.1.2.dev16.dist-info/top_level.txt +9 -0
- programkit/__init__.py +27 -0
- programkit/__init__.pyi +8 -0
- programkit/board.py +56 -0
- programkit/board.pyi +20 -0
- programkit/press.py +63 -0
- programkit/press.pyi +17 -0
- programkit/py.typed +0 -0
- programkit/rows.py +149 -0
- programkit/rows.pyi +34 -0
- sayso/__init__.py +109 -0
- sayso/__init__.pyi +124 -0
- sayso/auth.py +811 -0
- sayso/auth.pyi +102 -0
- sayso/client.py +1286 -0
- sayso/client.pyi +138 -0
- sayso/keepalive.py +371 -0
- sayso/keepalive.pyi +53 -0
- sayso/local.py +85 -0
- sayso/local.pyi +28 -0
- sayso/object_specs.py +706 -0
- sayso/object_specs.pyi +8 -0
- sayso/objects.py +411 -0
- sayso/objects.pyi +854 -0
- sayso/py.typed +0 -0
- sayso/schema_coverage.py +210 -0
- sayso/schema_coverage.pyi +13 -0
- sayso/transport.py +426 -0
- sayso/transport.pyi +47 -0
- songmodel/__init__.py +30 -0
- songmodel/__init__.pyi +9 -0
- songmodel/link.py +189 -0
- songmodel/link.pyi +53 -0
- songmodel/model.py +238 -0
- songmodel/model.pyi +40 -0
- songmodel/page.html +225 -0
- songmodel/page_data.py +236 -0
- songmodel/page_data.pyi +6 -0
- songmodel/py.typed +0 -0
- soundmodule/__init__.py +7 -0
- soundmodule/__init__.pyi +6 -0
- soundmodule/engine.py +1001 -0
- soundmodule/engine.pyi +196 -0
- soundmodule/names.py +298 -0
- soundmodule/names.pyi +30 -0
- soundmodule/pipes.py +105 -0
- soundmodule/pipes.pyi +31 -0
- soundmodule/py.typed +0 -0
- soundmodule/run.py +249 -0
- soundmodule/run.pyi +49 -0
- spectrum/__init__.py +72 -0
- spectrum/__init__.pyi +30 -0
- spectrum/analyzer.py +231 -0
- spectrum/analyzer.pyi +67 -0
- spectrum/fake_music.py +201 -0
- spectrum/fake_music.pyi +36 -0
- spectrum/pump_levels.py +126 -0
- spectrum/pump_levels.pyi +46 -0
- spectrum/py.typed +0 -0
- spectrum/spectrum_view.py +403 -0
- spectrum/spectrum_view.pyi +80 -0
- touchkit/__init__.py +4 -0
- touchkit/__init__.pyi +4 -0
- touchkit/cam.py +146 -0
- touchkit/cam.pyi +35 -0
- touchkit/clock.py +18 -0
- touchkit/clock.pyi +6 -0
- touchkit/ecp.py +134 -0
- touchkit/ecp.pyi +28 -0
- touchkit/net.py +347 -0
- touchkit/net.pyi +27 -0
- touchkit/py.typed +0 -0
- touchkit/settings.py +85 -0
- touchkit/settings.pyi +17 -0
- touchkit/shell.py +268 -0
- touchkit/shell.pyi +42 -0
- touchkit/ui.py +167 -0
- touchkit/ui.pyi +53 -0
touchkit/clock.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""touchkit.clock -- sleeping for milliseconds on every interpreter.
|
|
5
|
+
|
|
6
|
+
``time.sleep_ms`` is MicroPython's; CPython and CircuitPython have only
|
|
7
|
+
``time.sleep``. The tick functions are ``multimer``'s (``ticks_ms``,
|
|
8
|
+
``ticks_diff``, ``ticks_add``), which are the same on all three.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
sleep_ms = time.sleep_ms
|
|
15
|
+
except AttributeError:
|
|
16
|
+
|
|
17
|
+
def sleep_ms(ms):
|
|
18
|
+
time.sleep(ms / 1000)
|
touchkit/clock.pyi
ADDED
touchkit/ecp.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""touchkit.ecp -- Roku's External Control Protocol, for the remote and Cast.
|
|
5
|
+
|
|
6
|
+
Every call blocks on the network, so run it on ``scope.worker``. Each
|
|
7
|
+
request opens a socket and closes it when done: nothing is left open when
|
|
8
|
+
an app closes. The TV must have "Control by mobile apps" on (it is by
|
|
9
|
+
default).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from multimer import ticks_add, ticks_diff, ticks_ms
|
|
13
|
+
|
|
14
|
+
from appkit import net
|
|
15
|
+
|
|
16
|
+
PORT = 8060
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def request(host, method, path, timeout=3):
|
|
20
|
+
"""``(status, body)`` of one HTTP request to the Roku at *host*."""
|
|
21
|
+
pool = net.sockets()
|
|
22
|
+
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
|
|
23
|
+
try:
|
|
24
|
+
s.settimeout(timeout)
|
|
25
|
+
s.connect(pool.getaddrinfo(host, PORT)[0][-1])
|
|
26
|
+
s.send(
|
|
27
|
+
(
|
|
28
|
+
"%s %s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
|
29
|
+
% (method, path, host, PORT)
|
|
30
|
+
).encode()
|
|
31
|
+
)
|
|
32
|
+
data = b""
|
|
33
|
+
while len(data) < 16384:
|
|
34
|
+
chunk = net.recv(s, 2048)
|
|
35
|
+
if not chunk:
|
|
36
|
+
break
|
|
37
|
+
data += chunk
|
|
38
|
+
finally:
|
|
39
|
+
s.close()
|
|
40
|
+
head, _, body = data.partition(b"\r\n\r\n")
|
|
41
|
+
line = head.split(b"\r\n", 1)[0].split()
|
|
42
|
+
code = int(line[1]) if len(line) > 1 else 0
|
|
43
|
+
return code, body
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
#: A TV in standby takes about 5 s to answer a key, with 202.
|
|
47
|
+
KEY_TIMEOUT = 8
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def keypress(host, key):
|
|
51
|
+
"""Press *key* (``"Select"``, ``"VolumeUp"``, ``"PowerOn"``...) on the Roku at *host*."""
|
|
52
|
+
code, _ = request(host, "POST", "/keypress/" + key, timeout=KEY_TIMEOUT)
|
|
53
|
+
if not 200 <= code < 300: # 202 from a TV in standby
|
|
54
|
+
raise OSError("the TV answered %d" % code)
|
|
55
|
+
return code
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _tag(xml, tag):
|
|
59
|
+
a = xml.find(b"<" + tag + b">")
|
|
60
|
+
if a < 0:
|
|
61
|
+
return None
|
|
62
|
+
a += len(tag) + 2
|
|
63
|
+
b = xml.find(b"</" + tag + b">", a)
|
|
64
|
+
return xml[a:b].decode() if b > a else None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def device_info(host):
|
|
68
|
+
"""``{host, name, model, power}`` for the Roku at *host*, or None."""
|
|
69
|
+
code, body = request(host, "GET", "/query/device-info")
|
|
70
|
+
if code != 200:
|
|
71
|
+
return None
|
|
72
|
+
name = _tag(body, b"user-device-name") or _tag(body, b"friendly-device-name") or host
|
|
73
|
+
model = _tag(body, b"friendly-model-name") or _tag(body, b"model-name") or ""
|
|
74
|
+
power = _tag(body, b"power-mode") or ""
|
|
75
|
+
return {"host": host, "name": name, "model": model, "power": power}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def discover(seconds=2.5):
|
|
79
|
+
"""``[{host, name, model, power}]`` for every Roku that answers SSDP."""
|
|
80
|
+
msg = (
|
|
81
|
+
b"M-SEARCH * HTTP/1.1\r\nHost: 239.255.255.250:1900\r\nMan: \"ssdp:discover\"\r\n"
|
|
82
|
+
b"ST: roku:ecp\r\nMX: 2\r\n\r\n"
|
|
83
|
+
)
|
|
84
|
+
pool = net.sockets()
|
|
85
|
+
s = pool.socket(pool.AF_INET, pool.SOCK_DGRAM)
|
|
86
|
+
hosts = []
|
|
87
|
+
try:
|
|
88
|
+
s.settimeout(0.3)
|
|
89
|
+
dest = pool.getaddrinfo("239.255.255.250", 1900)[0][-1]
|
|
90
|
+
for _ in range(2):
|
|
91
|
+
s.sendto(msg, dest)
|
|
92
|
+
end = ticks_add(ticks_ms(), int(seconds * 1000))
|
|
93
|
+
while ticks_diff(end, ticks_ms()) > 0:
|
|
94
|
+
try:
|
|
95
|
+
data = net.recv(s, 1024)
|
|
96
|
+
except OSError:
|
|
97
|
+
continue
|
|
98
|
+
for line in data.split(b"\r\n"):
|
|
99
|
+
if line[:9].lower() == b"location:":
|
|
100
|
+
url = line[9:].strip().decode()
|
|
101
|
+
host = url.split("//", 1)[-1].split("/", 1)[0].split(":")[0]
|
|
102
|
+
if host not in hosts:
|
|
103
|
+
hosts.append(host)
|
|
104
|
+
finally:
|
|
105
|
+
s.close()
|
|
106
|
+
found = []
|
|
107
|
+
for h in hosts:
|
|
108
|
+
try:
|
|
109
|
+
info = device_info(h)
|
|
110
|
+
except OSError:
|
|
111
|
+
info = None
|
|
112
|
+
if info:
|
|
113
|
+
found.append(info)
|
|
114
|
+
return found
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def pick(tvs, prefer=None, last=None):
|
|
118
|
+
"""The index in *tvs* to choose, or None when *prefer* matches none.
|
|
119
|
+
|
|
120
|
+
*last* (a host) if it's there; else, with *prefer*, the first TV whose
|
|
121
|
+
name holds it, then the first whose model does; else, with no *prefer*,
|
|
122
|
+
0. A name is checked before any model because a short preference (say
|
|
123
|
+
"65") can sit inside another TV's model number ("75R655"), and a remote
|
|
124
|
+
that guesses sends its keys to the wrong set."""
|
|
125
|
+
for i, t in enumerate(tvs):
|
|
126
|
+
if last and t["host"] == last:
|
|
127
|
+
return i
|
|
128
|
+
if prefer:
|
|
129
|
+
for key in ("name", "model"):
|
|
130
|
+
for i, t in enumerate(tvs):
|
|
131
|
+
if prefer in (t[key] or ""):
|
|
132
|
+
return i
|
|
133
|
+
return None
|
|
134
|
+
return 0
|
touchkit/ecp.pyi
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Roku's External Control Protocol, for the remote and Cast."""
|
|
5
|
+
|
|
6
|
+
from typing import TypedDict
|
|
7
|
+
|
|
8
|
+
class _TV(TypedDict):
|
|
9
|
+
"""A Roku found on the network."""
|
|
10
|
+
|
|
11
|
+
host: str
|
|
12
|
+
name: str
|
|
13
|
+
model: str
|
|
14
|
+
power: str
|
|
15
|
+
|
|
16
|
+
PORT: int
|
|
17
|
+
KEY_TIMEOUT: int
|
|
18
|
+
|
|
19
|
+
def request(host: str, method: str, path: str, timeout: float = 3) -> tuple[int, bytes]:
|
|
20
|
+
"""`(status, body)` of one HTTP request to the Roku at `host`."""
|
|
21
|
+
def keypress(host: str, key: str) -> int:
|
|
22
|
+
"""Press `key` on the Roku at `host`; returns the status code."""
|
|
23
|
+
def device_info(host: str) -> _TV | None:
|
|
24
|
+
"""The TV at `host`, or None."""
|
|
25
|
+
def discover(seconds: float = 2.5) -> list[_TV]:
|
|
26
|
+
"""Every Roku that answers SSDP."""
|
|
27
|
+
def pick(tvs: list[_TV], prefer: str | None = None, last: str | None = None) -> int | None:
|
|
28
|
+
"""The index in `tvs` to choose, or None when `prefer` matches none."""
|
touchkit/net.py
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""touchkit.net -- Wi-Fi for the board's apps: when it's on is the set's call.
|
|
5
|
+
|
|
6
|
+
A desktop has no ``wifi`` or ``network`` module and is on its network
|
|
7
|
+
already: `up` is then True once the computer has an address, and
|
|
8
|
+
`ip` and `hostname` are the computer's own. CircuitPython has no
|
|
9
|
+
``network`` module either: there `ip`, `rssi` and `hostname` read
|
|
10
|
+
``wifi.radio``.
|
|
11
|
+
|
|
12
|
+
The board joins a known network at boot (`boot`) and stays joined
|
|
13
|
+
while apps come and go, like the display. An app that needs the network
|
|
14
|
+
calls `up` from a timer until it says True. The hostname comes from
|
|
15
|
+
``settings.json`` (see ``touchkit.settings``).
|
|
16
|
+
|
|
17
|
+
Once the board is on a network its clock gets set (`keep_time`), and
|
|
18
|
+
again each day, so no app has to ask for it. That works on CircuitPython
|
|
19
|
+
too, through ``appkit.net``.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import sys
|
|
23
|
+
import time
|
|
24
|
+
|
|
25
|
+
from . import settings
|
|
26
|
+
|
|
27
|
+
_CP = sys.implementation.name == "circuitpython"
|
|
28
|
+
_started = False
|
|
29
|
+
_webrepl_on = False
|
|
30
|
+
_clock_timer = None
|
|
31
|
+
_clock_asked = False
|
|
32
|
+
_CLOCK_CHECK_MS = 60_000
|
|
33
|
+
_CLOCK_JOB = "touchkit.clock"
|
|
34
|
+
_CP_JOIN_S = 5
|
|
35
|
+
# A join on CircuitPython holds the screen for 5 to 10 s whether it finds the
|
|
36
|
+
# network or not, and whatever its timeout. After one fails, `up` waits this
|
|
37
|
+
# long before the next, doubling up to _CP_RETRY_MAX_MS, so an offline board
|
|
38
|
+
# stays usable while apps poll.
|
|
39
|
+
_CP_RETRY_MS = 30_000
|
|
40
|
+
_CP_RETRY_MAX_MS = 300_000
|
|
41
|
+
_retry_ms = 0
|
|
42
|
+
_retry_at = None # the `_now_ms` before which `up` doesn't try to join again
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _host_ip():
|
|
46
|
+
"""This machine's IPv4 address on the route to the network, or None.
|
|
47
|
+
For a host with no ``network`` module (a desktop), which is on its LAN
|
|
48
|
+
already: a UDP socket "connected" to a documentation address (TEST-NET-1,
|
|
49
|
+
RFC 5737) sends nothing and names the interface the OS would use."""
|
|
50
|
+
try:
|
|
51
|
+
import socket
|
|
52
|
+
except ImportError: # CircuitPython names it socketpool
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
56
|
+
try:
|
|
57
|
+
s.connect(socket.getaddrinfo("192.0.2.1", 9)[0][-1])
|
|
58
|
+
sa = s.getsockname()
|
|
59
|
+
# CPython answers a tuple, MicroPython the packed sockaddr (the IPv4 address is bytes 4 to 8).
|
|
60
|
+
ip = sa[0] if isinstance(sa, tuple) else ".".join(str(b) for b in sa[4:8])
|
|
61
|
+
return None if ip.startswith("0.") else ip
|
|
62
|
+
except OSError:
|
|
63
|
+
return None
|
|
64
|
+
finally:
|
|
65
|
+
s.close()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _radio():
|
|
69
|
+
"""CircuitPython's ``wifi.radio``, where there is no ``network`` module; else None."""
|
|
70
|
+
try:
|
|
71
|
+
import wifi
|
|
72
|
+
|
|
73
|
+
return wifi.radio
|
|
74
|
+
except (ImportError, AttributeError):
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def hostname():
|
|
79
|
+
"""The board's name on the network, without ``.local``: ``boarddev.name()``."""
|
|
80
|
+
import boarddev
|
|
81
|
+
|
|
82
|
+
return boarddev.name()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _station_state():
|
|
86
|
+
"""``"connected"`` when ``boot.py`` already joined the station, else None.
|
|
87
|
+
A board set up with ``mcuctl wifi enable`` joins in ``boot.py``, before
|
|
88
|
+
any app starts, and stays reachable through it. CircuitPython has no
|
|
89
|
+
``network`` module, so it is always None there: its radio and web
|
|
90
|
+
workflow are the runtime's."""
|
|
91
|
+
if _CP:
|
|
92
|
+
return None
|
|
93
|
+
try:
|
|
94
|
+
import network
|
|
95
|
+
|
|
96
|
+
if network.WLAN(network.STA_IF).isconnected():
|
|
97
|
+
return "connected"
|
|
98
|
+
except Exception:
|
|
99
|
+
pass # no network module (a desktop), or a port that can't say
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def up():
|
|
104
|
+
"""True when the station is connected. When it isn't, starts a join: in
|
|
105
|
+
the background on MicroPython. On CircuitPython a join holds the screen
|
|
106
|
+
for seconds, so one is tried now and the next only after a wait that
|
|
107
|
+
grows from 30 s to 5 minutes; an app can still poll this often."""
|
|
108
|
+
global _started
|
|
109
|
+
try:
|
|
110
|
+
import wifi
|
|
111
|
+
except ImportError:
|
|
112
|
+
# No wifi module: a desktop, which has joined its network already
|
|
113
|
+
# (wifi.py is MicroPython's, and CPython can't import it).
|
|
114
|
+
return _host_ip() is not None
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
joined = wifi.radio.connected
|
|
118
|
+
except Exception:
|
|
119
|
+
joined = False
|
|
120
|
+
if joined:
|
|
121
|
+
_joined()
|
|
122
|
+
start_webrepl()
|
|
123
|
+
keep_time() # outside the try: a missing deskkit must be seen
|
|
124
|
+
return True
|
|
125
|
+
if _CP:
|
|
126
|
+
# CircuitPython's join blocks while it tries; wifi_manager joins what
|
|
127
|
+
# secrets.py or settings.toml hold.
|
|
128
|
+
if _join_due():
|
|
129
|
+
try:
|
|
130
|
+
import wifi_manager
|
|
131
|
+
|
|
132
|
+
ip = wifi_manager.connect(setup=False, timeout=_CP_JOIN_S)
|
|
133
|
+
except Exception as e:
|
|
134
|
+
print("touchkit: wifi:", e)
|
|
135
|
+
ip = None
|
|
136
|
+
if ip:
|
|
137
|
+
_joined()
|
|
138
|
+
else:
|
|
139
|
+
_join_failed()
|
|
140
|
+
return False
|
|
141
|
+
if not _started:
|
|
142
|
+
_started = True
|
|
143
|
+
try:
|
|
144
|
+
import wifi_manager
|
|
145
|
+
|
|
146
|
+
wifi_manager.connect(setup=False, wait=False)
|
|
147
|
+
except Exception as e:
|
|
148
|
+
print("touchkit: wifi:", e)
|
|
149
|
+
_started = False
|
|
150
|
+
return False
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _needs_polling():
|
|
154
|
+
"""True on CircuitPython, where a network that drops is rejoined only by a
|
|
155
|
+
call to `up`: the radio gives up retrying a few seconds after the access
|
|
156
|
+
point goes, and stays off when it comes back."""
|
|
157
|
+
try:
|
|
158
|
+
import wifi
|
|
159
|
+
except ImportError:
|
|
160
|
+
return False
|
|
161
|
+
return _CP and hasattr(wifi, "radio")
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _joined():
|
|
165
|
+
global _retry_ms, _retry_at
|
|
166
|
+
_retry_ms = 0
|
|
167
|
+
_retry_at = None
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _join_failed():
|
|
171
|
+
global _retry_ms, _retry_at
|
|
172
|
+
_retry_ms = min(max(2 * _retry_ms, _CP_RETRY_MS), _CP_RETRY_MAX_MS)
|
|
173
|
+
_retry_at = _now_ms() + _retry_ms
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _join_due():
|
|
177
|
+
return _retry_at is None or _now_ms() >= _retry_at
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _now_ms():
|
|
181
|
+
# Only CircuitPython waits between joins, and it has time.monotonic.
|
|
182
|
+
return int(time.monotonic() * 1000)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def start_webrepl():
|
|
186
|
+
"""WebREPL, once, when ``settings.json`` doesn't turn it off.
|
|
187
|
+
|
|
188
|
+
Does nothing without a webrepl module (a desktop), when something already
|
|
189
|
+
started it (``boot.py`` may have: starting it again would drop the listener
|
|
190
|
+
it opened), and on CircuitPython, which has no WebREPL: its remote access is
|
|
191
|
+
the web workflow that ``settings.toml`` turns on."""
|
|
192
|
+
global _webrepl_on
|
|
193
|
+
if _webrepl_on or not settings.get("webrepl", True):
|
|
194
|
+
return
|
|
195
|
+
if sys.implementation.name == "circuitpython" or webrepl_on():
|
|
196
|
+
return
|
|
197
|
+
try:
|
|
198
|
+
import webrepl
|
|
199
|
+
|
|
200
|
+
webrepl.start()
|
|
201
|
+
_webrepl_on = True
|
|
202
|
+
except ImportError:
|
|
203
|
+
pass # a desktop, or firmware without WebREPL: nothing to start
|
|
204
|
+
except Exception as e:
|
|
205
|
+
print("touchkit: webrepl:", e)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def webrepl_on():
|
|
209
|
+
"""True when WebREPL is listening, whoever started it."""
|
|
210
|
+
if _webrepl_on:
|
|
211
|
+
return True
|
|
212
|
+
w = sys.modules.get("webrepl") # main.py may have started it before the apps
|
|
213
|
+
return w is not None and getattr(w, "listen_s", None) is not None
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def boot():
|
|
217
|
+
"""Join a known network quietly (``secrets.py``'s or one wifi_manager
|
|
218
|
+
remembered), then start WebREPL. With none in range, carry on offline
|
|
219
|
+
and keep trying in the background; System's Wi-Fi setup asks for one.
|
|
220
|
+
Call it on the main thread once the launcher has booted.
|
|
221
|
+
|
|
222
|
+
A station that ``boot.py`` already joined is left alone: no rejoin, no
|
|
223
|
+
second WebREPL, so a board set up with
|
|
224
|
+
``mcuctl wifi enable`` keeps its connection."""
|
|
225
|
+
settings.move_hostname()
|
|
226
|
+
if _station_state() == "connected":
|
|
227
|
+
joined = True
|
|
228
|
+
else:
|
|
229
|
+
try:
|
|
230
|
+
import wifi_manager
|
|
231
|
+
|
|
232
|
+
joined = wifi_manager.connect(setup=False)
|
|
233
|
+
except Exception as e:
|
|
234
|
+
print("touchkit: wifi:", e)
|
|
235
|
+
joined = False
|
|
236
|
+
if joined:
|
|
237
|
+
start_webrepl()
|
|
238
|
+
keep_time() # outside the try: a missing deskkit must be seen
|
|
239
|
+
if not _needs_polling():
|
|
240
|
+
return
|
|
241
|
+
else:
|
|
242
|
+
if _CP:
|
|
243
|
+
_join_failed() # where a join blocks, the next waits a while
|
|
244
|
+
keep_time()
|
|
245
|
+
up()
|
|
246
|
+
import lvgl as lv
|
|
247
|
+
|
|
248
|
+
def poll(t):
|
|
249
|
+
# On CircuitPython the timer outlives the first join: it is what
|
|
250
|
+
# notices a dropped access point and tries again.
|
|
251
|
+
if up() and not _needs_polling():
|
|
252
|
+
t.delete()
|
|
253
|
+
|
|
254
|
+
lv.timer_create(poll, 5000, None)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _set_clock_if_due(_timer=None):
|
|
258
|
+
global _clock_asked
|
|
259
|
+
try:
|
|
260
|
+
import wifi
|
|
261
|
+
except ImportError:
|
|
262
|
+
return # a desktop keeps its own time
|
|
263
|
+
from appkit.worker import shared
|
|
264
|
+
from deskkit import localtime # ImportError when deskkit isn't installed
|
|
265
|
+
|
|
266
|
+
try:
|
|
267
|
+
joined = wifi.radio.connected
|
|
268
|
+
except Exception:
|
|
269
|
+
return
|
|
270
|
+
worker = shared()
|
|
271
|
+
if joined and localtime.due() and not worker.pending(_CLOCK_JOB):
|
|
272
|
+
# NTP, or a web server's Date header where NTP is blocked: it blocks.
|
|
273
|
+
_clock_asked = True
|
|
274
|
+
worker.submit(localtime.resync_if_due, key=_CLOCK_JOB)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def keep_time():
|
|
278
|
+
"""Set the board's clock as soon as the station has joined, then check
|
|
279
|
+
once a minute: a failed try is tried again, and the clock is set again
|
|
280
|
+
each day. The setting runs on the shared worker (`appkit.worker`), so the
|
|
281
|
+
screen keeps going. Call it on the main thread, as `boot` and `up` do;
|
|
282
|
+
once it has asked, calling it again leaves the rest to the timer."""
|
|
283
|
+
global _clock_timer
|
|
284
|
+
if _clock_timer is None:
|
|
285
|
+
import lvgl as lv
|
|
286
|
+
|
|
287
|
+
if not lv.is_initialized():
|
|
288
|
+
# An LVGL timer made before lv.init() crashes the interpreter.
|
|
289
|
+
raise RuntimeError("touchkit.net keeps the clock with an LVGL timer: start the display first")
|
|
290
|
+
_clock_timer = lv.timer_create(_set_clock_if_due, _CLOCK_CHECK_MS, None)
|
|
291
|
+
if not _clock_asked:
|
|
292
|
+
_set_clock_if_due()
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def setup_job():
|
|
296
|
+
"""The phone setup page until a network is joined: a job for the
|
|
297
|
+
foreground loop (``scope.foreground(..., steps=False)``): it blocks."""
|
|
298
|
+
try:
|
|
299
|
+
import wifi_manager
|
|
300
|
+
|
|
301
|
+
joined = wifi_manager.run()
|
|
302
|
+
except Exception as e:
|
|
303
|
+
print("touchkit: wifi setup:", e)
|
|
304
|
+
joined = False
|
|
305
|
+
if joined:
|
|
306
|
+
start_webrepl()
|
|
307
|
+
keep_time() # outside the try: a missing deskkit must be seen
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def rssi():
|
|
311
|
+
"""The signal from the access point in dBm, or None off the network."""
|
|
312
|
+
try:
|
|
313
|
+
import network
|
|
314
|
+
|
|
315
|
+
return network.WLAN(network.STA_IF).status("rssi")
|
|
316
|
+
except ImportError:
|
|
317
|
+
r = _radio()
|
|
318
|
+
info = r.ap_info if r is not None else None
|
|
319
|
+
return info.rssi if info is not None else None
|
|
320
|
+
except Exception:
|
|
321
|
+
return None
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def ip():
|
|
325
|
+
"""This board's IPv4 address, or None off the network."""
|
|
326
|
+
try:
|
|
327
|
+
import network
|
|
328
|
+
|
|
329
|
+
wl = network.WLAN(network.STA_IF)
|
|
330
|
+
if wl.isconnected():
|
|
331
|
+
return wl.ifconfig()[0]
|
|
332
|
+
except ImportError:
|
|
333
|
+
r = _radio()
|
|
334
|
+
if r is None:
|
|
335
|
+
return _host_ip()
|
|
336
|
+
addr = r.ipv4_address
|
|
337
|
+
return str(addr) if addr else None
|
|
338
|
+
except Exception:
|
|
339
|
+
pass
|
|
340
|
+
return None
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def ntp(timeout=2):
|
|
344
|
+
"""Set the clock from NTP (a blocking call: run it on the worker)."""
|
|
345
|
+
from appkit import net as appnet
|
|
346
|
+
|
|
347
|
+
appnet.ntp(timeout)
|
touchkit/net.pyi
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Wi-Fi for the board's apps: when it's on is the set's call."""
|
|
5
|
+
|
|
6
|
+
from . import settings as settings
|
|
7
|
+
|
|
8
|
+
def hostname() -> str | None:
|
|
9
|
+
"""The board's name on the network, without `.local`, or None."""
|
|
10
|
+
def up() -> bool:
|
|
11
|
+
"""True when the station is connected; when it isn't, starts a join."""
|
|
12
|
+
def start_webrepl() -> None:
|
|
13
|
+
"""Start WebREPL, once, when `settings.json` doesn't turn it off."""
|
|
14
|
+
def webrepl_on() -> bool:
|
|
15
|
+
"""True when WebREPL is listening, whoever started it."""
|
|
16
|
+
def boot() -> None:
|
|
17
|
+
"""Join a known network quietly, then start WebREPL."""
|
|
18
|
+
def keep_time() -> None:
|
|
19
|
+
"""Set the board's clock once the station has joined, and again each day."""
|
|
20
|
+
def setup_job() -> None:
|
|
21
|
+
"""The phone setup page until a network is joined: a job for the foreground loop."""
|
|
22
|
+
def rssi() -> int | None:
|
|
23
|
+
"""The signal from the access point in dBm, or None off the network."""
|
|
24
|
+
def ip() -> str | None:
|
|
25
|
+
"""This board's IPv4 address, or None off the network."""
|
|
26
|
+
def ntp(timeout: int = 2) -> None:
|
|
27
|
+
"""Set the clock from NTP (a blocking call: run it on the worker)."""
|
touchkit/py.typed
ADDED
|
File without changes
|
touchkit/settings.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""touchkit.settings -- what one board has that another doesn't, kept in JSON.
|
|
5
|
+
|
|
6
|
+
Two kinds of file, both optional, both in appkit's data root (``/data`` on
|
|
7
|
+
the board):
|
|
8
|
+
|
|
9
|
+
``/data/settings.json``
|
|
10
|
+
The whole set's: ``{"webrepl": false}`` keeps WebREPL off, say. The
|
|
11
|
+
board's name isn't here: ``boarddev.name("my-panel")`` sets it, and
|
|
12
|
+
`move_hostname` moves a ``"hostname"`` an older set saved here into it.
|
|
13
|
+
|
|
14
|
+
``/data/<app>/config.json``
|
|
15
|
+
One app's: the Roku remote's ``{"prefer": "Living room"}`` picks the TV
|
|
16
|
+
whose name holds that text when there are several, say. Each app's
|
|
17
|
+
docstring lists its keys.
|
|
18
|
+
|
|
19
|
+
Code holds the defaults; a file holds only what differs.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import json
|
|
23
|
+
|
|
24
|
+
from appkit import data
|
|
25
|
+
|
|
26
|
+
#: The set's defaults; ``settings.json`` overrides any of them.
|
|
27
|
+
DEFAULTS = {
|
|
28
|
+
"webrepl": True, # start WebREPL once Wi-Fi is up (its password is /webrepl_cfg.py's)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_settings = None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _read(path):
|
|
35
|
+
try:
|
|
36
|
+
with open(path, "rb") as f: # UTF-8 whatever the desktop's codepage
|
|
37
|
+
d = json.loads(f.read().decode("utf-8"))
|
|
38
|
+
except (OSError, ValueError):
|
|
39
|
+
return {}
|
|
40
|
+
return d if isinstance(d, dict) else {}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get(key, default=None):
|
|
44
|
+
"""A set-wide setting: ``settings.json``'s, else the default."""
|
|
45
|
+
global _settings
|
|
46
|
+
if _settings is None:
|
|
47
|
+
s = dict(DEFAULTS)
|
|
48
|
+
s.update(_read(data.join(data.root(), "settings.json")))
|
|
49
|
+
_settings = s
|
|
50
|
+
return _settings.get(key, default)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def move_hostname():
|
|
54
|
+
"""Make an older set's ``"hostname"`` setting the board's name, once.
|
|
55
|
+
|
|
56
|
+
The key comes out of ``settings.json`` either way. A name the board can't
|
|
57
|
+
take (a space, say) is dropped, with the reason printed. Returns the name
|
|
58
|
+
moved, or None.
|
|
59
|
+
"""
|
|
60
|
+
global _settings
|
|
61
|
+
path = data.join(data.root(), "settings.json")
|
|
62
|
+
saved = _read(path)
|
|
63
|
+
if "hostname" not in saved:
|
|
64
|
+
return None
|
|
65
|
+
name = saved.pop("hostname")
|
|
66
|
+
import boarddev
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
if name:
|
|
70
|
+
boarddev.name(name)
|
|
71
|
+
except ValueError as e:
|
|
72
|
+
print("touchkit: settings.json's hostname isn't kept:", e)
|
|
73
|
+
name = None
|
|
74
|
+
with open(path, "wb") as f:
|
|
75
|
+
f.write(json.dumps(saved).encode("utf-8"))
|
|
76
|
+
_settings = None
|
|
77
|
+
return name or None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def config(scope, defaults):
|
|
81
|
+
"""App *scope*'s settings: *defaults*, overridden by ``config.json`` in
|
|
82
|
+
its ``data_dir``. A dict, read afresh each time the app opens."""
|
|
83
|
+
c = dict(defaults)
|
|
84
|
+
c.update(_read(data.join(scope.data_dir, "config.json")))
|
|
85
|
+
return c
|
touchkit/settings.pyi
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""What one board has that another doesn't, kept in JSON."""
|
|
5
|
+
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from appkit.scope import AppScope
|
|
9
|
+
|
|
10
|
+
DEFAULTS: dict[str, Any]
|
|
11
|
+
|
|
12
|
+
def get(key: str, default: Any = None) -> Any:
|
|
13
|
+
"""A set-wide setting: `settings.json`'s, else the default."""
|
|
14
|
+
def config(scope: AppScope, defaults: dict[str, Any]) -> dict[str, Any]:
|
|
15
|
+
"""App `scope`'s settings: `defaults`, overridden by `config.json` in its `data_dir`."""
|
|
16
|
+
def move_hostname() -> str | None:
|
|
17
|
+
"""Make an older set's `"hostname"` setting the board's name, once; the name moved, or None."""
|