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-time",
"embedded-graphics",
"heapless",
"rtt-target",
"scd4x",
"ssd1351",

View File

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

View File

@ -13,17 +13,15 @@ use ssd1351::{
// Graphics
use embedded_graphics::{
mono_font::{
ascii::{FONT_5X7, FONT_6X10},
MonoTextStyle,
},
mono_font::{ascii::FONT_6X10, MonoTextStyle},
pixelcolor::Rgb565,
prelude::{Dimensions, Point, RgbColor},
prelude::{Point, WebColors},
text::{Alignment, Text},
Drawable,
};
use crate::Spi0BusMutex;
use crate::{Spi0BusMutex, SENSOR_DATA_SIGNAL};
use core::fmt::Write;
/// Output to the SSD1351 display
#[embassy_executor::task]
@ -48,26 +46,55 @@ pub async fn display_output_task(
display.reset(rst, &mut embassy_time::Delay).unwrap();
display.init().unwrap();
let text_style1 = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
let text_style2 = MonoTextStyle::new(&FONT_5X7, Rgb565::WHITE);
let text1 = "Hello, World!";
let text2 = "2khz.xyz";
let co2_text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::CSS_DARK_GREEN);
let temp_text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::CSS_ORANGE);
let humidity_text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::CSS_AQUA);
Text::with_alignment(
text1,
display.bounding_box().center() + Point::new(0, -6),
text_style1,
Alignment::Center,
)
.draw(&mut display)
.unwrap();
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();
Text::with_alignment(
text2,
display.bounding_box().center() + Point::new(0, 6),
text_style2,
Alignment::Center,
)
.draw(&mut display)
.unwrap();
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(
&co2_text_buf,
Point::new(1, 12),
co2_text_style,
Alignment::Left,
)
.draw(&mut display)
.unwrap();
Text::with_alignment(
&temp_text_buf,
Point::new(1, 24),
temp_text_style,
Alignment::Left,
)
.draw(&mut display)
.unwrap();
Text::with_alignment(
&humidity_text_buf,
Point::new(1, 36),
humidity_text_style,
Alignment::Left,
)
.draw(&mut display)
.unwrap();
}
}

View File

@ -16,19 +16,28 @@ use embassy_rp::{
peripherals::{I2C0, SPI0},
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 static_cell::StaticCell;
use display_task::display_output_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 {
I2C0_IRQ => embassy_rp::i2c::InterruptHandler<embassy_rp::peripherals::I2C0>;
});
type I2c0BusMutex = Mutex<NoopRawMutex, RefCell<i2c::I2c<'static, I2C0, i2c::Blocking>>>;
type Spi0BusMutex = Mutex<NoopRawMutex, RefCell<spi::Spi<'static, SPI0, spi::Blocking>>>;
static SENSOR_DATA_SIGNAL: Signal<CriticalSectionRawMutex, scd4x::types::SensorData> =
Signal::new();
/// Entrypoint
#[embassy_executor::main]

View File

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