树莓派pico rp2040 使用rust 在ssd1306上显示中文信息
在rp2040上用DHT22 + ssd1306显示温度信息,
用 embedded-graphics库和ssd1306库来实现。但实现的效果不是很理想,无法在ssd1306屏幕上显示中文。
为了解决这个问题,在github和crates.io上面找了几天。解决方法还是找到了,利用 u8g2-font这个库实现。。。
实现的办法如下:
Cargo.toml的[dependencies]节点下添加如下内容
embedded-hal = { version = "1.0.0" }
embedded-graphics = "0.8.1"
dht-sensor = "0.2.1"
ssd1306 = "0.8.1"
u8g2-fonts = { version = "0.4.0", features = ["embedded_graphics_textstyle"] }
src/main.rs文件头中添加引用
use embedded_graphics::{
pixelcolor::BinaryColor,
prelude::*,
primitives::{Line, PrimitiveStyle},
text::{Baseline, Text},
};
use u8g2_fonts::U8g2TextStyle;
src/main.rs:: fn main() 里面添加定义
...此处省略了很多代码
// Create a text style for drawing the font:
此处的u8g2_font_wqy12_t_gb2312是输出中文的重点
let character_style =
U8g2TextStyle::new(u8g2_fonts::fonts::u8g2_font_wqy12_t_gb2312, BinaryColor::On);
fn main() -> {
.... 此处省略了很多代码
loop {
// Empty the display:
// Draw 3 lines of text:
//reset before loop
let _ = display.clear(BinaryColor::Off);
write!(&mut line2, "湿度: {}%", humi).unwrap();
Text::with_baseline(
line2.as_str(),
Point::new(32, 38),
character_style.clone(),
Baseline::Top,
)
.draw(&mut display)
.unwrap();
... 此处省略了很多代码
display.flush().unwrap();
// delay for 1 sec
//per loop is 1 sec
timer.delay_ms(1000);
}
}
最终实现效果如图

本项目开源地址: https://github.com/sndnvaps/rp2040-display
u8g2-font库开源地址,主用提供中文字库:https://github.com/Finomnis/u8g2-fonts
目前支持的中文字库 https://github.com/olikraus/u8g2/wiki/fntgrpwqy ,https://github.com/olikraus/u8g2/wiki/fntgrpbb
laser杨万荣

浙公网安备 33010602011771号