Adcbattery

Struct Adcbattery 

Source
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>

Source

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);
Source

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 T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.