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
apps/chess/ble.py
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Two players over Bluetooth LE: a browser page and a board or computer.
|
|
5
|
+
|
|
6
|
+
A board, or a computer with Bluetooth, serves the Chess service while the
|
|
7
|
+
app runs (`Host`). A browser page can't find anything on the network, but
|
|
8
|
+
Chrome and Edge can open a Bluetooth chooser filtered on that service
|
|
9
|
+
(`Guest`): the person picks the board or computer, the page connects, and
|
|
10
|
+
asks for a game, so the page plays White.
|
|
11
|
+
|
|
12
|
+
The game's messages are `net`'s lines, each ended with a newline and cut
|
|
13
|
+
into pieces the link can carry. The central writes ``RX``; the peripheral
|
|
14
|
+
notifies ``TX``. GATT delivers in order and the link says when it drops, so
|
|
15
|
+
nothing here acknowledges or sends again.
|
|
16
|
+
|
|
17
|
+
bledev runs on asyncio and the app's screen runs no event loop. A browser
|
|
18
|
+
page runs asyncio's tasks on its own, on MicroPython or Pyodide; a board runs
|
|
19
|
+
them a step at a time from `poll`; a computer runs them on a thread of their
|
|
20
|
+
own.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import asyncio
|
|
24
|
+
import sys
|
|
25
|
+
|
|
26
|
+
from .net import line, parse
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
import bledev
|
|
30
|
+
except ImportError: # no Bluetooth here: the app plays over the network alone
|
|
31
|
+
bledev = None
|
|
32
|
+
|
|
33
|
+
SERVICE = "8f1d0001-6c2e-4b8a-9a63-c4e55e1f3a70"
|
|
34
|
+
RX = "8f1d0002-6c2e-4b8a-9a63-c4e55e1f3a70"
|
|
35
|
+
TX = "8f1d0003-6c2e-4b8a-9a63-c4e55e1f3a70"
|
|
36
|
+
|
|
37
|
+
#: The ATT MTU asked for. A browser never says what it got, so the page
|
|
38
|
+
#: writes 20-byte pieces, which every link carries.
|
|
39
|
+
MTU = 185
|
|
40
|
+
|
|
41
|
+
#: How long a link may take to come up and subscribe, in ms.
|
|
42
|
+
CONNECT_MS = 15000
|
|
43
|
+
|
|
44
|
+
BROWSER = sys.platform in ("webassembly", "emscripten")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
async def _yield():
|
|
48
|
+
await asyncio.sleep_ms(0)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class _Steps:
|
|
52
|
+
"""asyncio on MicroPython, or in a browser page: on a board each `step`
|
|
53
|
+
runs the ready tasks once; in a page the page's event loop runs them."""
|
|
54
|
+
|
|
55
|
+
def __init__(self):
|
|
56
|
+
self.asyncio = asyncio
|
|
57
|
+
|
|
58
|
+
def start(self, coro):
|
|
59
|
+
if sys.implementation.name != "micropython":
|
|
60
|
+
# Pyodide's loop is the page's; it runs whether or not Python is.
|
|
61
|
+
return self.asyncio.ensure_future(coro)
|
|
62
|
+
return self.asyncio.create_task(coro)
|
|
63
|
+
|
|
64
|
+
def cancel(self, task):
|
|
65
|
+
task.cancel()
|
|
66
|
+
|
|
67
|
+
def step(self):
|
|
68
|
+
if not BROWSER:
|
|
69
|
+
self.asyncio.run(_yield())
|
|
70
|
+
|
|
71
|
+
def finish(self, coro):
|
|
72
|
+
# Nothing steps a board's tasks once the app has gone, so its last
|
|
73
|
+
# one runs to the end here.
|
|
74
|
+
if BROWSER:
|
|
75
|
+
self.start(coro)
|
|
76
|
+
else:
|
|
77
|
+
self.asyncio.run(coro)
|
|
78
|
+
|
|
79
|
+
def stop(self):
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class _Thread:
|
|
84
|
+
"""asyncio on CPython: an event loop on a thread of its own."""
|
|
85
|
+
|
|
86
|
+
def __init__(self):
|
|
87
|
+
import threading
|
|
88
|
+
|
|
89
|
+
self.asyncio = asyncio
|
|
90
|
+
self.loop = asyncio.new_event_loop()
|
|
91
|
+
self.thread = threading.Thread(target=self.loop.run_forever, name="chess-ble", daemon=True)
|
|
92
|
+
self.thread.start()
|
|
93
|
+
|
|
94
|
+
def start(self, coro):
|
|
95
|
+
return self.asyncio.run_coroutine_threadsafe(coro, self.loop)
|
|
96
|
+
|
|
97
|
+
def cancel(self, task):
|
|
98
|
+
task.cancel()
|
|
99
|
+
|
|
100
|
+
def step(self):
|
|
101
|
+
pass
|
|
102
|
+
|
|
103
|
+
def finish(self, coro):
|
|
104
|
+
try:
|
|
105
|
+
self.start(coro).result(5)
|
|
106
|
+
except Exception as e:
|
|
107
|
+
print("chess: closing Bluetooth: %r" % (e,))
|
|
108
|
+
|
|
109
|
+
def stop(self):
|
|
110
|
+
self.loop.call_soon_threadsafe(self.loop.stop)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _runner():
|
|
114
|
+
# Pyodide is CPython, but a page can't start a thread.
|
|
115
|
+
if sys.implementation.name == "micropython" or BROWSER:
|
|
116
|
+
return _Steps()
|
|
117
|
+
return _Thread()
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
async def _sleep(ms):
|
|
121
|
+
await bledev.sleep_ms(ms)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class _Side:
|
|
125
|
+
"""What `Host` and `Guest` share: the messages in and out, and the
|
|
126
|
+
same calls as `net.Match`, so the app plays a game over either."""
|
|
127
|
+
|
|
128
|
+
#: Leaves room for a ``(kind, args)`` from the link itself.
|
|
129
|
+
find = None
|
|
130
|
+
|
|
131
|
+
def __init__(self, name, runner=None):
|
|
132
|
+
self.name = name
|
|
133
|
+
self.runner = runner or _runner()
|
|
134
|
+
self.connection = None
|
|
135
|
+
self.peer = None # the other device's Bluetooth name, while connected
|
|
136
|
+
self.error = None
|
|
137
|
+
self._in = [] # whole lines, and ("unreachable", [why]) notes
|
|
138
|
+
self._out = []
|
|
139
|
+
self._drop = False
|
|
140
|
+
self._closed = False
|
|
141
|
+
self._task = None
|
|
142
|
+
|
|
143
|
+
@property
|
|
144
|
+
def alive(self):
|
|
145
|
+
c = self.connection
|
|
146
|
+
return c is not None and c.is_connected()
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def waiting(self):
|
|
150
|
+
return len(self._out)
|
|
151
|
+
|
|
152
|
+
def send(self, kind, *args):
|
|
153
|
+
self._out.append(line(kind, *args))
|
|
154
|
+
|
|
155
|
+
def poll(self):
|
|
156
|
+
"""The messages that arrived, as ``(kind, args)``."""
|
|
157
|
+
self.runner.step()
|
|
158
|
+
got = []
|
|
159
|
+
while self._in:
|
|
160
|
+
item = self._in.pop(0)
|
|
161
|
+
msg = item if isinstance(item, tuple) else parse(item)
|
|
162
|
+
if msg is not None:
|
|
163
|
+
got.append(msg)
|
|
164
|
+
return got
|
|
165
|
+
|
|
166
|
+
def forget_peer(self):
|
|
167
|
+
"""Hang up once what's waiting (a ``bye``, say) has gone."""
|
|
168
|
+
if self.connection is not None:
|
|
169
|
+
self._drop = True
|
|
170
|
+
else:
|
|
171
|
+
self._out = []
|
|
172
|
+
|
|
173
|
+
def close(self):
|
|
174
|
+
self._closed = True
|
|
175
|
+
self._drop = True
|
|
176
|
+
task, self._task = self._task, None
|
|
177
|
+
if task is not None:
|
|
178
|
+
self.runner.finish(self._shut(task))
|
|
179
|
+
self.runner.stop()
|
|
180
|
+
|
|
181
|
+
async def _shut(self, task):
|
|
182
|
+
# Let a last "bye" go, then hang up and let the radio go.
|
|
183
|
+
if self.alive and self._out:
|
|
184
|
+
try:
|
|
185
|
+
await self._send_out(self.connection, 600)
|
|
186
|
+
except Exception:
|
|
187
|
+
pass
|
|
188
|
+
self.runner.cancel(task)
|
|
189
|
+
try:
|
|
190
|
+
c = self.connection
|
|
191
|
+
if c is not None and c.is_connected():
|
|
192
|
+
await c.disconnect()
|
|
193
|
+
except Exception:
|
|
194
|
+
pass
|
|
195
|
+
self._release()
|
|
196
|
+
|
|
197
|
+
def _release(self):
|
|
198
|
+
pass
|
|
199
|
+
|
|
200
|
+
# --- the link, on the runner
|
|
201
|
+
|
|
202
|
+
def _feed(self, buf, data):
|
|
203
|
+
# The lines in buf + data go to the inbox; returns what's left.
|
|
204
|
+
buf = buf + data
|
|
205
|
+
while True:
|
|
206
|
+
i = buf.find(b"\n")
|
|
207
|
+
if i < 0:
|
|
208
|
+
return buf
|
|
209
|
+
text = buf[:i]
|
|
210
|
+
buf = buf[i + 1 :]
|
|
211
|
+
try:
|
|
212
|
+
self._in.append(text.decode())
|
|
213
|
+
except UnicodeError:
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
async def _send_out(self, connection, budget_ms=None):
|
|
217
|
+
while self._out and connection.is_connected():
|
|
218
|
+
data = (self._out[0] + "\n").encode()
|
|
219
|
+
size = self._piece(connection)
|
|
220
|
+
for i in range(0, len(data), size):
|
|
221
|
+
await self._chunk(connection, data[i : i + size])
|
|
222
|
+
self._out.pop(0)
|
|
223
|
+
|
|
224
|
+
def _piece(self, connection):
|
|
225
|
+
return max(20, min(connection.mtu, MTU) - 3)
|
|
226
|
+
|
|
227
|
+
async def _talk(self, connection, receive):
|
|
228
|
+
"""Carry messages both ways until the link drops or is let go."""
|
|
229
|
+
self.connection = connection
|
|
230
|
+
self._drop = False
|
|
231
|
+
buf = b""
|
|
232
|
+
ours = False
|
|
233
|
+
try:
|
|
234
|
+
while connection.is_connected() and not self._closed:
|
|
235
|
+
try:
|
|
236
|
+
data = await receive(40)
|
|
237
|
+
if data:
|
|
238
|
+
buf = self._feed(buf, bytes(data))
|
|
239
|
+
except bledev.BLETimeoutError:
|
|
240
|
+
pass
|
|
241
|
+
await self._send_out(connection)
|
|
242
|
+
if self._drop and not self._out:
|
|
243
|
+
ours = True
|
|
244
|
+
await connection.disconnect()
|
|
245
|
+
break
|
|
246
|
+
except bledev.DisconnectedError:
|
|
247
|
+
pass
|
|
248
|
+
finally:
|
|
249
|
+
self.connection = None
|
|
250
|
+
self.peer = None
|
|
251
|
+
self._drop = False
|
|
252
|
+
self._out = []
|
|
253
|
+
if not ours and not self._closed:
|
|
254
|
+
# Gone from the other end: the game can't go on.
|
|
255
|
+
self._in.append(("bye", []))
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class Host(_Side):
|
|
259
|
+
"""A board or computer, serving the Chess service to one browser page at
|
|
260
|
+
a time while the app runs. *ble* is a bledev adapter, made on the runner
|
|
261
|
+
when not given (Windows wants its own thread's)."""
|
|
262
|
+
|
|
263
|
+
def __init__(self, name, ble=None, runner=None):
|
|
264
|
+
_Side.__init__(self, name, runner)
|
|
265
|
+
self.ble = ble
|
|
266
|
+
self._was_on = True
|
|
267
|
+
self._task = self.runner.start(self._serve())
|
|
268
|
+
|
|
269
|
+
def _adapter(self):
|
|
270
|
+
if self.ble is not None:
|
|
271
|
+
return self.ble
|
|
272
|
+
from bledev import auto
|
|
273
|
+
|
|
274
|
+
if sys.implementation.name == "micropython":
|
|
275
|
+
import bluetooth
|
|
276
|
+
|
|
277
|
+
self._was_on = bluetooth.BLE().active()
|
|
278
|
+
self.ble = auto.adapter()
|
|
279
|
+
return self.ble
|
|
280
|
+
|
|
281
|
+
async def _serve(self):
|
|
282
|
+
try:
|
|
283
|
+
ble = self._adapter()
|
|
284
|
+
try:
|
|
285
|
+
ble.config(mtu=MTU)
|
|
286
|
+
except bledev.UnsupportedError:
|
|
287
|
+
pass
|
|
288
|
+
service = bledev.Service(SERVICE)
|
|
289
|
+
rx = bledev.Characteristic(service, RX, write=True, write_no_response=True, capture=True, max_len=bledev.MAX_MTU - 3)
|
|
290
|
+
self._tx = bledev.Characteristic(service, TX, notify=True)
|
|
291
|
+
ble.register_services(service)
|
|
292
|
+
# The board's own name; Windows advertises the PC's.
|
|
293
|
+
name = bledev.advertised_name(ble, None, lambda: self.name)
|
|
294
|
+
except Exception as e:
|
|
295
|
+
self.error = e
|
|
296
|
+
print("chess: no Bluetooth game here: %r" % (e,))
|
|
297
|
+
return
|
|
298
|
+
while not self._closed:
|
|
299
|
+
try:
|
|
300
|
+
connection = await ble.advertise(100000, name=name, services=[SERVICE])
|
|
301
|
+
except Exception as e:
|
|
302
|
+
if self._closed:
|
|
303
|
+
return
|
|
304
|
+
self.error = e
|
|
305
|
+
print("chess: advertising: %r" % (e,))
|
|
306
|
+
await _sleep(2000)
|
|
307
|
+
continue
|
|
308
|
+
self.peer = getattr(connection.device, "name", None)
|
|
309
|
+
self._out = []
|
|
310
|
+
|
|
311
|
+
async def receive(ms, rx=rx, connection=connection):
|
|
312
|
+
got, data = await rx.written(timeout_ms=ms)
|
|
313
|
+
return data if got == connection or got is connection else None
|
|
314
|
+
|
|
315
|
+
try:
|
|
316
|
+
await self._talk(connection, receive)
|
|
317
|
+
except Exception as e:
|
|
318
|
+
self.error = e
|
|
319
|
+
print("chess: Bluetooth link: %r" % (e,))
|
|
320
|
+
try:
|
|
321
|
+
await connection.disconnect()
|
|
322
|
+
except Exception:
|
|
323
|
+
pass
|
|
324
|
+
|
|
325
|
+
async def _chunk(self, connection, piece):
|
|
326
|
+
while True:
|
|
327
|
+
try:
|
|
328
|
+
self._tx.notify(connection, piece)
|
|
329
|
+
return
|
|
330
|
+
except bledev.BusyError:
|
|
331
|
+
if not connection.is_connected():
|
|
332
|
+
raise bledev.DisconnectedError("link closed while sending")
|
|
333
|
+
await _sleep(2)
|
|
334
|
+
|
|
335
|
+
def _release(self):
|
|
336
|
+
if self.ble is not None and not self._was_on:
|
|
337
|
+
try:
|
|
338
|
+
self.ble.close()
|
|
339
|
+
except Exception:
|
|
340
|
+
pass
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
class Guest(_Side):
|
|
344
|
+
"""A browser page's side: `pick` opens the browser's Bluetooth chooser
|
|
345
|
+
and asks whoever is picked for a game."""
|
|
346
|
+
|
|
347
|
+
def __init__(self, name, ble=None, runner=None):
|
|
348
|
+
_Side.__init__(self, name, runner)
|
|
349
|
+
self.ble = ble
|
|
350
|
+
self._picking = False
|
|
351
|
+
|
|
352
|
+
def pick(self):
|
|
353
|
+
"""Open the chooser. Call it from a tap: the browser asks for one.
|
|
354
|
+
The game's answer comes through `poll`, or ``("unreachable",
|
|
355
|
+
[why])`` when nothing was picked or the link didn't come up."""
|
|
356
|
+
if self._picking:
|
|
357
|
+
return
|
|
358
|
+
self._picking = True
|
|
359
|
+
self._drop = False
|
|
360
|
+
self._task = self.runner.start(self._visit())
|
|
361
|
+
|
|
362
|
+
async def _visit(self):
|
|
363
|
+
try:
|
|
364
|
+
if self.ble is None:
|
|
365
|
+
from bledev import webble
|
|
366
|
+
|
|
367
|
+
self.ble = webble.WebBLE(services=[bledev.UUID(SERVICE)])
|
|
368
|
+
try:
|
|
369
|
+
device = await self.ble.find(service=SERVICE)
|
|
370
|
+
except bledev.BLETimeoutError:
|
|
371
|
+
self._in.append(("unreachable", ["nothing picked"]))
|
|
372
|
+
return
|
|
373
|
+
|
|
374
|
+
async def setup(connection):
|
|
375
|
+
service = await connection.service(bledev.UUID(SERVICE))
|
|
376
|
+
if service is None:
|
|
377
|
+
raise OSError("%s isn't playing Chess" % (device.name,))
|
|
378
|
+
rx = await service.characteristic(bledev.UUID(RX))
|
|
379
|
+
tx = await service.characteristic(bledev.UUID(TX))
|
|
380
|
+
await tx.subscribe(notify=True)
|
|
381
|
+
return rx, tx
|
|
382
|
+
|
|
383
|
+
connection, (rx, tx) = await bledev.connect_and_set_up(
|
|
384
|
+
self.ble, setup, device=device, timeout_ms=CONNECT_MS, setup_timeout_ms=CONNECT_MS
|
|
385
|
+
)
|
|
386
|
+
except Exception as e:
|
|
387
|
+
self.error = e
|
|
388
|
+
print("chess: Bluetooth: %r" % (e,))
|
|
389
|
+
self._in.append(("unreachable", [str(e)]))
|
|
390
|
+
return
|
|
391
|
+
finally:
|
|
392
|
+
self._picking = False
|
|
393
|
+
self._rx = rx
|
|
394
|
+
self.peer = device.name
|
|
395
|
+
self._out = [line("invite", self.name)]
|
|
396
|
+
|
|
397
|
+
async def receive(ms):
|
|
398
|
+
return await tx.notified(timeout_ms=ms)
|
|
399
|
+
|
|
400
|
+
try:
|
|
401
|
+
await self._talk(connection, receive)
|
|
402
|
+
except Exception as e:
|
|
403
|
+
self.error = e
|
|
404
|
+
print("chess: Bluetooth link: %r" % (e,))
|
|
405
|
+
try:
|
|
406
|
+
await connection.disconnect()
|
|
407
|
+
except Exception:
|
|
408
|
+
pass
|
|
409
|
+
|
|
410
|
+
async def _chunk(self, connection, piece):
|
|
411
|
+
await self._rx.write(piece)
|