use core::cell::RefCell;
use critical_section::Mutex;
use embedded_hal_bus::i2c::CriticalSectionDevice;
use esp_hal::{
Blocking,
i2c::master::{Config as I2cConfig, I2c},
peripherals::{GPIO8, GPIO9, I2C0},
time::Rate,
};
use static_cell::StaticCell;
use crate::Error;
pub type I2cBus = Mutex<RefCell<I2c<'static, Blocking>>>;
pub type I2cDevice = CriticalSectionDevice<'static, I2c<'static, Blocking>>;
#[expect(missing_docs, reason = "the fields are the pins they're named after")]
#[derive(Debug)]
pub struct I2cPins {
pub i2c: I2C0<'static>,
pub sda: GPIO8<'static>,
pub scl: GPIO9<'static>,
}
pub fn i2c(pins: I2cPins) -> Result<&'static I2cBus, Error> {
static BUS: StaticCell<I2cBus> = StaticCell::new();
let i2c = I2c::new(
pins.i2c,
I2cConfig::default().with_frequency(Rate::from_khz(400)),
)
.map_err(|_| Error::I2c)?
.with_sda(pins.sda)
.with_scl(pins.scl);
Ok(BUS.init(Mutex::new(RefCell::new(i2c))))
}