#![cfg(feature = "embassy")]
#![doc = "Embassy integration helpers for the BQ25887 driver."]
use embassy_embedded_hal::shared_bus::asynch::i2c::{I2cDevice, I2cDeviceWithConfig};
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::mutex::Mutex;
use embedded_hal_async::i2c::I2c;
use crate::{Bq25887Driver, DEFAULT_I2C_ADDRESS};
pub type SharedBus<M, BUS> = Mutex<M, BUS>;
pub type SharedDriver<'a, M, BUS> = Bq25887Driver<I2cDevice<'a, M, BUS>>;
pub type ConfiguredDriver<'a, M, BUS> = Bq25887Driver<I2cDeviceWithConfig<'a, M, BUS>>;
pub fn new_driver<'a, M, BUS>(bus: &'a SharedBus<M, BUS>) -> SharedDriver<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + 'a,
{
new_driver_with_address(bus, DEFAULT_I2C_ADDRESS)
}
pub fn new_driver_with_address<'a, M, BUS>(bus: &'a SharedBus<M, BUS>, address: u8) -> SharedDriver<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + 'a,
{
Bq25887Driver::new_with_address(I2cDevice::new(bus), address)
}
pub fn new_driver_with_config<'a, M, BUS>(
bus: &'a SharedBus<M, BUS>,
config: BUS::Config,
) -> ConfiguredDriver<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + embassy_embedded_hal::SetConfig + 'a,
{
new_driver_with_config_and_address(bus, config, DEFAULT_I2C_ADDRESS)
}
pub fn new_driver_with_config_and_address<'a, M, BUS>(
bus: &'a SharedBus<M, BUS>,
config: BUS::Config,
address: u8,
) -> ConfiguredDriver<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + embassy_embedded_hal::SetConfig + 'a,
{
Bq25887Driver::new_with_address(I2cDeviceWithConfig::new(bus, config), address)
}