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/perch/stage.py
ADDED
|
@@ -0,0 +1,1274 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Put a place and the chameleon on an LVGL screen, and keep them moving.
|
|
5
|
+
|
|
6
|
+
A `Stage` draws the place once into one RGB565 buffer behind an ``lv.image``.
|
|
7
|
+
The chameleon is a second image over it: each pose is drawn by svgio into its
|
|
8
|
+
own RGB565A8 buffer and kept, so a walk, a blink or a wave is a change of
|
|
9
|
+
picture and a move, which LVGL blends. The poses he will want are drawn ahead,
|
|
10
|
+
a step a tick, while he stands there. Building the shapes, parsing them and
|
|
11
|
+
drawing them are separate steps, each in its own timer tick, so nothing holds
|
|
12
|
+
the screen for long; `Stage.stats` says how long each took.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import gc
|
|
16
|
+
from multimer import ticks_diff, ticks_ms, ticks_us
|
|
17
|
+
|
|
18
|
+
import lvgl as lv
|
|
19
|
+
import svgio
|
|
20
|
+
|
|
21
|
+
from . import huey_props, huey_shift, huey_views, huey, scene_bench, scene_coast, scene_desk
|
|
22
|
+
from .vec import Doc, at, circle, colours, mix
|
|
23
|
+
|
|
24
|
+
TICK_MS = 40
|
|
25
|
+
SCENE_W, SCENE_H = 800, 480
|
|
26
|
+
NAVY = 0x0B2A4A
|
|
27
|
+
DOCS_KEPT = 3 # places whose shapes are kept, so going back to one skips building it
|
|
28
|
+
CACHE_BYTES = 6 * 1024 * 1024 # poses kept, beside the one on screen and the one just drawn
|
|
29
|
+
STRIDE = 63.3 # how far a full turn of the walk carries the body, in its own units
|
|
30
|
+
# Working memory for the two decoders: a place at 800 pixels wide wants about
|
|
31
|
+
# 600 KB, a pose about 150.
|
|
32
|
+
PLACE_MEMORY = 768 * 1024
|
|
33
|
+
POSE_MEMORY = 256 * 1024
|
|
34
|
+
BAND_ROWS = 120 # a place is painted about this many rows a tick
|
|
35
|
+
SLAB_SPARE = 1024 * 1024 # room beside the kept poses for the one on screen and the one being drawn
|
|
36
|
+
REST_TICKS = 30000 // TICK_MS # a collection about this often, while the chameleon stands still
|
|
37
|
+
|
|
38
|
+
# Where each kind of pose reaches, in the chameleon's units: x, y, width, height.
|
|
39
|
+
_BODY = (-222, -224, 416, 246)
|
|
40
|
+
_REACH = (-222, -224, 540, 246)
|
|
41
|
+
_HOLD = (-222, -250, 536, 274)
|
|
42
|
+
_FRONT = (-184, -296, 340, 318)
|
|
43
|
+
_SLEEP = (-222, -226, 506, 248)
|
|
44
|
+
|
|
45
|
+
# Where his eyes go when he looks about: the near eye, the far eye.
|
|
46
|
+
_LOOKS = (
|
|
47
|
+
((0.5, 0.1), (0.5, 0.1)),
|
|
48
|
+
((-0.6, 0.2), (0.7, -0.4)),
|
|
49
|
+
((0.9, 0.4), (-0.5, -0.6)),
|
|
50
|
+
((0.1, -0.7), (0.8, 0.3)),
|
|
51
|
+
)
|
|
52
|
+
# at the bench: on the joint, one eye up at you, both on you
|
|
53
|
+
_WORK_LOOKS = (((0.9, 0.55), (0.75, 0.35)), ((0.9, 0.55), (-0.6, -0.3)), ((-0.5, 0.1), (0.75, 0.35)))
|
|
54
|
+
_BUG = (300, -150) # where a bug hovers and the tongue strikes, in the head's units
|
|
55
|
+
# the solder joint on the workbench's board, in the chameleon's units: x, y, radius
|
|
56
|
+
_JOINT = (
|
|
57
|
+
(scene_bench.BOARD_X + 34 - scene_bench.HUEY_X) / scene_bench.HUEY_SCALE,
|
|
58
|
+
(scene_bench.BOARD_Y + 97.5 - scene_bench.HUEY_Y) / scene_bench.HUEY_SCALE,
|
|
59
|
+
7 / scene_bench.HUEY_SCALE,
|
|
60
|
+
)
|
|
61
|
+
_HELD = ("flower", "flag", "sparkler", "magnifier", "balloon", "mug")
|
|
62
|
+
_CHOICES = ("blink", "blink", "look", "look", "stroll", "tongue", "turn", "hold")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _coast(doc, variant):
|
|
66
|
+
night = variant == "night"
|
|
67
|
+
scene_coast.draw(doc, season="summer" if night else variant, night=night, who="stage", drift=False)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _bench(doc, variant):
|
|
71
|
+
scene_bench.draw(doc, who="stage")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _desk(doc, variant):
|
|
75
|
+
scene_desk.draw(doc, who="stage", time=variant)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# Each place: its variants (the first is where it opens), how to draw one, the
|
|
79
|
+
# colours the chameleon takes there (None when the variant is the scheme),
|
|
80
|
+
# where his perch is and how large he stands, how far along the perch he may
|
|
81
|
+
# walk, what he does there, and what he does first.
|
|
82
|
+
PLACES = {
|
|
83
|
+
"coast": {
|
|
84
|
+
"variants": ("summer", "autumn", "winter", "night", "spring"),
|
|
85
|
+
"build": _coast,
|
|
86
|
+
"schemes": {"spring": "blossom", "summer": "leaf", "autumn": "sunset", "winter": "snow", "night": "night"},
|
|
87
|
+
"at": (322, 404, 0.6),
|
|
88
|
+
"walk": (-40, 76),
|
|
89
|
+
"life": "stroll",
|
|
90
|
+
"first": ("blink", "stroll", "tongue"),
|
|
91
|
+
},
|
|
92
|
+
"bench": {
|
|
93
|
+
"variants": ("board", "brand", "copper", "leaf"),
|
|
94
|
+
"build": _bench,
|
|
95
|
+
"schemes": None,
|
|
96
|
+
"at": (scene_bench.HUEY_X, scene_bench.HUEY_Y, scene_bench.HUEY_SCALE),
|
|
97
|
+
"walk": None,
|
|
98
|
+
"life": "work",
|
|
99
|
+
"first": ("blink", "look"),
|
|
100
|
+
},
|
|
101
|
+
"desk": {
|
|
102
|
+
"variants": ("day", "evening", "night"),
|
|
103
|
+
"build": _desk,
|
|
104
|
+
"schemes": {"day": "leaf", "evening": "sunset", "night": "night"},
|
|
105
|
+
"at": scene_desk.SPOTS["books"],
|
|
106
|
+
"walk": (-22, 26),
|
|
107
|
+
"life": "desk",
|
|
108
|
+
"first": ("blink", "turn", "stroll"),
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
ORDER = ("coast", "bench", "desk")
|
|
112
|
+
VIEWS = ("wide", "mid", "close")
|
|
113
|
+
_ZOOM = {"wide": 1.0, "mid": 1.75, "close": 3.3}
|
|
114
|
+
# what the air carries, per place and variant: kind, how many
|
|
115
|
+
_AIR = {
|
|
116
|
+
("coast", "spring"): ("petal", 14),
|
|
117
|
+
("coast", "autumn"): ("leaf", 14),
|
|
118
|
+
("coast", "winter"): ("snow", 26),
|
|
119
|
+
("coast", "night"): ("firefly", 10),
|
|
120
|
+
("desk", "night"): ("firefly", 4),
|
|
121
|
+
}
|
|
122
|
+
_AIR_COLORS = {
|
|
123
|
+
"petal": (0xFFA9C4, 0xFFD3E0, 0xFFFFFF),
|
|
124
|
+
"leaf": (0xFFC83D, 0xF58B2A, 0xC9482B),
|
|
125
|
+
"snow": (0xFFFFFF, 0xFFFFFF, 0xE6F0F8),
|
|
126
|
+
"firefly": (0xE8FF8A, 0xFDFFE0, 0xC4DC4C),
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
# one turn of a sine in 64 steps, so moving the air costs no math calls
|
|
130
|
+
_SIN = (
|
|
131
|
+
0.0, 0.098, 0.195, 0.29, 0.383, 0.471, 0.556, 0.634, 0.707, 0.773, 0.831, 0.882, 0.924, 0.957, 0.981, 0.995,
|
|
132
|
+
1.0, 0.995, 0.981, 0.957, 0.924, 0.882, 0.831, 0.773, 0.707, 0.634, 0.556, 0.471, 0.383, 0.29, 0.195, 0.098,
|
|
133
|
+
0.0, -0.098, -0.195, -0.29, -0.383, -0.471, -0.556, -0.634, -0.707, -0.773, -0.831, -0.882, -0.924, -0.957, -0.981, -0.995,
|
|
134
|
+
-1.0, -0.995, -0.981, -0.957, -0.924, -0.882, -0.831, -0.773, -0.707, -0.634, -0.556, -0.471, -0.383, -0.29, -0.195, -0.098,
|
|
135
|
+
) # fmt: skip
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _uncache(picture):
|
|
139
|
+
"""Make LVGL forget *picture*: it may know it by an address the next one is given."""
|
|
140
|
+
try:
|
|
141
|
+
lv.image.cache_drop(picture)
|
|
142
|
+
lv.image.header_cache_drop(picture)
|
|
143
|
+
except TypeError:
|
|
144
|
+
# CPython's binding has these as methods of an image; any image will do
|
|
145
|
+
any_image = lv.image(lv.screen_active())
|
|
146
|
+
any_image.cache_drop(picture)
|
|
147
|
+
any_image.header_cache_drop(picture)
|
|
148
|
+
any_image.delete()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _check_svgio():
|
|
152
|
+
"""Raise, naming what's missing, when this firmware's svgio is older than the drawings need.
|
|
153
|
+
|
|
154
|
+
The drawings are records of svgio's compact form; the decoders work in
|
|
155
|
+
memory of their own, so a parse or a draw takes the same time whatever
|
|
156
|
+
the heap holds; the place is drawn in bands; colours are swapped as a
|
|
157
|
+
pose is drawn; and the pictures live in Buffers outside the heap.
|
|
158
|
+
"""
|
|
159
|
+
lacks = [name for name in ("expand", "Buffer") if not hasattr(svgio, name)]
|
|
160
|
+
if hasattr(svgio, "Buffer") and not hasattr(svgio.Buffer, "close"):
|
|
161
|
+
lacks.append("Buffer.close")
|
|
162
|
+
if not hasattr(svgio.SvgDecoder, "close"):
|
|
163
|
+
lacks.append("SvgDecoder.close")
|
|
164
|
+
if not lacks:
|
|
165
|
+
try:
|
|
166
|
+
decoder = svgio.SvgDecoder(memory=64 * 1024)
|
|
167
|
+
except TypeError:
|
|
168
|
+
lacks.append("SvgDecoder(memory=)")
|
|
169
|
+
else:
|
|
170
|
+
if not hasattr(decoder, "memory_peak"):
|
|
171
|
+
lacks.append("SvgDecoder.memory_peak")
|
|
172
|
+
decoder.open(b'<svg xmlns="http://www.w3.org/2000/svg" width="2" height="2"><rect width="2" height="2" fill="#fff"/></svg>')
|
|
173
|
+
try:
|
|
174
|
+
decoder.decode(bytearray(8), rows=(0, 2), colors={0xFFFFFF: 0})
|
|
175
|
+
except TypeError:
|
|
176
|
+
lacks.append("decode(rows=, colors=)")
|
|
177
|
+
decoder.close()
|
|
178
|
+
if lacks:
|
|
179
|
+
raise RuntimeError("Huey's drawings need a newer firmware: its svgio lacks %s" % ", ".join(lacks))
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class _Random:
|
|
183
|
+
# the same choices on every runtime and every run
|
|
184
|
+
def __init__(self, seed):
|
|
185
|
+
self.state = seed
|
|
186
|
+
|
|
187
|
+
def next(self):
|
|
188
|
+
self.state = (self.state * 1103515245 + 12345) & 0x7FFFFFFF
|
|
189
|
+
return (self.state >> 8) / 8388608.0
|
|
190
|
+
|
|
191
|
+
def below(self, n):
|
|
192
|
+
return int(self.next() * n) % n
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _pose(doc, spec, scheme):
|
|
196
|
+
"""The chameleon's shapes for a pose, and the box they fit in."""
|
|
197
|
+
kind = spec[0]
|
|
198
|
+
draw = huey.draw
|
|
199
|
+
if kind == "stand":
|
|
200
|
+
look, far = _LOOKS[spec[1]]
|
|
201
|
+
return draw(doc, look=look, look_far=far, blink=spec[2], colors=scheme, perch=None), _BODY
|
|
202
|
+
if kind == "walk":
|
|
203
|
+
return draw(doc, step=spec[1] / 8, colors=scheme, perch=None), _BODY
|
|
204
|
+
if kind == "happy":
|
|
205
|
+
return draw(doc, mood="happy", hand=1, look=(-0.2, 0.1), colors=scheme, perch=None), _BODY
|
|
206
|
+
if kind == "aim":
|
|
207
|
+
return draw(doc, look=(1, 0.0), look_far=(1, 0.3), nod=-4, colors=scheme, perch=None), _BODY
|
|
208
|
+
if kind == "tongue":
|
|
209
|
+
return draw(doc, look=(1, 0.0), look_far=(1, 0.3), nod=-4, tongue=spec[1], target=_BUG, colors=scheme, perch=None), _REACH
|
|
210
|
+
if kind == "work":
|
|
211
|
+
look, far = _WORK_LOOKS[spec[1]]
|
|
212
|
+
nod = 0 if spec[1] == 2 else 6
|
|
213
|
+
body = huey_props.hold(doc, "iron", lean=80, look=look, look_far=far, blink=spec[2], colors=scheme, perch=None, nod=nod)
|
|
214
|
+
# the joint glows where the iron's tip rests on the board
|
|
215
|
+
glow = doc.radial([(0, 0xFFF3B0, 0.9), (0.5, 0xFFB26B, 0.45), (1, 0xFFB26B, 0)])
|
|
216
|
+
return body + circle(_JOINT[0], _JOINT[1], _JOINT[2], glow), _HOLD
|
|
217
|
+
if kind == "hold":
|
|
218
|
+
mood = "happy" if spec[2] else "calm"
|
|
219
|
+
return huey_props.hold(doc, spec[1], look=(0.4, -0.2), look_far=(-0.5, 0.1), mood=mood, colors=scheme, perch=None), _HOLD
|
|
220
|
+
if kind == "front":
|
|
221
|
+
mood = "happy" if spec[1] else "calm"
|
|
222
|
+
return huey_views.front(doc, look=(0.0, 0.1), hand=spec[1], blink=spec[2], mood=mood, colors=scheme, perch=None), _FRONT
|
|
223
|
+
if kind == "sleep":
|
|
224
|
+
return huey.draw_sleep(doc, colors=scheme, perch=None), _SLEEP
|
|
225
|
+
raise ValueError("no such pose: %r" % (spec,))
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class Stage:
|
|
229
|
+
"""The picture on *root*, *size* pixels: a place, the chameleon, the air.
|
|
230
|
+
|
|
231
|
+
Call `tick` every `TICK_MS`. `go`, `next_place`, `next_variant` and
|
|
232
|
+
`next_view` change what is shown, and `tap` is a finger on the picture.
|
|
233
|
+
`cues` is a list of things for the chameleon to do next ("stroll",
|
|
234
|
+
"tongue", "blink", "look", "turn", "hold"), ahead of its own choices.
|
|
235
|
+
`wears` is a scheme from `huey.COLORS` for him to wear wherever he is, or
|
|
236
|
+
None for the colours of the place; the next `go` puts it on.
|
|
237
|
+
|
|
238
|
+
A stage with more poses is a subclass whose `shapes` draws them.
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
def __init__(self, root, size, place="coast", variant=None, view="wide", seed=7):
|
|
242
|
+
_check_svgio()
|
|
243
|
+
self.w, self.h = size
|
|
244
|
+
self.root = root
|
|
245
|
+
self.random = _Random(seed)
|
|
246
|
+
self.cues = []
|
|
247
|
+
self.wears = None
|
|
248
|
+
#: What the work cost, for whoever is timing it. Microseconds in
|
|
249
|
+
#: ``build`` (the shapes, in Python), ``tidy`` (the
|
|
250
|
+
#: collection as a place or the colours change), ``alloc`` (finding a
|
|
251
|
+
#: pose's buffer), ``parse`` and ``draw`` (svgio), with the longest
|
|
252
|
+
#: parse and draw of a pose; how many ``scenes`` and ``poses`` were
|
|
253
|
+
#: drawn; the longest tick while a place was loading and while it was
|
|
254
|
+
#: showing, how many showing ticks took over 150 ms, and what the
|
|
255
|
+
#: slowest was doing; milliseconds from the last change being asked for
|
|
256
|
+
#: to its picture and to the chameleon standing in it, and from opening
|
|
257
|
+
#: to the first tick, place, pose and step of a walk; and the most
|
|
258
|
+
#: bytes of picture ever held, and the most working memory a place
|
|
259
|
+
#: and a pose took in their decoders;
|
|
260
|
+
#: ``rests``, the collections made while the chameleon stood still;
|
|
261
|
+
#: ``swaps``, the changes of colour made by swapping colours as svgio drew;
|
|
262
|
+
#: and ``slow_steps``, what the showing ticks over 150 ms were doing.
|
|
263
|
+
self.stats = {
|
|
264
|
+
"build": 0,
|
|
265
|
+
"tidy": 0,
|
|
266
|
+
"alloc": 0,
|
|
267
|
+
"parse_max": 0,
|
|
268
|
+
"draw_max": 0,
|
|
269
|
+
"parse": 0,
|
|
270
|
+
"draw": 0,
|
|
271
|
+
"scenes": 0,
|
|
272
|
+
"poses": 0,
|
|
273
|
+
"tick_load_us": 0,
|
|
274
|
+
"tick_show_us": 0,
|
|
275
|
+
"slow_ticks": 0,
|
|
276
|
+
"slowest": None,
|
|
277
|
+
"picture_ms": None,
|
|
278
|
+
"change_ms": None,
|
|
279
|
+
"first_tick_ms": None,
|
|
280
|
+
"first_picture_ms": None,
|
|
281
|
+
"first_pose_ms": None,
|
|
282
|
+
"first_walk_ms": None,
|
|
283
|
+
"peak": 0,
|
|
284
|
+
"place_memory": 0,
|
|
285
|
+
"pose_memory": 0,
|
|
286
|
+
"rests": 0,
|
|
287
|
+
"swaps": 0,
|
|
288
|
+
"slow_steps": {},
|
|
289
|
+
}
|
|
290
|
+
self._born = ticks_ms()
|
|
291
|
+
self._asked = self._born # when the change being drawn was asked for
|
|
292
|
+
# The poses' pictures live in one block, taken first of all. A
|
|
293
|
+
# collection reads every block of the heap it keeps, a third of a
|
|
294
|
+
# second for six megabytes of pictures on a board, so the block is an
|
|
295
|
+
# svgio Buffer, outside the heap. (The Buffer itself is kept beside
|
|
296
|
+
# its memoryview, which alone wouldn't keep it.)
|
|
297
|
+
try:
|
|
298
|
+
self._slab, self._slab_block = self._bytes_for(CACHE_BYTES + SLAB_SPARE)
|
|
299
|
+
except MemoryError:
|
|
300
|
+
self._slab = self._slab_block = None # a pose then takes its buffer from the heap
|
|
301
|
+
self._slots = {} # id of a buffer cut from the slab -> (where, bytes)
|
|
302
|
+
self._drawing = None # the buffer a pose is being drawn into
|
|
303
|
+
self._collected = 0 # the tick of the last collection
|
|
304
|
+
self._decoder = svgio.SvgDecoder(memory=PLACE_MEMORY)
|
|
305
|
+
self._poser = svgio.SvgDecoder(memory=POSE_MEMORY)
|
|
306
|
+
self._noted = {} # (pose, scheme) -> the colours it draws, in order
|
|
307
|
+
self._prepare = None # getting ready for the next change of colours, a step a tick
|
|
308
|
+
self._eager = False # whether that comes before drawing poses ahead
|
|
309
|
+
self._docs = {} # (place, what the ground shows) -> its shapes
|
|
310
|
+
self._doc_order = []
|
|
311
|
+
self._frames = {} # (spec, flip, scheme) -> (descriptor, buffer, x offset, y offset)
|
|
312
|
+
self._order = [] # the same keys, least lately used first
|
|
313
|
+
self._bytes = 0
|
|
314
|
+
self._shown = None
|
|
315
|
+
self._made = None # the last pose drawn that isn't kept
|
|
316
|
+
self._loose = None # the pose on screen, when it isn't one of the kept
|
|
317
|
+
self._wanted = [] # poses to draw ahead, soonest first
|
|
318
|
+
self._maker = None # the pose being drawn, a step a tick
|
|
319
|
+
self._making = None
|
|
320
|
+
self._quiet = False # nothing is drawn while he walks or strikes
|
|
321
|
+
self._life = None
|
|
322
|
+
self._wait = 0
|
|
323
|
+
self._facing = 1
|
|
324
|
+
self._walked = 0.0 # along the perch, in the chameleon's units
|
|
325
|
+
self._air = []
|
|
326
|
+
self._air_kind = None
|
|
327
|
+
self._ticks = 0
|
|
328
|
+
self._step = None # what the pose being drawn did this tick, for `stats`
|
|
329
|
+
#: True while a place, a variant or a view is being changed.
|
|
330
|
+
self.busy = True
|
|
331
|
+
#: The pose on screen, as `Stage` names poses, or None.
|
|
332
|
+
self.pose = None
|
|
333
|
+
|
|
334
|
+
# the place: one buffer, navy until the first one is drawn
|
|
335
|
+
# svgio fills it: a row at a time from Python takes seconds on a board
|
|
336
|
+
self._ground, self._ground_block = self._bytes_for(self.w * (self.h + 2) * 2)
|
|
337
|
+
fill = '<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d"><rect width="%d" height="%d" fill="#%06x"/></svg>'
|
|
338
|
+
self._decoder.open((fill % (self.w, self.h, self.w, self.h, NAVY)).encode())
|
|
339
|
+
self._decoder.decode(self._ground, stride=self.w)
|
|
340
|
+
self._ground_dsc = self._descriptor(self._ground, self.w, self.h, lv.COLOR_FORMAT.RGB565)
|
|
341
|
+
self.ground = lv.image(root)
|
|
342
|
+
self.ground.set_src(self._ground_dsc)
|
|
343
|
+
self.ground.set_pos(0, 0)
|
|
344
|
+
self.ground.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
345
|
+
self._air_layer = lv.obj(root)
|
|
346
|
+
self._air_layer.remove_style_all()
|
|
347
|
+
self._air_layer.set_size(self.w, self.h)
|
|
348
|
+
self._air_layer.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
349
|
+
self._air_layer.remove_flag(lv.obj.FLAG.SCROLLABLE)
|
|
350
|
+
self.huey = lv.image(root)
|
|
351
|
+
self.huey.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
352
|
+
self.huey.add_flag(lv.obj.FLAG.HIDDEN)
|
|
353
|
+
self.bug = lv.image(root)
|
|
354
|
+
self.bug.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
355
|
+
self.bug.add_flag(lv.obj.FLAG.HIDDEN)
|
|
356
|
+
self._bug_frame = None
|
|
357
|
+
|
|
358
|
+
self.place = self.variant = self.view = self.scheme = None
|
|
359
|
+
self._rect = None
|
|
360
|
+
self._painted = None
|
|
361
|
+
self._loader = self._change(place, variant or PLACES[place]["variants"][0], view)
|
|
362
|
+
|
|
363
|
+
# -- buffers --------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
def _descriptor(self, data, w, h, cf):
|
|
366
|
+
d = lv.image_dsc_t()
|
|
367
|
+
d.header.magic = lv.IMAGE_HEADER_MAGIC
|
|
368
|
+
d.header.cf = cf
|
|
369
|
+
d.header.w = w
|
|
370
|
+
d.header.h = h
|
|
371
|
+
d.header.stride = w * 2
|
|
372
|
+
d.data_size = w * h * (3 if cf == lv.COLOR_FORMAT.RGB565A8 else 2)
|
|
373
|
+
d.data = data
|
|
374
|
+
return d
|
|
375
|
+
|
|
376
|
+
def held_bytes(self):
|
|
377
|
+
"""Bytes of picture held now: the place, the poses, the bug."""
|
|
378
|
+
total = len(self._ground) + self._bytes
|
|
379
|
+
if self._loose is not None:
|
|
380
|
+
total += len(self._loose[1])
|
|
381
|
+
if self._bug_frame:
|
|
382
|
+
total += len(self._bug_frame[1])
|
|
383
|
+
if self._made is not None:
|
|
384
|
+
total += len(self._made[1])
|
|
385
|
+
return total
|
|
386
|
+
|
|
387
|
+
def _since(self, t0, phase):
|
|
388
|
+
"""Add the microseconds since *t0* to a phase of `stats`; return now."""
|
|
389
|
+
now = ticks_us()
|
|
390
|
+
self.stats[phase] += ticks_diff(now, t0)
|
|
391
|
+
return now
|
|
392
|
+
|
|
393
|
+
def _roomiest(self, what, decoder):
|
|
394
|
+
peak = decoder.memory_peak
|
|
395
|
+
if peak > self.stats[what]:
|
|
396
|
+
self.stats[what] = peak
|
|
397
|
+
|
|
398
|
+
def _first(self, what):
|
|
399
|
+
if self.stats[what] is None:
|
|
400
|
+
self.stats[what] = ticks_diff(ticks_ms(), self._born)
|
|
401
|
+
|
|
402
|
+
def _tidy(self):
|
|
403
|
+
# svgio asks MicroPython for memory hundreds of times in a parse, and
|
|
404
|
+
# each request is looked for from the first free block on, past every
|
|
405
|
+
# picture held and everything dropped since the last collection: on an
|
|
406
|
+
# ESP32-P4 a place parsed in 0.8 s just after one and in 5 s without.
|
|
407
|
+
# Decoders with memory of their own don't need that, but what Python
|
|
408
|
+
# dropped still has to go, and better now than in the middle of a walk.
|
|
409
|
+
t0 = ticks_us()
|
|
410
|
+
gc.collect()
|
|
411
|
+
self._collected = self._ticks
|
|
412
|
+
self._since(t0, "tidy")
|
|
413
|
+
|
|
414
|
+
def _bytes_for(self, size):
|
|
415
|
+
"""*size* bytes for pictures, outside the collector's heap: a view of them, and their owner."""
|
|
416
|
+
block = svgio.Buffer(size)
|
|
417
|
+
return memoryview(block), block
|
|
418
|
+
|
|
419
|
+
def _buffer(self, size):
|
|
420
|
+
"""A buffer of *size* bytes for a pose's picture: a piece of the slab, if one is free."""
|
|
421
|
+
if self._slab is None:
|
|
422
|
+
return bytearray(size)
|
|
423
|
+
# the pieces in use are those of the pictures still held; any other
|
|
424
|
+
# was let go, whether or not `_spare` was told
|
|
425
|
+
held = [frame[1] for frame in self._frames.values()]
|
|
426
|
+
for frame in (self._shown, self._made, self._loose):
|
|
427
|
+
if frame is not None:
|
|
428
|
+
held.append(frame[1])
|
|
429
|
+
if self._drawing is not None:
|
|
430
|
+
held.append(self._drawing)
|
|
431
|
+
slots = self._slots = dict((id(data), self._slots[id(data)]) for data in held if id(data) in self._slots)
|
|
432
|
+
at = 0
|
|
433
|
+
for where, n in sorted(slots.values()):
|
|
434
|
+
if where - at >= size:
|
|
435
|
+
break
|
|
436
|
+
at = where + n
|
|
437
|
+
if at + size > len(self._slab):
|
|
438
|
+
return bytearray(size)
|
|
439
|
+
data = self._slab[at : at + size]
|
|
440
|
+
slots[id(data)] = (at, size)
|
|
441
|
+
return data
|
|
442
|
+
|
|
443
|
+
def _spare(self, data):
|
|
444
|
+
self._slots.pop(id(data), None)
|
|
445
|
+
|
|
446
|
+
# -- where things are -----------------------------------------------
|
|
447
|
+
|
|
448
|
+
def _fit(self, place, view):
|
|
449
|
+
"""The part of the place this screen shows: x, y, width, height."""
|
|
450
|
+
x, y, k = PLACES[place]["at"]
|
|
451
|
+
aspect = self.w / self.h
|
|
452
|
+
vw = SCENE_W / _ZOOM[view]
|
|
453
|
+
vh = vw / aspect
|
|
454
|
+
if vh > SCENE_H:
|
|
455
|
+
vh = SCENE_H
|
|
456
|
+
vw = vh * aspect
|
|
457
|
+
# keep the chameleon in the picture, and his face when close
|
|
458
|
+
if view == "close":
|
|
459
|
+
fx, fy = x + 118 * k, y - 118 * k
|
|
460
|
+
elif view == "mid" or vw < SCENE_W * 0.7:
|
|
461
|
+
fx, fy = x + 10 * k, y - 100 * k
|
|
462
|
+
else:
|
|
463
|
+
fx, fy = SCENE_W / 2, SCENE_H / 2
|
|
464
|
+
vx = min(max(fx - vw / 2, 0), SCENE_W - vw)
|
|
465
|
+
vy = min(max(fy - vh / 2, 0), SCENE_H - vh)
|
|
466
|
+
return vx, vy, vw, vh
|
|
467
|
+
|
|
468
|
+
def _origin(self):
|
|
469
|
+
"""The chameleon's own origin on the screen, in pixels."""
|
|
470
|
+
x, y, k = PLACES[self.place]["at"]
|
|
471
|
+
vx, vy, vw, _ = self._rect
|
|
472
|
+
s = self.w / vw
|
|
473
|
+
return (x + self._walked * k - vx) * s, (y - vy) * s
|
|
474
|
+
|
|
475
|
+
# -- poses ----------------------------------------------------------
|
|
476
|
+
|
|
477
|
+
def shapes(self, doc, spec, scheme):
|
|
478
|
+
"""The chameleon's shapes for the pose *spec* in *scheme*, and the box they fit in.
|
|
479
|
+
|
|
480
|
+
Returns:
|
|
481
|
+
The shapes, and ``(x, y, width, height)`` in the chameleon's units.
|
|
482
|
+
"""
|
|
483
|
+
return _pose(doc, spec, scheme)
|
|
484
|
+
|
|
485
|
+
def _key(self, spec, scheme=None):
|
|
486
|
+
return (spec, self._facing < 0, scheme or self.scheme)
|
|
487
|
+
|
|
488
|
+
def _source(self, spec, scheme, flip):
|
|
489
|
+
"""The pose *spec* as svgio opens it, and the box of the chameleon's units it shows."""
|
|
490
|
+
x, y, k = PLACES[self.place]["at"]
|
|
491
|
+
vx, vy, vw, vh = self._rect
|
|
492
|
+
doc = Doc()
|
|
493
|
+
body, (rx, ry, rw, rh) = self.shapes(doc, spec, scheme)
|
|
494
|
+
if flip:
|
|
495
|
+
body = at(0, 0, body, flip=True)
|
|
496
|
+
rx = -rx - rw
|
|
497
|
+
if self.view != "wide":
|
|
498
|
+
# zoomed in, it stays put, so only what the screen shows is drawn
|
|
499
|
+
left = max(rx, (vx - x) / k - self._walked)
|
|
500
|
+
top = max(ry, (vy - y) / k)
|
|
501
|
+
right = min(rx + rw, (vx + vw - x) / k - self._walked)
|
|
502
|
+
bottom = min(ry + rh, (vy + vh - y) / k)
|
|
503
|
+
rx, ry, rw, rh = left, top, max(right - left, 1), max(bottom - top, 1)
|
|
504
|
+
doc.add(body)
|
|
505
|
+
return doc.source((rx, ry, rw, rh)), rx, ry, rw, rh
|
|
506
|
+
|
|
507
|
+
def _keep(self, key, frame):
|
|
508
|
+
"""Hold a pose's picture under *key*, letting the least lately used go for room."""
|
|
509
|
+
self._frames[key] = frame
|
|
510
|
+
self._order.append(key)
|
|
511
|
+
self._bytes += len(frame[1])
|
|
512
|
+
while self._bytes > CACHE_BYTES and len(self._order) > 2:
|
|
513
|
+
old = self._order[0]
|
|
514
|
+
if self._frames[old] is self._shown:
|
|
515
|
+
old = self._order[1]
|
|
516
|
+
self._drop(old)
|
|
517
|
+
|
|
518
|
+
def _colours_of(self, spec, scheme):
|
|
519
|
+
"""Every colour the pose *spec* draws in *scheme*, in order: noted once, and kept."""
|
|
520
|
+
key = (spec, scheme)
|
|
521
|
+
noted = self._noted.get(key)
|
|
522
|
+
if noted is None:
|
|
523
|
+
t0 = ticks_us()
|
|
524
|
+
huey._kept_text.clear() # so every colour is drawn, and noted
|
|
525
|
+
noted = colours()
|
|
526
|
+
self.shapes(Doc(), spec, scheme)
|
|
527
|
+
colours(False)
|
|
528
|
+
if len(self._noted) >= 8:
|
|
529
|
+
self._noted.clear()
|
|
530
|
+
self._noted[key] = noted
|
|
531
|
+
self._since(t0, "build")
|
|
532
|
+
return noted
|
|
533
|
+
|
|
534
|
+
def _next_variant(self):
|
|
535
|
+
variants = PLACES[self.place]["variants"]
|
|
536
|
+
return variants[(variants.index(self.variant) + 1) % len(variants)]
|
|
537
|
+
|
|
538
|
+
def _ahead(self):
|
|
539
|
+
"""What the next change of colours will want, a piece a tick while he stands idle.
|
|
540
|
+
|
|
541
|
+
The colours of the resting pose here and in the next variant's
|
|
542
|
+
scheme, which pair off into the table the change swaps by, and the
|
|
543
|
+
next variant's place, if it has one of its own. Then the change has
|
|
544
|
+
only to parse and draw.
|
|
545
|
+
"""
|
|
546
|
+
rest = self._rest()
|
|
547
|
+
if rest[0] == "sleep":
|
|
548
|
+
return
|
|
549
|
+
following = self._next_variant()
|
|
550
|
+
schemes = PLACES[self.place]["schemes"]
|
|
551
|
+
yield self._colours_of(rest, self.scheme)
|
|
552
|
+
yield self._colours_of(rest, self.wears or (schemes[following] if schemes else following))
|
|
553
|
+
if schemes:
|
|
554
|
+
yield self._shapes(following)
|
|
555
|
+
|
|
556
|
+
def _beside(self, steps):
|
|
557
|
+
"""One step of the change of colours *steps*, taken beside another job; None once it is done."""
|
|
558
|
+
if steps is not None:
|
|
559
|
+
try:
|
|
560
|
+
next(steps)
|
|
561
|
+
return steps
|
|
562
|
+
except StopIteration as e:
|
|
563
|
+
self._shifted = e.value
|
|
564
|
+
return None
|
|
565
|
+
|
|
566
|
+
def _recolour(self, rest, old, new):
|
|
567
|
+
"""Take the resting pose from scheme *old* to *new* by swapping its colours.
|
|
568
|
+
|
|
569
|
+
A generator, a step a tick: the pose is built and parsed once in the
|
|
570
|
+
old colours, built again in the new only to pair each colour with its
|
|
571
|
+
counterpart, then drawn three times from the one parse, two on the
|
|
572
|
+
way and the last in the new colours, which is kept. Gives False,
|
|
573
|
+
having drawn nothing, when the two schemes' colours don't pair off.
|
|
574
|
+
"""
|
|
575
|
+
flip = self._facing < 0
|
|
576
|
+
x, y, k = PLACES[self.place]["at"]
|
|
577
|
+
s = self.w / self._rect[2]
|
|
578
|
+
was = self._colours_of(rest, old)
|
|
579
|
+
t0 = ticks_us()
|
|
580
|
+
text, rx, ry, rw, rh = self._source(rest, old, flip)
|
|
581
|
+
t0 = self._since(t0, "build")
|
|
582
|
+
w, h = self._poser.open(text, width=max(2, int(rw * k * s + 0.5)))
|
|
583
|
+
text = None
|
|
584
|
+
self._since(t0, "parse")
|
|
585
|
+
self._step = ("parse", rest)
|
|
586
|
+
yield
|
|
587
|
+
fresh = (rest, new) not in self._noted
|
|
588
|
+
now = self._colours_of(rest, new)
|
|
589
|
+
swap = {}
|
|
590
|
+
if len(was) != len(now):
|
|
591
|
+
return False
|
|
592
|
+
for a, b in zip(was, now):
|
|
593
|
+
if swap.setdefault(a, b) != b:
|
|
594
|
+
return False
|
|
595
|
+
if fresh:
|
|
596
|
+
# built just now, not while he stood idle: that was this tick's work
|
|
597
|
+
self._step = ("build", rest)
|
|
598
|
+
yield
|
|
599
|
+
for t in (0.33, 0.67, 1):
|
|
600
|
+
t0 = ticks_us()
|
|
601
|
+
data = self._drawing = self._buffer(w * h * 3)
|
|
602
|
+
planes = memoryview(data)
|
|
603
|
+
part = swap if t == 1 else dict((a, mix(a, b, t)) for a, b in swap.items())
|
|
604
|
+
self._poser.decode(planes[: w * h * 2], planes[w * h * 2 :], colors=part)
|
|
605
|
+
planes = self._drawing = None
|
|
606
|
+
self._since(t0, "draw")
|
|
607
|
+
self.stats["poses"] += 1
|
|
608
|
+
frame = (self._descriptor(data, w, h, lv.COLOR_FORMAT.RGB565A8), data, round(rx * k * s), round(ry * k * s))
|
|
609
|
+
if t == 1:
|
|
610
|
+
self._keep((rest, flip, new), frame)
|
|
611
|
+
self.stats["swaps"] += 1
|
|
612
|
+
else:
|
|
613
|
+
self._put(frame, rest)
|
|
614
|
+
self._step = ("draw", rest)
|
|
615
|
+
yield
|
|
616
|
+
return True
|
|
617
|
+
|
|
618
|
+
def _make(self, spec, scheme=None, keep=True):
|
|
619
|
+
"""Draw the pose *spec* in four steps: its shapes, the parse, its buffer, the picture.
|
|
620
|
+
|
|
621
|
+
A generator: each ``next`` does one step. The pose is kept under its
|
|
622
|
+
key, or left in ``_made`` when it isn't to be kept.
|
|
623
|
+
"""
|
|
624
|
+
scheme = scheme or self.scheme
|
|
625
|
+
flip = self._facing < 0
|
|
626
|
+
key = (spec, flip, scheme)
|
|
627
|
+
if keep and key in self._frames:
|
|
628
|
+
return
|
|
629
|
+
self._making = key
|
|
630
|
+
x, y, k = PLACES[self.place]["at"]
|
|
631
|
+
s = self.w / self._rect[2]
|
|
632
|
+
t0 = ticks_us()
|
|
633
|
+
text, rx, ry, rw, rh = self._source(spec, scheme, flip)
|
|
634
|
+
self._since(t0, "build")
|
|
635
|
+
self._step = ("build", spec)
|
|
636
|
+
yield
|
|
637
|
+
t0 = ticks_us()
|
|
638
|
+
# not the place's decoder: a place may be parsed between these steps
|
|
639
|
+
decoder = self._poser
|
|
640
|
+
w, h = decoder.open(text, width=max(2, int(rw * k * s + 0.5)))
|
|
641
|
+
text = None
|
|
642
|
+
self.stats["parse_max"] = max(self.stats["parse_max"], ticks_diff(ticks_us(), t0))
|
|
643
|
+
self._since(t0, "parse")
|
|
644
|
+
self._step = ("parse", spec)
|
|
645
|
+
yield
|
|
646
|
+
self._step = ("alloc", spec)
|
|
647
|
+
t0 = ticks_us()
|
|
648
|
+
data = self._drawing = self._buffer(w * h * 3)
|
|
649
|
+
self._since(t0, "alloc")
|
|
650
|
+
yield
|
|
651
|
+
self._step = ("draw", spec)
|
|
652
|
+
t0 = ticks_us()
|
|
653
|
+
planes = memoryview(data)
|
|
654
|
+
decoder.decode(planes[: w * h * 2], planes[w * h * 2 :])
|
|
655
|
+
self._roomiest("pose_memory", decoder)
|
|
656
|
+
planes = decoder = self._drawing = None
|
|
657
|
+
self.stats["draw_max"] = max(self.stats["draw_max"], ticks_diff(ticks_us(), t0))
|
|
658
|
+
self._since(t0, "draw")
|
|
659
|
+
self.stats["poses"] += 1
|
|
660
|
+
frame = (self._descriptor(data, w, h, lv.COLOR_FORMAT.RGB565A8), data, round(rx * k * s), round(ry * k * s))
|
|
661
|
+
self._making = None
|
|
662
|
+
if not keep:
|
|
663
|
+
self._made = frame
|
|
664
|
+
elif key not in self._frames:
|
|
665
|
+
self._keep(key, frame)
|
|
666
|
+
self.stats["peak"] = max(self.stats["peak"], self.held_bytes())
|
|
667
|
+
|
|
668
|
+
def _frame(self, spec):
|
|
669
|
+
"""The kept pose *spec*, drawn here and now if it hasn't been."""
|
|
670
|
+
key = self._key(spec)
|
|
671
|
+
if key not in self._frames:
|
|
672
|
+
for _ in self._make(spec):
|
|
673
|
+
pass
|
|
674
|
+
else:
|
|
675
|
+
self._order.remove(key)
|
|
676
|
+
self._order.append(key)
|
|
677
|
+
return self._frames[key]
|
|
678
|
+
|
|
679
|
+
def _have(self, spec):
|
|
680
|
+
return self._key(spec) in self._frames
|
|
681
|
+
|
|
682
|
+
def _ready(self, *specs):
|
|
683
|
+
"""Wait, a tick at a time, until every pose in *specs* is drawn.
|
|
684
|
+
|
|
685
|
+
A generator for the chameleon's life to ``yield from``. It puts the
|
|
686
|
+
poses at the head of the queue and gives True once they are all
|
|
687
|
+
there, or False if they can't all be kept at once.
|
|
688
|
+
"""
|
|
689
|
+
for _ in range(40 + 12 * len(specs)):
|
|
690
|
+
missing = [spec for spec in specs if not self._have(spec)]
|
|
691
|
+
if not missing:
|
|
692
|
+
return True
|
|
693
|
+
for spec in reversed(missing):
|
|
694
|
+
if self._key(spec) != self._making:
|
|
695
|
+
if spec in self._wanted:
|
|
696
|
+
self._wanted.remove(spec)
|
|
697
|
+
self._wanted.insert(0, spec)
|
|
698
|
+
yield 1
|
|
699
|
+
return False
|
|
700
|
+
|
|
701
|
+
def _work(self):
|
|
702
|
+
"""One step of drawing a pose ahead: the next in the queue, or the one begun."""
|
|
703
|
+
if self._maker is None:
|
|
704
|
+
if self._prepare is not None and (self._eager or not self._wanted):
|
|
705
|
+
# getting ready for the next change of colours: before the
|
|
706
|
+
# poses when colours were what last changed, as the next
|
|
707
|
+
# change is likely the same again; otherwise once they're drawn
|
|
708
|
+
try:
|
|
709
|
+
next(self._prepare)
|
|
710
|
+
except StopIteration:
|
|
711
|
+
self._prepare = None
|
|
712
|
+
return
|
|
713
|
+
while self._wanted:
|
|
714
|
+
spec = self._wanted.pop(0)
|
|
715
|
+
if not self._have(spec):
|
|
716
|
+
self._maker = self._make(spec)
|
|
717
|
+
break
|
|
718
|
+
else:
|
|
719
|
+
return
|
|
720
|
+
try:
|
|
721
|
+
next(self._maker)
|
|
722
|
+
except StopIteration:
|
|
723
|
+
self._maker = None
|
|
724
|
+
|
|
725
|
+
def _drop(self, key):
|
|
726
|
+
frame = self._frames.pop(key)
|
|
727
|
+
self._order.remove(key)
|
|
728
|
+
self._bytes -= len(frame[1])
|
|
729
|
+
self._spare(frame[1])
|
|
730
|
+
# LVGL may remember a picture by its address, which the next one could be given
|
|
731
|
+
_uncache(frame[0])
|
|
732
|
+
|
|
733
|
+
def _forget(self, keep_scheme=None):
|
|
734
|
+
"""Drop the kept poses, all of them or all but one scheme's."""
|
|
735
|
+
self._wanted = []
|
|
736
|
+
self._maker = self._making = None
|
|
737
|
+
for key in list(self._order):
|
|
738
|
+
if self._frames[key] is self._shown:
|
|
739
|
+
continue
|
|
740
|
+
if keep_scheme is None or key[2] != keep_scheme:
|
|
741
|
+
self._drop(key)
|
|
742
|
+
|
|
743
|
+
def _show(self, spec):
|
|
744
|
+
"""Put the kept pose *spec* on the screen."""
|
|
745
|
+
self._put(self._frame(spec), spec)
|
|
746
|
+
if spec[0] == "walk":
|
|
747
|
+
self._first("first_walk_ms")
|
|
748
|
+
|
|
749
|
+
def _put(self, frame, spec):
|
|
750
|
+
if self._loose is not None and self._loose is not frame:
|
|
751
|
+
# the pose it was showing wasn't kept: its buffer can be used again
|
|
752
|
+
_uncache(self._loose[0])
|
|
753
|
+
self._spare(self._loose[1])
|
|
754
|
+
self._loose = None
|
|
755
|
+
if frame not in self._frames.values():
|
|
756
|
+
self._loose = frame
|
|
757
|
+
self._shown = frame
|
|
758
|
+
self.pose = spec
|
|
759
|
+
self.huey.set_src(frame[0])
|
|
760
|
+
self._place_huey()
|
|
761
|
+
self.huey.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
762
|
+
self._first("first_pose_ms")
|
|
763
|
+
|
|
764
|
+
def _place_huey(self):
|
|
765
|
+
if self._shown is None:
|
|
766
|
+
return
|
|
767
|
+
ox, oy = self._origin()
|
|
768
|
+
self.huey.set_pos(round(ox) + self._shown[2], round(oy) + self._shown[3])
|
|
769
|
+
|
|
770
|
+
# -- the place ------------------------------------------------------
|
|
771
|
+
|
|
772
|
+
def _ground_key(self, variant=None):
|
|
773
|
+
# the bench is the same bench whatever colours the chameleon wears
|
|
774
|
+
return (variant or self.variant) if PLACES[self.place]["schemes"] else None
|
|
775
|
+
|
|
776
|
+
def _shapes(self, variant=None):
|
|
777
|
+
"""The place's shapes in *variant* (the one showing, unless given), built now or kept from before."""
|
|
778
|
+
variant = variant or self.variant
|
|
779
|
+
key = (self.place, self._ground_key(variant))
|
|
780
|
+
doc = self._docs.get(key)
|
|
781
|
+
if doc is None:
|
|
782
|
+
t0 = ticks_us()
|
|
783
|
+
doc = Doc()
|
|
784
|
+
PLACES[self.place]["build"](doc, variant)
|
|
785
|
+
self._since(t0, "build")
|
|
786
|
+
self._docs[key] = doc
|
|
787
|
+
while len(self._doc_order) >= DOCS_KEPT:
|
|
788
|
+
del self._docs[self._doc_order.pop(0)]
|
|
789
|
+
else:
|
|
790
|
+
self._doc_order.remove(key)
|
|
791
|
+
self._doc_order.append(key)
|
|
792
|
+
return doc
|
|
793
|
+
|
|
794
|
+
def _rest(self):
|
|
795
|
+
"""The pose he comes back to in this place."""
|
|
796
|
+
life = PLACES[self.place]["life"]
|
|
797
|
+
if life == "work":
|
|
798
|
+
return ("work", 0, 0)
|
|
799
|
+
if life == "desk" and self.variant == "night":
|
|
800
|
+
return ("sleep",)
|
|
801
|
+
return ("stand", 0, 0)
|
|
802
|
+
|
|
803
|
+
def _blend(self, rest, scheme):
|
|
804
|
+
"""Draw the resting pose in a scheme part way to the next, and show it."""
|
|
805
|
+
yield from self._make(rest, scheme, keep=False)
|
|
806
|
+
self._put(self._made, rest)
|
|
807
|
+
self._made = None
|
|
808
|
+
yield
|
|
809
|
+
|
|
810
|
+
def _change(self, place, variant, view):
|
|
811
|
+
"""Go to a place, a variant of it and a view: the steps, one a tick.
|
|
812
|
+
|
|
813
|
+
After a collection the place takes three: its shapes are built,
|
|
814
|
+
parsed, then drawn over the old picture in one go. Where only the colours change, the
|
|
815
|
+
chameleon is then redrawn part way to the new ones, twice, on his way
|
|
816
|
+
to them.
|
|
817
|
+
"""
|
|
818
|
+
self.busy = True
|
|
819
|
+
self._life = None
|
|
820
|
+
self._quiet = False
|
|
821
|
+
# a pose half drawn is dropped: the poses share one decoder
|
|
822
|
+
self._maker = self._making = self._drawing = None
|
|
823
|
+
self._prepare = None
|
|
824
|
+
self.bug.add_flag(lv.obj.FLAG.HIDDEN)
|
|
825
|
+
old_scheme = self.scheme
|
|
826
|
+
schemes = PLACES[place]["schemes"]
|
|
827
|
+
new_scheme = self.wears or (schemes[variant] if schemes else variant)
|
|
828
|
+
moved = place != self.place or view != self.view
|
|
829
|
+
was_asleep = self.place is not None and self._rest() == ("sleep",)
|
|
830
|
+
if moved:
|
|
831
|
+
# somewhere else, or seen from somewhere else: it stays in the old
|
|
832
|
+
# picture until the new one is drawn, then comes back there
|
|
833
|
+
self._forget()
|
|
834
|
+
self._bug_frame = None
|
|
835
|
+
self._walked = 0.0
|
|
836
|
+
self._facing = 1
|
|
837
|
+
self.place, self.variant, self.view = place, variant, view
|
|
838
|
+
self._rect = self._fit(place, view)
|
|
839
|
+
rest = self._rest()
|
|
840
|
+
blends = []
|
|
841
|
+
if not moved and old_scheme and old_scheme != new_scheme and rest[0] != "sleep" and not was_asleep:
|
|
842
|
+
blends = [0.33, 0.67]
|
|
843
|
+
if old_scheme != new_scheme:
|
|
844
|
+
# nothing drawn in the old colours will be wanted again
|
|
845
|
+
self._forget()
|
|
846
|
+
# What the last place and its poses left is collected. A collection
|
|
847
|
+
# has the poses' block to read, a third of a second on a board, so it
|
|
848
|
+
# waits until the new picture is up.
|
|
849
|
+
tidy = moved or old_scheme != new_scheme
|
|
850
|
+
# Where the colours change, the chameleon turns through them while the
|
|
851
|
+
# place is painted, a step of each a tick, not after it.
|
|
852
|
+
shift = self._recolour(rest, old_scheme, new_scheme) if blends else None
|
|
853
|
+
self._shifted = False
|
|
854
|
+
if self._painted != (place, self._ground_key(), view):
|
|
855
|
+
doc = self._shapes()
|
|
856
|
+
shift = self._beside(shift)
|
|
857
|
+
yield
|
|
858
|
+
t0 = ticks_us()
|
|
859
|
+
text = doc.source(self._rect)
|
|
860
|
+
doc = None
|
|
861
|
+
t0 = self._since(t0, "build")
|
|
862
|
+
self._decoder.open(text, width=self.w)
|
|
863
|
+
text = None
|
|
864
|
+
self._since(t0, "parse")
|
|
865
|
+
shift = self._beside(shift)
|
|
866
|
+
yield
|
|
867
|
+
# a band a tick, so the picture arrives down the screen and
|
|
868
|
+
# no tick waits for all of it. Bands are whole strips of
|
|
869
|
+
# svgio's, so no row is drawn twice, and LVGL is told of the
|
|
870
|
+
# band alone: redrawing the whole screen for each cost more
|
|
871
|
+
# than the bands saved.
|
|
872
|
+
strip = max(1, 65536 // (4 * self.w))
|
|
873
|
+
band = strip * max(1, (BAND_ROWS + strip - 1) // strip)
|
|
874
|
+
area = lv.area_t()
|
|
875
|
+
self.ground.get_coords(area)
|
|
876
|
+
top = area.y1
|
|
877
|
+
for y0 in range(0, self.h, band):
|
|
878
|
+
y1 = min(y0 + band, self.h)
|
|
879
|
+
t0 = ticks_us()
|
|
880
|
+
self._decoder.decode(self._ground, stride=self.w, rows=(y0, y1))
|
|
881
|
+
self._since(t0, "draw")
|
|
882
|
+
area.y1, area.y2 = top + y0, top + y1 - 1
|
|
883
|
+
self.ground.invalidate_area(area)
|
|
884
|
+
if y1 < self.h:
|
|
885
|
+
shift = self._beside(shift)
|
|
886
|
+
yield
|
|
887
|
+
self._roomiest("place_memory", self._decoder)
|
|
888
|
+
self.stats["scenes"] += 1
|
|
889
|
+
if moved:
|
|
890
|
+
self.huey.add_flag(lv.obj.FLAG.HIDDEN)
|
|
891
|
+
self._shown = self.pose = None
|
|
892
|
+
self._painted = (place, self._ground_key(), view)
|
|
893
|
+
self._first("first_picture_ms")
|
|
894
|
+
self.stats["picture_ms"] = ticks_diff(ticks_ms(), self._asked)
|
|
895
|
+
self._set_air()
|
|
896
|
+
yield
|
|
897
|
+
else:
|
|
898
|
+
self._set_air()
|
|
899
|
+
if tidy:
|
|
900
|
+
self._tidy()
|
|
901
|
+
if not self._shifted:
|
|
902
|
+
yield
|
|
903
|
+
while shift is not None:
|
|
904
|
+
shift = self._beside(shift)
|
|
905
|
+
if shift is not None:
|
|
906
|
+
yield
|
|
907
|
+
if blends and not self._shifted:
|
|
908
|
+
# the two schemes' colours don't pair off: each step is a pose
|
|
909
|
+
# built and parsed afresh
|
|
910
|
+
for t in blends:
|
|
911
|
+
yield from self._blend(rest, huey_shift.between(old_scheme, new_scheme, t))
|
|
912
|
+
for name in list(huey.COLORS):
|
|
913
|
+
if ">" in name:
|
|
914
|
+
del huey.COLORS[name]
|
|
915
|
+
self.scheme = new_scheme
|
|
916
|
+
yield from self._make(rest)
|
|
917
|
+
self._show(rest)
|
|
918
|
+
self.stats["change_ms"] = ticks_diff(ticks_ms(), self._asked)
|
|
919
|
+
self._forget(new_scheme)
|
|
920
|
+
self.stats["peak"] = max(self.stats["peak"], self.held_bytes())
|
|
921
|
+
# what he will want next, drawn ahead while he stands there
|
|
922
|
+
wide = view == "wide"
|
|
923
|
+
ahead = []
|
|
924
|
+
if rest[0] != "sleep":
|
|
925
|
+
ahead.append((rest[0], rest[1], 1))
|
|
926
|
+
if PLACES[place]["walk"] is not None and wide and rest[0] == "stand":
|
|
927
|
+
ahead.extend(("walk", n) for n in range(8))
|
|
928
|
+
if rest[0] == "work":
|
|
929
|
+
ahead.extend((("work", 1, 0), ("work", 2, 0)))
|
|
930
|
+
else:
|
|
931
|
+
ahead.extend(("stand", n, 0) for n in range(1, len(_LOOKS)))
|
|
932
|
+
if wide:
|
|
933
|
+
ahead.extend((("aim",), ("tongue", 0.5), ("tongue", 1), ("happy",)))
|
|
934
|
+
self._wanted = ahead
|
|
935
|
+
self._prepare = self._ahead()
|
|
936
|
+
self._eager = not moved
|
|
937
|
+
if not self.cues:
|
|
938
|
+
self.cues.extend(PLACES[place]["first"])
|
|
939
|
+
self.busy = False
|
|
940
|
+
self._life = self._live()
|
|
941
|
+
self._wait = 4
|
|
942
|
+
|
|
943
|
+
# -- what he does ---------------------------------------------------
|
|
944
|
+
|
|
945
|
+
def _live(self):
|
|
946
|
+
"""His life in this place: one thing after another, for ever."""
|
|
947
|
+
life = PLACES[self.place]["life"]
|
|
948
|
+
rest = self._rest()
|
|
949
|
+
can_walk = PLACES[self.place]["walk"] is not None and self.view == "wide" and rest[0] == "stand"
|
|
950
|
+
pause = 6
|
|
951
|
+
while True:
|
|
952
|
+
self._quiet = False
|
|
953
|
+
if rest[0] == "stand" and not self._have(rest):
|
|
954
|
+
# he has turned round: the same pose, facing the other way
|
|
955
|
+
yield from self._ready(rest)
|
|
956
|
+
self._show(rest)
|
|
957
|
+
if self._ticks - self._collected > REST_TICKS and self._maker is None:
|
|
958
|
+
# A collection reads every picture held, which takes a third
|
|
959
|
+
# of a second on a board. Left alone it would come when the
|
|
960
|
+
# heap filled, as likely as not mid-stride; here he is still.
|
|
961
|
+
t0 = ticks_us()
|
|
962
|
+
gc.collect()
|
|
963
|
+
self._collected = self._ticks
|
|
964
|
+
self._since(t0, "tidy")
|
|
965
|
+
self._step = ("tidy", None)
|
|
966
|
+
self.stats["rests"] += 1
|
|
967
|
+
yield pause
|
|
968
|
+
# what he was asked to do comes promptly; his own ideas take their time
|
|
969
|
+
pause = 6 if self.cues else 12 + self.random.below(30)
|
|
970
|
+
if self.cues:
|
|
971
|
+
what = self.cues.pop(0)
|
|
972
|
+
else:
|
|
973
|
+
what = _CHOICES[self.random.below(len(_CHOICES))]
|
|
974
|
+
if rest[0] == "sleep":
|
|
975
|
+
continue
|
|
976
|
+
if what == "blink":
|
|
977
|
+
shut = (rest[0], rest[1], 1)
|
|
978
|
+
if (yield from self._ready(shut)):
|
|
979
|
+
self._show(shut)
|
|
980
|
+
yield 4
|
|
981
|
+
elif what == "look":
|
|
982
|
+
if life == "work":
|
|
983
|
+
spec = ("work", 1 + self.random.below(2), 0)
|
|
984
|
+
else:
|
|
985
|
+
spec = ("stand", 1 + self.random.below(len(_LOOKS) - 1), 0)
|
|
986
|
+
if (yield from self._ready(spec)):
|
|
987
|
+
self._show(spec)
|
|
988
|
+
yield 20 + self.random.below(25)
|
|
989
|
+
elif what == "stroll" and can_walk:
|
|
990
|
+
yield from self._stroll()
|
|
991
|
+
elif what == "tongue" and rest[0] == "stand" and self.view != "close" and self._facing > 0:
|
|
992
|
+
yield from self._catch()
|
|
993
|
+
elif what == "turn" and life == "desk":
|
|
994
|
+
yield from self._greet()
|
|
995
|
+
elif what == "hold" and rest[0] == "stand" and self._facing > 0:
|
|
996
|
+
prop = _HELD[self.random.below(len(_HELD))]
|
|
997
|
+
if (yield from self._ready(("hold", prop, 0), ("hold", prop, 1))):
|
|
998
|
+
self._show(("hold", prop, 0))
|
|
999
|
+
yield 30
|
|
1000
|
+
self._show(("hold", prop, 1))
|
|
1001
|
+
yield 45
|
|
1002
|
+
|
|
1003
|
+
def _stroll(self):
|
|
1004
|
+
"""A walk to the other end of the perch, once every step of it is drawn."""
|
|
1005
|
+
low, high = PLACES[self.place]["walk"]
|
|
1006
|
+
goal = high if self._walked < (low + high) / 2 else low
|
|
1007
|
+
facing = 1 if goal > self._walked else -1
|
|
1008
|
+
if facing != self._facing:
|
|
1009
|
+
self._facing = facing
|
|
1010
|
+
if not (yield from self._ready(("stand", 0, 0))):
|
|
1011
|
+
return
|
|
1012
|
+
self._show(("stand", 0, 0))
|
|
1013
|
+
yield 6
|
|
1014
|
+
if not (yield from self._ready(*[("walk", n) for n in range(8)])):
|
|
1015
|
+
return
|
|
1016
|
+
self._quiet = True
|
|
1017
|
+
phase = 0
|
|
1018
|
+
per_tick = STRIDE / 8 / 3 # three ticks a picture, eight pictures a stride
|
|
1019
|
+
while (goal - self._walked) * facing > 0:
|
|
1020
|
+
self._show(("walk", (phase // 3) % 8))
|
|
1021
|
+
self._walked += per_tick * facing
|
|
1022
|
+
self._place_huey()
|
|
1023
|
+
phase += 1
|
|
1024
|
+
yield 1
|
|
1025
|
+
self._show(("stand", 0, 0))
|
|
1026
|
+
self._quiet = False
|
|
1027
|
+
yield 8
|
|
1028
|
+
|
|
1029
|
+
def _catch(self):
|
|
1030
|
+
"""A bug drifts in ahead of him, and the tongue takes it."""
|
|
1031
|
+
if not (yield from self._ready(("aim",), ("tongue", 0.5), ("tongue", 1), ("happy",))):
|
|
1032
|
+
return
|
|
1033
|
+
x, y, k = PLACES[self.place]["at"]
|
|
1034
|
+
s = self.w / self._rect[2]
|
|
1035
|
+
if self._bug_frame is None:
|
|
1036
|
+
t0 = ticks_us()
|
|
1037
|
+
doc = Doc()
|
|
1038
|
+
doc.add(huey._bug(0, 0, 1.4))
|
|
1039
|
+
text = doc.source((-30, -30, 60, 60))
|
|
1040
|
+
t0 = self._since(t0, "build")
|
|
1041
|
+
w, h = self._decoder.open(text, width=max(2, int(60 * k * s + 0.5)))
|
|
1042
|
+
t0 = self._since(t0, "parse")
|
|
1043
|
+
data = bytearray(w * h * 3)
|
|
1044
|
+
planes = memoryview(data)
|
|
1045
|
+
self._decoder.decode(planes[: w * h * 2], planes[w * h * 2 :])
|
|
1046
|
+
self._since(t0, "draw")
|
|
1047
|
+
self._bug_frame = (self._descriptor(data, w, h, lv.COLOR_FORMAT.RGB565A8), data, w, h)
|
|
1048
|
+
self.bug.set_src(self._bug_frame[0])
|
|
1049
|
+
yield 1
|
|
1050
|
+
self._quiet = True
|
|
1051
|
+
half = self._bug_frame[2] // 2
|
|
1052
|
+
ox, oy = self._origin()
|
|
1053
|
+
bx = ox + (_BUG[0] + 4) * k * s
|
|
1054
|
+
by = oy + (_BUG[1] + 22 + 4) * k * s
|
|
1055
|
+
self.bug.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
1056
|
+
for i in range(24, -1, -1):
|
|
1057
|
+
# it perchs in from up and to the right
|
|
1058
|
+
wobble = ((i * 7) % 9 - 4) * (i / 24)
|
|
1059
|
+
self.bug.set_pos(int(bx + i * 5 * s + wobble) - half, int(by - i * 3.2 * s + wobble * 2) - half)
|
|
1060
|
+
if i == 14:
|
|
1061
|
+
self._show(("aim",))
|
|
1062
|
+
yield 1
|
|
1063
|
+
yield 6
|
|
1064
|
+
self._show(("tongue", 0.5))
|
|
1065
|
+
yield 1
|
|
1066
|
+
self._show(("tongue", 1))
|
|
1067
|
+
yield 4
|
|
1068
|
+
self.bug.add_flag(lv.obj.FLAG.HIDDEN)
|
|
1069
|
+
self._show(("tongue", 0.5))
|
|
1070
|
+
yield 1
|
|
1071
|
+
self._show(("happy",))
|
|
1072
|
+
self._quiet = False
|
|
1073
|
+
yield 22
|
|
1074
|
+
|
|
1075
|
+
def _greet(self):
|
|
1076
|
+
"""He turns to face you, waves, and turns back."""
|
|
1077
|
+
if not (yield from self._ready(("front", 0, 0), ("front", 1, 0), ("front", 1, 1))):
|
|
1078
|
+
return
|
|
1079
|
+
self._show(("front", 0, 0))
|
|
1080
|
+
yield 10
|
|
1081
|
+
self._show(("front", 1, 0))
|
|
1082
|
+
yield 26
|
|
1083
|
+
self._show(("front", 1, 1))
|
|
1084
|
+
yield 4
|
|
1085
|
+
self._show(("front", 1, 0))
|
|
1086
|
+
yield 18
|
|
1087
|
+
|
|
1088
|
+
def _react(self):
|
|
1089
|
+
"""A finger found him: he is pleased, then goes back to what he was doing."""
|
|
1090
|
+
life = PLACES[self.place]["life"]
|
|
1091
|
+
if self._rest()[0] == "sleep":
|
|
1092
|
+
yield 1
|
|
1093
|
+
elif life == "desk":
|
|
1094
|
+
yield from self._greet()
|
|
1095
|
+
elif life == "work":
|
|
1096
|
+
if (yield from self._ready(("work", 2, 0))):
|
|
1097
|
+
self._show(("work", 2, 0))
|
|
1098
|
+
yield 30
|
|
1099
|
+
elif (yield from self._ready(("happy",))):
|
|
1100
|
+
self._show(("happy",))
|
|
1101
|
+
yield 34
|
|
1102
|
+
self._life = self._live()
|
|
1103
|
+
yield 1
|
|
1104
|
+
|
|
1105
|
+
# -- the air --------------------------------------------------------
|
|
1106
|
+
|
|
1107
|
+
def _set_air(self):
|
|
1108
|
+
kind, count = _AIR.get((self.place, self.variant), (None, 0))
|
|
1109
|
+
self._air_kind = kind
|
|
1110
|
+
while len(self._air) > count:
|
|
1111
|
+
self._air.pop()[0].delete()
|
|
1112
|
+
colors = _AIR_COLORS.get(kind, ())
|
|
1113
|
+
unit = max(self.h / 480, 0.5)
|
|
1114
|
+
for i in range(count):
|
|
1115
|
+
if i >= len(self._air):
|
|
1116
|
+
dot = lv.obj(self._air_layer)
|
|
1117
|
+
dot.remove_style_all()
|
|
1118
|
+
dot.remove_flag(lv.obj.FLAG.CLICKABLE)
|
|
1119
|
+
self._air.append([dot, 0.0, 0.0, 0.0, 0.0])
|
|
1120
|
+
item = self._air[i]
|
|
1121
|
+
dot = item[0]
|
|
1122
|
+
if kind == "snow":
|
|
1123
|
+
size = int((3 + i % 4) * unit)
|
|
1124
|
+
dot.set_size(size, size)
|
|
1125
|
+
elif kind == "firefly":
|
|
1126
|
+
size = int((5 + i % 3) * unit)
|
|
1127
|
+
dot.set_size(size, size)
|
|
1128
|
+
else:
|
|
1129
|
+
dot.set_size(int((8 + i % 4) * unit), int((4 + i % 2) * unit))
|
|
1130
|
+
dot.set_style_radius(lv.RADIUS_CIRCLE, 0)
|
|
1131
|
+
dot.set_style_bg_color(lv.color_hex(colors[i % 3]), 0)
|
|
1132
|
+
dot.set_style_bg_opa(255, 0)
|
|
1133
|
+
item[1] = self.random.next() * self.w
|
|
1134
|
+
item[2] = self.random.next() * self.h
|
|
1135
|
+
item[3] = 0.3 + self.random.next() * 0.7 # how fast this one goes
|
|
1136
|
+
item[4] = self.random.next() * 6.28
|
|
1137
|
+
if kind == "firefly":
|
|
1138
|
+
# low and to the right, where the picture has its own
|
|
1139
|
+
item[1] = self.w * (0.45 + 0.5 * self.random.next())
|
|
1140
|
+
item[2] = self.h * (0.45 + 0.45 * self.random.next())
|
|
1141
|
+
|
|
1142
|
+
def _move_air(self):
|
|
1143
|
+
kind = self._air_kind
|
|
1144
|
+
if not kind:
|
|
1145
|
+
return
|
|
1146
|
+
t = self._ticks
|
|
1147
|
+
unit = self.h / 480
|
|
1148
|
+
for i, item in enumerate(self._air):
|
|
1149
|
+
dot, x, y, speed, phase = item
|
|
1150
|
+
if kind == "firefly":
|
|
1151
|
+
# a slow perch, and a glow that comes and goes
|
|
1152
|
+
turn = int(t * speed * 0.6 + phase * 10)
|
|
1153
|
+
x += _SIN[turn & 63] * 0.5 * unit
|
|
1154
|
+
y += _SIN[(turn * 2 + 16) & 63] * 0.3 * unit
|
|
1155
|
+
dot.set_style_bg_opa(int(150 + 100 * _SIN[(t * 2 + i * 9) & 63]), 0)
|
|
1156
|
+
else:
|
|
1157
|
+
fall = 1.6 if kind == "snow" else 1.1
|
|
1158
|
+
drift = 0.5 if kind == "snow" else 1.3
|
|
1159
|
+
y += fall * speed * unit
|
|
1160
|
+
x += (drift * speed - 0.9 * _SIN[(t + int(phase * 10)) & 63]) * unit
|
|
1161
|
+
if y > self.h:
|
|
1162
|
+
y, x = -8.0, self.random.next() * self.w
|
|
1163
|
+
if x > self.w:
|
|
1164
|
+
x = -8.0
|
|
1165
|
+
item[1], item[2] = x, y
|
|
1166
|
+
dot.set_pos(int(x), int(y))
|
|
1167
|
+
|
|
1168
|
+
# -- from outside ---------------------------------------------------
|
|
1169
|
+
|
|
1170
|
+
def tick(self, timer=None):
|
|
1171
|
+
"""One step: part of a place or a pose drawn, a picture changed, the air moved."""
|
|
1172
|
+
t0 = ticks_us()
|
|
1173
|
+
self._ticks += 1
|
|
1174
|
+
self._first("first_tick_ms")
|
|
1175
|
+
loading = self._loader is not None
|
|
1176
|
+
if loading:
|
|
1177
|
+
try:
|
|
1178
|
+
next(self._loader)
|
|
1179
|
+
except StopIteration:
|
|
1180
|
+
self._loader = None
|
|
1181
|
+
else:
|
|
1182
|
+
if not self._quiet:
|
|
1183
|
+
self._work()
|
|
1184
|
+
if self._life is not None:
|
|
1185
|
+
if self._wait > 0:
|
|
1186
|
+
self._wait -= 1
|
|
1187
|
+
else:
|
|
1188
|
+
try:
|
|
1189
|
+
self._wait = (next(self._life) or 1) - 1
|
|
1190
|
+
except StopIteration:
|
|
1191
|
+
self._life = self._live()
|
|
1192
|
+
self._move_air()
|
|
1193
|
+
spent = ticks_diff(ticks_us(), t0)
|
|
1194
|
+
longest = "tick_load_us" if loading else "tick_show_us"
|
|
1195
|
+
if spent > self.stats[longest]:
|
|
1196
|
+
self.stats[longest] = spent
|
|
1197
|
+
if spent > 150000 and not loading:
|
|
1198
|
+
self.stats["slow_ticks"] += 1
|
|
1199
|
+
what = self._step[0] if self._step else "life"
|
|
1200
|
+
self.stats["slow_steps"][what] = self.stats["slow_steps"].get(what, 0) + 1
|
|
1201
|
+
if not loading and spent == self.stats["tick_show_us"]:
|
|
1202
|
+
self.stats["slowest"] = (self._step, self.pose)
|
|
1203
|
+
self._step = None
|
|
1204
|
+
|
|
1205
|
+
def go(self, place=None, variant=None, view=None):
|
|
1206
|
+
"""Show *place* (its first variant unless given), or another *view* of this one.
|
|
1207
|
+
|
|
1208
|
+
Returns:
|
|
1209
|
+
False while an earlier change is still being drawn, and nothing happens.
|
|
1210
|
+
"""
|
|
1211
|
+
if self._loader is not None:
|
|
1212
|
+
return False
|
|
1213
|
+
place = place or self.place
|
|
1214
|
+
if variant is None:
|
|
1215
|
+
variant = self.variant if place == self.place else PLACES[place]["variants"][0]
|
|
1216
|
+
self._asked = ticks_ms()
|
|
1217
|
+
self._loader = self._change(place, variant, view or self.view)
|
|
1218
|
+
return True
|
|
1219
|
+
|
|
1220
|
+
def next_place(self):
|
|
1221
|
+
return self.go(ORDER[(ORDER.index(self.place) + 1) % len(ORDER)])
|
|
1222
|
+
|
|
1223
|
+
def next_variant(self):
|
|
1224
|
+
variants = PLACES[self.place]["variants"]
|
|
1225
|
+
return self.go(variant=variants[(variants.index(self.variant) + 1) % len(variants)])
|
|
1226
|
+
|
|
1227
|
+
def next_view(self):
|
|
1228
|
+
return self.go(view=VIEWS[(VIEWS.index(self.view) + 1) % len(VIEWS)])
|
|
1229
|
+
|
|
1230
|
+
def hit(self, x, y):
|
|
1231
|
+
"""True when (*x*, *y*) on the screen is on the chameleon."""
|
|
1232
|
+
if self._shown is None:
|
|
1233
|
+
return False
|
|
1234
|
+
ox, oy = self._origin()
|
|
1235
|
+
left, top = ox + self._shown[2], oy + self._shown[3]
|
|
1236
|
+
header = self._shown[0].header
|
|
1237
|
+
return left <= x < left + header.w and top <= y < top + header.h
|
|
1238
|
+
|
|
1239
|
+
def tap(self, x, y):
|
|
1240
|
+
"""A finger at (*x*, *y*): True when it was on the chameleon, who answers."""
|
|
1241
|
+
if self._loader is not None or not self.hit(x, y):
|
|
1242
|
+
return False
|
|
1243
|
+
self._life = self._react()
|
|
1244
|
+
self._wait = 0
|
|
1245
|
+
self._quiet = False
|
|
1246
|
+
self.bug.add_flag(lv.obj.FLAG.HIDDEN)
|
|
1247
|
+
return True
|
|
1248
|
+
|
|
1249
|
+
def close(self):
|
|
1250
|
+
"""Let go of every buffer, once the images are deleted or about to be."""
|
|
1251
|
+
self._loader = self._life = self._maker = None
|
|
1252
|
+
self._frames = {}
|
|
1253
|
+
self._order = []
|
|
1254
|
+
self._wanted = []
|
|
1255
|
+
self._docs = {}
|
|
1256
|
+
self._doc_order = []
|
|
1257
|
+
self._shown = self._made = self._loose = self._bug_frame = self.pose = None
|
|
1258
|
+
# svgio keeps a Buffer's bytes after the Buffer is collected, for the
|
|
1259
|
+
# next one, and gives them back to the system only when it is closed.
|
|
1260
|
+
# So they are closed here, megabytes that another app may want, once
|
|
1261
|
+
# nothing will draw from them: the two images are hidden first.
|
|
1262
|
+
for image in (self.huey, self.ground):
|
|
1263
|
+
try:
|
|
1264
|
+
image.add_flag(lv.obj.FLAG.HIDDEN)
|
|
1265
|
+
except Exception:
|
|
1266
|
+
pass # deleted already, with its screen
|
|
1267
|
+
for block in (self._slab_block, self._ground_block):
|
|
1268
|
+
if block is not None:
|
|
1269
|
+
block.close()
|
|
1270
|
+
self._ground = memoryview(bytearray(0))
|
|
1271
|
+
self._slab = self._slab_block = self._ground_block = self._drawing = None
|
|
1272
|
+
self._slots = {}
|
|
1273
|
+
self._bytes = 0
|
|
1274
|
+
self._air = []
|