Drawing CO2, temp. & RH to the screen

This commit is contained in:
Adam Macdonald 2025-03-01 20:24:57 +00:00
parent 39a59a2658
commit d59a15c91b
5 changed files with 72 additions and 30 deletions

1
Cargo.lock generated
View File

@ -869,6 +869,7 @@ dependencies = [
"embassy-sync", "embassy-sync",
"embassy-time", "embassy-time",
"embedded-graphics", "embedded-graphics",
"heapless",
"rtt-target", "rtt-target",
"scd4x", "scd4x",
"ssd1351", "ssd1351",

View File

@ -38,6 +38,9 @@ scd4x = { git = "https://github.com/twokilohertz/scd4x-rs.git", branch = "conver
ssd1351 = "0.5.0" ssd1351 = "0.5.0"
display-interface-spi = "0.5.0" display-interface-spi = "0.5.0"
# Extra
heapless = "0.8.0"
[profile.dev] [profile.dev]
opt-level = "s" opt-level = "s"

View File

@ -13,17 +13,15 @@ use ssd1351::{
// Graphics // Graphics
use embedded_graphics::{ use embedded_graphics::{
mono_font::{ mono_font::{ascii::FONT_6X10, MonoTextStyle},
ascii::{FONT_5X7, FONT_6X10},
MonoTextStyle,
},
pixelcolor::Rgb565, pixelcolor::Rgb565,
prelude::{Dimensions, Point, RgbColor}, prelude::{Point, WebColors},
text::{Alignment, Text}, text::{Alignment, Text},
Drawable, Drawable,
}; };
use crate::Spi0BusMutex; use crate::{Spi0BusMutex, SENSOR_DATA_SIGNAL};
use core::fmt::Write;
/// Output to the SSD1351 display /// Output to the SSD1351 display
#[embassy_executor::task] #[embassy_executor::task]
@ -48,26 +46,55 @@ pub async fn display_output_task(
display.reset(rst, &mut embassy_time::Delay).unwrap(); display.reset(rst, &mut embassy_time::Delay).unwrap();
display.init().unwrap(); display.init().unwrap();
let text_style1 = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE); let co2_text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::CSS_DARK_GREEN);
let text_style2 = MonoTextStyle::new(&FONT_5X7, Rgb565::WHITE); let temp_text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::CSS_ORANGE);
let text1 = "Hello, World!"; let humidity_text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::CSS_AQUA);
let text2 = "2khz.xyz";
let mut co2_text_buf = heapless::String::<16>::new();
let mut temp_text_buf = heapless::String::<16>::new();
let mut humidity_text_buf = heapless::String::<16>::new();
loop {
// Clear contents of buffers
co2_text_buf.clear();
temp_text_buf.clear();
humidity_text_buf.clear();
// Wait on sensor data & format into the buffers
let data = SENSOR_DATA_SIGNAL.wait().await;
write!(&mut co2_text_buf, "CO2: {} ppm", data.co2).unwrap();
write!(&mut temp_text_buf, "Temp: {:.1} C", data.temperature).unwrap();
write!(&mut humidity_text_buf, "RH: {:.1} %", data.humidity).unwrap();
// Clear the display
display.clear();
// Draw the text to the screen
Text::with_alignment( Text::with_alignment(
text1, &co2_text_buf,
display.bounding_box().center() + Point::new(0, -6), Point::new(1, 12),
text_style1, co2_text_style,
Alignment::Center, Alignment::Left,
) )
.draw(&mut display) .draw(&mut display)
.unwrap(); .unwrap();
Text::with_alignment( Text::with_alignment(
text2, &temp_text_buf,
display.bounding_box().center() + Point::new(0, 6), Point::new(1, 24),
text_style2, temp_text_style,
Alignment::Center, Alignment::Left,
)
.draw(&mut display)
.unwrap();
Text::with_alignment(
&humidity_text_buf,
Point::new(1, 36),
humidity_text_style,
Alignment::Left,
) )
.draw(&mut display) .draw(&mut display)
.unwrap(); .unwrap();
} }
}

View File

@ -16,19 +16,28 @@ use embassy_rp::{
peripherals::{I2C0, SPI0}, peripherals::{I2C0, SPI0},
spi, spi,
}; };
use embassy_sync::blocking_mutex::{raw::NoopRawMutex, Mutex}; use embassy_sync::{
blocking_mutex::{
raw::{CriticalSectionRawMutex, NoopRawMutex},
Mutex,
},
signal::Signal,
};
use embassy_time::Timer; use embassy_time::Timer;
use static_cell::StaticCell; use static_cell::StaticCell;
use display_task::display_output_task; use display_task::display_output_task;
use sensor_task::sensor_read_task; use sensor_task::sensor_read_task;
type I2c0BusMutex = Mutex<NoopRawMutex, RefCell<i2c::I2c<'static, I2C0, i2c::Blocking>>>;
type Spi0BusMutex = Mutex<NoopRawMutex, RefCell<spi::Spi<'static, SPI0, spi::Blocking>>>;
embassy_rp::bind_interrupts!(struct Irqs { embassy_rp::bind_interrupts!(struct Irqs {
I2C0_IRQ => embassy_rp::i2c::InterruptHandler<embassy_rp::peripherals::I2C0>; I2C0_IRQ => embassy_rp::i2c::InterruptHandler<embassy_rp::peripherals::I2C0>;
}); });
type I2c0BusMutex = Mutex<NoopRawMutex, RefCell<i2c::I2c<'static, I2C0, i2c::Blocking>>>; static SENSOR_DATA_SIGNAL: Signal<CriticalSectionRawMutex, scd4x::types::SensorData> =
type Spi0BusMutex = Mutex<NoopRawMutex, RefCell<spi::Spi<'static, SPI0, spi::Blocking>>>; Signal::new();
/// Entrypoint /// Entrypoint
#[embassy_executor::main] #[embassy_executor::main]

View File

@ -6,7 +6,7 @@ use rtt_target::rprintln;
// Sensor // Sensor
use scd4x::Scd4x; use scd4x::Scd4x;
use crate::I2c0BusMutex; use crate::{I2c0BusMutex, SENSOR_DATA_SIGNAL};
/// Read CO2/temp./humidity data from the sensor /// Read CO2/temp./humidity data from the sensor
#[embassy_executor::task] #[embassy_executor::task]
@ -41,6 +41,8 @@ pub async fn sensor_read_task(i2c_bus: &'static I2c0BusMutex) {
data.temperature, data.temperature,
data.humidity data.humidity
); );
SENSOR_DATA_SIGNAL.signal(data);
} }
Err(error) => { Err(error) => {
rprintln!( rprintln!(