use embedded_hal::digital::{OutputPin, PinState};
use embedded_time::duration::Nanoseconds;
use crate::{
step_mode::StepMode32,
traits::{
EnableDirectionControl, EnableStepControl, EnableStepModeControl,
SetDirection, SetStepMode, Step as StepTrait,
},
};
pub struct DRV8825<Enable, Fault, Sleep, Reset, Mode0, Mode1, Mode2, Step, Dir>
{
enable: Enable,
fault: Fault,
sleep: Sleep,
reset: Reset,
mode0: Mode0,
mode1: Mode1,
mode2: Mode2,
step: Step,
dir: Dir,
}
impl DRV8825<(), (), (), (), (), (), (), (), ()> {
pub fn new() -> Self {
Self {
enable: (),
fault: (),
sleep: (),
reset: (),
mode0: (),
mode1: (),
mode2: (),
step: (),
dir: (),
}
}
}
impl<Reset, Mode0, Mode1, Mode2, Step, Dir, OutputPinError>
EnableStepModeControl<(Reset, Mode0, Mode1, Mode2)>
for DRV8825<(), (), (), (), (), (), (), Step, Dir>
where
Reset: OutputPin<Error = OutputPinError>,
Mode0: OutputPin<Error = OutputPinError>,
Mode1: OutputPin<Error = OutputPinError>,
Mode2: OutputPin<Error = OutputPinError>,
{
type WithStepModeControl =
DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, Dir>;
fn enable_step_mode_control(
self,
(reset, mode0, mode1, mode2): (Reset, Mode0, Mode1, Mode2),
) -> Self::WithStepModeControl {
DRV8825 {
enable: self.enable,
fault: self.fault,
sleep: self.sleep,
reset,
mode0,
mode1,
mode2,
step: self.step,
dir: self.dir,
}
}
}
impl<Reset, Mode0, Mode1, Mode2, Step, Dir, OutputPinError> SetStepMode
for DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, Dir>
where
Reset: OutputPin<Error = OutputPinError>,
Mode0: OutputPin<Error = OutputPinError>,
Mode1: OutputPin<Error = OutputPinError>,
Mode2: OutputPin<Error = OutputPinError>,
{
const SETUP_TIME: Nanoseconds = Nanoseconds(650);
const HOLD_TIME: Nanoseconds = Nanoseconds(650);
type Error = OutputPinError;
type StepMode = StepMode32;
fn apply_mode_config(
&mut self,
step_mode: Self::StepMode,
) -> Result<(), Self::Error> {
self.reset.try_set_low()?;
use PinState::*;
use StepMode32::*;
let (mode0, mode1, mode2) = match step_mode {
Full => (Low, Low, Low),
M2 => (High, Low, Low),
M4 => (Low, High, Low),
M8 => (High, High, Low),
M16 => (Low, Low, High),
M32 => (High, High, High),
};
self.mode0.try_set_state(mode0)?;
self.mode1.try_set_state(mode1)?;
self.mode2.try_set_state(mode2)?;
Ok(())
}
fn enable_driver(&mut self) -> Result<(), Self::Error> {
self.reset.try_set_high()
}
}
impl<Reset, Mode0, Mode1, Mode2, Step, Dir, OutputPinError>
EnableDirectionControl<Dir>
for DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, ()>
where
Dir: OutputPin<Error = OutputPinError>,
{
type WithDirectionControl =
DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, Dir>;
fn enable_direction_control(self, dir: Dir) -> Self::WithDirectionControl {
DRV8825 {
enable: self.enable,
fault: self.fault,
sleep: self.sleep,
reset: self.reset,
mode0: self.mode0,
mode1: self.mode1,
mode2: self.mode2,
step: self.step,
dir,
}
}
}
impl<Reset, Mode0, Mode1, Mode2, Step, Dir, OutputPinError> SetDirection
for DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, Dir>
where
Dir: OutputPin<Error = OutputPinError>,
{
const SETUP_TIME: Nanoseconds = Nanoseconds(650);
type Dir = Dir;
type Error = OutputPinError;
fn dir(&mut self) -> &mut Self::Dir {
&mut self.dir
}
}
impl<Reset, Mode0, Mode1, Mode2, Step, Dir, OutputPinError>
EnableStepControl<Step>
for DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, (), Dir>
where
Step: OutputPin<Error = OutputPinError>,
{
type WithStepControl =
DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, Dir>;
fn enable_step_control(self, step: Step) -> Self::WithStepControl {
DRV8825 {
enable: self.enable,
fault: self.fault,
sleep: self.sleep,
reset: self.reset,
mode0: self.mode0,
mode1: self.mode1,
mode2: self.mode2,
step,
dir: self.dir,
}
}
}
impl<Reset, Mode0, Mode1, Mode2, Step, Dir, OutputPinError> StepTrait
for DRV8825<(), (), (), Reset, Mode0, Mode1, Mode2, Step, Dir>
where
Step: OutputPin<Error = OutputPinError>,
{
const PULSE_LENGTH: Nanoseconds = Nanoseconds(1900);
type Step = Step;
type Error = OutputPinError;
fn step(&mut self) -> &mut Self::Step {
&mut self.step
}
}