led-controller-version-3 3.0.2__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.
- led_controller_version_3-3.0.2.dist-info/METADATA +172 -0
- led_controller_version_3-3.0.2.dist-info/RECORD +52 -0
- led_controller_version_3-3.0.2.dist-info/WHEEL +4 -0
- led_controller_version_3-3.0.2.dist-info/entry_points.txt +13 -0
- lefx/device/respeaker/__init__.py +35 -0
- lefx/device/respeaker/cli.py +146 -0
- lefx/device/respeaker/contention.py +523 -0
- lefx/device/respeaker/provider.py +158 -0
- lefx/device/respeaker/registration.py +130 -0
- lefx/device/respeaker/sink.py +108 -0
- lefx/device/respeaker/transport.py +344 -0
- lefx/device/respeaker/xvf.py +313 -0
- lefx/engine/__init__.py +110 -0
- lefx/engine/composer.py +203 -0
- lefx/engine/config.py +27 -0
- lefx/engine/errors.py +84 -0
- lefx/engine/inputs.py +196 -0
- lefx/engine/invocation.py +129 -0
- lefx/engine/layers.py +114 -0
- lefx/engine/library.py +181 -0
- lefx/engine/packages/__init__.py +45 -0
- lefx/engine/packages/loader.py +375 -0
- lefx/engine/packages/manifest.py +308 -0
- lefx/engine/registry.py +347 -0
- lefx/engine/renderer.py +63 -0
- lefx/engine/runtime.py +393 -0
- lefx/engine/store.py +125 -0
- lefx/interfaces/__init__.py +40 -0
- lefx/interfaces/api.py +301 -0
- lefx/interfaces/cli.py +380 -0
- lefx/interfaces/client.py +165 -0
- lefx/interfaces/config.py +305 -0
- lefx/interfaces/discovery.py +308 -0
- lefx/interfaces/hosting.py +123 -0
- lefx/interfaces/paths.py +79 -0
- lefx/interfaces/presentation.py +118 -0
- lefx/interfaces/service.py +468 -0
- lefx/sdk/__init__.py +176 -0
- lefx/sdk/calibration.py +202 -0
- lefx/sdk/color.py +108 -0
- lefx/sdk/context.py +72 -0
- lefx/sdk/definitions.py +526 -0
- lefx/sdk/effects.py +63 -0
- lefx/sdk/errors.py +90 -0
- lefx/sdk/parameters.py +469 -0
- lefx/sdk/ports.py +76 -0
- lefx/sdk/validation.py +150 -0
- lefx/sdk/values.py +212 -0
- lefx/sets/core_set/__init__.py +32 -0
- lefx/sets/core_set/core-set.lefxset +0 -0
- lefx/sets/smartspeaker_set/__init__.py +32 -0
- lefx/sets/smartspeaker_set/smartspeaker-set.lefxset +0 -0
lefx/sdk/definitions.py
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
"""Type-specific effect definitions.
|
|
2
|
+
|
|
3
|
+
Earlier generations used one flat definition class with optional fields and a
|
|
4
|
+
separate validation function. That made contradictory contracts constructible —
|
|
5
|
+
a state with runtime inputs, an event without a duration — and left "which
|
|
6
|
+
option is allowed when" as a chain of conditionals nobody could read as a whole.
|
|
7
|
+
|
|
8
|
+
V3 answers the question structurally instead. There are four definition classes,
|
|
9
|
+
one per lifecycle form, and a field that does not apply to a form simply does
|
|
10
|
+
not exist on it. A state **cannot** declare runtime inputs; there is no
|
|
11
|
+
attribute to declare them on.
|
|
12
|
+
|
|
13
|
+
What used to live in ``layer_rules`` and ``capabilities`` is gone, because all
|
|
14
|
+
of it followed from the type: only events queue, only finite forms have a
|
|
15
|
+
duration, transparency is already stated by ``composition``. The two genuine
|
|
16
|
+
choices that remain are the state's slots and whether a finite form lets a
|
|
17
|
+
caller override its duration.
|
|
18
|
+
|
|
19
|
+
Two invariants make effect code straightforward:
|
|
20
|
+
|
|
21
|
+
* every configuration parameter declares a default, so resolved configuration
|
|
22
|
+
always contains every declared key;
|
|
23
|
+
* every runtime input is either required-and-nullable or has a default, so the
|
|
24
|
+
engine can always present every declared key.
|
|
25
|
+
|
|
26
|
+
Together they mean a renderer reads ``ctx.params["color"]`` rather than
|
|
27
|
+
``params.get("color", 0x00C066)`` — which is how re-implemented parsing crept
|
|
28
|
+
into packages before.
|
|
29
|
+
|
|
30
|
+
``None`` carries one meaning throughout: nothing here, leave what is below
|
|
31
|
+
untouched. It means that in a frame and it means that in a nullable colour
|
|
32
|
+
parameter. Black is a colour and hides what is below it; the two are not
|
|
33
|
+
interchangeable, and no code path may substitute one for the other.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
from __future__ import annotations
|
|
37
|
+
|
|
38
|
+
from dataclasses import dataclass, field
|
|
39
|
+
from enum import Enum
|
|
40
|
+
from types import MappingProxyType
|
|
41
|
+
from typing import Any, ClassVar, Mapping
|
|
42
|
+
|
|
43
|
+
from .errors import SchemaError
|
|
44
|
+
from .parameters import ParamDefinition, ParamType
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class DefinitionKind(str, Enum):
|
|
48
|
+
"""The single discriminator for a definition's lifecycle form."""
|
|
49
|
+
|
|
50
|
+
STATE = "state"
|
|
51
|
+
CONTROLLED_OVERLAY = "controlled_overlay"
|
|
52
|
+
TIMED_OVERLAY = "timed_overlay"
|
|
53
|
+
EVENT = "event"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class DefinitionType(str, Enum):
|
|
57
|
+
"""The user-facing grouping used by listings and the control surface."""
|
|
58
|
+
|
|
59
|
+
STATE = "state"
|
|
60
|
+
OVERLAY = "overlay"
|
|
61
|
+
EVENT = "event"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class OverlayMode(str, Enum):
|
|
65
|
+
CONTROLLED = "controlled"
|
|
66
|
+
TIMED = "timed"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class StateSlot(str, Enum):
|
|
70
|
+
"""The two places a state may occupy."""
|
|
71
|
+
|
|
72
|
+
BACKGROUND = "background"
|
|
73
|
+
PRIMARY = "primary"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class DurationField(str, Enum):
|
|
77
|
+
"""Which parameter a finite form uses to express its length."""
|
|
78
|
+
|
|
79
|
+
DURATION_MS = "duration_ms"
|
|
80
|
+
TOTAL_MS = "total_ms"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class ColorModel(str, Enum):
|
|
84
|
+
NONE = "none"
|
|
85
|
+
MONO = "mono"
|
|
86
|
+
DUAL = "dual"
|
|
87
|
+
PALETTE = "palette"
|
|
88
|
+
GRADIENT = "gradient"
|
|
89
|
+
RANDOM_RANGE = "random_range"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class CompositionMode(str, Enum):
|
|
93
|
+
OPAQUE = "opaque"
|
|
94
|
+
TRANSPARENT = "transparent"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class InputMode(str, Enum):
|
|
98
|
+
PUSH = "push"
|
|
99
|
+
PULL = "pull"
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
_COLOR_MODEL_FIELDS: Mapping[ColorModel, frozenset[str]] = {
|
|
103
|
+
ColorModel.NONE: frozenset(),
|
|
104
|
+
ColorModel.MONO: frozenset({"color"}),
|
|
105
|
+
ColorModel.DUAL: frozenset({"color", "secondary_color"}),
|
|
106
|
+
ColorModel.PALETTE: frozenset({"colors"}),
|
|
107
|
+
ColorModel.GRADIENT: frozenset({"gradient"}),
|
|
108
|
+
ColorModel.RANDOM_RANGE: frozenset({"color_range", "random_seed"}),
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
_ALL_COLOR_FIELDS: frozenset[str] = frozenset(
|
|
112
|
+
name for names in _COLOR_MODEL_FIELDS.values() for name in names
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass(slots=True, frozen=True)
|
|
117
|
+
class InputSamplingPolicy:
|
|
118
|
+
"""How a controlled overlay obtains its runtime inputs, and when it is stale.
|
|
119
|
+
|
|
120
|
+
``failure_after_ms`` is derived rather than configured so that the grace
|
|
121
|
+
period can never contradict the heartbeat settings it is built from.
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
mode: InputMode = InputMode.PUSH
|
|
125
|
+
provider_id: str | None = None
|
|
126
|
+
interval_ms: int = 0
|
|
127
|
+
heartbeat_interval_ms: int = 1000
|
|
128
|
+
max_missed_heartbeats: int = 3
|
|
129
|
+
|
|
130
|
+
def __post_init__(self) -> None:
|
|
131
|
+
if not isinstance(self.mode, InputMode):
|
|
132
|
+
raise SchemaError(f"Input sampling mode {self.mode!r} is not a valid InputMode")
|
|
133
|
+
if self.provider_id is not None:
|
|
134
|
+
if self.mode is not InputMode.PULL:
|
|
135
|
+
raise SchemaError("provider_id is only allowed with pull sampling")
|
|
136
|
+
if not self.provider_id.strip():
|
|
137
|
+
raise SchemaError("provider_id must not be empty")
|
|
138
|
+
if self.interval_ms < 0:
|
|
139
|
+
raise SchemaError("interval_ms must be >= 0")
|
|
140
|
+
if self.heartbeat_interval_ms < 100:
|
|
141
|
+
raise SchemaError("heartbeat_interval_ms must be >= 100")
|
|
142
|
+
if self.max_missed_heartbeats < 1:
|
|
143
|
+
raise SchemaError("max_missed_heartbeats must be >= 1")
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def failure_after_ms(self) -> int:
|
|
147
|
+
return self.heartbeat_interval_ms * self.max_missed_heartbeats
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _is_valid_id(value: str) -> bool:
|
|
151
|
+
return (
|
|
152
|
+
bool(value)
|
|
153
|
+
and value.isidentifier()
|
|
154
|
+
and not value.startswith("_")
|
|
155
|
+
and value == value.lower()
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@dataclass(slots=True, frozen=True, kw_only=True)
|
|
160
|
+
class DefinitionBase:
|
|
161
|
+
"""Fields every definition has, regardless of lifecycle form."""
|
|
162
|
+
|
|
163
|
+
kind: ClassVar[DefinitionKind]
|
|
164
|
+
|
|
165
|
+
id: str
|
|
166
|
+
title: str
|
|
167
|
+
description: str
|
|
168
|
+
parameter_schema: Mapping[str, ParamDefinition] = field(default_factory=dict)
|
|
169
|
+
color_model: ColorModel = ColorModel.NONE
|
|
170
|
+
composition: CompositionMode = CompositionMode.OPAQUE
|
|
171
|
+
animated: bool = False
|
|
172
|
+
directional: bool = False
|
|
173
|
+
tags: tuple[str, ...] = ()
|
|
174
|
+
version: int = 1
|
|
175
|
+
|
|
176
|
+
def __post_init__(self) -> None:
|
|
177
|
+
self._check_identity()
|
|
178
|
+
self._freeze_schemas()
|
|
179
|
+
self._check_schema(self.parameter_schema, label="config")
|
|
180
|
+
self._check_configuration_is_total()
|
|
181
|
+
self._check_color_model()
|
|
182
|
+
self._check_visual_flags()
|
|
183
|
+
self._check_alias_collisions()
|
|
184
|
+
self._check_form()
|
|
185
|
+
|
|
186
|
+
# -- vocabulary ---------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
@property
|
|
189
|
+
def definition_type(self) -> DefinitionType:
|
|
190
|
+
if self.kind is DefinitionKind.STATE:
|
|
191
|
+
return DefinitionType.STATE
|
|
192
|
+
if self.kind is DefinitionKind.EVENT:
|
|
193
|
+
return DefinitionType.EVENT
|
|
194
|
+
return DefinitionType.OVERLAY
|
|
195
|
+
|
|
196
|
+
@property
|
|
197
|
+
def overlay_mode(self) -> OverlayMode | None:
|
|
198
|
+
if self.kind is DefinitionKind.CONTROLLED_OVERLAY:
|
|
199
|
+
return OverlayMode.CONTROLLED
|
|
200
|
+
if self.kind is DefinitionKind.TIMED_OVERLAY:
|
|
201
|
+
return OverlayMode.TIMED
|
|
202
|
+
return None
|
|
203
|
+
|
|
204
|
+
@property
|
|
205
|
+
def runtime_input_schema(self) -> Mapping[str, ParamDefinition]:
|
|
206
|
+
"""Empty for every form except the controlled overlay, which overrides it."""
|
|
207
|
+
return MappingProxyType({})
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def input_sampling(self) -> InputSamplingPolicy | None:
|
|
211
|
+
return None
|
|
212
|
+
|
|
213
|
+
# -- construction checks ------------------------------------------------
|
|
214
|
+
|
|
215
|
+
def _check_identity(self) -> None:
|
|
216
|
+
if not _is_valid_id(self.id):
|
|
217
|
+
raise SchemaError(
|
|
218
|
+
f"Definition id {self.id!r} must be lowercase snake_case "
|
|
219
|
+
"and must not start with an underscore"
|
|
220
|
+
)
|
|
221
|
+
if not self.title.strip():
|
|
222
|
+
raise SchemaError(f"Definition {self.id!r} must declare a title")
|
|
223
|
+
if not self.description.strip():
|
|
224
|
+
raise SchemaError(f"Definition {self.id!r} must declare a description")
|
|
225
|
+
if self.version < 1:
|
|
226
|
+
raise SchemaError(f"Definition {self.id!r} version must be >= 1")
|
|
227
|
+
for tag in self.tags:
|
|
228
|
+
if not isinstance(tag, str) or not tag.strip():
|
|
229
|
+
raise SchemaError(f"Definition {self.id!r} declares an empty tag")
|
|
230
|
+
if not isinstance(self.color_model, ColorModel):
|
|
231
|
+
raise SchemaError(f"Definition {self.id!r} declares an invalid color_model")
|
|
232
|
+
if not isinstance(self.composition, CompositionMode):
|
|
233
|
+
raise SchemaError(f"Definition {self.id!r} declares an invalid composition")
|
|
234
|
+
|
|
235
|
+
def _freeze_schemas(self) -> None:
|
|
236
|
+
object.__setattr__(self, "parameter_schema", MappingProxyType(dict(self.parameter_schema)))
|
|
237
|
+
|
|
238
|
+
def _check_schema(self, schema: Mapping[str, ParamDefinition], *, label: str) -> None:
|
|
239
|
+
for key, param in schema.items():
|
|
240
|
+
if not isinstance(param, ParamDefinition):
|
|
241
|
+
raise SchemaError(f"Definition {self.id!r} {label} {key!r} is not a ParamDefinition")
|
|
242
|
+
if key != param.name:
|
|
243
|
+
raise SchemaError(
|
|
244
|
+
f"Definition {self.id!r} {label} schema key/name mismatch: "
|
|
245
|
+
f"{key!r} != {param.name!r}"
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
def _check_configuration_is_total(self) -> None:
|
|
249
|
+
"""Configuration must always resolve completely.
|
|
250
|
+
|
|
251
|
+
A configuration field that is neither required nor defaulted would be
|
|
252
|
+
absent from resolved configuration, forcing every renderer to invent a
|
|
253
|
+
fallback — which is exactly how packages ended up re-implementing value
|
|
254
|
+
parsing. Requiring a default keeps that decision in the schema.
|
|
255
|
+
|
|
256
|
+
A nullable configuration field is explicitly allowed, and ``None`` there
|
|
257
|
+
carries the same meaning it has in a frame: leave the position alone.
|
|
258
|
+
That is how a definition offers "paint the background black" and "let
|
|
259
|
+
the layer below show through" as one parameter rather than two.
|
|
260
|
+
"""
|
|
261
|
+
for name, param in self.parameter_schema.items():
|
|
262
|
+
if param.required:
|
|
263
|
+
raise SchemaError(
|
|
264
|
+
f"Definition {self.id!r} config {name!r} is required; configuration "
|
|
265
|
+
"fields must declare a default instead so they always resolve"
|
|
266
|
+
)
|
|
267
|
+
if not param.has_default:
|
|
268
|
+
raise SchemaError(
|
|
269
|
+
f"Definition {self.id!r} config {name!r} must declare a default"
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def _check_color_model(self) -> None:
|
|
273
|
+
names = set(self.parameter_schema)
|
|
274
|
+
required = _COLOR_MODEL_FIELDS[self.color_model]
|
|
275
|
+
missing = required - names
|
|
276
|
+
if missing:
|
|
277
|
+
raise SchemaError(
|
|
278
|
+
f"Definition {self.id!r} color model {self.color_model.value!r} "
|
|
279
|
+
f"requires config fields: {', '.join(sorted(missing))}"
|
|
280
|
+
)
|
|
281
|
+
if self.color_model is ColorModel.NONE:
|
|
282
|
+
present = names & _ALL_COLOR_FIELDS
|
|
283
|
+
if present:
|
|
284
|
+
raise SchemaError(
|
|
285
|
+
f"Definition {self.id!r} uses color model 'none' but declares "
|
|
286
|
+
f"color configuration: {', '.join(sorted(present))}"
|
|
287
|
+
)
|
|
288
|
+
if "brightness" in names:
|
|
289
|
+
raise SchemaError(
|
|
290
|
+
f"Definition {self.id!r} uses color model 'none' and must not "
|
|
291
|
+
"declare brightness"
|
|
292
|
+
)
|
|
293
|
+
return
|
|
294
|
+
if "brightness" not in names:
|
|
295
|
+
raise SchemaError(
|
|
296
|
+
f"Definition {self.id!r} is colored and must declare config.brightness"
|
|
297
|
+
)
|
|
298
|
+
# Reserved-name rules already pin each colour field to its type, so the
|
|
299
|
+
# model only has to check presence.
|
|
300
|
+
|
|
301
|
+
def _check_visual_flags(self) -> None:
|
|
302
|
+
names = self.parameter_schema
|
|
303
|
+
if self.animated and "speed" not in names:
|
|
304
|
+
raise SchemaError(f"Animated definition {self.id!r} must declare config.speed")
|
|
305
|
+
if not self.animated and "speed" in names:
|
|
306
|
+
raise SchemaError(
|
|
307
|
+
f"Definition {self.id!r} declares config.speed but is not marked animated"
|
|
308
|
+
)
|
|
309
|
+
if self.directional and "reverse" not in names:
|
|
310
|
+
raise SchemaError(f"Directional definition {self.id!r} must declare config.reverse")
|
|
311
|
+
if not self.directional and "reverse" in names:
|
|
312
|
+
raise SchemaError(
|
|
313
|
+
f"Definition {self.id!r} declares config.reverse but is not marked directional"
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
def _check_alias_collisions(self) -> None:
|
|
317
|
+
"""Aliases live in one namespace across config and runtime inputs.
|
|
318
|
+
|
|
319
|
+
A config alias that shadows a runtime input name would make the same
|
|
320
|
+
word mean two different things depending on which payload it arrived in.
|
|
321
|
+
"""
|
|
322
|
+
canonical = set(self.parameter_schema) | set(self.runtime_input_schema)
|
|
323
|
+
owners: dict[str, str] = {}
|
|
324
|
+
for schema in (self.parameter_schema, self.runtime_input_schema):
|
|
325
|
+
for name, param in schema.items():
|
|
326
|
+
for alias in param.aliases:
|
|
327
|
+
if alias in canonical:
|
|
328
|
+
raise SchemaError(
|
|
329
|
+
f"Definition {self.id!r} alias {alias!r} on {name!r} "
|
|
330
|
+
"collides with a canonical field name"
|
|
331
|
+
)
|
|
332
|
+
owner = owners.get(alias)
|
|
333
|
+
if owner is not None:
|
|
334
|
+
raise SchemaError(
|
|
335
|
+
f"Definition {self.id!r} alias {alias!r} is shared by "
|
|
336
|
+
f"{owner!r} and {name!r}"
|
|
337
|
+
)
|
|
338
|
+
owners[alias] = name
|
|
339
|
+
|
|
340
|
+
def _check_form(self) -> None:
|
|
341
|
+
"""Hook for form-specific rules; the base form has none."""
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
@dataclass(slots=True, frozen=True, kw_only=True)
|
|
345
|
+
class StateDefinition(DefinitionBase):
|
|
346
|
+
"""A persistent visual ground state.
|
|
347
|
+
|
|
348
|
+
Runs until replaced or cleared, has no duration and no runtime inputs.
|
|
349
|
+
``slots`` declares which of the two state places the definition is designed
|
|
350
|
+
for; only a background state can be restored on service start.
|
|
351
|
+
"""
|
|
352
|
+
|
|
353
|
+
kind: ClassVar[DefinitionKind] = DefinitionKind.STATE
|
|
354
|
+
|
|
355
|
+
slots: tuple[StateSlot, ...] = (StateSlot.PRIMARY,)
|
|
356
|
+
restorable: bool = False
|
|
357
|
+
|
|
358
|
+
def _check_form(self) -> None:
|
|
359
|
+
if not self.slots:
|
|
360
|
+
raise SchemaError(f"State {self.id!r} must declare at least one slot")
|
|
361
|
+
for slot in self.slots:
|
|
362
|
+
if not isinstance(slot, StateSlot):
|
|
363
|
+
raise SchemaError(f"State {self.id!r} declares an invalid slot {slot!r}")
|
|
364
|
+
if len(set(self.slots)) != len(self.slots):
|
|
365
|
+
raise SchemaError(f"State {self.id!r} declares a slot twice")
|
|
366
|
+
if self.restorable and StateSlot.BACKGROUND not in self.slots:
|
|
367
|
+
raise SchemaError(
|
|
368
|
+
f"State {self.id!r} is marked restorable but does not allow the "
|
|
369
|
+
"background slot; only the background state is persisted"
|
|
370
|
+
)
|
|
371
|
+
for finite in ("duration_ms", "total_ms"):
|
|
372
|
+
if finite in self.parameter_schema:
|
|
373
|
+
raise SchemaError(
|
|
374
|
+
f"State {self.id!r} declares config.{finite}; states are indefinite"
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
@dataclass(slots=True, frozen=True, kw_only=True)
|
|
379
|
+
class ControlledOverlayDefinition(DefinitionBase):
|
|
380
|
+
"""An indefinite overlay addressed by channel and fed with mutable inputs.
|
|
381
|
+
|
|
382
|
+
This is the only form that may declare runtime inputs, and therefore the
|
|
383
|
+
only one with an input sampling policy.
|
|
384
|
+
"""
|
|
385
|
+
|
|
386
|
+
kind: ClassVar[DefinitionKind] = DefinitionKind.CONTROLLED_OVERLAY
|
|
387
|
+
|
|
388
|
+
runtime_inputs: Mapping[str, ParamDefinition] = field(default_factory=dict)
|
|
389
|
+
sampling: InputSamplingPolicy = field(default_factory=InputSamplingPolicy)
|
|
390
|
+
|
|
391
|
+
@property
|
|
392
|
+
def runtime_input_schema(self) -> Mapping[str, ParamDefinition]:
|
|
393
|
+
return self.runtime_inputs
|
|
394
|
+
|
|
395
|
+
@property
|
|
396
|
+
def input_sampling(self) -> InputSamplingPolicy:
|
|
397
|
+
return self.sampling
|
|
398
|
+
|
|
399
|
+
def _check_form(self) -> None:
|
|
400
|
+
object.__setattr__(self, "runtime_inputs", MappingProxyType(dict(self.runtime_inputs)))
|
|
401
|
+
self._check_schema(self.runtime_inputs, label="runtime input")
|
|
402
|
+
|
|
403
|
+
overlap = set(self.runtime_inputs) & set(self.parameter_schema)
|
|
404
|
+
if overlap:
|
|
405
|
+
raise SchemaError(
|
|
406
|
+
f"Controlled overlay {self.id!r} declares {', '.join(sorted(overlap))} "
|
|
407
|
+
"as both configuration and runtime input; a runtime value must not "
|
|
408
|
+
"be able to shadow stable configuration"
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
for name, param in self.runtime_inputs.items():
|
|
412
|
+
if param.required and not param.nullable:
|
|
413
|
+
raise SchemaError(
|
|
414
|
+
f"Controlled overlay {self.id!r} runtime input {name!r} is required "
|
|
415
|
+
"and must be nullable: no value has arrived yet when the instance "
|
|
416
|
+
"starts, and the value becomes null again when the source fails"
|
|
417
|
+
)
|
|
418
|
+
if not param.required and not param.has_default:
|
|
419
|
+
raise SchemaError(
|
|
420
|
+
f"Controlled overlay {self.id!r} runtime input {name!r} must be "
|
|
421
|
+
"required or declare a default so every declared key is always present"
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
if not isinstance(self.sampling, InputSamplingPolicy):
|
|
425
|
+
raise SchemaError(f"Controlled overlay {self.id!r} declares an invalid sampling policy")
|
|
426
|
+
if self.sampling.mode is InputMode.PULL and not self.runtime_inputs:
|
|
427
|
+
raise SchemaError(
|
|
428
|
+
f"Controlled overlay {self.id!r} uses pull sampling but declares no runtime inputs"
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
@dataclass(slots=True, frozen=True, kw_only=True)
|
|
433
|
+
class _FiniteDefinition(DefinitionBase):
|
|
434
|
+
"""Shared rules for the two forms the engine ends automatically."""
|
|
435
|
+
|
|
436
|
+
duration_field: DurationField = DurationField.DURATION_MS
|
|
437
|
+
supports_duration_override: bool = False
|
|
438
|
+
|
|
439
|
+
def _check_finite_duration(self) -> None:
|
|
440
|
+
if not isinstance(self.duration_field, DurationField):
|
|
441
|
+
raise SchemaError(f"Definition {self.id!r} declares an invalid duration_field")
|
|
442
|
+
name = self.duration_field.value
|
|
443
|
+
param = self.parameter_schema.get(name)
|
|
444
|
+
if param is None:
|
|
445
|
+
raise SchemaError(
|
|
446
|
+
f"Definition {self.id!r} declares duration_field {name!r} "
|
|
447
|
+
f"but has no config.{name}"
|
|
448
|
+
)
|
|
449
|
+
if param.type is not ParamType.DURATION_MS:
|
|
450
|
+
raise SchemaError(
|
|
451
|
+
f"Definition {self.id!r} config.{name} must use type 'duration_ms'"
|
|
452
|
+
)
|
|
453
|
+
other = "total_ms" if name == "duration_ms" else "duration_ms"
|
|
454
|
+
if other in self.parameter_schema:
|
|
455
|
+
raise SchemaError(
|
|
456
|
+
f"Definition {self.id!r} declares both duration_ms and total_ms; "
|
|
457
|
+
"a finite form has exactly one length"
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
@dataclass(slots=True, frozen=True, kw_only=True)
|
|
462
|
+
class TimedOverlayDefinition(_FiniteDefinition):
|
|
463
|
+
"""A finite overlay above a state, activated once and removed on expiry.
|
|
464
|
+
|
|
465
|
+
Has no channel and no runtime inputs: start and end are fixed at activation.
|
|
466
|
+
"""
|
|
467
|
+
|
|
468
|
+
kind: ClassVar[DefinitionKind] = DefinitionKind.TIMED_OVERLAY
|
|
469
|
+
|
|
470
|
+
def _check_form(self) -> None:
|
|
471
|
+
self._check_finite_duration()
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
@dataclass(slots=True, frozen=True, kw_only=True)
|
|
475
|
+
class EventDefinition(_FiniteDefinition):
|
|
476
|
+
"""A one-shot prioritized signal that passes through the event queue."""
|
|
477
|
+
|
|
478
|
+
kind: ClassVar[DefinitionKind] = DefinitionKind.EVENT
|
|
479
|
+
|
|
480
|
+
default_priority: int | None = None
|
|
481
|
+
|
|
482
|
+
def _check_form(self) -> None:
|
|
483
|
+
self._check_finite_duration()
|
|
484
|
+
if self.default_priority is not None and isinstance(self.default_priority, bool):
|
|
485
|
+
raise SchemaError(f"Event {self.id!r} default_priority must be an integer")
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
AnyDefinition = (
|
|
489
|
+
StateDefinition | ControlledOverlayDefinition | TimedOverlayDefinition | EventDefinition
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
DEFINITION_CLASSES: Mapping[DefinitionKind, type[DefinitionBase]] = MappingProxyType(
|
|
494
|
+
{
|
|
495
|
+
DefinitionKind.STATE: StateDefinition,
|
|
496
|
+
DefinitionKind.CONTROLLED_OVERLAY: ControlledOverlayDefinition,
|
|
497
|
+
DefinitionKind.TIMED_OVERLAY: TimedOverlayDefinition,
|
|
498
|
+
DefinitionKind.EVENT: EventDefinition,
|
|
499
|
+
}
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def resolved_default_configuration(definition: DefinitionBase) -> dict[str, Any]:
|
|
504
|
+
"""Every configuration key with its declared default, already canonical."""
|
|
505
|
+
return {name: param.default for name, param in definition.parameter_schema.items()}
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
__all__ = [
|
|
509
|
+
"AnyDefinition",
|
|
510
|
+
"ColorModel",
|
|
511
|
+
"CompositionMode",
|
|
512
|
+
"ControlledOverlayDefinition",
|
|
513
|
+
"DEFINITION_CLASSES",
|
|
514
|
+
"DefinitionBase",
|
|
515
|
+
"DefinitionKind",
|
|
516
|
+
"DefinitionType",
|
|
517
|
+
"DurationField",
|
|
518
|
+
"EventDefinition",
|
|
519
|
+
"InputMode",
|
|
520
|
+
"InputSamplingPolicy",
|
|
521
|
+
"OverlayMode",
|
|
522
|
+
"StateDefinition",
|
|
523
|
+
"StateSlot",
|
|
524
|
+
"TimedOverlayDefinition",
|
|
525
|
+
"resolved_default_configuration",
|
|
526
|
+
]
|
lefx/sdk/effects.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""The base class every effect package implements."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from typing import Any, ClassVar, Mapping
|
|
7
|
+
|
|
8
|
+
from .context import InputContext, RenderContext
|
|
9
|
+
from .definitions import DefinitionBase
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BaseEffect(ABC):
|
|
13
|
+
"""One effect implementation, bound to exactly one definition.
|
|
14
|
+
|
|
15
|
+
The engine creates one instance per activation, so an implementation may
|
|
16
|
+
hold Python state for the run it is part of. It must not hold external
|
|
17
|
+
resources: V3 has no start, stop, reset or finished hook, and an instance is
|
|
18
|
+
dropped without notice when the engine ends it.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
definition: ClassVar[DefinitionBase]
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def get_definition(cls) -> DefinitionBase:
|
|
25
|
+
return cls.definition
|
|
26
|
+
|
|
27
|
+
def sample_inputs(self, ctx: InputContext) -> Mapping[str, Any] | None:
|
|
28
|
+
"""Provide runtime inputs for a pull-sampled controlled overlay.
|
|
29
|
+
|
|
30
|
+
Only called when the definition declares pull sampling without a
|
|
31
|
+
provider id. Must not block: it runs inside the render loop.
|
|
32
|
+
"""
|
|
33
|
+
del ctx
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def render(self, ctx: RenderContext) -> list[int | None]:
|
|
38
|
+
"""Return exactly ``ctx.led_count`` entries.
|
|
39
|
+
|
|
40
|
+
Each entry is either an RGB integer or ``None``, and the difference
|
|
41
|
+
between ``None`` and black is the whole point of the layer model:
|
|
42
|
+
|
|
43
|
+
``None``
|
|
44
|
+
Contribute nothing at this position. Whatever the layers below
|
|
45
|
+
composed stays visible — a direction marker over a state leaves
|
|
46
|
+
eleven of twelve positions ``None`` so the state shows through.
|
|
47
|
+
|
|
48
|
+
``0x000000``
|
|
49
|
+
Black is a colour. It overwrites what is below and switches the LED
|
|
50
|
+
off. Use it to hide the layer below, never as a stand-in for "no
|
|
51
|
+
contribution".
|
|
52
|
+
|
|
53
|
+
Only a transparent definition may return ``None``; an opaque one fills
|
|
54
|
+
every position. ``ctx.transparent_frame()`` and ``ctx.blank_frame()``
|
|
55
|
+
give the two neutral starting points.
|
|
56
|
+
|
|
57
|
+
Must be deterministic for a given context and fast: it runs once per
|
|
58
|
+
frame per active layer, and it performs no I/O.
|
|
59
|
+
"""
|
|
60
|
+
raise NotImplementedError
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
__all__ = ["BaseEffect"]
|
lefx/sdk/errors.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Error types shared by value normalization, schema construction and validation.
|
|
2
|
+
|
|
3
|
+
Three kinds of failure are distinguished on purpose:
|
|
4
|
+
|
|
5
|
+
``SchemaError``
|
|
6
|
+
The definition itself is wrong. Raised while a definition is being
|
|
7
|
+
constructed, so a broken contract can never reach the registry.
|
|
8
|
+
|
|
9
|
+
``ValueNormalizationError``
|
|
10
|
+
A single value could not be brought into canonical form. Carries a code and
|
|
11
|
+
optional suggestions but no field path — the caller knows the field.
|
|
12
|
+
|
|
13
|
+
``ParameterValidationError``
|
|
14
|
+
One or more fields of a command payload were rejected. Aggregates issues so
|
|
15
|
+
a caller learns about every problem at once rather than one per round trip.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SchemaError(ValueError):
|
|
25
|
+
"""A definition or parameter declaration violates the LEFX V3 contract."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ValueNormalizationError(ValueError):
|
|
29
|
+
"""A single value could not be normalized into its canonical form."""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
message: str,
|
|
34
|
+
*,
|
|
35
|
+
code: str,
|
|
36
|
+
value: Any,
|
|
37
|
+
suggestions: tuple[str, ...] = (),
|
|
38
|
+
) -> None:
|
|
39
|
+
super().__init__(message)
|
|
40
|
+
self.code = code
|
|
41
|
+
self.value = value
|
|
42
|
+
self.suggestions = suggestions
|
|
43
|
+
|
|
44
|
+
def to_dict(self, *, field: str | None = None) -> dict[str, Any]:
|
|
45
|
+
payload: dict[str, Any] = {
|
|
46
|
+
"code": self.code,
|
|
47
|
+
"value": self.value,
|
|
48
|
+
"message": str(self),
|
|
49
|
+
}
|
|
50
|
+
if field is not None:
|
|
51
|
+
payload["field"] = field
|
|
52
|
+
if self.suggestions:
|
|
53
|
+
payload["suggestions"] = list(self.suggestions)
|
|
54
|
+
return payload
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(slots=True, frozen=True)
|
|
58
|
+
class ValidationIssue:
|
|
59
|
+
"""One rejected field, with everything a caller needs to correct it."""
|
|
60
|
+
|
|
61
|
+
code: str
|
|
62
|
+
field: str
|
|
63
|
+
message: str
|
|
64
|
+
value: Any = None
|
|
65
|
+
suggestions: tuple[str, ...] = ()
|
|
66
|
+
|
|
67
|
+
def to_dict(self) -> dict[str, Any]:
|
|
68
|
+
payload: dict[str, Any] = {
|
|
69
|
+
"code": self.code,
|
|
70
|
+
"field": self.field,
|
|
71
|
+
"message": self.message,
|
|
72
|
+
"value": self.value,
|
|
73
|
+
}
|
|
74
|
+
if self.suggestions:
|
|
75
|
+
payload["suggestions"] = list(self.suggestions)
|
|
76
|
+
return payload
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class ParameterValidationError(ValueError):
|
|
80
|
+
"""A command payload was rejected. Reports every issue, not just the first."""
|
|
81
|
+
|
|
82
|
+
def __init__(self, issues: list[ValidationIssue]) -> None:
|
|
83
|
+
self.issues = tuple(issues)
|
|
84
|
+
super().__init__("; ".join(issue.message for issue in issues))
|
|
85
|
+
|
|
86
|
+
def to_dict(self) -> dict[str, Any]:
|
|
87
|
+
return {
|
|
88
|
+
"code": "validation_failed",
|
|
89
|
+
"issues": [issue.to_dict() for issue in self.issues],
|
|
90
|
+
}
|