omnipython-applibs 0.1.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.
Files changed (143) hide show
  1. appkit/__init__.py +52 -0
  2. appkit/__init__.pyi +17 -0
  3. appkit/data.py +149 -0
  4. appkit/data.pyi +25 -0
  5. appkit/foreground.py +357 -0
  6. appkit/foreground.pyi +45 -0
  7. appkit/home.py +19 -0
  8. appkit/home.pyi +8 -0
  9. appkit/hover.py +59 -0
  10. appkit/hover.pyi +13 -0
  11. appkit/index.py +199 -0
  12. appkit/index.pyi +30 -0
  13. appkit/launcher.py +1075 -0
  14. appkit/launcher.pyi +96 -0
  15. appkit/manifest.py +75 -0
  16. appkit/manifest.pyi +8 -0
  17. appkit/net.py +167 -0
  18. appkit/net.pyi +34 -0
  19. appkit/places.py +202 -0
  20. appkit/places.pyi +39 -0
  21. appkit/py.typed +0 -0
  22. appkit/scope.py +636 -0
  23. appkit/scope.pyi +110 -0
  24. appkit/shell.py +121 -0
  25. appkit/shell.pyi +32 -0
  26. appkit/worker.py +409 -0
  27. appkit/worker.pyi +83 -0
  28. controlkit/__init__.py +19 -0
  29. controlkit/__init__.pyi +10 -0
  30. controlkit/_draw.py +88 -0
  31. controlkit/_safe.py +29 -0
  32. controlkit/dotmatrix.py +101 -0
  33. controlkit/dotmatrix.pyi +23 -0
  34. controlkit/encoder.py +297 -0
  35. controlkit/encoder.pyi +55 -0
  36. controlkit/font5x7.py +22 -0
  37. controlkit/font5x7.pyi +11 -0
  38. controlkit/lamp_button.py +92 -0
  39. controlkit/lamp_button.pyi +40 -0
  40. controlkit/meter.py +86 -0
  41. controlkit/meter.pyi +30 -0
  42. controlkit/mock.py +116 -0
  43. controlkit/mock.pyi +35 -0
  44. controlkit/pad_grid.py +113 -0
  45. controlkit/pad_grid.pyi +41 -0
  46. controlkit/py.typed +0 -0
  47. controlkit/slider.py +185 -0
  48. controlkit/slider.pyi +49 -0
  49. controlkit/sprites.py +279 -0
  50. controlkit/sprites.pyi +22 -0
  51. controlkit/theme.py +120 -0
  52. controlkit/theme.pyi +15 -0
  53. deskkit/__init__.py +74 -0
  54. deskkit/__init__.pyi +18 -0
  55. deskkit/forecast.py +261 -0
  56. deskkit/forecast.pyi +40 -0
  57. deskkit/localtime.py +215 -0
  58. deskkit/localtime.pyi +33 -0
  59. deskkit/py.typed +0 -0
  60. deskkit/shell.py +113 -0
  61. deskkit/shell.pyi +33 -0
  62. omnipython_applibs-0.1.2.dist-info/METADATA +90 -0
  63. omnipython_applibs-0.1.2.dist-info/RECORD +143 -0
  64. omnipython_applibs-0.1.2.dist-info/WHEEL +5 -0
  65. omnipython_applibs-0.1.2.dist-info/licenses/LICENSE +21 -0
  66. omnipython_applibs-0.1.2.dist-info/top_level.txt +9 -0
  67. programkit/__init__.py +27 -0
  68. programkit/__init__.pyi +8 -0
  69. programkit/board.py +56 -0
  70. programkit/board.pyi +20 -0
  71. programkit/press.py +63 -0
  72. programkit/press.pyi +17 -0
  73. programkit/py.typed +0 -0
  74. programkit/rows.py +149 -0
  75. programkit/rows.pyi +34 -0
  76. sayso/__init__.py +109 -0
  77. sayso/__init__.pyi +124 -0
  78. sayso/auth.py +811 -0
  79. sayso/auth.pyi +102 -0
  80. sayso/client.py +1286 -0
  81. sayso/client.pyi +138 -0
  82. sayso/keepalive.py +371 -0
  83. sayso/keepalive.pyi +53 -0
  84. sayso/local.py +85 -0
  85. sayso/local.pyi +28 -0
  86. sayso/object_specs.py +706 -0
  87. sayso/object_specs.pyi +8 -0
  88. sayso/objects.py +411 -0
  89. sayso/objects.pyi +854 -0
  90. sayso/py.typed +0 -0
  91. sayso/schema_coverage.py +210 -0
  92. sayso/schema_coverage.pyi +13 -0
  93. sayso/transport.py +426 -0
  94. sayso/transport.pyi +47 -0
  95. songmodel/__init__.py +30 -0
  96. songmodel/__init__.pyi +9 -0
  97. songmodel/link.py +189 -0
  98. songmodel/link.pyi +53 -0
  99. songmodel/model.py +238 -0
  100. songmodel/model.pyi +40 -0
  101. songmodel/page.html +225 -0
  102. songmodel/page_data.py +236 -0
  103. songmodel/page_data.pyi +6 -0
  104. songmodel/py.typed +0 -0
  105. soundmodule/__init__.py +7 -0
  106. soundmodule/__init__.pyi +6 -0
  107. soundmodule/engine.py +1001 -0
  108. soundmodule/engine.pyi +196 -0
  109. soundmodule/names.py +298 -0
  110. soundmodule/names.pyi +30 -0
  111. soundmodule/pipes.py +105 -0
  112. soundmodule/pipes.pyi +31 -0
  113. soundmodule/py.typed +0 -0
  114. soundmodule/run.py +249 -0
  115. soundmodule/run.pyi +49 -0
  116. spectrum/__init__.py +72 -0
  117. spectrum/__init__.pyi +30 -0
  118. spectrum/analyzer.py +231 -0
  119. spectrum/analyzer.pyi +67 -0
  120. spectrum/fake_music.py +201 -0
  121. spectrum/fake_music.pyi +36 -0
  122. spectrum/pump_levels.py +126 -0
  123. spectrum/pump_levels.pyi +46 -0
  124. spectrum/py.typed +0 -0
  125. spectrum/spectrum_view.py +403 -0
  126. spectrum/spectrum_view.pyi +80 -0
  127. touchkit/__init__.py +4 -0
  128. touchkit/__init__.pyi +4 -0
  129. touchkit/cam.py +146 -0
  130. touchkit/cam.pyi +35 -0
  131. touchkit/clock.py +18 -0
  132. touchkit/clock.pyi +6 -0
  133. touchkit/ecp.py +134 -0
  134. touchkit/ecp.pyi +28 -0
  135. touchkit/net.py +347 -0
  136. touchkit/net.pyi +27 -0
  137. touchkit/py.typed +0 -0
  138. touchkit/settings.py +85 -0
  139. touchkit/settings.pyi +17 -0
  140. touchkit/shell.py +268 -0
  141. touchkit/shell.pyi +42 -0
  142. touchkit/ui.py +167 -0
  143. touchkit/ui.pyi +53 -0
