Skip to content

Commit

Permalink
feat(input): added ability to recognize physical button presses
Browse files Browse the repository at this point in the history
  • Loading branch information
SreeDan committed Jun 30, 2024
1 parent f189d87 commit c149453
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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));
}
}

Expand Down

0 comments on commit c149453

Please sign in to comment.