use bmi323_driver::{
    AccelConfig, ActiveLevel, AverageSamples, Bmi323, EventReportMode, I2C_ADDRESS_PRIMARY,
    InterruptChannel, InterruptPinConfig, InterruptRoute, InterruptSource, MotionAxes,
    NoMotionConfig, OutputDataRate, OutputMode, ReferenceUpdate,
};
use embedded_hal_async::delay::DelayNs;
use embedded_hal_async::digital::Wait;
use embedded_hal_async::i2c::I2c;

#[allow(dead_code)]
async fn embassy_style_no_motion_task<I2C, INT, D>(
    i2c: I2C,
    int1: &mut INT,
    delay: &mut D,
) -> Result<(), bmi323_driver::Error<I2C::Error>>
where
    I2C: I2c,
    INT: Wait,
    D: DelayNs,
{
    let mut imu = Bmi323::new_i2c(i2c, I2C_ADDRESS_PRIMARY);

    imu.init(delay).await?;

    imu.enable_feature_engine(delay).await?;
    imu.set_accel_config(AccelConfig {
        average: AverageSamples::Avg2,
        odr: OutputDataRate::Hz50,
        ..Default::default()
    })
    .await?;

    imu.configure_no_motion(NoMotionConfig {
        axes: MotionAxes::XYZ,
        threshold: NoMotionConfig::threshold_from_g(0.03),
        hysteresis: NoMotionConfig::hysteresis_from_g(0.01),
        duration: 100, // 100 / 50 s = 2.0 s below threshold before event
        wait_time: 1,  // 1 / 50 s = 20 ms clear delay after slope stays low
        reference_update: ReferenceUpdate::EverySample,
        report_mode: EventReportMode::AllEvents,
        interrupt_hold: 3, // 0.625 ms * 2^3 = 5 ms interrupt hold
    })
    .await?;
    imu.set_interrupt_latching(true).await?;
    imu.configure_interrupt_pin(
        InterruptChannel::Int1,
        InterruptPinConfig {
            active_level: ActiveLevel::High,
            output_mode: OutputMode::PushPull,
            enabled: true,
        },
    )
    .await?;

    imu.map_interrupt(InterruptSource::NoMotion, InterruptRoute::Int1)
        .await?;

    loop {
        let status = imu.wait_for_interrupt(int1, InterruptChannel::Int1).await?;

        if status.no_motion() {
            let accel = imu.read_accel().await?;
            let _ = accel;
        }
    }
}

fn main() {}
