From 9b77aa29594c56a2f2a9ff95626669249ca66ada Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Mon, 8 May 2023 08:08:25 +0100 Subject: [PATCH 1/6] reset button thread looper --- sphinx-key/src/core/events.rs | 3 +++ sphinx-key/src/main.rs | 6 +++++- sphinx-key/src/periph/button.rs | 23 +++++++++++++++++++++++ sphinx-key/src/periph/led.rs | 5 ++--- sphinx-key/src/periph/mod.rs | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 sphinx-key/src/periph/button.rs diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 8670a1a..b39770b 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -41,6 +41,9 @@ pub enum Status { Connected, Signing, Ota, + Reset1, + Reset2, + Reset3, } pub const ROOT_STORE: &str = "/sdcard/store"; diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 8ce3c02..b85b50f 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -7,6 +7,7 @@ mod periph; use crate::core::control::{controller_from_seed, FlashPersister}; use crate::core::{config::*, events::*}; // use crate::periph::led::led_control_loop; +use crate::periph::button::button_loop; #[allow(unused_imports)] use crate::periph::sd::{mount_sd_card, simple_fs_test}; @@ -39,12 +40,15 @@ fn main() -> Result<()> { thread::sleep(Duration::from_secs(1)); let peripherals = Peripherals::take().unwrap(); - let _pins = peripherals.pins; + let pins = peripherals.pins; let (led_tx, _led_rx) = mpsc::channel(); // LED control thread // led_control_loop(pins.gpio0, peripherals.rmt.channel0, led_rx); + // BUTTON thread + button_loop(pins.gpio8, led_tx.clone()); + led_tx.send(Status::MountingSDCard).unwrap(); println!("About to mount the sdcard..."); while let Err(_e) = mount_sd_card() { diff --git a/sphinx-key/src/periph/button.rs b/sphinx-key/src/periph/button.rs new file mode 100644 index 0000000..e2ebb84 --- /dev/null +++ b/sphinx-key/src/periph/button.rs @@ -0,0 +1,23 @@ +use crate::core::events::Status; +use esp_idf_hal::gpio; +use esp_idf_hal::gpio::*; +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender) { + thread::spawn(move || { + let mut button = PinDriver::input(gpio8).unwrap(); + button.set_pull(Pull::Down).unwrap(); + loop { + // we are using thread::sleep here to make sure the watchdog isn't triggered + thread::sleep(Duration::from_millis(10)); + if button.is_high() { + log::info!("=> GPIO8 HIGH!"); + tx.send(Status::Reset1).unwrap(); + } else { + log::info!("=> GPIO8 LOW!"); + } + } + }); +} diff --git a/sphinx-key/src/periph/led.rs b/sphinx-key/src/periph/led.rs index 05fef42..37ecdeb 100644 --- a/sphinx-key/src/periph/led.rs +++ b/sphinx-key/src/periph/led.rs @@ -1,6 +1,4 @@ use crate::core::events::Status; -// use embedded_hal::delay::blocking::DelayUs; -use esp_idf_hal::delay::FreeRtos; use esp_idf_hal::rmt::config::TransmitConfig; use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, TxRmtDriver}; use esp_idf_hal::{gpio, rmt}; @@ -84,7 +82,8 @@ impl Led { } // Set high and wait tx.start_blocking(&signal).unwrap(); - FreeRtos::delay_ms(self.blink_length); + // FreeRtos::delay_ms(self.blink_length); + thread::sleep(Duration::from_millis(self.blink_length.into())); // Set low let mut signal = FixedLengthSignal::<24>::new(); for i in 0..24 { diff --git a/sphinx-key/src/periph/mod.rs b/sphinx-key/src/periph/mod.rs index bb1f34b..9677bf2 100644 --- a/sphinx-key/src/periph/mod.rs +++ b/sphinx-key/src/periph/mod.rs @@ -1,2 +1,3 @@ +pub mod button; pub mod led; pub mod sd; From 35b7efeb96ffece367499111b8200d578181f298 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Mon, 8 May 2023 09:13:47 +0100 Subject: [PATCH 2/6] track button state --- sphinx-key/src/periph/button.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sphinx-key/src/periph/button.rs b/sphinx-key/src/periph/button.rs index e2ebb84..08749ee 100644 --- a/sphinx-key/src/periph/button.rs +++ b/sphinx-key/src/periph/button.rs @@ -9,14 +9,21 @@ pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender) { thread::spawn(move || { let mut button = PinDriver::input(gpio8).unwrap(); button.set_pull(Pull::Down).unwrap(); + let mut high = false; loop { // we are using thread::sleep here to make sure the watchdog isn't triggered - thread::sleep(Duration::from_millis(10)); + thread::sleep(Duration::from_millis(15)); if button.is_high() { - log::info!("=> GPIO8 HIGH!"); + if !high { + high = true; + log::info!("=> GPIO8 HIGH!"); + } tx.send(Status::Reset1).unwrap(); } else { - log::info!("=> GPIO8 LOW!"); + if high { + high = false; + log::info!("=> GPIO8 LOW!"); + } } } }); From a41e180444dfb8375028fa7c72b89fc76c033f13 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Mon, 8 May 2023 22:13:37 +0100 Subject: [PATCH 3/6] track duration of button hold --- sphinx-key/src/main.rs | 2 + sphinx-key/src/periph/button.rs | 24 +++++++++++- sphinx-key/up.sh | 66 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100755 sphinx-key/up.sh diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index b85b50f..1f905bf 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -49,6 +49,8 @@ fn main() -> Result<()> { // BUTTON thread button_loop(pins.gpio8, led_tx.clone()); + // thread::sleep(Duration::from_secs(100)); + led_tx.send(Status::MountingSDCard).unwrap(); println!("About to mount the sdcard..."); while let Err(_e) = mount_sd_card() { diff --git a/sphinx-key/src/periph/button.rs b/sphinx-key/src/periph/button.rs index 08749ee..08e25f3 100644 --- a/sphinx-key/src/periph/button.rs +++ b/sphinx-key/src/periph/button.rs @@ -5,24 +5,44 @@ use std::sync::mpsc; use std::thread; use std::time::Duration; +const MILLIS: u16 = 10_000; + +const PAUSE: u16 = 15; + pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender) { thread::spawn(move || { let mut button = PinDriver::input(gpio8).unwrap(); button.set_pull(Pull::Down).unwrap(); let mut high = false; + let mut high_times = 0; + let mut low_times = 0; loop { // we are using thread::sleep here to make sure the watchdog isn't triggered - thread::sleep(Duration::from_millis(15)); + thread::sleep(Duration::from_millis(PAUSE.into())); if button.is_high() { if !high { high = true; log::info!("=> GPIO8 HIGH!"); + high_times = 0; + } + if high { + high_times = high_times + 1; + if PAUSE * high_times > MILLIS { + // stayed held down? + } } - tx.send(Status::Reset1).unwrap(); } else { if high { high = false; log::info!("=> GPIO8 LOW!"); + tx.send(Status::Reset1).unwrap(); + low_times = 0; + } + if !high { + low_times = low_times + 1; + if PAUSE * low_times > MILLIS { + // stayed not held down? + } } } } diff --git a/sphinx-key/up.sh b/sphinx-key/up.sh new file mode 100755 index 0000000..bd293d4 --- /dev/null +++ b/sphinx-key/up.sh @@ -0,0 +1,66 @@ +check_exists() { + command -v "$1" > /dev/null +} +check_port() { + cargo espflash board-info "$1" &> /dev/null +} +if ! check_exists esptool.py +then + echo "esptool.py not installed!" + echo "install with this command: pip install esptool" + exit 1 +fi +if ! check_exists ldproxy +then + echo "ldproxy not installed!" + echo "install with this command: cargo install ldproxy" + exit 1 +fi +if ! check_exists cargo-espflash +then + echo "cargo-espflash not installed!" + echo "install with this command: cargo install cargo-espflash" + exit 1 +fi +if [ -z "$SSID" ] +then + echo "Please set environment variable SSID to the SSID of the wifi you'll use to configure your sphinx-key." + exit 1 +fi +if [ -z "$PASS" ] +then + echo "Please set environment variable PASS to the password of the wifi you'll use to configure your sphinx-key." + exit 1 +fi +if [ ${#PASS} -lt 8 ] +then + echo "Please set PASS to a password longer than 7 characters." + exit 1 +fi +for FILE in /dev/tty.* +do + # Check for port on macOS + if check_port $FILE + then + PORT=$FILE + break + fi +done +if [ -z "$PORT" ] +then + # Check for port on linux + if check_port /dev/ttyUSB0 + then + PORT=/dev/ttyUSB0 + fi +fi +if [ -z "$PORT" ] +then + echo "ESP likely not connected! Exiting now." + echo "Make sure the ESP is connected with a data USB cable, and try again." + exit 1 +fi +cargo build --release && +esptool.py --chip esp32c3 elf2image target/riscv32imc-esp-espidf/release/sphinx-key && +esptool.py --chip esp32c3 -p $PORT -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x10000 target/riscv32imc-esp-espidf/release/sphinx-key.bin && +espmonitor $PORT From 67293d89768fef227d402358cbd41db98d3e5a61 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Tue, 9 May 2023 21:30:49 +0100 Subject: [PATCH 4/6] low reading means btn pressed --- sphinx-key/src/periph/button.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sphinx-key/src/periph/button.rs b/sphinx-key/src/periph/button.rs index 08e25f3..5b0ad86 100644 --- a/sphinx-key/src/periph/button.rs +++ b/sphinx-key/src/periph/button.rs @@ -28,20 +28,20 @@ pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender) { if high { high_times = high_times + 1; if PAUSE * high_times > MILLIS { - // stayed held down? + // stayed not held down } } } else { if high { high = false; log::info!("=> GPIO8 LOW!"); - tx.send(Status::Reset1).unwrap(); low_times = 0; } if !high { low_times = low_times + 1; if PAUSE * low_times > MILLIS { - // stayed not held down? + // stayed held down + tx.send(Status::Reset1).unwrap(); } } } From 2b13098443cf6e4db5541e548f16b88197d2bca1 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Tue, 9 May 2023 21:49:49 +0100 Subject: [PATCH 5/6] set_pull(Pull::Up) --- sphinx-key/src/periph/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx-key/src/periph/button.rs b/sphinx-key/src/periph/button.rs index 5b0ad86..91793e7 100644 --- a/sphinx-key/src/periph/button.rs +++ b/sphinx-key/src/periph/button.rs @@ -12,7 +12,7 @@ const PAUSE: u16 = 15; pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender) { thread::spawn(move || { let mut button = PinDriver::input(gpio8).unwrap(); - button.set_pull(Pull::Down).unwrap(); + button.set_pull(Pull::Up).unwrap(); let mut high = false; let mut high_times = 0; let mut low_times = 0; From f8f5c67c620220b9385d2940a0286564580cf886 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Sat, 13 May 2023 09:23:25 +0100 Subject: [PATCH 6/6] mrg --- sphinx-key/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 1f905bf..4d18b42 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -47,7 +47,7 @@ fn main() -> Result<()> { // led_control_loop(pins.gpio0, peripherals.rmt.channel0, led_rx); // BUTTON thread - button_loop(pins.gpio8, led_tx.clone()); + // button_loop(pins.gpio8, led_tx.clone()); // thread::sleep(Duration::from_secs(100));