diff --git a/blinky/src/main.rs b/blinky/src/main.rs index b8c62fd..1acdbaa 100644 --- a/blinky/src/main.rs +++ b/blinky/src/main.rs @@ -21,6 +21,15 @@ use stm32f7xx_hal::{pac, prelude::*}; use blinky::init_heap; +const TICK_TIMER_HZ: u32 = 1_000_000; +const RUN_COMMANDS_COUNT: usize = 6; +const START_COMMANDS_COUNT: usize = 0; +const STOP_COMMANDS_COUNT: usize = 0; +const LEDS_COUNT: usize = 4; +const AXES_COUNT: usize = 2; +const SPINDLES_COUNT: usize = 2; +const ACTIVE_COMMANDS_COUNT: usize = 1; + pub fn get_run_commands() -> [Command<'static, TIMER_HZ>; 6] { [ Command::Led( @@ -83,7 +92,17 @@ fn main() -> ! { let user_button_timer = super_timer.sub(); let mut user_button = SwitchDevice::new_active_high(user_button_pin, user_button_timer); - let mut robot_builder = RobotBuilder::new(); + let mut robot_builder: RobotBuilder< + '_, + TICK_TIMER_HZ, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + LEDS_COUNT, + AXES_COUNT, + SPINDLES_COUNT, + ACTIVE_COMMANDS_COUNT, + > = RobotBuilder::new(); let green_led_pin = gpiob.pb0.into_push_pull_output(); let green_led_timer = super_timer.sub(); diff --git a/gridbot-tahi/src/main.rs b/gridbot-tahi/src/main.rs index 1320f0a..5bd4d22 100644 --- a/gridbot-tahi/src/main.rs +++ b/gridbot-tahi/src/main.rs @@ -36,6 +36,14 @@ use gridbot_tahi::{ init_heap, }; +const RUN_COMMANDS_COUNT: usize = 32; +const START_COMMANDS_COUNT: usize = 4; +const STOP_COMMANDS_COUNT: usize = 4; +const LEDS_COUNT: usize = 4; +const AXES_COUNT: usize = 4; +const SPINDLES_COUNT: usize = 4; +const ACTIVE_COMMANDS_COUNT: usize = 8; + const TICK_TIMER_MAX: u32 = u32::MAX; const TICK_TIMER_HZ: u32 = 1_000_000; type TickTimerDevice = Counter; @@ -103,7 +111,17 @@ fn main() -> ! { let mut user_button: UserButton = SwitchDevice::new_active_high(user_button_pin, user_button_timer); - let mut robot_builder: RobotBuilder = RobotBuilder::new(); + let mut robot_builder: RobotBuilder< + '_, + TICK_TIMER_HZ, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + LEDS_COUNT, + AXES_COUNT, + SPINDLES_COUNT, + ACTIVE_COMMANDS_COUNT, + > = RobotBuilder::new(); let green_led_pin: GreenLedPin = gpiob.pb0.into_push_pull_output(); let green_led_timer: GreenLedTimer = super_timer.sub(); diff --git a/robokit/src/robot.rs b/robokit/src/robot.rs index 99882ef..5f0bd1b 100644 --- a/robokit/src/robot.rs +++ b/robokit/src/robot.rs @@ -10,17 +10,43 @@ use crate::error::Error; use crate::runner::{Command, Runner}; use crate::scheduler::Scheduler; -pub struct RobotBuilder<'a, const LED_TIMER_HZ: u32> { - run_commands: Vec, 32>, - start_commands: Vec, 4>, - stop_commands: Vec, 4>, - leds: FnvIndexMap<&'a str, BoxActuator>, 16>, - axes: FnvIndexMap<&'a str, BoxActuator, 16>, - spindles: FnvIndexMap<&'a str, BoxActuator, 16>, +pub struct RobotBuilder< + 'a, + const LED_TIMER_HZ: u32, + const RUN_COMMANDS_COUNT: usize, + const START_COMMANDS_COUNT: usize, + const STOP_COMMANDS_COUNT: usize, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMANDS_COUNT: usize, +> { + run_commands: Vec, RUN_COMMANDS_COUNT>, + start_commands: Vec, START_COMMANDS_COUNT>, + stop_commands: Vec, STOP_COMMANDS_COUNT>, + leds: FnvIndexMap<&'a str, BoxActuator>, LEDS_COUNT>, + axes: FnvIndexMap<&'a str, BoxActuator, AXES_COUNT>, + spindles: FnvIndexMap<&'a str, BoxActuator, SPINDLES_COUNT>, } -pub struct Robot<'a, const LED_TIMER_HZ: u32> { - scheduler: Scheduler, Runner<'a, LED_TIMER_HZ>>, +pub struct Robot< + 'a, + const LED_TIMER_HZ: u32, + const RUN_COMMANDS_COUNT: usize, + const START_COMMANDS_COUNT: usize, + const STOP_COMMANDS_COUNT: usize, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMANDS_COUNT: usize, +> { + scheduler: Scheduler< + Command<'a, LED_TIMER_HZ>, + Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMANDS_COUNT>, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + >, } #[derive(Copy, Clone, Debug)] @@ -33,7 +59,29 @@ pub enum RobotBuilderError { TooManyStopCommands, } -impl<'a, const LED_TIMER_HZ: u32> RobotBuilder<'a, LED_TIMER_HZ> { +impl< + 'a, + const LED_TIMER_HZ: u32, + const RUN_COMMANDS_COUNT: usize, + const START_COMMANDS_COUNT: usize, + const STOP_COMMANDS_COUNT: usize, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMANDS_COUNT: usize, + > + RobotBuilder< + 'a, + LED_TIMER_HZ, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + LEDS_COUNT, + AXES_COUNT, + SPINDLES_COUNT, + ACTIVE_COMMANDS_COUNT, + > +{ pub fn new() -> Self { Self { run_commands: Vec::new(), @@ -111,7 +159,19 @@ impl<'a, const LED_TIMER_HZ: u32> RobotBuilder<'a, LED_TIMER_HZ> { Ok(()) } - pub fn build(self) -> Robot<'a, LED_TIMER_HZ> { + pub fn build( + self, + ) -> Robot< + 'a, + LED_TIMER_HZ, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + LEDS_COUNT, + AXES_COUNT, + SPINDLES_COUNT, + ACTIVE_COMMANDS_COUNT, + > { let runner = Runner::new(self.leds, self.axes, self.spindles); let scheduler = Scheduler::new( runner, @@ -123,8 +183,38 @@ impl<'a, const LED_TIMER_HZ: u32> RobotBuilder<'a, LED_TIMER_HZ> { } } -impl<'a, const LED_TIMER_HZ: u32> Robot<'a, LED_TIMER_HZ> { - pub fn new(scheduler: Scheduler, Runner<'a, LED_TIMER_HZ>>) -> Self { +impl< + 'a, + const LED_TIMER_HZ: u32, + const RUN_COMMANDS_COUNT: usize, + const START_COMMANDS_COUNT: usize, + const STOP_COMMANDS_COUNT: usize, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMANDS_COUNT: usize, + > + Robot< + 'a, + LED_TIMER_HZ, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + LEDS_COUNT, + AXES_COUNT, + SPINDLES_COUNT, + ACTIVE_COMMANDS_COUNT, + > +{ + pub fn new( + scheduler: Scheduler< + Command<'a, LED_TIMER_HZ>, + Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMANDS_COUNT>, + RUN_COMMANDS_COUNT, + START_COMMANDS_COUNT, + STOP_COMMANDS_COUNT, + >, + ) -> Self { Self { scheduler } } diff --git a/robokit/src/runner.rs b/robokit/src/runner.rs index fae4132..46fa1ca 100644 --- a/robokit/src/runner.rs +++ b/robokit/src/runner.rs @@ -20,18 +20,33 @@ pub enum RunnerAction { Reset, } -pub struct Runner<'a, const LED_TIMER_HZ: u32> { - active_commands: Deque, 8>, - leds: FnvIndexMap<&'a str, BoxActuator>, 16>, - axes: FnvIndexMap<&'a str, BoxActuator, 16>, - spindles: FnvIndexMap<&'a str, BoxActuator, 16>, +pub struct Runner< + 'a, + const LED_TIMER_HZ: u32, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMMANDS_COUNT: usize, +> { + active_commands: Deque, ACTIVE_COMMMANDS_COUNT>, + leds: FnvIndexMap<&'a str, BoxActuator>, LEDS_COUNT>, + axes: FnvIndexMap<&'a str, BoxActuator, AXES_COUNT>, + spindles: FnvIndexMap<&'a str, BoxActuator, SPINDLES_COUNT>, } -impl<'a, const LED_TIMER_HZ: u32> Runner<'a, LED_TIMER_HZ> { +impl< + 'a, + const LED_TIMER_HZ: u32, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMMANDS_COUNT: usize, + > Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMMANDS_COUNT> +{ pub fn new( - leds: FnvIndexMap<&'a str, BoxActuator>, 16>, - axes: FnvIndexMap<&'a str, BoxActuator, 16>, - spindles: FnvIndexMap<&'a str, BoxActuator, 16>, + leds: FnvIndexMap<&'a str, BoxActuator>, LEDS_COUNT>, + axes: FnvIndexMap<&'a str, BoxActuator, AXES_COUNT>, + spindles: FnvIndexMap<&'a str, BoxActuator, SPINDLES_COUNT>, ) -> Self { Self { active_commands: Deque::new(), @@ -50,7 +65,16 @@ pub enum RunnerError<'a> { } */ -impl<'a, const LED_TIMER_HZ: u32> Actuator for Runner<'a, LED_TIMER_HZ> { +impl< + 'a, + const LED_TIMER_HZ: u32, + const LEDS_COUNT: usize, + const AXES_COUNT: usize, + const SPINDLES_COUNT: usize, + const ACTIVE_COMMMANDS_COUNT: usize, + > Actuator + for Runner<'a, LED_TIMER_HZ, LEDS_COUNT, AXES_COUNT, SPINDLES_COUNT, ACTIVE_COMMMANDS_COUNT> +{ type Action = RunnerAction>; type Error = BoxError; diff --git a/robokit/src/scheduler.rs b/robokit/src/scheduler.rs index 52256b5..c35b373 100644 --- a/robokit/src/scheduler.rs +++ b/robokit/src/scheduler.rs @@ -17,18 +17,29 @@ pub enum SchedulerState { StopLoop, } -pub struct Scheduler -where +pub struct Scheduler< + Command, + Runner, + const RUN_COMMANDS_COUNT: usize, + const START_COMMANDS_COUNT: usize, + const STOP_COMMANDS_COUNT: usize, +> where Runner: Actuator>, { runner: Runner, state: SchedulerState, - run_commands: Vec, - start_commands: Vec, - stop_commands: Vec, + run_commands: Vec, + start_commands: Vec, + stop_commands: Vec, } -impl Scheduler +impl< + Command, + Runner, + const RUN_COMMANDS_COUNT: usize, + const START_COMMANDS_COUNT: usize, + const STOP_COMMANDS_COUNT: usize, + > Scheduler where Command: Copy + Clone + Debug + Format, Runner: Actuator>, @@ -36,9 +47,9 @@ where { pub fn new( runner: Runner, - run_commands: Vec, - start_commands: Vec, - stop_commands: Vec, + run_commands: Vec, + start_commands: Vec, + stop_commands: Vec, ) -> Self { Self { runner,