pub struct Adcbattery<'d> { /* private fields */ }Expand description
Battery voltage monitor using the ESP32-S3 ADC1 peripheral.
Created automatically by crate::Lilka::init and available as
crate::Lilka::adc_battery.
The hardware path is:
Li-Po cell → 100 kΩ → GPIO3 (ADC1 CH2) → ESP32-S3 ADC1
└── 33 kΩ ──┘The voltage divider ratio is 33 / (100 + 33) ≈ 0.248, so the ADC sees
roughly 0.25× the actual battery voltage.
§Example — polling battery level
use lilkaoxide::prelude::*;
loop {
let mv = lilka.adc_battery.read_voltage_mv();
if mv < 3_300 {
defmt::warn!("Low battery: {} mV", mv);
}
// ... game logic
}Implementations§
Source§impl<'d> Adcbattery<'d>
impl<'d> Adcbattery<'d>
Sourcepub fn read(&mut self) -> u16
pub fn read(&mut self) -> u16
Reads the raw ADC sample from the battery pin.
Returns a 12-bit value in the range 0 – 4095 where:
0→ 0 V at the ADC input (battery deeply discharged / disconnected).4095→ full-scale voltage for the configured attenuation (~3.1 V at 11 dB).
Prefer Adcbattery::read_voltage_mv for human-readable values.
§Example
let raw: u16 = adc.read();
defmt::info!("Raw ADC: {}", raw);Sourcepub fn read_voltage_mv(&mut self) -> u16
pub fn read_voltage_mv(&mut self) -> u16
Reads the battery voltage in millivolts (mV).
Applies the voltage-divider formula to convert the raw ADC reading into the actual battery terminal voltage:
V_bat = (raw / 4095) × 3.1 V / (33 / (100 + 33))
= (raw / 4095) × 11.73 V [in volts]Then multiplied by 1 000 to yield millivolts.
§Accuracy notes
- The formula assumes
Attenuation::_11dB(full-scale ≈ 3.1 V) and the fixed 100 kΩ / 33 kΩ divider on the Lilka PCB. - Typical Li-Po range: 3 000 mV (empty) – 4 200 mV (full charge).
- ADC non-linearity may introduce ±50–100 mV error near the rails.
§Example
let mv = adc.read_voltage_mv();
// Rough battery percentage (linear approximation)
let pct = ((mv.saturating_sub(3_000)) as u32 * 100 / 1_200) as u8;
defmt::info!("Battery: {} mV (~{}%)", mv, pct);Auto Trait Implementations§
impl<'d> Freeze for Adcbattery<'d>
impl<'d> RefUnwindSafe for Adcbattery<'d>
impl<'d> Send for Adcbattery<'d>
impl<'d> Sync for Adcbattery<'d>
impl<'d> Unpin for Adcbattery<'d>
impl<'d> !UnwindSafe for Adcbattery<'d>
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere
U: From<T>,
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
§impl<T, U> TryFrom<U> for Twhere
U: Into<T>,
impl<T, U> TryFrom<U> for Twhere
U: Into<T>,
§impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.