• 简体中文
  • rs_lvgl LVGL 9.6

    面向嵌入式 Rust 的
    LVGL 开发库。

    把 LVGL 的控件、布局和动画接入 Rust 嵌入式项目。用 Cargo 构建,在桌面调试,再连接设备的显示与输入。

    组件工作台Rust + LVGL
    LVGL 工作台中的滑块、开关与选择控件
    桌面组件工作台,也可以在浏览器中操作。打开预览

    从构建到设备接入

    Cargo

    原生库,随项目一起构建

    Cargo 编译仓库内的 LVGL 9.6 并生成 Rust 绑定。项目自己的 lv_conf.h、C 源码和字体,也可以加入这次构建。

    配置原生构建
    Rust

    使用熟悉的 Rust 类型

    直接调用控件 API,或添加 rs_lvgl_ui,用函数组合页面、用消息处理输入。应用状态保存在自己的结构体中。

    开始排列控件
    Features

    按设备选择控件与字体

    通过 Cargo features 选择控件、字体和媒体扩展。先用默认配置运行示例,再为设备选择需要的部分。

    选择所需特性
    no_std + alloc

    接上自己的显示与输入

    库支持带分配器的 no_std 应用。把屏幕刷新、触摸或按键输入、时钟接入平台代码,就可以驱动 LVGL。

    接入设备

    先试一个设置开关

    切换通知开关,看看状态文字和计数如何变化。旁边的 Rust 代码就是这个应用;修改标题或事件处理后点击运行,就能在浏览器中试用。

    让开关响应点击
    switch.rs
    use rs_lvgl_ui::prelude::*;
    
    struct Settings {
        enabled: bool,
        changes: u32,
    }
    
    #[derive(Clone)]
    enum NotificationSettingsAction {
        Enabled(bool),
        Reset,
    }
    
    impl App for Settings {
        type Action = NotificationSettingsAction;
    
        fn update(&mut self, action: NotificationSettingsAction) {
            match action {
                NotificationSettingsAction::Enabled(value) => {
                    self.enabled = value;
                    self.changes += 1;
                }
                NotificationSettingsAction::Reset => {
                    self.enabled = false;
                    self.changes = 0;
                }
            }
        }
    
        fn view(&self, theme: &Theme) -> View<NotificationSettingsAction> {
            column([
                label("Notifications")
                    .font(theme.title_font)
                    .text_color(theme.text),
                row([
                    switch(self.enabled)
                        .on_value_changed(|value| NotificationSettingsAction::Enabled(value != 0)),
                    label(if self.enabled { "Enabled" } else { "Disabled" }),
                ])
                .style(Style::surface(theme))
                .gap(24),
                label(format!("Changed {} times", self.changes)),
                button("Reset")
                    .on_click(NotificationSettingsAction::Reset)
                    .style(Style::accent(theme)),
            ])
            .style(Style::screen(theme))
            .font(theme.body_font)
            .text_color(theme.text)
            .padding(32)
            .gap(24)
        }
    }
    
    fn app() -> impl App {
        Settings {
            enabled: false,
            changes: 0,
        }
    }
    

    正在加载交互示例…

    接下来,接入你的项目

    开始编写页面,接入已有的板级工程,或找一个完整示例运行。