Created
September 19, 2020 15:55
-
-
Save jessebraham/9b7ddcfa12f2d9d77de9dacc4c2ec9b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use embedded_hal::adc::{Channel, OneShot}; | |
/// The ESP8266 has only a single ADC channel, which is multiplexed with the | |
/// system voltage. You can use the ADC to read an external voltage, or read | |
/// the system voltage (VDD33), but not both. | |
#[derive(Debug, Copy, Clone)] | |
pub enum AdcMode { | |
External, | |
Vdd33, | |
} | |
/// ADC peripheral. Keeps track of which operating mode is currently active. | |
#[derive(Debug, Copy, Clone)] | |
pub struct Adc { | |
/// The current operating mode of the ADC peripheral. | |
mode: AdcMode, | |
} | |
impl Adc { | |
/// Initialize the ADC peripheral in the given mode. | |
pub fn init(mode: AdcMode) -> Self { | |
Self { mode } | |
} | |
/// Set the current operating mode of the (already initialized) ADC to | |
/// `mode`. | |
pub fn set_mode(&mut self, mode: AdcMode) { | |
self.mode = mode; | |
} | |
/// Read a sample from the ADC. Depending on the operating mode this will | |
/// either read the external voltage or the system voltage. | |
pub fn sample(self) -> u16 { | |
match self.mode { | |
AdcMode::External => unsafe { system_adc_read() }, | |
AdcMode::Vdd33 => unsafe { system_get_vdd33() }, | |
} | |
} | |
} | |
/// Pin type struct for the A0 pin, which is connected to the ADC. This pin | |
/// cannot be used as a GPIO, only as an analog input. | |
pub struct A0; | |
impl Channel<Adc> for A0 { | |
type ID = u8; | |
fn channel() -> u8 { | |
0u8 | |
} | |
} | |
impl<WORD, PIN> OneShot<Adc, WORD, PIN> for Adc | |
where | |
WORD: From<u16>, | |
PIN: Channel<Adc, ID = u8>, | |
{ | |
type Error = (); | |
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> { | |
Ok(self.sample().into()) | |
} | |
} | |
// FIXME: reverse-engineer and implement this ROM function in Rust | |
unsafe fn system_adc_read() -> u16 { | |
unimplemented!() | |
} | |
// FIXME: reverse-engineer and implement this ROM function in Rust | |
unsafe fn system_get_vdd33() -> u16 { | |
unimplemented!() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment