|
| 1 | +import lvgl as lv |
| 2 | +import sys |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | + |
| 6 | +sys.path.append("..") |
| 7 | +sys.path.append(os.getcwd()) |
| 8 | +import testrunner # noqa |
| 9 | + |
| 10 | +# This is a basic test to test buttons, labels, |
| 11 | +# RGB colors, layout aligment and events. |
| 12 | + |
| 13 | + |
| 14 | +async def demo(scr, display=None): |
| 15 | + def get_button(scr, text, align, color): |
| 16 | + scr.set_style_pad_all(10, 0) |
| 17 | + _btn = lv.slider(scr) |
| 18 | + _btn.set_width(lv.pct(75)) |
| 19 | + _btn.set_height(lv.pct(10)) |
| 20 | + _lab = lv.label(_btn) |
| 21 | + _lab.set_text(text) |
| 22 | + _lab.set_style_text_color(lv.color_white(), 0) |
| 23 | + _lab.center() |
| 24 | + _btn.set_style_align(align, 0) |
| 25 | + _btn.set_style_bg_color(lv.color_make(*color), lv.PART.INDICATOR) |
| 26 | + _btn.set_style_bg_color(lv.color_make(*color), lv.PART.MAIN) |
| 27 | + _btn.set_style_bg_color(lv.color_make(*color), lv.PART.KNOB) |
| 28 | + return _btn, text |
| 29 | + |
| 30 | + buttons = [ |
| 31 | + ("RED", lv.ALIGN.TOP_MID, (255, 0, 0)), |
| 32 | + ("GREEN", lv.ALIGN.BOTTOM_MID, (0, 255, 0)), |
| 33 | + ("BLUE", lv.ALIGN.CENTER, (0, 0, 255)), |
| 34 | + ] |
| 35 | + |
| 36 | + def button_cb(event, name, slider): |
| 37 | + if slider.get_value() == 100: |
| 38 | + print(f"{name} VALUE: {slider.get_value()}") |
| 39 | + |
| 40 | + _all_btns = [get_button(scr, *btn) for btn in buttons] |
| 41 | + |
| 42 | + for btn, name in _all_btns: |
| 43 | + btn.add_event_cb( |
| 44 | + lambda event, button_name=name, slider=btn: button_cb( |
| 45 | + event, button_name, slider |
| 46 | + ), |
| 47 | + lv.EVENT.VALUE_CHANGED, |
| 48 | + None, |
| 49 | + ) |
| 50 | + |
| 51 | + await asyncio.sleep_ms(500) # await so the frame can be rendered |
| 52 | + # simulate touch events |
| 53 | + if display: |
| 54 | + print("INDEV + SLIDER TEST:") |
| 55 | + display.debug_indev(press=False) |
| 56 | + display.debug_display(False) |
| 57 | + for _btn, name in _all_btns: |
| 58 | + pos = _btn.get_x(), _btn.get_y() |
| 59 | + pos2 = _btn.get_x2(), _btn.get_y2() |
| 60 | + x1, y1 = pos |
| 61 | + x2, y2 = pos2 |
| 62 | + y_mid = y2 - ((y2 - y1) // 2) |
| 63 | + await display.swipe(x1 + 5, y_mid, x2 + (y2 - y1), y_mid, ms=500) |
| 64 | + await asyncio.sleep_ms(100) |
| 65 | + |
| 66 | + return _all_btns |
| 67 | + |
| 68 | + |
| 69 | +__file__ = globals().get("__file__", "test") |
| 70 | + |
| 71 | +try: |
| 72 | + from display_mode import MODE as _mode |
| 73 | +except Exception: |
| 74 | + _mode = None |
| 75 | + |
| 76 | +testrunner.run(demo, __file__, mode=_mode) |
| 77 | +testrunner.devicereset() |
0 commit comments