Skip to content

Commit d19acc6

Browse files
committed
libs/m5ui: Fix some M5UI bugs.
Signed-off-by: lbuque <[email protected]>
1 parent 01c8b3e commit d19acc6

File tree

6 files changed

+128
-55
lines changed

6 files changed

+128
-55
lines changed

docs/en/m5ui/slider.rst

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,6 @@ M5Slider
223223
bar.set_bg_grad_color(0x00FF00, 255, 0xFF0000, 255, lv.GRAD_DIR.HOR, lv.PART.MAIN | lv.STATE.DEFAULT)
224224
bar.set_bg_grad_color(0x00FF00, 255, 0xFF0000, 255, lv.GRAD_DIR.HOR, lv.PART.INDICATOR | lv.STATE.DEFAULT)
225225
226-
.. py:method:: set_value(value, anim)
227-
228-
Set the value of the slider.
229-
230-
:param int value: The value to set.
231-
:param bool anim: Whether to animate the change.
232-
:return: None
233-
234-
UiFlow2 Code Block:
235-
236-
|set_value.png|
237-
238-
MicroPython Code Block:
239-
240-
.. code-block:: python
241-
242-
slider_0.set_value(50, True)
243-
244226
.. py:method:: get_value()
245227
246228
Get the current value of the slider.
@@ -258,24 +240,6 @@ M5Slider
258240
259241
value = slider_0.get_value()
260242
261-
.. py:method:: set_range(min_value, max_value)
262-
263-
Set the range of the slider.
264-
265-
:param int min_value: The minimum value of the range.
266-
:param int max_value: The maximum value of the range.
267-
:return: None
268-
269-
UiFlow2 Code Block:
270-
271-
|set_range.png|
272-
273-
MicroPython Code Block:
274-
275-
.. code-block:: python
276-
277-
slider_0.set_range(0, 200)
278-
279243
.. py:method:: set_mode(mode)
280244
281245
Set the mode of the slider.

docs/en/m5ui/textarea.rst

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -409,23 +409,6 @@ M5TextArea
409409
410410
textarea_0.set_password_mode(True)
411411
412-
.. py:method:: set_one_line(text)
413-
414-
Set whether the textarea should be single line or multi-line.
415-
416-
:param bool text: True for single line, False for multi-line.
417-
:return: None
418-
419-
UiFlow2 Code Block:
420-
421-
|set_one_line.png|
422-
423-
MicroPython Code Block:
424-
425-
.. code-block:: python
426-
427-
textarea_0.set_one_line(True)
428-
429412
.. py:method:: set_accepted_chars(chars)
430413
431414
Set the characters that are accepted in the textarea. Only these characters can be entered.

m5stack/libs/m5ui/port.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ def deinit():
8484
import M5
8585

8686
M5.Lcd.lvgl_deinit()
87+
lv.mp_lv_deinit_gc()
8788
else:
8889
pass

m5stack/libs/m5ui/slider.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .base import M5Base
66
import lvgl as lv
7+
import warnings
78

89

