From 81c06c02b9621d93c4128bdade36e1ac793cdc15 Mon Sep 17 00:00:00 2001 From: Claas Date: Fri, 30 Jan 2026 23:26:48 +0100 Subject: [PATCH] Implement reading buttons --- .cargo/config.toml | 4 +++- src/main.rs | 59 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 7ae8236..fd4a2c1 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,8 @@ [target.riscv32imc-unknown-none-elf] # Parition table can also be converted to .bin using espflash and set in espflash.toml -runner = "espflash flash --monitor --chip esp32c3 --log-format defmt --partition-table ./partition-table.csv" +# You might need to change the --target-app-partition to app0 or app1 depending on which is active. +# You can find out which partition is active under "Apps" on https://thelastoutpostworkshop.github.io/ESPConnect/ +runner = "espflash flash --monitor --chip esp32c3 --log-format defmt --partition-table ./partition-table.csv --target-app-partition app1" [env] DEFMT_LOG="info" diff --git a/src/main.rs b/src/main.rs index 3c45811..7f37ca1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ mod spi; use defmt::{error, info}; use embassy_executor::Spawner; -use embassy_time::{Duration, Timer}; -use esp_hal::analog::adc::{Adc, AdcConfig, Attenuation}; +use embassy_time::Timer; +use esp_hal::analog::adc::{Adc, AdcCalLine, AdcConfig, Attenuation}; use esp_hal::gpio::{self, Input, InputConfig}; use esp_hal::peripherals::{ADC1, GPIO1, GPIO2, GPIO3, LPWR}; use esp_hal::rtc_cntl::sleep::{RtcioWakeupSource, WakeupLevel}; @@ -31,14 +31,6 @@ extern crate alloc; // For more information see: esp_bootloader_esp_idf::esp_app_desc!(); -#[embassy_executor::task] -async fn hello_world() { - loop { - info!("Hello world!"); - Timer::after(Duration::from_secs(1)).await; - } -} - #[derive(Debug, thiserror::Error)] enum ApplicationError { #[error("Error setting up SPI")] @@ -122,14 +114,55 @@ async fn handle_power_button( #[embassy_executor::task] async fn handle_buttons(adc: ADC1<'static>, button_1: GPIO1<'static>, button_2: GPIO2<'static>) { let mut configuration = AdcConfig::new(); - let mut pin_1 = configuration.enable_pin(button_1, Attenuation::_11dB); - let mut pin_2 = configuration.enable_pin(button_2, Attenuation::_11dB); + let mut pin_1 = configuration + .enable_pin_with_cal::<_, AdcCalLine>>(button_1, Attenuation::_11dB); + let mut pin_2 = configuration + .enable_pin_with_cal::<_, AdcCalLine>>(button_2, Attenuation::_11dB); let mut adc = Adc::new(adc, configuration).into_async(); + /// Measured values and rough midway points + /// Midway points: ~2850 ~2300 ~1550 ~550 + /// Recorded values: 3087, 2629, 2013, 1117, 4 + const PIN_1_RANGES: [u16; 5] = [2850, 2300, 1550, 550, u16::MIN]; + + enum Pin { + One, + Two, + } + + /// Measured values and rough midway points + /// Midway points: ~2350 ~850 + /// Recorded values: 3087, 1670, 4 + const PIN_2_RANGES: [u16; 3] = [2350, 850, u16::MIN]; + fn get_button_number(pin_value: u16, ranges: &[u16], pin: Pin) -> Option { + let number_of_buttons: u8 = match pin { + Pin::One => 4, + Pin::Two => 2, + }; + + for button_number in 0..number_of_buttons { + let start = ranges[usize::from(button_number) + 1]; + let end = ranges[usize::from(button_number)]; + // if (start..end).contains(&pin_value) { + if start < pin_value && pin_value <= end { + return Some(button_number); + } + } + + None + } + loop { let value_1 = adc.read_oneshot(&mut pin_1).await; + let button = get_button_number(value_1, &PIN_1_RANGES, Pin::One); let value_2 = adc.read_oneshot(&mut pin_2).await; - info!("ADC value read: {} {}", value_1, value_2); + let button_2 = get_button_number(value_2, &PIN_2_RANGES, Pin::Two); + + info!( + "Button 1: {:?} {}, Button 2: {:?} {}", + button, value_1, button_2, value_2 + ); + Timer::after_secs(1).await; } } -- 2.51.2