Skip to main content
RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/keyscan/
magnetic.rs

1use embassy_nrf::saadc::{self, Saadc};
2use rktk::drivers::interface::magnetic::Adc;
3
4pub struct NrfAdc<'a, const N: usize> {
5    saadc: Saadc<'a, N>,
6}
7
8impl<'a, const N: usize> NrfAdc<'a, N> {
9    pub fn new(saadc: Saadc<'a, N>) -> Self {
10        Self { saadc }
11    }
12}
13
14impl<'a, const N: usize> Adc for NrfAdc<'a, N> {
15    type Error = saadc::Error;
16
17    async fn read(&mut self) -> Result<u16, Self::Error> {
18        let mut buf = [0; N];
19        // Double-sample: the first sample pre-charges/discharges the internal S&H capacitor to the channel voltage,
20        // and the second sample provides an extremely stable and accurate reading.
21        self.saadc.sample(&mut buf).await;
22        self.saadc.sample(&mut buf).await;
23        Ok(buf[0] as u16)
24    }
25}