910
class M5Slider(lv.slider):
@@ -21,6 +22,19 @@ class M5Slider(lv.slider):
2122
:param bg_c: The background color of the slider.
2223
:param color: The color of the slider indicator.
2324
:param parent: The parent object of the slider. If not specified, it will be set to the active screen.
25+
26+
UiFlow2 Code Block:
27+
28+
None
29+
30+
MicroPython Code Block:
31+
32+
.. code-block:: python
33+
34+
from m5ui import M5Slider
35+
import lvgl as lv
36+
37+
slider_0 = M5Slider(x=50, y=50, w=200, h=20, min_value=0, max_value=100, value=25)
2438
"""
2539

2640
def __init__(
@@ -44,11 +58,60 @@ def __init__(
4458
self.set_size(w, h)
4559
self.set_pos(x, y)
4660
self.set_mode(mode)
47-
self.set_range(min_value, max_value)
48-
self.set_value(value, False)
61+
super().set_range(min_value, max_value)
62+
super().set_value(value, False)
4963
self.set_bg_color(bg_c, 51, lv.PART.MAIN | lv.STATE.DEFAULT)
5064
self.set_bg_color(color, lv.OPA.COVER, lv.PART.INDICATOR | lv.STATE.DEFAULT)
5165

66+
def set_value(self, value: int, anim: bool = False) -> None:
67+
"""Set the value of the slider.
68+
69+
:param int value: The value to set.
70+
:param bool anim: Whether to animate the change.
71+
:return: None
72+
73+
UiFlow2 Code Block:
74+
75+
|set_value.png|
76+
77+
MicroPython Code Block:
78+
79+
.. code-block:: python
80+
81+
slider_0.set_value(50, True)
82+
"""
83+
if not isinstance(value, int):
84+
raise ValueError("Value must be an integer.")
85+
if value < self.get_min_value():
86+
warnings.warn(f"Value is less than min_value, setting to {self.get_min_value()}.")
87+
value = self.get_min_value()
88+
if value > self.get_max_value():
89+
warnings.warn(f"Value is greater than max_value, setting to {self.get_max_value()}.")
90+
value = self.get_max_value()
91+
super().set_value(value, anim)
92+
93+
def set_range(self, min_value: int, max_value: int) -> None:
94+
"""Set the range of the slider.
95+
96+
:param int min_value: The minimum value of the range.
97+
:param int max_value: The maximum value of the range.
98+
:return: None
99+
100+
UiFlow2 Code Block:
101+
102+
|set_range.png|
103+
104+
MicroPython Code Block:
105+
106+
.. code-block:: python
107+
108+
slider_0.set_range(0, 200)
109+
"""
110+
if not isinstance(min_value, int) or not isinstance(max_value, int):
111+
raise ValueError("min_value and max_value must be integers.")
112+
super().set_range(min_value, max_value)
113+
self.set_value(self.get_value(), False)
114+
52115
def set_style_radius(self, radius: int, part: int) -> None:
53116
if radius < 0:
54117
raise ValueError("Radius must be a non-negative integer.")

m5stack/libs/m5ui/textarea.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ def __init__(
4949
self.set_border_color(border_c, lv.OPA.COVER, lv.PART.MAIN | lv.STATE.DEFAULT)
5050
self.set_text_color(text_c, lv.OPA.COVER, lv.PART.MAIN | lv.STATE.DEFAULT)
5151

52+
self._h = h
53+
54+
def set_one_line(self, en: bool) -> None:
55+
"""Set whether the textarea should be single line or multi-line.
56+
57+
:param bool text: True for single line, False for multi-line.
58+
:return: None
59+
60+
UiFlow2 Code Block:
61+
62+
|set_one_line.png|
63+
64+
MicroPython Code Block:
65+
66+
.. code-block:: python
67+
68+
textarea_0.set_one_line(True)
69+
"""
70+
if en:
71+
self._h = self.get_height()
72+
super().set_one_line(en)
73+
if not en:
74+
self.set_height(self._h)
75+
5276
def set_style_radius(self, radius: int, part: int) -> None:
5377
if radius < 0:
5478
raise ValueError("Radius must be a non-negative integer.")

tests/m5ui/test_slider.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import lvgl as lv
6+
import sys
7+
import time
8+
9+
sys.path.append("../../m5stack/libs")
10+
import m5ui
11+
import unittest
12+
13+
14+
class Test(unittest.TestCase):
15+
def __init__(self) -> None:
16+
super().__init__()
17+
m5ui.init()
18+
page0 = m5ui.M5Page()
19+
self.slider0 = m5ui.M5Slider(value=25, parent=page0)
20+
self.slider0.set_pos(50, 50)
21+
page0.screen_load()
22+
23+
def test_set_range(self):
24+
self.slider0.set_range(40, 50)
25+
self.assertEqual(self.slider0.get_value(), 40)
26+
27+
def test_set_value(self):
28+
self.slider0.set_value(60)
29+
self.assertEqual(self.slider0.get_value(), 50)
30+
self.slider0.set_value(30)
31+
self.assertEqual(self.slider0.get_value(), 40)
32+
33+
def tearDown(self):
34+
time.sleep(3)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)