Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions docs/doc/en/lvgl/lvgl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: LVGL GUI
update:
- date: 2026-11-09
author: Tao
version: 1.0.0
content: Initial document
---

## Introduction

MaixPy provides the [LVGL](https://lvgl.io/) 9.1.0 Python bindings through the `maix_lvgl` module. The API is close to the MicroPython `lvgl` binding:

```python
import maix_lvgl as lv
```

`maix_lvgl` is built by MaixCDK's LVGL component and is already installed with the MaixPy wheel, so **no separate installation is needed**.

API documentation:

- [lvgl module](/api/maix_lvgl/lvgl.html)
- [lvgl functions](/api/maix_lvgl/lvgl_functions.html)
- [lvgl classes](/api/maix_lvgl/lvgl_classes.html)
- [maix_lvgl backend](/api/maix_lvgl/maix_lvgl.html)

## Quick start

```python
import time
from maix import app, display, touchscreen
import maix_lvgl as lv

disp = display.Display()
ts = touchscreen.TouchScreen()

try:
lv.init(disp, ts) # pass MaixPy display and touchscreen

scr = lv.screen_active()
btn = lv.button(scr)
btn.set_size(240, 120)
btn.center()

label = lv.label(btn)
label.set_text("Hello MaixCAM2")

def on_click(event):
label.set_text("Clicked!")

btn.add_event_cb(on_click, lv.EVENT.CLICKED, None)

while not app.need_exit():
lv.timer_handler() # advances the LVGL tick
time.sleep(0.005)
finally:
lv.deinit()
ts.close()
disp.close()
```

A button is shown in the center of the screen. Tapping it changes the label to `Clicked!`.

## Initialization and main loop

`lv.init(display, touchscreen)` accepts the MaixPy `maix.display.Display` and `maix.touchscreen.TouchScreen` objects:

- `lv.init()` opens the display and touchscreen automatically.
- `lv.init(disp, False)` uses the display without touch input.

Call `lv.timer_handler()` in the main loop. This is the `maix_lvgl` version: it advances `lv_tick_inc()` using the monotonic clock before running LVGL's timer handler. **Do not use the raw `lvgl.timer_handler()`**, otherwise the tick never advances and clicks/animations stop working.

Call `lv.deinit()` when finished. If you created `display`/`touchscreen` yourself, close them separately.

## Widgets

```python
scr = lv.screen_active()

# Label
label = lv.label(scr)
label.set_text("Hello")
label.center()

# Button with a label
btn = lv.button(scr)
btn.set_size(240, 120)
btn.center()
btn_label = lv.label(btn)
btn_label.set_text("Click me")
btn_label.center()

# Slider
slider = lv.slider(scr)
slider.set_size(200, 20)
slider.center()

# Switch
sw = lv.switch(scr)
sw.center()
```

Event callbacks use the same style as LVGL:

```python
def on_click(event):
print("clicked")

btn.add_event_cb(on_click, lv.EVENT.CLICKED, None)
```

## Display and touch

- The display driver renders LVGL into a BGRA8888 buffer and shows it with `display.show()`.
- Touch coordinates are processed by the MaixPy touch driver and already use the 640×480 screen coordinate system.
- Only one `maix.display.Display` user is allowed at a time. Stop the launcher or other apps before running.
- Tap targets must be large enough; very small buttons are easy to miss.

## Notes

- **JPEG images**: jpegio is not compiled in (it depends on LVGL 9.5 internals, which are not available in 9.1.0), so `lv.image` JPEG decoding is unavailable.
- **Chinese text**: only Montserrat fonts are enabled (matching MaixCDK's LVGL configuration). Chinese glyphs require a CJK bitmap font or FreeType.
- **Version**: LVGL 9.1.0, generated with [omnipython-lvgl](https://github.com/PyDevices/omnipython-lvgl).
- When building MaixPy yourself, `setup.py` builds `maix_lvgl` and packages it into the MaixPy wheel.
5 changes: 5 additions & 0 deletions docs/doc/en/sidebar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ items:
- file: gui/i18n.md
label: I18N (Multi language)

- label: LVGL GUI
items:
- file: lvgl/lvgl.md
label: Using LVGL

- label: AI Vision
items:
- file: vision/ai.md
Expand Down
124 changes: 124 additions & 0 deletions docs/doc/zh/lvgl/lvgl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: LVGL 图形界面
update:
- date: 2026-11-09
author: Tao
version: 1.0.0
content: 初版文档
---

## 简介

MaixPy 通过 `maix_lvgl` 模块提供 [LVGL](https://lvgl.io/) 9.1.0 的 Python 绑定,写法与 MicroPython 的 `lvgl` 基本一致:

```python
import maix_lvgl as lv
```

`maix_lvgl` 由 MaixCDK 的 LVGL 组件构建,并已随 MaixPy 的 wheel 一起安装,**无需单独安装**。

API 文档:

- [lvgl 模块](/api/maix_lvgl/lvgl.html)
- [lvgl 函数](/api/maix_lvgl/lvgl_functions.html)
- [lvgl 类](/api/maix_lvgl/lvgl_classes.html)
- [maix_lvgl 后端](/api/maix_lvgl/maix_lvgl.html)

## 快速开始

```python
import time
from maix import app, display, touchscreen
import maix_lvgl as lv

disp = display.Display()
ts = touchscreen.TouchScreen()

try:
lv.init(disp, ts) # 传入 MaixPy 的屏幕和触摸屏

scr = lv.screen_active()
btn = lv.button(scr)
btn.set_size(240, 120)
btn.center()

label = lv.label(btn)
label.set_text("Hello MaixCAM2")

def on_click(event):
label.set_text("Clicked!")

btn.add_event_cb(on_click, lv.EVENT.CLICKED, None)

while not app.need_exit():
lv.timer_handler() # 自动推进 LVGL tick
time.sleep(0.005)
finally:
lv.deinit()
ts.close()
disp.close()
```

运行后屏幕中央会显示一个按钮,点击后文字变为 `Clicked!`。

## 初始化与主循环

`lv.init(display, touchscreen)` 接受 MaixPy 的 `maix.display.Display` 和 `maix.touchscreen.TouchScreen` 对象:

- 不传参数时自动打开屏幕和触摸屏:`lv.init()`
- 只需要屏幕、不需要触摸:`lv.init(disp, False)`

主循环中调用 `lv.timer_handler()`。该函数由 `maix_lvgl` 提供,会先按单调时间推进 `lv_tick_inc()`,再执行 LVGL 的 timer handler。**不要直接使用原生 `lvgl.timer_handler()`**,否则 tick 不会前进,点击和动画都会失效。

退出时调用 `lv.deinit()`;如果 `display`/`touchscreen` 是你自己创建的,还需要分别调用它们的 `close()`。

## 常用控件

```python
scr = lv.screen_active()

# 标签
label = lv.label(scr)
label.set_text("Hello")
label.center()

# 按钮 + 按钮上的标签
btn = lv.button(scr)
btn.set_size(240, 120)
btn.center()
btn_label = lv.label(btn)
btn_label.set_text("Click me")
btn_label.center()

# 滑块
slider = lv.slider(scr)
slider.set_size(200, 20)
slider.center()

# 开关
sw = lv.switch(scr)
sw.center()
```

事件回调和 LVGL 原生写法一致:

```python
def on_click(event):
print("clicked")

btn.add_event_cb(on_click, lv.EVENT.CLICKED, None)
```

## 屏幕与触摸

- 显示驱动会把 LVGL 渲染到 BGRA8888 缓冲区,再通过 `display.show()` 显示。
- 触摸坐标由 MaixPy 的触摸驱动处理,已经是 640×480 的屏幕坐标。
- 同一时间只能有一个 `maix.display.Display` 使用者。运行前请停止 launcher 或其他占用屏幕的应用。
- 点击目标要足够大,过小的按钮容易点偏。

## 注意事项

- **JPEG 图片**:当前绑定未编入 jpegio(其依赖 LVGL 9.5 的内部接口,与 9.1.0 不兼容),`lv.image` 的 JPEG 解码不可用。
- **中文显示**:默认只启用了 Montserrat 英文字体(与 MaixCDK 的 LVGL 配置一致)。显示中文需要 CJK 点阵字体或 FreeType。
- **版本**:LVGL 9.1.0,绑定生成器参考 [omnipython-lvgl](https://github.com/PyDevices/omnipython-lvgl)。
- 如果自行构建 MaixPy,`setup.py` 会自动构建 `maix_lvgl` 并打包进 MaixPy wheel。
5 changes: 5 additions & 0 deletions docs/doc/zh/sidebar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ items:
- file: gui/i18n.md
label: I18N 国际化(多语言)

- label: LVGL 图形界面
items:
- file: lvgl/lvgl.md
label: LVGL 使用

- label: AI 视觉
items:
- file: vision/ai.md
Expand Down
14 changes: 14 additions & 0 deletions maix_lvgl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
build/
dist/
*.so
*.egg-info/
__pycache__/
*.pyc
.build/
binding/lvgl_python.c
binding/lvgl.pyi
binding/lv_conf.h
binding/.binding_stamp.json

# Generated API documentation (recreated by setup.py)
docs/api/
10 changes: 10 additions & 0 deletions maix_lvgl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# !!! This file is auto generated by MaixCDK, don't modify it manually !!!

cmake_minimum_required(VERSION 3.13)

# cmake used vars defined in MaixCDK/tools/cmake/project.py's get_configs function

include(${SDK_PATH}/tools/cmake/compile.cmake)
project(${PROJECT_ID})

21 changes: 21 additions & 0 deletions maix_lvgl/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Sipeed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading
Loading