appkit/worker.pyi ADDED
@@ -0,0 +1,83 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """One background thread for blocking calls, shared by every app."""
5
+
6
+ from collections.abc import Callable
7
+ from typing import Any
8
+
9
+ STACK: int
10
+
11
+ _Done = Callable[[Any, BaseException | None], Any]
12
+
13
+ class _Base:
14
+ busy: bool
15
+ ran: int
16
+ errors: int
17
+ def __init__(self) -> None: ...
18
+ def queued(self, owner: Any = None) -> int:
19
+ """How many jobs are waiting, or `owner`'s."""
20
+
21
+ class Worker(_Base):
22
+ """Runs jobs one at a time on a background thread."""
23
+
24
+ def __init__(self, stack: int | None = None) -> None: ...
25
+ def submit(
26
+ self,
27
+ work: Callable[[], Any],
28
+ done: _Done | None = None,
29
+ key: Any = None,
30
+ owner: Any = None,
31
+ release: Callable[[], Any] | None = None,
32
+ ) -> None:
33
+ """Run `work()` on the thread, then `done(value, error)` on the main thread."""
34
+ def pending(self, key: Any, owner: Any = None) -> bool:
35
+ """True while a job with `key` is waiting or running."""
36
+ def cancel(self, owner: Any) -> None:
37
+ """Drop everything `owner` queued that hasn't started."""
38
+
39
+ class TimerWorker(_Base):
40
+ """Runs jobs one per timer tick, where there are no threads."""
41
+
42
+ def __init__(self) -> None: ...
43
+ def submit(
44
+ self,
45
+ work: Callable[[], Any],
46
+ done: _Done | None = None,
47
+ key: Any = None,
48
+ owner: Any = None,
49
+ release: Callable[[], Any] | None = None,
50
+ ) -> None:
51
+ """Queue `work()`, then `done(value, error)` once it has run."""
52
+ def pending(self, key: Any, owner: Any = None) -> bool:
53
+ """True while a job with `key` is waiting or running."""
54
+ def cancel(self, owner: Any) -> None:
55
+ """Drop everything `owner` queued that hasn't started."""
56
+
57
+ class ScopedWorker:
58
+ """A worker as one app's scope sees it."""
59
+
60
+ worker: Worker | TimerWorker
61
+ owner: Any
62
+ def __init__(self, worker: Worker | TimerWorker, owner: Any) -> None: ...
63
+ @property
64
+ def busy(self) -> bool: ...
65
+ def submit(
66
+ self,
67
+ work: Callable[[], Any],
68
+ done: _Done | None = None,
69
+ key: Any = None,
70
+ release: Callable[[], Any] | None = None,
71
+ ) -> None:
72
+ """Run `work()` in the background, then `done(value, error)` on the main thread."""
73
+ def pending(self, key: Any) -> bool:
74
+ """True while this app has a job with `key` waiting or running."""
75
+ def queued(self) -> int:
76
+ """How many of this app's jobs are waiting."""
77
+ def cancel(self) -> None:
78
+ """Drop every job this app queued that hasn't started."""
79
+
80
+ def threads() -> bool:
81
+ """True where this interpreter can start a thread."""
82
+ def shared() -> Worker | TimerWorker:
83
+ """The program's one worker, started on first use."""
controlkit/__init__.py ADDED
@@ -0,0 +1,19 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """The shared controls: every knob, slider, meter, button and pad the apps draw.
5
+
6
+ Each control is a plain class over LVGL, placed by its parent's coordinates:
7
+ the encoder with its LED ring, the slider with an optional lamp, the LED
8
+ segment meter, the lamp button, the pad grid, and the dot-matrix display.
9
+ A panel is a layout of these. The layouts live beside the apps that use them
10
+ (see the README), so this package imports nothing from any app.
11
+ """
12
+
13
+ from .lamp_button import LampButton
14
+ from .meter import LedMeter
15
+ from .pad_grid import PadGrid
16
+ from .slider import Slider
17
+ from .theme import THEMES
18
+
19
+ __all__ = ["LampButton", "LedMeter", "PadGrid", "Slider", "THEMES"]
@@ -0,0 +1,10 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ from .lamp_button import LampButton as LampButton
5
+ from .meter import LedMeter as LedMeter
6
+ from .pad_grid import PadGrid as PadGrid
7
+ from .slider import Slider as Slider
8
+ from .theme import THEMES as THEMES
9
+
10
+ __all__ = ['LampButton', 'LedMeter', 'PadGrid', 'Slider', 'THEMES']
controlkit/_draw.py ADDED
@@ -0,0 +1,88 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """The few drawing helpers every control shares: plain boxes, labels, grain."""
5
+
6
+ import lvgl as lv
7
+
8
+ from . import sprites
9
+
10
+
11
+ def plain(obj, clickable=False):
12
+ obj.remove_style_all()
13
+ obj.remove_flag(lv.obj.FLAG.SCROLLABLE)
14
+ if not clickable:
15
+ obj.remove_flag(lv.obj.FLAG.CLICKABLE)
16
+ return obj
17
+
18
+
19
+ def box(parent, x, y, w, h):
20
+ obj = plain(lv.obj(parent))
21
+ obj.set_pos(x, y)
22
+ obj.set_size(w, h)
23
+ return obj
24
+
25
+
26
+ def label(parent, text, rgb, font=None, spacing=1):
27
+ text_obj = lv.label(parent)
28
+ text_obj.set_text(text)
29
+ text_obj.set_style_text_color(lv.color_hex(rgb), 0)
30
+ text_obj.set_style_text_font(font or lv.font_montserrat_14, 0)
31
+ text_obj.set_style_text_letter_space(spacing, 0)
32
+ return text_obj
33
+
34
+
35
+ def text_width(text, font, spacing):
36
+ width = 0
37
+ for i, char in enumerate(text):
38
+ following = ord(text[i + 1]) if i + 1 < len(text) else 0
39
+ width += font.get_glyph_width(ord(char), following) + spacing
40
+ return width
41
+
42
+
43
+ def fill(obj, top, bottom=None):
44
+ obj.set_style_bg_opa(lv.OPA.COVER, 0)
45
+ obj.set_style_bg_color(lv.color_hex(top), 0)
46
+ if bottom is not None:
47
+ obj.set_style_bg_grad_color(lv.color_hex(bottom), 0)
48
+ obj.set_style_bg_grad_dir(lv.GRAD_DIR.VER, 0)
49
+
50
+
51
+ def finish(obj, rgb, grain):
52
+ """Give *obj* a grained finish: "matte", "brushed" or None for flat."""
53
+ if grain:
54
+ obj.set_style_bg_image_src(sprites.texture(rgb, grain == "brushed"), 0)
55
+ obj.set_style_bg_image_tiled(True, 0)
56
+
57
+
58
+ class Lamp:
59
+ """An indicator LED: a dark lens, and a lit image shown over it."""
60
+
61
+ def __init__(self, parent, cx, cy, lens, theme):
62
+ self._theme = theme
63
+ self._lens = lens
64
+ self._cx = cx
65
+ self._cy = cy
66
+ self._parent = parent
67
+ off = box(parent, int(cx - lens - 1), int(cy - lens - 1), int(2 * lens + 2), int(2 * lens + 2))
68
+ fill(off, theme["led_off"])
69
+ off.set_style_radius(lv.RADIUS_CIRCLE, 0)
70
+ off.set_style_border_width(1, 0)
71
+ off.set_style_border_color(lv.color_hex(theme["led_rim"]), 0)
72
+ self._image = lv.image(parent)
73
+ self._image.remove_flag(lv.obj.FLAG.CLICKABLE)
74
+ self._image.add_flag(lv.obj.FLAG.HIDDEN)
75
+ self.rgb = None
76
+
77
+ def set(self, rgb):
78
+ """Light it in *rgb*, or put it out with None."""
79
+ if rgb == self.rgb:
80
+ return
81
+ self.rgb = rgb
82
+ if rgb is None:
83
+ self._image.add_flag(lv.obj.FLAG.HIDDEN)
84
+ return
85
+ buf, size = sprites.small_led(self._lens, rgb, self._theme)
86
+ self._image.set_src(buf)
87
+ self._image.set_pos(int(self._cx - size / 2 + 0.5), int(self._cy - size / 2 + 0.5))
88
+ self._image.remove_flag(lv.obj.FLAG.HIDDEN)
controlkit/_safe.py ADDED
@@ -0,0 +1,29 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Keep an exception from escaping into LVGL.
5
+
6
+ On MicroPython, a callback that raises leaves the binding's re-entrancy
7
+ count raised, and from then on LVGL's timers quietly stop. Every callback
8
+ the panel gives LVGL goes through ``safe``: the error is printed and the
9
+ panel carries on.
10
+ """
11
+
12
+ import sys
13
+
14
+
15
+ def safe(fn):
16
+ def wrapper(*args):
17
+ try:
18
+ return fn(*args)
19
+ except Exception as exc: # noqa: BLE001
20
+ printer = getattr(sys, "print_exception", None)
21
+ if printer is not None:
22
+ printer(exc)
23
+ else:
24
+ import traceback
25
+
26
+ traceback.print_exception(type(exc), exc, exc.__traceback__)
27
+ return None
28
+
29
+ return wrapper
@@ -0,0 +1,101 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """A character LCD in the style of a 16x2 dot-matrix module.
5
+
6
+ Each line is a canvas. Setting a line repaints only the characters that
7
+ changed, so a value that turns from 100 to 101 costs one character cell:
8
+ 36 small rectangles, not a whole display.
9
+ """
10
+
11
+ import lvgl as lv
12
+
13
+ from . import font5x7
14
+ from ._safe import safe
15
+
16
+
17
+ class DotMatrix:
18
+ def __init__(self, parent, columns, lines, pitch, theme):
19
+ self.columns = columns
20
+ self.pitch = pitch
21
+ self.cell_w = 6 * pitch
22
+ self.cell_h = 8 * pitch
23
+ self.width = columns * self.cell_w - pitch
24
+ self.height = lines * self.cell_h + (lines - 1) * pitch
25
+ self._on = lv.color_hex(theme["dot_on"])
26
+ self._off = lv.color_hex(theme["dot_off"])
27
+ self._bg = lv.color_hex(theme["display_bg"])
28
+ self._dot = max(1, pitch - 1)
29
+ self._rect = lv.draw_rect_dsc_t()
30
+ self._area = lv.area_t()
31
+
32
+ self.obj = lv.obj(parent)
33
+ self.obj.remove_style_all()
34
+ self.obj.remove_flag(lv.obj.FLAG.SCROLLABLE)
35
+ self.obj.remove_flag(lv.obj.FLAG.CLICKABLE)
36
+ self.obj.set_size(self.width, self.height)
37
+ self._canvases = []
38
+ self._bufs = []
39
+ self._text = []
40
+ for line in range(lines):
41
+ buf = lv.draw_buf_create(self.width, self.cell_h, lv.COLOR_FORMAT.RGB565, 0)
42
+ canvas = lv.canvas(self.obj)
43
+ canvas.set_draw_buf(buf)
44
+ canvas.set_pos(0, line * (self.cell_h + pitch))
45
+ canvas.fill_bg(self._bg, lv.OPA.COVER)
46
+ self._canvases.append(canvas)
47
+ self._bufs.append(buf)
48
+ self._text.append(None)
49
+ self.set_line(line, "")
50
+ self.obj.add_event_cb(safe(self._on_delete), lv.EVENT.DELETE, None)
51
+
52
+ def _on_delete(self, _event):
53
+ # The canvases go with the object; their buffers are ours to free.
54
+ for buf in self._bufs:
55
+ try:
56
+ buf.destroy()
57
+ except Exception:
58
+ pass
59
+ self._bufs = []
60
+
61
+ def text(self, line):
62
+ """What line *line* shows now, padded to the display's width."""
63
+ return self._text[line]
64
+
65
+ def set_line(self, line, text):
66
+ """Show *text* on line *line*, cut or padded to the display's width."""
67
+ text = (str(text) + " " * self.columns)[: self.columns]
68
+ old = self._text[line]
69
+ if text == old:
70
+ return
71
+ canvas = self._canvases[line]
72
+ layer = lv.layer_t()
73
+ canvas.init_layer(layer)
74
+ for column in range(self.columns):
75
+ char = text[column]
76
+ if old is None or old[column] != char:
77
+ self._paint(layer, column, char)
78
+ canvas.finish_layer(layer)
79
+ self._text[line] = text
80
+
81
+ def _paint(self, layer, column, char):
82
+ d = self._rect
83
+ area = self._area
84
+ pitch = self.pitch
85
+ dot = self._dot
86
+ x0 = column * self.cell_w
87
+ rows = font5x7.rows(char)
88
+ for row in range(7):
89
+ bits = rows[row]
90
+ y = row * pitch
91
+ for col in range(5):
92
+ d.init()
93
+ d.bg_color = self._on if bits & (16 >> col) else self._off
94
+ d.bg_opa = 255
95
+ if dot > 2:
96
+ d.radius = 1
97
+ area.x1 = x0 + col * pitch
98
+ area.y1 = y
99
+ area.x2 = area.x1 + dot - 1
100
+ area.y2 = y + dot - 1
101
+ lv.draw_rect(layer, d, area)
@@ -0,0 +1,23 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """A character LCD in the style of a 16x2 dot-matrix module."""
5
+ from typing import Any
6
+
7
+ from .theme import _Theme
8
+
9
+ class DotMatrix:
10
+ """A dot-matrix display of `columns` x `lines` characters inside `parent`."""
11
+
12
+ columns: int
13
+ pitch: int
14
+ cell_w: int
15
+ cell_h: int
16
+ width: int
17
+ height: int
18
+ obj: Any
19
+ def __init__(self, parent: Any, columns: int, lines: int, pitch: int, theme: _Theme) -> None: ...
20
+ def text(self, line: int) -> str:
21
+ """What line `line` shows now, padded to the display's width."""
22
+ def set_line(self, line: int, text: object) -> None:
23
+ """Show `text` on line `line`, cut or padded to the display's width."""
controlkit/encoder.py ADDED
@@ -0,0 +1,297 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """One endless encoder: a knurled cap, its pointer, and a ring of 16 LEDs.
5
+
6
+ The cap and the unlit ring are one shared image. What moves is the pointer
7
+ (a small image, rotated) and the lit LEDs (one small image each, shown,
8
+ hidden or dimmed). A change of value touches only the LEDs whose brightness
9
+ changed and the pointer, so LVGL redraws a few small rectangles.
10
+
11
+ The ring shows the value in one of three ways, chosen with `mode=`:
12
+
13
+ - `"fill"`, the default, lights the ring from its start up to the value;
14
+ - `"bipolar"` lights it from the middle out to the value, for a control that
15
+ rests in the centre such as pan or detune. The two middle LEDs stay lit;
16
+ - `"dot"` lights only the LED at the value, for a choice among steps.
17
+
18
+ Feel:
19
+
20
+ - drag up or right to raise, down or left to lower; about 200 pixels of
21
+ travel (scaled to the panel) covers the range, four times that in fine mode;
22
+ - the wheel and the arrow keys step it while it has focus, faster when the
23
+ notches come quickly, one at a time in fine mode;
24
+ - Home and End go to the ends, Backspace or a double-click returns it to the
25
+ value the current patch gave it.
26
+ """
27
+
28
+ import math
29
+
30
+ import lvgl as lv
31
+
32
+ from . import sprites
33
+ from ._safe import safe
34
+
35
+ LEDS = 16
36
+ MAX = 127
37
+ MODES = ("fill", "bipolar", "dot")
38
+ _START = 135
39
+ _SWEEP = 270
40
+ _FAST_MS = 60
41
+
42
+
43
+ #: The smallest square, in pixels, that still holds a ring and a knob.
44
+ MIN_ROOM = 10
45
+
46
+
47
+ def geometry(width, height, scale):
48
+ """The sizes of one encoder that fits a *width* x *height* cell.
49
+
50
+ A cell too short for the legend and a knob of `MIN_ROOM` pixels drops the
51
+ legend (``"legend"`` is then 0). One too small even for that raises
52
+ ValueError.
53
+ """
54
+ legend = max(16, int(20 * scale))
55
+ room = min(width - 4, height - legend - 2)
56
+ if room < MIN_ROOM:
57
+ legend = 0
58
+ room = min(width - 4, height - 2)
59
+ if room < MIN_ROOM:
60
+ raise ValueError(
61
+ "an encoder needs a cell of at least %d x %d pixels, not %d x %d"
62
+ % (MIN_ROOM + 4, MIN_ROOM + 2, width, height)
63
+ )
64
+ led_ring = room / 2 - max(3, room * 0.07)
65
+ lens = max(1.6, led_ring * 0.085)
66
+ # In a small cell the margins leave the knob nothing: it shrinks less
67
+ # than the ring, and overlaps the LEDs.
68
+ knob_r = max(led_ring * 0.5, led_ring - lens * 1.5 - max(2, led_ring * 0.07))
69
+ size = int(2 * (led_ring + lens + 2)) | 1
70
+ pointer_w = max(2, int(knob_r * 0.09 + 0.5))
71
+ return {
72
+ "legend": legend,
73
+ "size": size,
74
+ "led_ring": led_ring,
75
+ "lens_r": lens,
76
+ "knob_r": knob_r,
77
+ "pointer_w": pointer_w,
78
+ "pointer_len": max(5, int(knob_r * 0.42)),
79
+ "pointer_gap": max(1, int(knob_r * 0.28)),
80
+ }
81
+
82
+
83
+ class Encoder:
84
+ """One macro's encoder, at (*x*, *y*) in *parent*, *width* x *height*.
85
+
86
+ *mode* is how the ring shows the value: one of `MODES`.
87
+ """
88
+
89
+ def __init__(self, panel, parent, index, label, x, y, width, height, geom, theme, enabled=True, mode="fill"):
90
+ if mode not in MODES:
91
+ raise ValueError("mode must be one of %s, not %r" % (", ".join(MODES), mode))
92
+ self.panel = panel
93
+ self.index = index
94
+ self.value = 0
95
+ self.enabled = enabled
96
+ self.mode = mode
97
+ self._theme = theme
98
+ self._levels = [0] * LEDS
99
+ self._drag = None
100
+ self._last_key_ms = 0
101
+ self._fast = 1
102
+
103
+ cell = lv.obj(parent)
104
+ cell.remove_style_all()
105
+ cell.remove_flag(lv.obj.FLAG.SCROLLABLE)
106
+ cell.set_pos(x, y)
107
+ cell.set_size(width, height)
108
+ cell.set_ext_click_area(2)
109
+ self.obj = cell
110
+
111
+ # The legend, screen-printed above the knob.
112
+ legend = lv.label(cell)
113
+ legend.set_text(label.upper() if enabled else "")
114
+ legend.set_long_mode(lv.label.LONG_MODE.DOTS)
115
+ legend.set_width(width)
116
+ legend.set_height(16)
117
+ legend.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
118
+ legend.set_style_text_font(lv.font_montserrat_14, 0)
119
+ legend.set_style_text_letter_space(1 if width >= 100 else 0, 0)
120
+ legend.set_style_text_color(lv.color_hex(theme["legend"] if enabled else theme["legend_dim"]), 0)
121
+ legend.set_pos(0, max(0, (geom["legend"] - 16) // 2))
122
+ if not geom["legend"]:
123
+ legend.add_flag(lv.obj.FLAG.HIDDEN)
124
+ self.legend = legend
125
+
126
+ # The knob and its unlit ring, centred in the room under the legend.
127
+ size = geom["size"]
128
+ cx = width // 2
129
+ cy = geom["legend"] + (height - geom["legend"]) // 2
130
+ self.cx = cx
131
+ self.cy = cy
132
+ art = lv.image(cell)
133
+ art.set_src(sprites.knob(geom, theme))
134
+ art.set_pos(cx - size // 2, cy - size // 2)
135
+ art.remove_flag(lv.obj.FLAG.CLICKABLE)
136
+
137
+ # The lit LEDs, one image each, placed on the unlit ones.
138
+ led_buf, led_size = sprites.led(geom["lens_r"], theme)
139
+ self._leds = []
140
+ for angle in sprites.led_angles(LEDS, _START, _SWEEP):
141
+ a = math.radians(angle)
142
+ lx = cx + geom["led_ring"] * math.cos(a)
143
+ ly = cy + geom["led_ring"] * math.sin(a)
144
+ led = lv.image(cell)
145
+ led.set_src(led_buf)
146
+ led.set_pos(int(lx - led_size / 2 + 0.5), int(ly - led_size / 2 + 0.5))
147
+ led.remove_flag(lv.obj.FLAG.CLICKABLE)
148
+ led.add_flag(lv.obj.FLAG.HIDDEN)
149
+ self._leds.append(led)
150
+
151
+ # The pointer, pivoting about the knob's centre.
152
+ plen = geom["pointer_len"]
153
+ gap = geom["pointer_gap"]
154
+ pw = geom["pointer_w"] + 2
155
+ pointer = lv.image(cell)
156
+ pointer.set_src(sprites.pointer(geom["pointer_w"], plen, theme))
157
+ pointer.set_pos(cx - pw // 2, cy - gap - plen)
158
+ pointer.set_pivot(pw // 2, plen + gap)
159
+ pointer.remove_flag(lv.obj.FLAG.CLICKABLE)
160
+ self.pointer = pointer
161
+ if not enabled:
162
+ pointer.set_style_image_opa(110, 0)
163
+
164
+ if enabled:
165
+ cell.add_event_cb(safe(self._on_pressed), lv.EVENT.PRESSED, None)
166
+ cell.add_event_cb(safe(self._on_pressing), lv.EVENT.PRESSING, None)
167
+ cell.add_event_cb(safe(self._on_double), lv.EVENT.DOUBLE_CLICKED, None)
168
+ cell.add_event_cb(safe(self._on_key), lv.EVENT.KEY, None)
169
+ cell.add_event_cb(safe(self._on_focus), lv.EVENT.FOCUSED, None)
170
+ cell.add_event_cb(safe(self._on_defocus), lv.EVENT.DEFOCUSED, None)
171
+ else:
172
+ cell.remove_flag(lv.obj.FLAG.CLICKABLE)
173
+ self._show(0, force=True)
174
+
175
+ # ---- what the panel calls ---------------------------------------------
176
+
177
+ def set_value(self, value):
178
+ """Show *value* (0 to 127). Never reports back to the panel.
179
+
180
+ An encoder past the adapter's macros stays at 0, its ring dark, so it
181
+ doesn't look like a live control.
182
+ """
183
+ if not self.enabled:
184
+ return
185
+ value = 0 if value < 0 else (MAX if value > MAX else int(value))
186
+ self._show(value)
187
+
188
+ def _show(self, value, force=False):
189
+ if value == self.value and not force:
190
+ return
191
+ self.value = value
192
+ for i in range(LEDS):
193
+ level = self._level(i, value) if self.enabled else 0
194
+ level = 0 if level <= 0 else (255 if level >= 1 else int(level * 255))
195
+ if level < 24:
196
+ level = 0
197
+ if level == self._levels[i] and not force:
198
+ continue
199
+ led = self._leds[i]
200
+ if level == 0:
201
+ led.add_flag(lv.obj.FLAG.HIDDEN)
202
+ else:
203
+ if self._levels[i] == 0 or force:
204
+ led.remove_flag(lv.obj.FLAG.HIDDEN)
205
+ led.set_style_image_opa(level, 0)
206
+ self._levels[i] = level
207
+ angle = _START + _SWEEP * value / MAX - 270
208
+ self.pointer.set_rotation(int(angle * 10) % 3600)
209
+
210
+ def _level(self, i, value):
211
+ """How bright LED *i* is at *value* in this mode: 0 or less is dark, 1 or more is full."""
212
+ if self.mode == "dot":
213
+ # The dot crosses from one LED to the next, so it moves through all 128.
214
+ away = value * (LEDS - 1) / MAX - i
215
+ return 1 - (away if away >= 0 else -away)
216
+ # Sixteen LEDs over 0..127: each fills over 1/16 of the range, so the
217
+ # leading one fades in and the ring moves smoothly through all 128.
218
+ fill = value * LEDS / MAX
219
+ if self.mode == "bipolar":
220
+ # Sixteen LEDs have no middle one, so the two either side of the
221
+ # top mark the centre and the ring grows away from them.
222
+ half = LEDS // 2
223
+ if i >= half:
224
+ return 1 if i == half else fill - i
225
+ return 1 if i == half - 1 else i + 1 - fill
226
+ return fill - i
227
+
228
+ def lit(self):
229
+ """How bright each LED is, 0 to 255."""
230
+ return list(self._levels)
231
+
232
+ # ---- input --------------------------------------------------------------
233
+
234
+ def _change(self, value):
235
+ value = 0 if value < 0 else (MAX if value > MAX else int(value))
236
+ if value == self.value:
237
+ return
238
+ self._show(value)
239
+ self.panel._turned(self, value)
240
+
241
+ def _point(self):
242
+ indev = lv.indev_active()
243
+ if indev is None:
244
+ return None
245
+ p = lv.point_t()
246
+ indev.get_point(p)
247
+ return p.x, p.y
248
+
249
+ def _on_pressed(self, _event):
250
+ point = self._point()
251
+ if point is not None:
252
+ self._drag = (point[0], point[1], self.value)
253
+
254
+ def _on_pressing(self, _event):
255
+ if self._drag is None:
256
+ return
257
+ point = self._point()
258
+ if point is None:
259
+ return
260
+ x0, y0, start = self._drag
261
+ travel = self.panel.travel * (4 if self.panel.fine else 1)
262
+ moved = (y0 - point[1]) + (point[0] - x0)
263
+ self._change(start + int(moved * MAX / travel))
264
+
265
+ def _on_double(self, _event):
266
+ self._change(self.panel.default_value(self.index))
267
+
268
+ def _on_key(self, event):
269
+ key = event.get_key()
270
+ now = lv.tick_get()
271
+ if self.panel.fine:
272
+ step = 1
273
+ else:
274
+ # Notches that come quickly turn it faster, up to four a notch.
275
+ if now - self._last_key_ms < _FAST_MS:
276
+ self._fast = min(4, self._fast + 1)
277
+ else:
278
+ self._fast = 1
279
+ step = self._fast
280
+ self._last_key_ms = now
281
+ if key in (lv.KEY.RIGHT, lv.KEY.UP):
282
+ self._change(self.value + step)
283
+ elif key in (lv.KEY.LEFT, lv.KEY.DOWN):
284
+ self._change(self.value - step)
285
+ elif key == lv.KEY.HOME:
286
+ self._change(0)
287
+ elif key == lv.KEY.END:
288
+ self._change(MAX)
289
+ elif key in (lv.KEY.BACKSPACE, lv.KEY.DEL):
290
+ self._change(self.panel.default_value(self.index))
291
+
292
+ def _on_focus(self, _event):
293
+ self.legend.set_style_text_color(lv.color_hex(self._theme["legend_lit"]), 0)
294
+ self.panel._focused(self)
295
+
296
+ def _on_defocus(self, _event):
297
+ self.legend.set_style_text_color(lv.color_hex(self._theme["legend"]), 0)