樹莓派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號