From 43ccd3afb9a0c80cf9fd77ba4f51463d2276b5c6 Mon Sep 17 00:00:00 2001 From: Claas Date: Sat, 31 Jan 2026 17:25:29 +0100 Subject: [PATCH] Read pin that might give battery value need to leave it for now as the battery won't discharge while connected for logging so I can't observe a change. Need to implement actual display rendering first --- src/{button.rs => input.rs} | 23 +++++++++++++++-------- src/main.rs | 27 ++++++++++++--------------- 2 files changed, 27 insertions(+), 23 deletions(-) rename src/{button.rs => input.rs} (77%) diff --git a/src/button.rs b/src/input.rs similarity index 77% rename from src/button.rs rename to src/input.rs index 01b8138..360e177 100644 --- a/src/button.rs +++ b/src/input.rs @@ -1,8 +1,10 @@ +//! Reads analog values from GPIO pins. These values are used to determine the state of buttons and battery level. + use defmt::info; use esp_hal::{ Async, analog::adc::{Adc, AdcCalLine, AdcConfig, AdcPin, Attenuation}, - peripherals::{ADC1, GPIO1, GPIO2}, + peripherals::{ADC1, GPIO0, GPIO1, GPIO2}, }; /// Measured values and rough midway points @@ -37,17 +39,20 @@ fn get_active_button(pin_value: u16, ranges: &[u16], pin: Pin) -> Option { None } -pub(crate) struct Button<'a> { +pub(crate) struct Analog<'a> { adc: Adc<'a, ADC1<'a>, Async>, pin: ( + AdcPin, ADC1<'a>, AdcCalLine>>, AdcPin, ADC1<'a>, AdcCalLine>>, AdcPin, ADC1<'a>, AdcCalLine>>, ), } -impl<'a> Button<'a> { - pub(crate) fn new(adc: ADC1<'a>, pin_1: GPIO1<'a>, pin_2: GPIO2<'a>) -> Self { +impl<'a> Analog<'a> { + pub(crate) fn new(adc: ADC1<'a>, pin_0: GPIO0<'a>, pin_1: GPIO1<'a>, pin_2: GPIO2<'a>) -> Self { let mut configuration = AdcConfig::new(); + let pin_0 = configuration + .enable_pin_with_cal::<_, AdcCalLine>>(pin_0, Attenuation::_11dB); let pin_1 = configuration .enable_pin_with_cal::<_, AdcCalLine>>(pin_1, Attenuation::_11dB); let pin_2 = configuration @@ -56,20 +61,22 @@ impl<'a> Button<'a> { Self { adc, - pin: (pin_1, pin_2), + pin: (pin_0, pin_1, pin_2), } } - async fn read_values(&mut self) -> (u16, u16) { + async fn read_values(&mut self) -> (u16, u16, u16) { let value_1 = self.adc.read_oneshot(&mut self.pin.0).await; let value_2 = self.adc.read_oneshot(&mut self.pin.1).await; - (value_1, value_2) + let value_3 = self.adc.read_oneshot(&mut self.pin.2).await; + (value_1, value_2, value_3) } pub(crate) async fn poll(&mut self) { let values = self.read_values().await; let button_1 = get_active_button(values.0, &PIN_1_RANGES, Pin::One); let button_2 = get_active_button(values.1, &PIN_2_RANGES, Pin::Two); + info!("Battery? {}", values.2); match (button_1, button_2) { (Some(button_1), Some(button_2)) => { info!("Button 1: {}, Button 2: {}", button_1, button_2); @@ -87,7 +94,7 @@ impl<'a> Button<'a> { } } -impl<'a> Future for Button<'a> { +impl<'a> Future for Analog<'a> { type Output = (); fn poll( diff --git a/src/main.rs b/src/main.rs index efdd25c..81af62a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,17 +7,16 @@ )] #![deny(clippy::large_stack_frames)] -mod button; mod eink_display; +mod input; mod spi; -use bt_hci::cmd::info; use defmt::{error, info}; use embassy_executor::Spawner; use embassy_time::Timer; -use esp_hal::analog::adc::{Adc, AdcCalLine, AdcChannel, AdcConfig, Attenuation}; +use esp_hal::analog::adc::AdcChannel; use esp_hal::gpio::{self, Input, InputConfig}; -use esp_hal::peripherals::{ADC1, ADC2, GPIO0, GPIO1, GPIO2, GPIO3, LPWR}; +use esp_hal::peripherals::{ADC2, GPIO0, GPIO3, LPWR}; use esp_hal::rtc_cntl::sleep::{RtcioWakeupSource, WakeupLevel}; use esp_hal::rtc_cntl::{reset_reason, wakeup_cause}; use esp_hal::system::Cpu; @@ -25,8 +24,8 @@ use esp_hal::timer::timg::TimerGroup; use esp_hal::{clock::CpuClock, rtc_cntl::Rtc}; use {esp_backtrace as _, esp_println as _}; -use crate::button::Button; use crate::eink_display::EinkDisplay; +use crate::input::Analog; extern crate alloc; @@ -107,9 +106,6 @@ async fn handle_power_button( real_time_control.sleep_deep(&[&rtcio]); } -#[embassy_executor::task] -async fn handle_battery(adc: ADC2<'static>, pin: GPIO0<'static>) {} - /// Just a convenience replacement for main to be able to return errors async fn run(spawner: Spawner) -> Result<(), ApplicationError> { let reset_reason = reset_reason(Cpu::ProCpu); @@ -151,11 +147,12 @@ async fn run(spawner: Spawner) -> Result<(), ApplicationError> { // Busy let busy = peripherals.GPIO6; - // Battery - let battery_adc = peripherals.GPIO0.adc_channel(); - info!("ADC channel for battery: {:?}", battery_adc); - // Buttons - let mut button = Button::new(peripherals.ADC1, peripherals.GPIO1, peripherals.GPIO2); + let mut analog = Analog::new( + peripherals.ADC1, + peripherals.GPIO0, + peripherals.GPIO1, + peripherals.GPIO2, + ); let direct_memory_access_channel = peripherals.DMA_CH0; let sd_card_chip_select = peripherals.GPIO12; @@ -177,7 +174,7 @@ async fn run(spawner: Spawner) -> Result<(), ApplicationError> { .map_err(ApplicationError::SetUpEinkDisplay)?; let mut frame = [0x00u8; eink_display::BUFFER_SIZE]; - frame[0..eink_display::BUFFER_SIZE / 2].fill(0xFF); + frame[0..eink_display::BUFFER_SIZE / 2].fill(0x33); // frame[eink_display::BUFFER_SIZE / 2..].fill(0x00); display @@ -192,7 +189,7 @@ async fn run(spawner: Spawner) -> Result<(), ApplicationError> { ))?; loop { - button.poll().await; + analog.poll().await; Timer::after_secs(1).await; } -- 2.51.2