-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adc oneshot driver #302
Adc oneshot driver #302
Conversation
AdcChannelDriver: remove Sync implementation examples: update adc_oneshot
@ivmarkov I'm trying to use the updated OneShot driver in a battery voltage monitor but can't seem to be able to store the
with this code pub struct Battery<'d, T, M>
where
T: ADCPin,
M: Borrow<AdcDriver<'d, T::Adc>>,
{
adc: AdcDriver<'d, T::Adc>,
channel: AdcChannelDriver<'d, T, M>,
voltage: Option<f32>,
}
impl<'d, T, M> Battery<'d, T, M>
where
T: ADCPin,
M: Borrow<AdcDriver<'d, T::Adc>>,
{
pub fn new(
adc: impl Peripheral<P = T::Adc> + 'd,
pin: impl Peripheral<P = T> + 'd,
) -> Result<Self, EspError> {
let adc = AdcDriver::new(adc)?;
let channel = AdcChannelDriver::new(
&adc,
pin,
AdcChannelConfig {
attenuation: attenuation::DB_11,
calibration: true,
..Default::default()
},
)?;
Ok(Self {
adc,
channel,
voltage: None,
})
}
pub fn get_voltage(&mut self) -> Result<f32, EspError> {
const SAMPLES: usize = 128;
let mut max = 0u32;
let mut min = 0xffffffffu32;
let mut total = 0u32;
for _ in 0..SAMPLES {
let value = self.adc.read(&mut self.channel)? as u32;
if value > max {
max = value;
}
if value < min {
min = value;
}
total += value;
std::thread::sleep(std::time::Duration::from_millis(10));
}
// remove the min and max values
let total = total - min - max;
let value = total / (SAMPLES as u32 - 2);
log::info!("Battery ADC value={value} min={min} max={max}");
Ok((value*2) as f32 / 1000.0)
}
} Any ideas? |
Yes. TL; DR: You only need to keep around the Long story: basic Rust - you are trying to keep - in a single struct - an instance of a struct ( That's why Rust these days has |
Implementation of the esp-idf v5 adc oneshot driver.
It would be nice to somehow replicate two C #define's info cfg's from esp-idf-sys so that we don't have to replicate logic determining if curve fitting and/or line fitting are suported based on version and cpu type: