#![cfg(feature = "embassy")]
#![doc = "Embassy integration helpers for the BQ25887 driver."]
use embassy_embedded_hal::shared_bus::I2cDeviceError;
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::{ErrorType, I2c};
use crate::{BQ25887Error, 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 type SharedDriverError<BUS> = BQ25887Error<I2cDeviceError<<BUS as ErrorType>::Error>>;
pub type SharedDriverResult<'a, M, BUS> = Result<SharedDriver<'a, M, BUS>, SharedDriverError<BUS>>;
pub type ConfiguredDriverError<BUS> = SharedDriverError<BUS>;
pub type ConfiguredDriverResult<'a, M, BUS> = Result<ConfiguredDriver<'a, M, BUS>, ConfiguredDriverError<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 async fn new_driver_with_status_cache<'a, M, BUS>(bus: &'a SharedBus<M, BUS>) -> SharedDriverResult<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + 'a,
{
new_driver_with_status_cache_and_address(bus, DEFAULT_I2C_ADDRESS).await
}
pub async fn new_driver_with_status_cache_and_address<'a, M, BUS>(
bus: &'a SharedBus<M, BUS>,
address: u8,
) -> SharedDriverResult<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + 'a,
{
let mut driver = new_driver_with_address(bus, address);
driver.refresh_status_register_cache().await?;
Ok(driver)
}
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)
}
pub async fn new_driver_with_config_and_status_cache<'a, M, BUS>(
bus: &'a SharedBus<M, BUS>,
config: BUS::Config,
) -> ConfiguredDriverResult<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + embassy_embedded_hal::SetConfig + 'a,
{
new_driver_with_config_and_status_cache_and_address(bus, config, DEFAULT_I2C_ADDRESS).await
}
pub async fn new_driver_with_config_and_status_cache_and_address<'a, M, BUS>(
bus: &'a SharedBus<M, BUS>,
config: BUS::Config,
address: u8,
) -> ConfiguredDriverResult<'a, M, BUS>
where
M: RawMutex + 'a,
BUS: I2c + embassy_embedded_hal::SetConfig + 'a,
{
let mut driver = new_driver_with_config_and_address(bus, config, address);
driver.refresh_status_register_cache().await?;
Ok(driver)
}