Compare commits

...

2 Commits

View File

@ -22,34 +22,54 @@ use ssd1306::{I2CDisplayInterface, Ssd1306, size::*, rotation::*};
use ssd1306::mode::DisplayConfig; use ssd1306::mode::DisplayConfig;
use std::thread; use std::thread;
use log::info; use std::sync::{Arc, Mutex};
use log::{info, warn};
fn oled_task(i2c: I2cDriver) -> Result<()> { #[derive(Debug, Clone, Default)]
info!("Staring OLED Task"); pub struct Pms7003Data {
// PM concentrations (CF=1, standard particle) in μg/m³
pub pm1_0_cf1: u16,
pub pm2_5_cf1: u16,
pub pm10_cf1: u16,
let interface = I2CDisplayInterface::new(i2c); // PM concentrations (atmospheric environment) in μg/m³
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0) pub pm1_0_atm: u16,
.into_buffered_graphics_mode(); pub pm2_5_atm: u16,
pub pm10_atm: u16,
display.init().unwrap(); // Particle counts (particles per 0.1L air)
pub particles_0_3um: u16,
pub particles_0_5um: u16,
pub particles_1_0um: u16,
pub particles_2_5um: u16,
pub particles_5_0um: u16,
pub particles_10um: u16,
}
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("../rust.raw"), 64); // u16::from_be_bytes is too verbose
let im = Image::new(&raw, Point::new(32, 0)); fn get_u16(buf: &[u8]) -> u16 { ((buf[0] as u16) << 8) | buf[1] as u16 }
im.draw(&mut display).unwrap(); fn read_pm_data(buf: &[u8; 32]) -> Option<Pms7003Data> {
display.flush().map_err(|e| anyhow!("Could not flush display! {:?}", e))?; let checksum: u16 = buf.iter().take(29).copied().map(u16::from).sum();
let csum_target = get_u16(&buf[30..32]);
FreeRtos::delay_ms(3000); if checksum != csum_target {
loop { warn!("Checksum from PMS7003 READ does not match, skipping read");
display.clear(BinaryColor::Off).map_err(|e| anyhow!("Could not clear display! {:?}", e))?; return None
Text::new(
"PM 2.5: 4",
Point::new(20, 40),
MonoTextStyle::new(&FONT_10X20, BinaryColor::On),
).draw(&mut display).map_err(|e| anyhow!("Could not draw text! {:?}", e))?;
display.flush().map_err(|e| anyhow!("Could not flush display! {:?}", e))?;
FreeRtos::delay_ms(500);
} }
Some(Pms7003Data {
pm1_0_cf1: get_u16(&buf[ 4..6 ]),
pm2_5_cf1: get_u16(&buf[ 6..8 ]),
pm10_cf1: get_u16(&buf[ 8..10]),
pm1_0_atm: get_u16(&buf[10..12]),
pm2_5_atm: get_u16(&buf[12..14]),
pm10_atm: get_u16(&buf[14..16]),
particles_0_3um: get_u16(&buf[16..18]),
particles_0_5um: get_u16(&buf[18..20]),
particles_1_0um: get_u16(&buf[20..22]),
particles_2_5um: get_u16(&buf[22..24]),
particles_5_0um: get_u16(&buf[24..26]),
particles_10um: get_u16(&buf[26..28]),
})
} }
fn send_pms7003_command(uart: &UartDriver, cmd: u8, datah: u8, datal: u8) -> Result<()> { fn send_pms7003_command(uart: &UartDriver, cmd: u8, datah: u8, datal: u8) -> Result<()> {
@ -66,29 +86,58 @@ fn send_pms7003_command(uart: &UartDriver, cmd: u8, datah: u8, datal: u8) -> Res
Ok(()) Ok(())
} }
fn pm_sensor_task(uart: &UartDriver) -> Result<()> { fn pm_sensor_task(pm_data: Arc<Mutex<Pms7003Data>>, uart: &UartDriver) -> Result<()> {
info!("Staring UART PM Sensor Task"); info!("Staring UART PM Sensor Task");
send_pms7003_command(uart, 0xE1, 0x00, 0x00)?; send_pms7003_command(uart, 0xE1, 0x00, 0x00)?;
let mut buf = [0_u8; 32];
loop { loop {
send_pms7003_command(uart, 0xE2, 0x00, 0x00)?; send_pms7003_command(uart, 0xE2, 0x00, 0x00)?;
let mut buf = [0_u8; 32];
let len = uart.read(&mut buf, BLOCK)?; let len = uart.read(&mut buf, BLOCK)?;
if len > 0 { if len > 0 {
info!("Read some bytes! {}", len); if let Some(data) = read_pm_data(&buf) {
for i in 0..len { let mut pm_data = pm_data.lock().unwrap();
info!("0x{:02x}", buf[i]); *pm_data = data;
} }
} else { } else {
info!("No bytes read"); // info!("No bytes read");
} }
uart.clear_rx()?; uart.clear_rx()?;
FreeRtos::delay_ms(5000); FreeRtos::delay_ms(5000);
} }
} }
fn oled_task(pm_data: Arc<Mutex<Pms7003Data>>, i2c: I2cDriver) -> Result<()> {
info!("Staring OLED Task");
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().unwrap();
let raw: ImageRaw<BinaryColor> = ImageRaw::new(include_bytes!("../rust.raw"), 64);
let im = Image::new(&raw, Point::new(32, 0));
im.draw(&mut display).unwrap();
display.flush().map_err(|e| anyhow!("Could not flush display! {:?}", e))?;
FreeRtos::delay_ms(2000);
loop {
let current_pm_data = pm_data.lock().unwrap().clone();
display.clear(BinaryColor::Off).map_err(|e| anyhow!("Could not clear display! {:?}", e))?;
Text::new(
&format!("PM 2.5: {}", current_pm_data.pm2_5_atm),
Point::new(20, 40),
MonoTextStyle::new(&FONT_10X20, BinaryColor::On),
).draw(&mut display).map_err(|e| anyhow!("Could not draw text! {:?}", e))?;
display.flush().map_err(|e| anyhow!("Could not flush display! {:?}", e))?;
FreeRtos::delay_ms(500);
}
}
fn main() -> Result<()> { fn main() -> Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime // It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
@ -120,14 +169,19 @@ fn main() -> Result<()> {
&config, &config,
)?; )?;
let pm_data = Arc::new(Mutex::new(Pms7003Data::default()));
let pm_data1 = Arc::clone(&pm_data);
let pm_data2 = Arc::clone(&pm_data);
let handles = vec! [ let handles = vec! [
thread::spawn(move || { thread::spawn(move || {
if let Err(e) = oled_task(i2c) { if let Err(e) = oled_task(pm_data1, i2c) {
log::error!("OLED Task Error: {:?}", e); log::error!("OLED Task Error: {:?}", e);
} }
}), }),
thread::spawn(move || { thread::spawn(move || {
if let Err(e) = pm_sensor_task(&uart) { if let Err(e) = pm_sensor_task(pm_data2, &uart) {
log::error!("PM Sensor Task Error: {:?}", e); log::error!("PM Sensor Task Error: {:?}", e);
} }
}), }),