macro_rules! driver {
(
doc = $doc:literal,
i2c = $i2c:path,
delay = $delay:path,
async = [$($asy:tt)*],
await = [$($aw:tt)*] $(,)?
) => {
pub $($asy)* fn reset<P, D>(rst: &mut P, delay: &mut D) -> Result<(), P::Error>
where
P: embedded_hal::digital::OutputPin,
D: $delay,
{
rst.set_low()?;
delay.delay_us($crate::RESET_PULSE_US) $($aw)*;
rst.set_high()?;
delay.delay_ms($crate::BOOT_DELAY_MS) $($aw)*;
Ok(())
}
#[doc = $doc]
#[derive(Debug)]
pub struct Cst820<I2C> {
i2c: I2C,
address: u8,
}
impl<I2C> Cst820<I2C>
where
I2C: $i2c,
{
pub fn new(i2c: I2C) -> Self {
Self::with_address(i2c, $crate::DEFAULT_DEVICE_ADDRESS)
}
pub fn with_address(i2c: I2C, address: u8) -> Self {
Self { i2c, address }
}
pub fn release(self) -> I2C {
self.i2c
}
pub $($asy)* fn chip_id(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<$crate::registers::ChipId>() $($aw)* ?.value())
}
pub $($asy)* fn identify(&mut self) -> Result<bool, I2C::Error> {
Ok(self.chip_id() $($aw)* ? == $crate::CST820_CHIP_ID)
}
pub $($asy)* fn project_id(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<$crate::registers::ProjId>() $($aw)* ?.value())
}
pub $($asy)* fn firmware_version(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<$crate::registers::FwVersion>() $($aw)* ?.value())
}
pub $($asy)* fn configure_gestures(
&mut self,
mask: $crate::registers::MotionMask,
) -> Result<(), I2C::Error> {
self.write_register(mask) $($aw)*
}
pub $($asy)* fn read_touch(&mut self) -> Result<$crate::Touch, I2C::Error> {
let mut buffer = [0; $crate::Touch::LEN];
self.i2c
.write_read(self.address, &[$crate::Touch::START.addr()], &mut buffer)
$($aw)* ?;
Ok($crate::Touch::from_bytes(&buffer))
}
pub $($asy)* fn configure_interrupts(
&mut self,
irq: $crate::registers::IrqCtl,
) -> Result<(), I2C::Error> {
self.write_register(irq) $($aw)*
}
pub $($asy)* fn set_auto_sleep(&mut self, seconds: Option<u8>) -> Result<(), I2C::Error> {
use $crate::registers::{AutoSleepTime, DisAutoSleep};
match seconds {
Some(seconds) => {
self.write_register(AutoSleepTime::new().with_value(seconds)) $($aw)* ?;
self.write_register(DisAutoSleep::new()) $($aw)*
}
None => self.write_register(DisAutoSleep::new().with_value(1)) $($aw)*,
}
}
pub $($asy)* fn sleep(&mut self) -> Result<(), I2C::Error> {
self.write_register($crate::registers::SleepMode::deep_sleep()) $($aw)*
}
pub $($asy)* fn read_register<R>(&mut self) -> Result<R, I2C::Error>
where
R: $crate::registers::ReadableRegister,
{
let mut buffer = [0];
self.i2c
.write_read(self.address, &[*R::REGISTER_ADDRESS], &mut buffer)
$($aw)* ?;
Ok(R::from_bits(buffer[0]))
}
pub $($asy)* fn write_register<R>(&mut self, register: R) -> Result<(), I2C::Error>
where
R: $crate::registers::WritableRegister,
{
self.i2c
.write(self.address, &[*R::REGISTER_ADDRESS, register.to_bits()])
$($aw)*
}
pub $($asy)* fn modify_register<R, F>(&mut self, f: F) -> Result<(), I2C::Error>
where
R: $crate::registers::ReadableRegister + $crate::registers::WritableRegister,
F: FnOnce(R) -> R,
{
let register = self.read_register::<R>() $($aw)* ?;
self.write_register(f(register)) $($aw)*
}
}
};
}