#![allow(dead_code)]

use crate::hardware::mct8316a::motor_startup::DutyCycle;

#[derive(Debug, Clone)]
pub struct TrapConfig1 {
    pub open_loop_handoff_cycles: OpenLoopHandoffCycles,
    pub avs_negative_current_limit: AVSNegativeCurrentLimit,
    pub avs_limit_hysteresis: AVSLimitHysteresis,
    pub isd_bemf_threshold: IsdBemfThreshold,
    pub isd_cycle_threshold: IsdCycleThreshold,
    pub open_loop_zc_detection_threshold: OpenLoopZcDetectionThreshold,
    pub fast_startup_div_factor: FastStartupDivFactor,
}

impl Default for TrapConfig1 {
    fn default() -> Self {
        Self {
            open_loop_handoff_cycles: OpenLoopHandoffCycles::Cycles3,
            avs_negative_current_limit: AVSNegativeCurrentLimit::Limit0,
            avs_limit_hysteresis: AVSLimitHysteresis::Limit20,
            isd_bemf_threshold: IsdBemfThreshold::Limit0,
            isd_cycle_threshold: IsdCycleThreshold::Limit2,
            open_loop_zc_detection_threshold: OpenLoopZcDetectionThreshold::Degrees5,
            fast_startup_div_factor: FastStartupDivFactor::Value1,
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub enum OpenLoopHandoffCycles {
    Cycles3 = 0x0,
    Cycles6 = 0x1,
    Cycles12 = 0x2,
    Cycles24 = 0x3,
}

#[derive(Debug, Copy, Clone)]
pub enum AVSNegativeCurrentLimit {
    Limit0 = 0x0,
    LimitNeg40 = 0x1,
    LimitNeg30 = 0x2,
    LimitNeg20 = 0x3,
    LimitNeg10 = 0x4,
    Limit10 = 0x5,
    Limit20 = 0x6,
    Limit30 = 0x7,
}

#[derive(Debug, Copy, Clone)]
pub enum AVSLimitHysteresis {
    Limit20 = 0x0,
    Limit10 = 0x1,
}

#[derive(Debug, Copy, Clone)]
pub enum IsdBemfThreshold {
    Limit0 = 0x00,
    Limit200 = 0x01,
    Limit400 = 0x02,
    Limit600 = 0x03,
    Limit800 = 0x04,
    Limit1000 = 0x05,
    Limit1200 = 0x06,
    Limit1400 = 0x07,
    Limit1600 = 0x08,
    Limit1800 = 0x09,
    Limit2000 = 0x0A,
    Limit2200 = 0x0B,
    Limit2400 = 0x0C,
    Limit2600 = 0x0D,
    Limit2800 = 0x0E,
    Limit3000 = 0x0F,
    Limit3200 = 0x10,
    Limit3400 = 0x11,
    Limit3600 = 0x12,
    Limit3800 = 0x13,
    Limit4000 = 0x14,
    Limit4200 = 0x15,
    Limit4400 = 0x16,
    Limit4600 = 0x17,
    Limit4800 = 0x18,
    Limit5000 = 0x19,
    Limit5200 = 0x1A,
    Limit5400 = 0x1B,
    Limit5600 = 0x1C,
    Limit5800 = 0x1D,
    Limit6000 = 0x1E,
    Limit6200 = 0x1F,
}

#[derive(Debug, Copy, Clone)]
pub enum IsdCycleThreshold {
    Limit2 = 0x0,
    Limit5 = 0x1,
    Limit8 = 0x2,
    Limit11 = 0x3,
    Limit14 = 0x4,
    Limit17 = 0x5,
    Limit20 = 0x6,
    Limit23 = 0x7,
}

#[derive(Debug, Copy, Clone)]
pub enum OpenLoopZcDetectionThreshold {
    Degrees5 = 0x0,
    Degrees8 = 0x1,
    Degrees12 = 0x2,
    Degrees15 = 0x3,
}

#[derive(Debug, Copy, Clone)]
pub enum FastStartupDivFactor {
    Value1 = 0x0,
    Value2 = 0x1,
    Value4 = 0x2,
    Value8 = 0x3,
}

#[derive(Debug, Clone)]
pub struct TrapConfig2 {
    /// Max of 0xF (15us)
    pub blanking_time_microseconds: u8,
    /// Max of 0x7 (7us)
    pub comparator_deglitch_time_microseconds: u8,
    pub align_duty_cycle: DutyCycle,
}

impl Default for TrapConfig2 {
    fn default() -> Self {
        Self {
            blanking_time_microseconds: 0,
            comparator_deglitch_time_microseconds: 0,
            align_duty_cycle: DutyCycle::Percent10,
        }
    }
}
