Use logger crate

This commit is contained in:
hodasemi 2024-03-15 07:31:23 +01:00
parent 32853138fe
commit 2a2fd6b140
2 changed files with 20 additions and 30 deletions

View file

@ -26,7 +26,8 @@ fixed = "1.23.1"
[dev-dependencies]
embassy-executor = { version = "0.5.0", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "integrated-timers"] }
embassy-futures = { version = "0.1.1" }
embassy_serial = { git = "https://gavania.de/hodasemi/embassy_serial" }
embassy-usb-logger = { version = "0.1.0" }
log = "0.4"
[[example]]
name = "rp-pico-dht22"

View file

@ -4,17 +4,16 @@
use core::panic::PanicInfo;
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_rp::{
bind_interrupts,
gpio::{Level, Output},
peripherals::{PIN_15, PIO0},
pio::{InterruptHandler, Pio},
peripherals::{PIN_15, PIO0, USB},
pio::{InterruptHandler as PioInterruptHandler, Pio},
usb::{Driver, InterruptHandler as USBInterrupthandler},
};
use embassy_time::Timer;
use dht_pio::{Dht, Dht22, DhtError};
use embassy_serial;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
@ -28,20 +27,25 @@ fn panic(_info: &PanicInfo) -> ! {
static mut PANIC_LED: Option<Output<'static, PIN_15>> = None;
bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => InterruptHandler<PIO0>;
PIO0_IRQ_0 => PioInterruptHandler<PIO0>;
USBCTRL_IRQ => USBInterrupthandler<USB>;
});
#[embassy_executor::task]
async fn logger_task(driver: Driver<'static, USB>) {
embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver);
}
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());
unsafe {
PANIC_LED = Some(Output::new(p.PIN_15, Level::Low));
}
// debug: serial port
let (mut serial, usb_future) =
embassy_serial::Serial::new(p.USB, embassy_serial::SerialConfig::new());
let driver = Driver::new(p.USB, Irqs);
spawner.spawn(logger_task(driver)).unwrap();
// pio access
let pio = p.PIO0;
@ -60,29 +64,14 @@ async fn main(_spawner: Spawner) {
loop {
match dht.read().await {
Ok(reading) => {
serial.send_msg("temp:\n").await.unwrap();
serial
.send_number(reading.temperature as u32, 10)
.await
.unwrap();
serial.send_msg("\n").await.unwrap();
serial.send_msg("humid:\n").await.unwrap();
serial
.send_number(reading.humidity as u32, 10)
.await
.unwrap();
serial.send_msg("\n").await.unwrap();
log::info!("temp: {}, humid: {}", reading.temperature, reading.humidity);
}
Err(err) => match err {
DhtError::Timeout => serial.send_msg("dht timeout error\n").await.unwrap(),
DhtError::Timeout => log::info!("dht timeout error"),
DhtError::CrcMismatch(data, crc) => {
serial.send_msg("dht checksum error\n").await.unwrap();
serial.send_number(data, 16).await.unwrap();
serial.send_msg("\n").await.unwrap();
serial.send_number(crc, 16).await.unwrap();
serial.send_msg("\n").await.unwrap();
log::info!("dht checksum error:\ndata: {data}\ncrc: {crc}");
}
DhtError::ReadError => serial.send_msg("dht read error\n").await.unwrap(),
DhtError::ReadError => log::info!("dht read error"),
},
}
@ -90,5 +79,5 @@ async fn main(_spawner: Spawner) {
}
};
join(usb_future, dht_reading).await;
dht_reading.await;
}