Skip to content

Commit c6bcb71

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/m5ui: Add LVGL Spinner widget.
Signed-off-by: tinyu <[email protected]>
1 parent bb694a9 commit c6bcb71

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

m5stack/libs/m5ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"M5Scale": "scale",
2525
"M5Slider": "slider",
2626
"M5Spinbox": "spinbox",
27+
"M5Spinner": "spinner",
2728
"M5Switch": "switch",
2829
"M5TextArea": "textarea",
2930
"M5Win": "win",

m5stack/libs/m5ui/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"scale.py",
2828
"slider.py",
2929
"spinbox.py",
30+
"spinner.py",
3031
"switch.py",
3132
"textarea.py",
3233
"win.py",

m5stack/libs/m5ui/spinner.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
from m5ui.base import M5Base
6+
import lvgl as lv
7+
8+
9+
class M5Spinner(lv.spinner):
10+
"""Create a spinner object.
11+
12+
:param int x: The x position of the spinner.
13+
:param int y: The y position of the spinner.
14+
:param int w: The width of the spinner.
15+
:param int h: The height of the spinner.
16+
:param int anim_t: The animation time in milliseconds.
17+
:param int angle: The angle of the spinner in degrees.
18+
:param lv.obj parent: The parent object to attach the spinner to. If not specified, the spinner will be attached to the default screen.
19+
20+
UiFlow2 Code Block:
21+
22+
None
23+
24+
MicroPython Code Block:
25+
26+
.. code-block:: python
27+
28+
from m5ui import M5Spinner
29+
import lvgl as lv
30+
31+
m5ui.init()
32+
spinner_0 = M5Spinner(x=120, y=80, w=60, h=30, anim_t=1000, angle=180, parent=page0)
33+
"""
34+
35+
def __init__(
36+
self,
37+
x=0,
38+
y=0,
39+
w=0,
40+
h=0,
41+
anim_t=10000,
42+
angle=180,
43+
parent=None,
44+
):
45+
if parent is None:
46+
parent = lv.screen_active()
47+
super().__init__(parent)
48+
self.set_pos(x, y)
49+
self.set_size(w, h)
50+
self.set_anim_params(anim_t, angle)
51+
52+
def set_spinner_color(self, color, opa: int, part: int):
53+
"""Set the color of the spinner.
54+
55+
:param int color: The color of the spinner in hexadecimal format.
56+
57+
UiFlow2 Code Block:
58+
59+
|set_spinner_color.png|
60+
61+
MicroPython Code Block:
62+
63+
.. code-block:: python
64+
65+
label_0.set_spinner_color(0x2196F3, lv.PART.MAIN | lv.STATE.DEFAULT)
66+
"""
67+
if isinstance(color, int):
68+
color = lv.color_hex(color)
69+
if part == lv.PART.KNOB | lv.STATE.DEFAULT:
70+
self.set_bg_color(color, opa, lv.PART.KNOB | lv.STATE.DEFAULT)
71+
else:
72+
self.set_style_arc_color(color, part)
73+
self.set_style_arc_opa(opa, part)
74+
75+
def __getattr__(self, name):
76+
if hasattr(M5Base, name):
77+
method = getattr(M5Base, name)
78+
bound_method = lambda *args, **kwargs: method(self, *args, **kwargs)
79+
setattr(self, name, bound_method)
80+
return bound_method
81+
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")

0 commit comments

Comments
 (0)