Skip to content
Snippets Groups Projects
Commit be65be1d authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Store encoders into a shared global structure

parent 9801007f
No related branches found
No related tags found
1 merge request!6Pid
......@@ -4,6 +4,7 @@ use crate::{
};
use embassy_executor::Spawner;
use embassy_stm32::{peripherals::WWDG, time::hz};
use embassy_sync::once_lock::OnceLock;
use futures::FutureExt;
use heapless::Vec;
use i2c2_target::{I2C2, MESSAGE_SIZE};
......@@ -14,15 +15,18 @@ mod watchdog;
pub const I2C_ADDRESS: u8 = 0x57;
static ENCODERS: OnceLock<Encoders<'static>> = OnceLock::new();
struct State {
_i2c: I2C2,
encoders: Encoders<'static>,
encoders: &'static Encoders<'static>,
tick_count: Relative,
}
impl State {
fn new(i2c2: I2C2, encoders: Encoders<'static>) -> Self {
let tick_count = Relative::new(&encoders);
async fn new(i2c2: I2C2) -> Self {
let encoders = ENCODERS.get().await;
let tick_count = Relative::new(encoders);
Self {
_i2c: i2c2,
encoders,
......@@ -39,14 +43,15 @@ pub fn start_i2c_target(
encoders: Encoders<'static>,
_wwdg: WWDG,
) {
spawner.spawn(i2c_engine(i2c2, encoders)).unwrap();
ENCODERS.get_or_init(|| encoders);
spawner.spawn(i2c_engine(i2c2)).unwrap();
spawner.spawn(watchdog::watchdog()).unwrap();
spawner.spawn(motor_control::motor_control(motors)).unwrap();
}
#[embassy_executor::task]
async fn i2c_engine(i2c2: I2C2, encoders: Encoders<'static>) {
let state = State::new(i2c2, encoders);
async fn i2c_engine(i2c2: I2C2) {
let state = State::new(i2c2).await;
I2C2::start(&i2c_callback, state).await;
}
......@@ -150,7 +155,7 @@ fn process_command(
response.push(watchdog::get_shutdown_timeout()).unwrap();
}
[CMD_ENCODER_TICKS] => {
let (left, right) = state.tick_count.ticks(&state.encoders);
let (left, right) = state.tick_count.ticks(state.encoders);
let (left, right) = (left.to_le_bytes(), right.to_le_bytes());
response.push(left[0]).unwrap();
response.push(left[1]).unwrap();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment