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