use core::marker::PhantomData;
use embedded_hal::i2c::I2c;
use embedded_hal::spi::SpiDevice;
use embedded_hal_async::i2c::I2c as AsyncI2c;
use embedded_hal_async::spi::SpiDevice as AsyncSpiDevice;
use crate::registers::TransportKind;
use crate::{
AccelRange, AsyncI2cTransport, AsyncSpiTransport, GyroRange, SyncI2cTransport, SyncSpiTransport,
};
pub struct Bmi323<T> {
pub(crate) transport: T,
pub(crate) kind: TransportKind,
pub(crate) accel_range: AccelRange,
pub(crate) gyro_range: GyroRange,
}
pub struct Bmi323Async<T> {
pub(crate) transport: T,
pub(crate) kind: TransportKind,
pub(crate) accel_range: AccelRange,
pub(crate) gyro_range: GyroRange,
pub(crate) _not_sync: PhantomData<fn() -> ()>,
}
impl<I2C> Bmi323<SyncI2cTransport<I2C>>
where
I2C: I2c,
{
pub fn new_i2c(i2c: I2C, address: u8) -> Self {
Self {
transport: SyncI2cTransport { bus: i2c, address },
kind: TransportKind::I2c,
accel_range: AccelRange::G8,
gyro_range: GyroRange::Dps2000,
}
}
pub fn destroy(self) -> I2C {
self.transport.bus
}
}
impl<SPI> Bmi323<SyncSpiTransport<SPI>>
where
SPI: SpiDevice<u8>,
{
pub fn new_spi(spi: SPI) -> Self {
Self {
transport: SyncSpiTransport { bus: spi },
kind: TransportKind::Spi,
accel_range: AccelRange::G8,
gyro_range: GyroRange::Dps2000,
}
}
pub fn destroy(self) -> SPI {
self.transport.bus
}
}
impl<I2C> Bmi323Async<AsyncI2cTransport<I2C>>
where
I2C: AsyncI2c,
{
pub fn new_i2c(i2c: I2C, address: u8) -> Self {
Self {
transport: AsyncI2cTransport { bus: i2c, address },
kind: TransportKind::I2c,
accel_range: AccelRange::G8,
gyro_range: GyroRange::Dps2000,
_not_sync: PhantomData,
}
}
pub fn destroy(self) -> I2C {
self.transport.bus
}
}
impl<SPI> Bmi323Async<AsyncSpiTransport<SPI>>
where
SPI: AsyncSpiDevice<u8>,
{
pub fn new_spi(spi: SPI) -> Self {
Self {
transport: AsyncSpiTransport { bus: spi },
kind: TransportKind::Spi,
accel_range: AccelRange::G8,
gyro_range: GyroRange::Dps2000,
_not_sync: PhantomData,
}
}
pub fn destroy(self) -> SPI {
self.transport.bus
}
}