use adc_interpolator::{AdcInterpolator, Config};
use embedded_hal::adc::{Channel, OneShot};
#[derive(Debug)]
pub struct Gp2d12<Pin> {
interpolator: AdcInterpolator<Pin, u32, 18>,
}
type Error<Adc, ADC, Word, Pin> = nb::Error<<Adc as OneShot<ADC, Word, Pin>>::Error>;
impl<Pin> Gp2d12<Pin> {
pub fn new<ADC>(pin: Pin, max_voltage: u32, precision: u32) -> Self
where
Pin: Channel<ADC>,
{
let config = Config {
max_voltage,
precision,
voltage_to_values: [
(420, 80),
(450, 75),
(480, 70),
(510, 65),
(540, 60),
(580, 55),
(620, 50),
(680, 45),
(760, 40),
(850, 35),
(975, 30),
(1140, 28),
(1380, 20),
(1520, 18),
(1660, 16),
(1860, 14),
(2125, 12),
(2450, 10),
],
};
let interpolator = AdcInterpolator::new(pin, config);
Self { interpolator }
}
pub fn free(self) -> Pin {
self.interpolator.free()
}
pub fn distance<Adc, ADC>(
&mut self,
adc: &mut Adc,
) -> Result<Option<u32>, Error<Adc, ADC, u32, Pin>>
where
Pin: Channel<ADC>,
Adc: OneShot<ADC, u32, Pin>,
{
self.interpolator.read(adc)
}
pub fn min_distance(&self) -> u32 {
self.interpolator.min_value()
}
pub fn max_distance(&self) -> u32 {
self.interpolator.max_value()
}
}