From c149453415fccb510261ec3e3fc296d6948a79aa Mon Sep 17 00:00:00 2001 From: sreekara Date: Sun, 30 Jun 2024 16:43:58 -0400 Subject: [PATCH] feat(input): added ability to recognize physical button presses --- src/main.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 899bc67..d9e784d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,13 @@ use dotenv::dotenv; use embedded_hal::delay::DelayNs; use embedded_svc::http::{client::Client, Headers, Status}; -use esp_idf_hal::{delay::Delay, io::Read, peripherals::Peripherals, sys::esp_crt_bundle_attach}; +use esp_idf_hal::{ + delay::{Delay, FreeRtos}, + gpio::{IOPin, PinDriver}, + io::Read, + peripherals::Peripherals, + sys::esp_crt_bundle_attach, +}; use esp_idf_svc::{ eventloop::EspSystemEventLoop, http::client::{Configuration as HttpConfig, EspHttpConnection}, @@ -15,6 +21,12 @@ use std::{ time::Duration, }; +#[derive(PartialEq, Eq)] +enum ButtonStatus { + Low, + High, +} + fn main() { esp_idf_svc::sys::link_patches(); esp_idf_svc::log::EspLogger::initialize_default(); @@ -24,7 +36,17 @@ fn main() { let sys_loop = EspSystemEventLoop::take().unwrap(); let nvs = EspDefaultNvsPartition::take().unwrap(); - // let mut wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs)).unwrap(); + let mut btn1_status = ButtonStatus::High; + let mut btn2_status = ButtonStatus::High; + let mut btn3_status = ButtonStatus::High; + let mut btn_pin1 = PinDriver::input(peripherals.pins.gpio5.downgrade()).unwrap(); + let mut btn_pin2 = PinDriver::input(peripherals.pins.gpio1.downgrade()).unwrap(); + let mut btn_pin3 = PinDriver::input(peripherals.pins.gpio17.downgrade()).unwrap(); + btn_pin1.set_pull(esp_idf_hal::gpio::Pull::Up).unwrap(); + btn_pin2.set_pull(esp_idf_hal::gpio::Pull::Up).unwrap(); + btn_pin3.set_pull(esp_idf_hal::gpio::Pull::Up).unwrap(); + let mut btn_lock = false; + let mut wifi_driver = BlockingWifi::wrap( EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs)).unwrap(), sys_loop, @@ -33,7 +55,7 @@ fn main() { wifi(&mut wifi_driver); - let check_playback = thread::spawn(|| { + let check_playback = thread::Builder::new().stack_size(64 * 1024).spawn(|| { loop { let httpconnection = EspHttpConnection::new(&HttpConfig { // use_global_ca_store: true, @@ -43,9 +65,8 @@ fn main() { .expect("Could not establish http connection"); let mut httpclient = Client::wrap(httpconnection); - let url_root = include_str!("../api_url.txt"); - // let formatted_url = std::format!("{}/current_playback", url_root); - let formatted_url = "http://23.20.233.1:8080/current_playback"; + let url_root = include_str!("../api_url.txt").replace("\n", ""); + let formatted_url = std::format!("{}/current_playback", url_root); let request = httpclient .get(&formatted_url) @@ -68,15 +89,43 @@ fn main() { continue; } - println!("{:?}", response_str); - println!("{:#?}", playing_json); + // println!("{:?}", response_str); + // println!("{:#?}", playing_json); Delay::new_default().delay_ms(1000); } }); loop { - Delay::new_default().delay_ms(1); + if btn_pin1.is_high() && btn1_status == ButtonStatus::Low { + println!("Button 1 Pressed"); + // Using a button lock to make sure register one button input at a time + btn_lock = false; + btn1_status = ButtonStatus::High; + } else if btn_pin1.is_low() && !btn_lock { + btn_lock = true; + btn1_status = ButtonStatus::Low; + } + + if btn_pin2.is_high() && btn2_status == ButtonStatus::Low { + println!("Button 2 Pressed"); + btn_lock = false; + btn2_status = ButtonStatus::High; + } else if btn_pin2.is_low() && !btn_lock { + btn_lock = true; + btn2_status = ButtonStatus::Low; + } + + if btn_pin3.is_high() && btn3_status == ButtonStatus::Low { + println!("Button 3 Pressed"); + btn_lock = false; + btn3_status = ButtonStatus::High; + } else if btn_pin3.is_low() && !btn_lock { + btn_lock = true; + btn3_status = ButtonStatus::Low; + } + + thread::sleep(Duration::from_millis(100)); } }