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