implemented measurement and display tasks
This commit is contained in:
@@ -7,11 +7,13 @@ use mmc56x3::MMC56X3;
|
||||
|
||||
use crate::init::sbc::Irqs;
|
||||
|
||||
pub type MagSensor = MMC56X3<I2c<'static, I2C1, Async>, embassy_time::Delay>;
|
||||
|
||||
pub async fn init_sensor(
|
||||
i2c: Peri<'static, embassy_rp::peripherals::I2C1>,
|
||||
sda: Peri<'static, embassy_rp::peripherals::PIN_14>,
|
||||
scl: Peri<'static, embassy_rp::peripherals::PIN_15>,
|
||||
) -> Result<MMC56X3<I2c<'static, I2C1, Async>, embassy_time::Delay>, mmc56x3::Error<i2c::Error>> {
|
||||
) -> Result<MagSensor, mmc56x3::Error<i2c::Error>> {
|
||||
let i2c = I2c::new_async(i2c, scl, sda, Irqs, i2c::Config::default());
|
||||
|
||||
let mut device = MMC56X3::new(i2c, embassy_time::Delay);
|
||||
|
||||
@@ -1,19 +1,29 @@
|
||||
use embassy_rp::{
|
||||
Peri,
|
||||
gpio::{Level, Output},
|
||||
spi::{self, Spi},
|
||||
peripherals::SPI0,
|
||||
spi::{self, Blocking, Spi},
|
||||
};
|
||||
use embassy_time::Timer;
|
||||
use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use embedded_hal_bus::spi::{ExclusiveDevice, NoDelay};
|
||||
use mipidsi::{
|
||||
Builder,
|
||||
Builder, Display, NoResetPin,
|
||||
interface::SpiInterface,
|
||||
models::ST7789,
|
||||
options::{Orientation, Rotation},
|
||||
};
|
||||
use static_cell::StaticCell;
|
||||
|
||||
pub type SPIDisplay = Display<
|
||||
SpiInterface<
|
||||
'static,
|
||||
ExclusiveDevice<Spi<'static, SPI0, Blocking>, Output<'static>, NoDelay>,
|
||||
Output<'static>,
|
||||
>,
|
||||
ST7789,
|
||||
NoResetPin,
|
||||
>;
|
||||
|
||||
pub async fn init_display(
|
||||
inner: Peri<'static, embassy_rp::peripherals::SPI0>,
|
||||
clk: Peri<'static, embassy_rp::peripherals::PIN_6>,
|
||||
@@ -21,7 +31,7 @@ pub async fn init_display(
|
||||
cs: Peri<'static, embassy_rp::peripherals::PIN_5>,
|
||||
dc: Peri<'static, embassy_rp::peripherals::PIN_9>,
|
||||
rst: Peri<'static, embassy_rp::peripherals::PIN_10>,
|
||||
) -> Result<impl DrawTarget<Color = Rgb565>, ()> {
|
||||
) -> Result<SPIDisplay, ()> {
|
||||
let mut spi_cfg = spi::Config::default();
|
||||
spi_cfg.frequency = 62_500_000;
|
||||
|
||||
@@ -43,13 +53,10 @@ pub async fn init_display(
|
||||
|
||||
let di = SpiInterface::new(spi_dev, dc_display, buffer);
|
||||
|
||||
static DELAY: StaticCell<embassy_time::Delay> = StaticCell::new();
|
||||
let delay = DELAY.init(embassy_time::Delay);
|
||||
|
||||
let display = Builder::new(ST7789, di)
|
||||
.display_size(240, 320)
|
||||
.orientation(Orientation::new().rotate(Rotation::Deg90))
|
||||
.init(delay)
|
||||
.init(&mut embassy_time::Delay)
|
||||
.map_err(|_| ())?; // TODO: pass error
|
||||
|
||||
Ok(display)
|
||||
|
||||
56
src/main.rs
56
src/main.rs
@@ -2,28 +2,37 @@
|
||||
#![no_main]
|
||||
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::Timer;
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_graphics::draw_target::DrawTargetExt;
|
||||
use log::error;
|
||||
|
||||
use crate::display::CoMoDisplay;
|
||||
use crate::{
|
||||
display::CoMoDisplay,
|
||||
init::{i2c_sensor::MagSensor, spi_display::SPIDisplay},
|
||||
};
|
||||
|
||||
mod display;
|
||||
mod init;
|
||||
|
||||
static SENSOR_CHANNEL: Channel<CriticalSectionRawMutex, mmc56x3::MagneticMessurement, 4> =
|
||||
Channel::new();
|
||||
|
||||
static DISPLAY_CHANNEL: Channel<CriticalSectionRawMutex, f32, 1> = Channel::new();
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
let p = init::hardware_init();
|
||||
init::logger::init_logger(spawner, p.USB);
|
||||
|
||||
let Ok(mag) = init::i2c_sensor::init_sensor(p.I2C1, p.PIN_14, p.PIN_15).await else {
|
||||
let Ok(mag_sens) = init::i2c_sensor::init_sensor(p.I2C1, p.PIN_14, p.PIN_15).await else {
|
||||
error!("Failed to init sensor");
|
||||
|
||||
init::reboot().await;
|
||||
return;
|
||||
};
|
||||
|
||||
let Ok(mut display) =
|
||||
let Ok(display) =
|
||||
init::spi_display::init_display(p.SPI0, p.PIN_6, p.PIN_7, p.PIN_5, p.PIN_9, p.PIN_10).await
|
||||
else {
|
||||
error!("Failed to init display");
|
||||
@@ -32,10 +41,41 @@ async fn main(spawner: Spawner) {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut como_display = CoMoDisplay::new(display.color_converted());
|
||||
spawner.must_spawn(measure_task(mag_sens));
|
||||
spawner.must_spawn(display_task(display));
|
||||
|
||||
como_display.draw();
|
||||
|
||||
Timer::after_secs(5).await;
|
||||
Timer::after_secs(30).await;
|
||||
init::reboot().await;
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn measure_task(mut sensor: MagSensor) {
|
||||
let sample_window = Duration::from_millis(500);
|
||||
|
||||
loop {
|
||||
match sensor.read_messurement().await {
|
||||
Ok(reading) => {
|
||||
SENSOR_CHANNEL.send(reading).await;
|
||||
}
|
||||
Err(_) => {
|
||||
error!("Failed to read measurement");
|
||||
}
|
||||
}
|
||||
|
||||
Timer::after(sample_window).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn display_task(mut display: SPIDisplay) {
|
||||
let mut como_display = CoMoDisplay::new(display.color_converted());
|
||||
loop {
|
||||
let _reading = DISPLAY_CHANNEL.receive().await;
|
||||
match como_display.draw() {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
error!("Failed to draw");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user