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/game.py
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""A game of chess under the full rules, on the engine's board.
|
|
5
|
+
|
|
6
|
+
`Game` holds the moves played and knows what is legal, when the game is
|
|
7
|
+
over and why: checkmate, stalemate, the fifty-move rule, threefold
|
|
8
|
+
repetition or too little material to mate. It writes each move in standard
|
|
9
|
+
notation, takes moves back, and plays the engine at a `LEVELS` level.
|
|
10
|
+
Nothing here draws or touches the network, so it runs anywhere Python does.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from . import engine
|
|
14
|
+
from .engine import (
|
|
15
|
+
BISHOP,
|
|
16
|
+
KING,
|
|
17
|
+
KNIGHT,
|
|
18
|
+
PAWN,
|
|
19
|
+
QUEEN,
|
|
20
|
+
ROOK,
|
|
21
|
+
WHITE,
|
|
22
|
+
square_name,
|
|
23
|
+
square_of,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
#: The engine's levels: a name, how many plies it looks ahead (0 is the
|
|
27
|
+
#: moves' weights alone), how long it may think in ms, and how much noise
|
|
28
|
+
#: (in points; a pawn is 20) it adds to each move's score. The first two
|
|
29
|
+
#: overlook pieces left hanging, so a beginner can win. On an ESP32-S3 the
|
|
30
|
+
#: engine looks at a few hundred positions a second, so there the times,
|
|
31
|
+
#: not the depths, set how far the top three see.
|
|
32
|
+
LEVELS = (
|
|
33
|
+
("Beginner", 0, None, 50),
|
|
34
|
+
("Novice", 1, None, 24),
|
|
35
|
+
("Casual", 2, 3000, 8),
|
|
36
|
+
("Club", 3, 4000, 0),
|
|
37
|
+
("Strong", 8, 5000, 0),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
#: Why a game ended, by the word `Game.result` carries.
|
|
41
|
+
REASONS = {
|
|
42
|
+
"checkmate": "Checkmate",
|
|
43
|
+
"stalemate": "Stalemate",
|
|
44
|
+
"fifty": "Draw by the fifty-move rule",
|
|
45
|
+
"repetition": "Draw by repetition",
|
|
46
|
+
"material": "Draw: not enough to mate",
|
|
47
|
+
"resigned": "Resigned",
|
|
48
|
+
"agreed": "Draw agreed",
|
|
49
|
+
"left": "The other player left",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
PROMOTIONS = (QUEEN, ROOK, BISHOP, KNIGHT)
|
|
53
|
+
LETTERS = {QUEEN: "q", ROOK: "r", BISHOP: "b", KNIGHT: "n"}
|
|
54
|
+
PIECE_OF = {"q": QUEEN, "r": ROOK, "b": BISHOP, "n": KNIGHT}
|
|
55
|
+
_SAN = {ROOK: "R", KNIGHT: "N", BISHOP: "B", KING: "K", QUEEN: "Q"}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Move:
|
|
59
|
+
"""One move played, with what a screen needs to show it.
|
|
60
|
+
|
|
61
|
+
``frm`` and ``to`` are squares (21 is a1). ``piece`` is what moved and
|
|
62
|
+
``promotion`` what a pawn became (0 if none). ``captured`` is the square
|
|
63
|
+
of the piece taken (the passed pawn's own square en passant) or 0, and
|
|
64
|
+
``rook`` is ``(from, to)`` for the rook when castling, else None.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
def __init__(self, frm, to, piece, promotion, captured, rook, san):
|
|
68
|
+
self.frm = frm
|
|
69
|
+
self.to = to
|
|
70
|
+
self.piece = piece
|
|
71
|
+
self.promotion = promotion
|
|
72
|
+
self.captured = captured
|
|
73
|
+
self.rook = rook
|
|
74
|
+
self.san = san
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def uci(self):
|
|
78
|
+
"""``"e2e4"``, or ``"e7e8q"`` for a promotion."""
|
|
79
|
+
return square_name(self.frm) + square_name(self.to) + (LETTERS[self.promotion & 14] if self.promotion else "")
|
|
80
|
+
|
|
81
|
+
def __repr__(self):
|
|
82
|
+
return "<Move %s %s>" % (self.uci, self.san)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def parse_uci(text):
|
|
86
|
+
"""``(from, to, promotion)`` from ``"e7e8q"``; None when it isn't one."""
|
|
87
|
+
if len(text) not in (4, 5):
|
|
88
|
+
return None
|
|
89
|
+
frm = square_of(text[:2])
|
|
90
|
+
to = square_of(text[2:4])
|
|
91
|
+
promo = PIECE_OF.get(text[4:], 0) if len(text) == 5 else 0
|
|
92
|
+
if frm is None or to is None or (len(text) == 5 and not promo):
|
|
93
|
+
return None
|
|
94
|
+
return frm, to, promo
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class Game:
|
|
98
|
+
"""A game from *fen* (the usual start by default).
|
|
99
|
+
|
|
100
|
+
``result`` is None while it goes on, else ``(reason, winner)``: a key
|
|
101
|
+
of `REASONS` and `engine.WHITE`, `engine.BLACK` or None for a draw.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def __init__(self, fen=engine.START):
|
|
105
|
+
self.start = fen
|
|
106
|
+
self.pos = engine.Position(fen)
|
|
107
|
+
self.moves = []
|
|
108
|
+
self.result = None
|
|
109
|
+
self._counts = {}
|
|
110
|
+
self._keys = []
|
|
111
|
+
self._legal = None
|
|
112
|
+
self._remember()
|
|
113
|
+
|
|
114
|
+
# --- what the board holds ----------------------------------------------
|
|
115
|
+
|
|
116
|
+
@property
|
|
117
|
+
def turn(self):
|
|
118
|
+
"""`engine.WHITE` or `engine.BLACK`: whose move it is."""
|
|
119
|
+
return self.pos.to_play
|
|
120
|
+
|
|
121
|
+
def piece(self, square):
|
|
122
|
+
"""The piece on *square*: 0 for none, else a piece number with the
|
|
123
|
+
colour in its low bit."""
|
|
124
|
+
return self.pos.board[square]
|
|
125
|
+
|
|
126
|
+
def squares(self):
|
|
127
|
+
"""``[(square, piece), ...]`` for every piece on the board."""
|
|
128
|
+
board = self.pos.board
|
|
129
|
+
return [(i, board[i]) for i in range(21, 99) if board[i] and board[i] != engine.EDGE]
|
|
130
|
+
|
|
131
|
+
def in_check(self):
|
|
132
|
+
"""True when the side to move is in check."""
|
|
133
|
+
return self.pos.in_check(self.turn)
|
|
134
|
+
|
|
135
|
+
def king(self, colour):
|
|
136
|
+
"""The square *colour*'s king stands on."""
|
|
137
|
+
k = KING | colour
|
|
138
|
+
board = self.pos.board
|
|
139
|
+
for i in range(21, 99):
|
|
140
|
+
if board[i] == k:
|
|
141
|
+
return i
|
|
142
|
+
return 0
|
|
143
|
+
|
|
144
|
+
@property
|
|
145
|
+
def last(self):
|
|
146
|
+
"""The last `Move` played, or None."""
|
|
147
|
+
return self.moves[-1] if self.moves else None
|
|
148
|
+
|
|
149
|
+
# --- what may be played ------------------------------------------------
|
|
150
|
+
|
|
151
|
+
def legal(self):
|
|
152
|
+
"""``[(from, to), ...]``: every legal move, a promotion once."""
|
|
153
|
+
if self._legal is None:
|
|
154
|
+
if self.result is not None:
|
|
155
|
+
self._legal = []
|
|
156
|
+
else:
|
|
157
|
+
self._legal = [(mv[1], mv[2]) for mv in self.pos.legal_moves()]
|
|
158
|
+
return self._legal
|
|
159
|
+
|
|
160
|
+
def targets(self, square):
|
|
161
|
+
"""The squares the piece on *square* may move to."""
|
|
162
|
+
return [e for s, e in self.legal() if s == square]
|
|
163
|
+
|
|
164
|
+
def is_promotion(self, frm, to):
|
|
165
|
+
"""True when the move takes a pawn to the last row."""
|
|
166
|
+
return (self.pos.board[frm] & 14) == PAWN and (to > 90 or to < 30)
|
|
167
|
+
|
|
168
|
+
# --- playing -----------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
def play(self, frm, to, promotion=QUEEN):
|
|
171
|
+
"""Play a move and return its `Move`; None when it isn't legal.
|
|
172
|
+
|
|
173
|
+
*promotion* is the piece a pawn becomes on the last row: `QUEEN`,
|
|
174
|
+
`ROOK`, `BISHOP` or `KNIGHT`.
|
|
175
|
+
"""
|
|
176
|
+
if (frm, to) not in self.legal():
|
|
177
|
+
return None
|
|
178
|
+
pos = self.pos
|
|
179
|
+
board = pos.board
|
|
180
|
+
piece = board[frm]
|
|
181
|
+
colour = piece & 1
|
|
182
|
+
promo = 0
|
|
183
|
+
if self.is_promotion(frm, to):
|
|
184
|
+
promo = (promotion & 14) | colour
|
|
185
|
+
if promo & 14 not in PROMOTIONS:
|
|
186
|
+
return None
|
|
187
|
+
san = self._san(frm, to, piece, promo)
|
|
188
|
+
captured = to if board[to] else 0
|
|
189
|
+
rook = None
|
|
190
|
+
undo = pos.make(frm, to, promo or QUEEN)
|
|
191
|
+
if undo[9]:
|
|
192
|
+
captured = undo[9] # en passant
|
|
193
|
+
if undo[6]:
|
|
194
|
+
rook = (undo[6], undo[7])
|
|
195
|
+
pos.enpassant = undo[4]
|
|
196
|
+
if captured or (piece & 14) == PAWN:
|
|
197
|
+
pos.draw_timeout = 0
|
|
198
|
+
else:
|
|
199
|
+
pos.draw_timeout += 1
|
|
200
|
+
pos.moveno += 1
|
|
201
|
+
pos.to_play = 1 - colour
|
|
202
|
+
pos.prepared = False
|
|
203
|
+
self._legal = None
|
|
204
|
+
move = Move(frm, to, piece, promo, captured, rook, san)
|
|
205
|
+
self.moves.append(move)
|
|
206
|
+
self._remember()
|
|
207
|
+
self._judge()
|
|
208
|
+
if self.result is not None and self.result[0] == "checkmate":
|
|
209
|
+
move.san += "#"
|
|
210
|
+
elif self.in_check():
|
|
211
|
+
move.san += "+"
|
|
212
|
+
return move
|
|
213
|
+
|
|
214
|
+
def play_uci(self, text):
|
|
215
|
+
"""Play a move written ``"e2e4"`` or ``"e7e8q"``; None when it isn't legal."""
|
|
216
|
+
parsed = parse_uci(text)
|
|
217
|
+
if parsed is None:
|
|
218
|
+
return None
|
|
219
|
+
frm, to, promo = parsed
|
|
220
|
+
if self.is_promotion(frm, to) != bool(promo):
|
|
221
|
+
return None
|
|
222
|
+
return self.play(frm, to, promo or QUEEN)
|
|
223
|
+
|
|
224
|
+
def undo(self, count=1):
|
|
225
|
+
"""Take back the last *count* moves (they're replayed from the start)."""
|
|
226
|
+
keep = self.moves[: max(0, len(self.moves) - count)]
|
|
227
|
+
self.__init__(self.start)
|
|
228
|
+
for m in keep:
|
|
229
|
+
self.play(m.frm, m.to, m.promotion or QUEEN)
|
|
230
|
+
|
|
231
|
+
def finish(self, reason, winner=None):
|
|
232
|
+
"""End the game for a reason the board can't see: a resignation,
|
|
233
|
+
an agreed draw, or a player who left."""
|
|
234
|
+
if self.result is None:
|
|
235
|
+
self.result = (reason, winner)
|
|
236
|
+
self._legal = None
|
|
237
|
+
|
|
238
|
+
def outcome(self):
|
|
239
|
+
"""The result in words, such as ``"Checkmate: White wins"``; "" while it goes on."""
|
|
240
|
+
if self.result is None:
|
|
241
|
+
return ""
|
|
242
|
+
reason, winner = self.result
|
|
243
|
+
if winner is None:
|
|
244
|
+
return REASONS[reason]
|
|
245
|
+
return "%s: %s wins" % (REASONS[reason], "White" if winner == WHITE else "Black")
|
|
246
|
+
|
|
247
|
+
def think(self, level, progress=None):
|
|
248
|
+
"""The engine's move at *level* (an index into `LEVELS`), as
|
|
249
|
+
``(from, to, promotion)``; None when there is none. Slow: call it
|
|
250
|
+
off the screen's thread, and `halt` it to stop early."""
|
|
251
|
+
_name, depth, ms, noise = LEVELS[level]
|
|
252
|
+
found = self.pos.think(depth, ms, noise, progress)
|
|
253
|
+
if found is None:
|
|
254
|
+
return None
|
|
255
|
+
frm, to = found[0], found[1]
|
|
256
|
+
return frm, to, (QUEEN if self.is_promotion(frm, to) else 0)
|
|
257
|
+
|
|
258
|
+
def halt(self):
|
|
259
|
+
"""Stop a `think` running on another thread."""
|
|
260
|
+
self.pos.halt()
|
|
261
|
+
|
|
262
|
+
# --- the rules the engine doesn't keep -----------------------------------
|
|
263
|
+
|
|
264
|
+
def _key(self):
|
|
265
|
+
# En passant counts towards a repetition only when it could be played.
|
|
266
|
+
pos = self.pos
|
|
267
|
+
ep = pos.enpassant
|
|
268
|
+
if ep:
|
|
269
|
+
pawn = PAWN | pos.to_play
|
|
270
|
+
back = ep - (10 - 20 * pos.to_play)
|
|
271
|
+
if pos.board[back - 1] != pawn and pos.board[back + 1] != pawn:
|
|
272
|
+
ep = 0
|
|
273
|
+
return bytes(pos.board[21:99]) + bytes((pos.to_play, pos.castles, ep))
|
|
274
|
+
|
|
275
|
+
def _remember(self):
|
|
276
|
+
key = self._key()
|
|
277
|
+
n = self._counts.get(key, 0) + 1
|
|
278
|
+
self._counts[key] = n
|
|
279
|
+
self._keys.append(key)
|
|
280
|
+
self.pos.current_repetitions = n
|
|
281
|
+
|
|
282
|
+
def _judge(self):
|
|
283
|
+
pos = self.pos
|
|
284
|
+
if not self.legal():
|
|
285
|
+
if self.in_check():
|
|
286
|
+
self.result = ("checkmate", 1 - pos.to_play)
|
|
287
|
+
else:
|
|
288
|
+
self.result = ("stalemate", None)
|
|
289
|
+
elif pos.draw_timeout >= 100:
|
|
290
|
+
self.result = ("fifty", None)
|
|
291
|
+
elif pos.current_repetitions >= 3:
|
|
292
|
+
self.result = ("repetition", None)
|
|
293
|
+
elif engine.insufficient_material(pos.board):
|
|
294
|
+
self.result = ("material", None)
|
|
295
|
+
if self.result is not None:
|
|
296
|
+
self._legal = []
|
|
297
|
+
|
|
298
|
+
def _san(self, frm, to, piece, promo):
|
|
299
|
+
kind = piece & 14
|
|
300
|
+
board = self.pos.board
|
|
301
|
+
if kind == KING and (frm - to) * (frm - to) == 4:
|
|
302
|
+
return "O-O" if to > frm else "O-O-O"
|
|
303
|
+
capture = board[to] != 0 or (kind == PAWN and (frm - to) % 10 != 0)
|
|
304
|
+
dest = square_name(to)
|
|
305
|
+
if kind == PAWN:
|
|
306
|
+
out = (square_name(frm)[0] + "x" + dest) if capture else dest
|
|
307
|
+
if promo:
|
|
308
|
+
out += "=" + _SAN[promo & 14]
|
|
309
|
+
return out
|
|
310
|
+
# Another piece of the same kind that could go there needs telling apart.
|
|
311
|
+
same_col = same_row = False
|
|
312
|
+
others = [s for s, e in self.legal() if e == to and s != frm and board[s] == piece]
|
|
313
|
+
for s in others:
|
|
314
|
+
if s % 10 == frm % 10:
|
|
315
|
+
same_col = True
|
|
316
|
+
if s // 10 == frm // 10:
|
|
317
|
+
same_row = True
|
|
318
|
+
name = square_name(frm)
|
|
319
|
+
qualifier = ""
|
|
320
|
+
if others:
|
|
321
|
+
if not same_col:
|
|
322
|
+
qualifier = name[0]
|
|
323
|
+
elif not same_row:
|
|
324
|
+
qualifier = name[1]
|
|
325
|
+
else:
|
|
326
|
+
qualifier = name
|
|
327
|
+
return _SAN[kind] + qualifier + ("x" if capture else "") + dest
|
apps/chess/net.py
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Two players on one network: finding each other, and the moves between them.
|
|
5
|
+
|
|
6
|
+
Each Chess app on the network advertises itself as ``chess-<name>`` through
|
|
7
|
+
medialink's `Network` finder (SSDP, and mDNS where the device has it), on
|
|
8
|
+
a UDP port of its own (`PORT` on a board). `Match.find` lists the others; `Match.invite` says
|
|
9
|
+
hello to one and asks for a game. From then on the two talk over a
|
|
10
|
+
medialink `Link`, peer to peer: each message is acknowledged, and sent again
|
|
11
|
+
until it is.
|
|
12
|
+
|
|
13
|
+
The messages are short lines of text, the same over any link:
|
|
14
|
+
|
|
15
|
+
``chess1 invite NAME``
|
|
16
|
+
NAME asks for a game. The one who asks plays White.
|
|
17
|
+
``chess1 accept NAME`` / ``chess1 decline NAME``
|
|
18
|
+
The answer.
|
|
19
|
+
``chess1 move PLY UCI``
|
|
20
|
+
A move, such as ``chess1 move 0 e2e4``. PLY counts from 0, so a move
|
|
21
|
+
sent twice is played once.
|
|
22
|
+
``chess1 resign``, ``chess1 new``, ``chess1 bye``
|
|
23
|
+
Resign, start again with the same colours, or leave.
|
|
24
|
+
|
|
25
|
+
`parse` and `line` read and write them, so another transport can carry the
|
|
26
|
+
same game.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from .engine import ticks_diff, ticks_ms
|
|
30
|
+
|
|
31
|
+
PROTOCOL = "chess1"
|
|
32
|
+
PREFIX = "chess-"
|
|
33
|
+
|
|
34
|
+
#: How often an unacknowledged message is sent again, in ms.
|
|
35
|
+
RETRY_MS = 1000
|
|
36
|
+
|
|
37
|
+
#: The UDP port a board listens on. A computer takes any free port, so two
|
|
38
|
+
#: games can run on one machine; a board's sockets can't say which port they
|
|
39
|
+
#: were given, so a board takes this one.
|
|
40
|
+
PORT = 47811
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def line(kind, *args):
|
|
44
|
+
"""The text of a message: ``line("move", 3, "e7e5")``."""
|
|
45
|
+
return " ".join([PROTOCOL, kind] + [str(a) for a in args])
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def parse(data):
|
|
49
|
+
"""``(kind, [args])`` from a message's bytes or text; None for anything
|
|
50
|
+
that isn't this protocol's."""
|
|
51
|
+
if isinstance(data, (bytes, bytearray, memoryview)):
|
|
52
|
+
try:
|
|
53
|
+
data = bytes(data).decode()
|
|
54
|
+
except UnicodeError:
|
|
55
|
+
return None
|
|
56
|
+
words = data.split()
|
|
57
|
+
if len(words) < 2 or words[0] != PROTOCOL:
|
|
58
|
+
return None
|
|
59
|
+
return words[1], words[2:]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Match:
|
|
63
|
+
"""This device's side of two-player games on the local network.
|
|
64
|
+
|
|
65
|
+
*name* is how it shows on the others' lists. *finder* is what finds
|
|
66
|
+
them: medialink's `Network` unless another is given (its `MDNS` on a
|
|
67
|
+
medialink too old to have `Network`). Raises OSError (or
|
|
68
|
+
ImportError) when the device has no network or no way to find peers.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
def __init__(self, name, finder=None):
|
|
72
|
+
import medialink
|
|
73
|
+
|
|
74
|
+
self._medialink = medialink
|
|
75
|
+
self.name = name
|
|
76
|
+
transport = medialink.UDPTransport(0).open()
|
|
77
|
+
if not transport.port:
|
|
78
|
+
transport.close()
|
|
79
|
+
transport = medialink.UDPTransport(PORT)
|
|
80
|
+
self.link = medialink.Link(transport, retries=1, dead_ms=8000)
|
|
81
|
+
try:
|
|
82
|
+
if finder is None:
|
|
83
|
+
# medialink before 0.8.2 has mDNS alone.
|
|
84
|
+
finder = medialink.Network() if hasattr(medialink, "Network") else medialink.MDNS()
|
|
85
|
+
self.finder = finder
|
|
86
|
+
self.finder.advertise(PREFIX + name, self.link.transport.port)
|
|
87
|
+
except Exception:
|
|
88
|
+
self.link.close()
|
|
89
|
+
raise
|
|
90
|
+
self._out = []
|
|
91
|
+
self._tried = None
|
|
92
|
+
# A send waits for its answer, and the screen's timers can run
|
|
93
|
+
# meanwhile; one of them polling must not send the same line again.
|
|
94
|
+
self._sending = False
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def alive(self):
|
|
98
|
+
"""True while the other player has been heard from lately."""
|
|
99
|
+
return self.link.alive
|
|
100
|
+
|
|
101
|
+
def find(self, timeout=2):
|
|
102
|
+
"""``[(name, address), ...]``: the other Chess apps on the network.
|
|
103
|
+
Blocks for *timeout* seconds: call it off the screen's thread."""
|
|
104
|
+
found = {}
|
|
105
|
+
for peer, address in self.finder.find(None, timeout=timeout):
|
|
106
|
+
if not peer.startswith(PREFIX) or peer == PREFIX + self.name:
|
|
107
|
+
continue
|
|
108
|
+
name = peer[len(PREFIX):]
|
|
109
|
+
# A computer may answer with its loopback address as well, which
|
|
110
|
+
# only reaches it from itself: keep the one the others can reach.
|
|
111
|
+
if name not in found or found[name][0].startswith("127."):
|
|
112
|
+
found[name] = address
|
|
113
|
+
return sorted(found.items())
|
|
114
|
+
|
|
115
|
+
def invite(self, address):
|
|
116
|
+
"""Say hello to the app at *address* and ask it for a game.
|
|
117
|
+
Raises `medialink.PeerUnreachable` when it doesn't answer."""
|
|
118
|
+
self._out = []
|
|
119
|
+
self._sending = True
|
|
120
|
+
try:
|
|
121
|
+
self.link.connect(address, retries=2)
|
|
122
|
+
finally:
|
|
123
|
+
self._sending = False
|
|
124
|
+
self.send("invite", self.name)
|
|
125
|
+
|
|
126
|
+
def send(self, kind, *args):
|
|
127
|
+
"""Queue a message for the other player and send what's waiting."""
|
|
128
|
+
self._out.append(line(kind, *args))
|
|
129
|
+
self.flush()
|
|
130
|
+
|
|
131
|
+
def flush(self):
|
|
132
|
+
"""Send the waiting messages, oldest first, until one isn't answered."""
|
|
133
|
+
if self._sending:
|
|
134
|
+
return False
|
|
135
|
+
unreachable = self._medialink.PeerUnreachable
|
|
136
|
+
self._sending = True
|
|
137
|
+
try:
|
|
138
|
+
while self._out and self.link.peer is not None:
|
|
139
|
+
try:
|
|
140
|
+
self.link.send_event(self._out[0], retries=0)
|
|
141
|
+
except unreachable:
|
|
142
|
+
self._tried = ticks_ms()
|
|
143
|
+
return False
|
|
144
|
+
self._out.pop(0)
|
|
145
|
+
finally:
|
|
146
|
+
self._sending = False
|
|
147
|
+
return True
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def waiting(self):
|
|
151
|
+
"""How many messages the other player hasn't acknowledged yet."""
|
|
152
|
+
return len(self._out)
|
|
153
|
+
|
|
154
|
+
def poll(self):
|
|
155
|
+
"""The messages that arrived, as ``(kind, args)``; retries what is
|
|
156
|
+
waiting every `RETRY_MS`."""
|
|
157
|
+
got = []
|
|
158
|
+
if self._sending:
|
|
159
|
+
return got
|
|
160
|
+
for data in self.link.events():
|
|
161
|
+
msg = parse(data)
|
|
162
|
+
if msg is not None:
|
|
163
|
+
got.append(msg)
|
|
164
|
+
if self._out and (self._tried is None or ticks_diff(ticks_ms(), self._tried) >= RETRY_MS):
|
|
165
|
+
self.flush()
|
|
166
|
+
return got
|
|
167
|
+
|
|
168
|
+
def forget_peer(self):
|
|
169
|
+
"""Stop talking to the current opponent, so another may say hello."""
|
|
170
|
+
self._out = []
|
|
171
|
+
self.link.peer = None
|
|
172
|
+
self.link.peer_token = None
|
|
173
|
+
|
|
174
|
+
def close(self):
|
|
175
|
+
"""Say goodbye, stop advertising and let the port go."""
|
|
176
|
+
if self.link.peer is not None and self.link.alive:
|
|
177
|
+
try:
|
|
178
|
+
self.link.send_event(line("bye"), retries=0)
|
|
179
|
+
except Exception:
|
|
180
|
+
pass
|
|
181
|
+
try:
|
|
182
|
+
self.finder.deinit()
|
|
183
|
+
finally:
|
|
184
|
+
self.link.close()
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class Matches:
|
|
188
|
+
"""Several ways to reach a friend at once, such as the network and
|
|
189
|
+
Bluetooth, behind `Match`'s calls. The game is with whichever one an
|
|
190
|
+
answer or a hello last came through; the others wait meanwhile."""
|
|
191
|
+
|
|
192
|
+
def __init__(self, name, matches):
|
|
193
|
+
self.name = name
|
|
194
|
+
self.matches = list(matches)
|
|
195
|
+
self.current = None
|
|
196
|
+
|
|
197
|
+
def _of(self, kind):
|
|
198
|
+
for m in self.matches:
|
|
199
|
+
if getattr(m, kind, None) is not None:
|
|
200
|
+
return m
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
@property
|
|
204
|
+
def can_find(self):
|
|
205
|
+
"""True when one of them can list the others nearby."""
|
|
206
|
+
return self._of("find") is not None
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def can_pick(self):
|
|
210
|
+
"""True when one of them asks the browser to pick a device."""
|
|
211
|
+
return self._of("pick") is not None
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def alive(self):
|
|
215
|
+
return self.current is not None and self.current.alive
|
|
216
|
+
|
|
217
|
+
@property
|
|
218
|
+
def waiting(self):
|
|
219
|
+
return 0 if self.current is None else self.current.waiting
|
|
220
|
+
|
|
221
|
+
def find(self, timeout=2):
|
|
222
|
+
found = []
|
|
223
|
+
for i, m in enumerate(self.matches):
|
|
224
|
+
if getattr(m, "find", None) is not None:
|
|
225
|
+
found += [(name, (i, address)) for name, address in m.find(timeout=timeout)]
|
|
226
|
+
return found
|
|
227
|
+
|
|
228
|
+
def invite(self, address):
|
|
229
|
+
i, address = address
|
|
230
|
+
self.current = self.matches[i]
|
|
231
|
+
self.current.invite(address)
|
|
232
|
+
|
|
233
|
+
def pick(self):
|
|
234
|
+
self.current = self._of("pick")
|
|
235
|
+
self.current.pick()
|
|
236
|
+
|
|
237
|
+
def send(self, kind, *args):
|
|
238
|
+
if self.current is not None:
|
|
239
|
+
self.current.send(kind, *args)
|
|
240
|
+
|
|
241
|
+
def poll(self):
|
|
242
|
+
got = []
|
|
243
|
+
for m in self.matches:
|
|
244
|
+
for msg in m.poll():
|
|
245
|
+
if self.current is None or (m is not self.current and not self.current.alive):
|
|
246
|
+
self.current = m
|
|
247
|
+
if m is self.current:
|
|
248
|
+
got.append(msg)
|
|
249
|
+
return got
|
|
250
|
+
|
|
251
|
+
def forget_peer(self):
|
|
252
|
+
if self.current is not None:
|
|
253
|
+
self.current.forget_peer()
|
|
254
|
+
self.current = None
|
|
255
|
+
|
|
256
|
+
def close(self):
|
|
257
|
+
for m in self.matches:
|
|
258
|
+
try:
|
|
259
|
+
m.close()
|
|
260
|
+
except Exception as e:
|
|
261
|
+
print("chess: closing: %r" % (e,))
|
apps/clock/__init__.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Clock: an analog clock that fills the screen, on the time at your chosen place."""
|
|
5
|
+
|
|
6
|
+
import lvgl as lv
|
|
7
|
+
from deskkit import forecast, localtime
|
|
8
|
+
from deskkit.clockface import AnalogClock
|
|
9
|
+
|
|
10
|
+
FACE = "Grand Classic"
|
|
11
|
+
_TICK_MS = 200 # the second hand is at most this late
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main(scope):
|
|
15
|
+
root = scope.root
|
|
16
|
+
root.remove_flag(lv.obj.FLAG.SCROLLABLE)
|
|
17
|
+
clock = AnalogClock(root, scope.size, localtime.clock_time, FACE)
|
|
18
|
+
|
|
19
|
+
# A clock showing a wrong time looks right, so say so until it's been set.
|
|
20
|
+
waiting = lv.label(root)
|
|
21
|
+
waiting.set_text("Setting the time")
|
|
22
|
+
waiting.set_style_text_color(lv.color_hex(clock.skin["second"]), 0)
|
|
23
|
+
waiting.set_style_text_font(lv.font_montserrat_16, 0)
|
|
24
|
+
waiting.align(lv.ALIGN.CENTER, 0, 70)
|
|
25
|
+
|
|
26
|
+
def tick(_timer=None):
|
|
27
|
+
clock.update_time()
|
|
28
|
+
if localtime.is_set():
|
|
29
|
+
waiting.add_flag(lv.obj.FLAG.HIDDEN)
|
|
30
|
+
else:
|
|
31
|
+
waiting.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
32
|
+
|
|
33
|
+
scope.every(_TICK_MS, tick)
|
|
34
|
+
tick()
|
|
35
|
+
# The board sets its clock and finds its timezone from its IP address when
|
|
36
|
+
# it joins a network (touchkit.net, deskkit.localtime). A place chosen in
|
|
37
|
+
# Weather wins: the forecast's reply gives that place's offset.
|
|
38
|
+
forecast.keep_fresh(scope)
|
apps/clock/app.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "clock", "title": "Clock", "category": "Desk", "icon": "schedule", "color": "#8A651E", "bar": "none"}
|