use syunit::*;
use crate::data::MicroSteps;
use crate::math::movements::DefinedActuator;
use crate::{StepperConst, SyncActuator, SyncActuatorGroup, StepperConfig};
pub use syact_macros::StepperActuatorGroup;
mod builder;
pub use builder::{DriveError, DriveMode, StepperBuilder, StartStopBuilder};
mod ctrl;
pub use ctrl::{Controller, GenericPWM};
mod motor;
pub type Stepper<S, D> = motor::ThreadedStepper<StartStopBuilder, GenericPWM<S, D>>;
#[derive(Debug, Clone)]
pub enum StepError {
TimeTooShort(Time),
TimeIsIncorrect(Time)
}
impl core::fmt::Display for StepError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::TimeTooShort(t) =>
f.write_fmt(format_args!("Bad step time! Time given ({}) is smaller than STEP_PULSE_TIME ({})", t, ctrl::STEP_PULSE_TIME)),
Self::TimeIsIncorrect(t) =>
f.write_fmt(format_args!("Bad step time! Time given ({}) is invalid!", t))
}
}
}
impl std::error::Error for StepError { }
pub trait StepperActuator : SyncActuator + DefinedActuator {
fn consts(&self) -> &StepperConst;
fn config(&self) -> &StepperConfig;
fn set_config(&mut self, config : StepperConfig);
fn microsteps(&self) -> MicroSteps;
fn set_microsteps(&mut self, micro : MicroSteps);
fn step_ang(&self) -> Delta;
}
pub trait StepperActuatorGroup<T, const C : usize> : SyncActuatorGroup<T, C>
where
T: StepperActuator + ?Sized + 'static
{
fn set_config(&mut self, config : StepperConfig) {
self.for_each_mut(|comp, _| {
comp.set_config(config.clone())
});
}
fn microsteps(&self) -> [MicroSteps; C] {
self.for_each(|comp, _| {
comp.microsteps()
})
}
fn set_micro(&mut self, micro : [MicroSteps; C]) {
self.for_each_mut(|comp, index| {
comp.set_microsteps(micro[index]);
});
}
}