From d59a15c91b88b33bf0c9f0cad6386ca19d35e17b Mon Sep 17 00:00:00 2001 From: Adam Macdonald <72780006+twokilohertz@users.noreply.github.com> Date: Sat, 1 Mar 2025 20:24:57 +0000 Subject: [PATCH] Drawing CO2, temp. & RH to the screen --- Cargo.lock | 1 + Cargo.toml | 3 ++ src/display_task.rs | 79 ++++++++++++++++++++++++++++++--------------- src/main.rs | 15 +++++++-- src/sensor_task.rs | 4 ++- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78aaf65..aae1f1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -869,6 +869,7 @@ dependencies = [ "embassy-sync", "embassy-time", "embedded-graphics", + "heapless", "rtt-target", "scd4x", "ssd1351", diff --git a/Cargo.toml b/Cargo.toml index 3872c9d..695e6ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/display_task.rs b/src/display_task.rs index 358f488..1dcdba0 100644 --- a/src/display_task.rs +++ b/src/display_task.rs @@ -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(); + } } diff --git a/src/main.rs b/src/main.rs index 4a8508b..1f7eb9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>>; +type Spi0BusMutex = Mutex>>; + embassy_rp::bind_interrupts!(struct Irqs { I2C0_IRQ => embassy_rp::i2c::InterruptHandler; }); -type I2c0BusMutex = Mutex>>; -type Spi0BusMutex = Mutex>>; +static SENSOR_DATA_SIGNAL: Signal = + Signal::new(); /// Entrypoint #[embassy_executor::main] diff --git a/src/sensor_task.rs b/src/sensor_task.rs index c20ab59..a12065d 100644 --- a/src/sensor_task.rs +++ b/src/sensor_task.rs @@ -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!(