【rust/树莓派】使用rppal&embedded-graphics控制st7789 LCD屏幕

说在前面

  • 树莓派版本:4b
  • LCD模块:ST7789V2 240*280 LCD
  • 树莓派系统:Linux raspberrypi 5.15.76-v8+ #1597 SMP aarch64 GNU/Linux
  • rust版本:rustc 1.73.0

模块详情

  • 某雪的1.69inch LCD模块,包含杜邦线

准备工作

  • 树莓派相关见上篇文章
  • 安装rust
    这里直接在树莓派系统上安装的rust,未使用交叉编译等手段。

开始

  • 相关依赖包

    toml 复制代码
    [dependencies]
    rppal = { version = "0.15.0", features = ["hal"] }
    embedded-graphics = { version = "0.8.1", features = ["defmt"]}
    display-interface-spi = "0.4.1"
    mipidsi = "0.7.1"
  • 代码

    rust 复制代码
    use display_interface_spi::SPIInterfaceNoCS;
    use embedded_graphics::mono_font::ascii::FONT_10X20;
    use embedded_graphics::mono_font::MonoTextStyle;
    use embedded_graphics::pixelcolor::Rgb565;
    use embedded_graphics::prelude::{Point, RgbColor, DrawTarget};
    use embedded_graphics::text::Text;
    use embedded_graphics::Drawable;
    use mipidsi::Builder;
    use rppal::gpio::Gpio;
    use rppal::spi::Spi;
    use std::process::ExitCode;
    
    fn main() -> ExitCode {
        // GPIO
        let gpio = Gpio::new().unwrap();
        // reset
        let rst = gpio.get(27).unwrap().into_output();
        // backlight
        let mut backlight = gpio.get(18).unwrap().into_output();
        // data/commend
        let dc = gpio.get(25).unwrap().into_output();
    	// spi
        let spi = Spi::new(
            rppal::spi::Bus::Spi0,
            rppal::spi::SlaveSelect::Ss0,
            60_000_000,
            rppal::spi::Mode::Mode0,
        )
        .unwrap();
        let di = SPIInterfaceNoCS::new(spi, dc);
    
        let mut delay = rppal::hal::Delay::new();
        let mut display = Builder::st7789(di)
            .with_display_size(240, 280)
            .with_orientation(mipidsi::Orientation::Landscape(true))
            .with_invert_colors(mipidsi::ColorInversion::Inverted)
            .init(&mut delay, Some(rst))
            .unwrap();
    
        // Text
        let char_w = 10;
        let text = "Hello World ^_^;";
        let mut text_x = 120;
        let text_y = 280 / 2;
        let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
    
        // Alternating color
        let colors = [Rgb565::RED, Rgb565::GREEN, Rgb565::BLUE];
    
        // Clear the display initially
        display.clear(colors[0]).unwrap();
    
        // Turn on backlight
        backlight.set_high();
    
        let mut last = std::time::Instant::now();
        let mut counter = 0;
        loop {
            let elapsed = last.elapsed().as_secs_f64();
            if elapsed < 0.00125 {
                continue;
            }
            last = std::time::Instant::now();
            counter += 1;
    
            // Fill the display with alternating colors every 8 frames
            display.clear(colors[(counter / 8) % colors.len()]).unwrap();
    
            // Draw text
            let right = Text::new(text, Point::new(text_x, text_y), text_style)
                .draw(&mut display)
                .unwrap();
            text_x = if right.x <= 0 { 240 } else { text_x - char_w };
        }
    
        // Turn off backlight and clear the display
        backlight.set_low();
        display.clear(Rgb565::BLACK).unwrap();
    
        ExitCode::SUCCESS
    }

运行

rust 复制代码
cargo run

结果

相关推荐
§ꦿCFོ༉37 分钟前
嵌入式八股文—C语言入门学习(四)
c语言·开发语言·学习
投图匠1 小时前
十种不同风格的 CTP接口 框架
开发语言·python
皮皮林5511 小时前
比 Xshell 还好用的 SSH 客户端神器,MobaXterm 太爱了!
后端
我不会起名字3222 小时前
会话缓存失效排查:区分 Redis 失效与节点广播
redis·后端·go·即时通讯·im·故障排查
深入云栈4 小时前
Netty 4.2.x 源码深度解析 (二十三):ByteBuf 核心设计 —— 索引、类型与零拷贝
java·后端·架构
JackJiang4 小时前
IM踩坑:聊天消息ID怎么设计?一个UUID搞不定,我用双ID解决
后端·架构
开心猴爷4 小时前
iOS 上架 App 流程
后端·ios
爱笑的k114 小时前
nano-vllm 源码剖析:千行 Python 手写离线推理引擎的调度、显存与并行时序
开发语言·python·vllm
code_Bo4 小时前
不只命令行:我把跨 Agent 会话同步做成了桌面应用
前端·javascript·后端
用户8356290780515 小时前
使用 Python 控制 Excel 工作表的分页位置
后端·python