button status sender

This commit is contained in:
Evan Feenstra
2023-06-13 16:39:23 -07:00
parent ad46e2c184
commit 7238ec85da
4 changed files with 51 additions and 19 deletions

View File

@@ -76,6 +76,12 @@ This will encrypt your seed and send to the hardware, along with your home wifi
`espflash target/riscv32imc-esp-espidf/debug/sphinx-key --monitor`
### button test
`espflash target/riscv32imc-esp-espidf/debug/btn`
`espmonitor $PORT`
## dependencies
##### cargo nightly:

View File

@@ -10,8 +10,16 @@ use esp_idf_hal::peripherals::Peripherals;
use esp_idf_svc::nvs::EspNvs;
use esp_idf_svc::nvs::*;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
thread::sleep(Duration::from_secs(1));
let peripherals = Peripherals::take().unwrap();
let pins = peripherals.pins;
@@ -22,5 +30,7 @@ fn main() -> anyhow::Result<()> {
// BUTTON thread
periph::button::button_loop(pins.gpio8, led_tx.clone());
Ok(())
loop {
thread::sleep(Duration::from_millis(1000));
}
}

View File

@@ -7,41 +7,55 @@ use std::time::Duration;
const MILLIS: u16 = 10_000;
const PAUSE: u16 = 15;
const PAUSE: u16 = 50;
pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender<Status>) {
thread::spawn(move || {
let mut button = PinDriver::input(gpio8).unwrap();
button.set_pull(Pull::Up).unwrap();
let mut high = false;
let mut high_times = 0;
let mut pressed = false;
let mut up_times = 0;
let mut low_times = 0;
let mut last_status = Status::Starting;
loop {
// we are using thread::sleep here to make sure the watchdog isn't triggered
thread::sleep(Duration::from_millis(PAUSE.into()));
if button.is_high() {
if !high {
high = true;
log::info!("=> GPIO8 HIGH!");
high_times = 0;
if pressed {
pressed = false;
log::info!("=> Button let up!");
up_times = 0;
}
if high {
high_times = high_times + 1;
if PAUSE * high_times > MILLIS {
// stayed not held down
if !pressed {
up_times = up_times + 1;
if PAUSE * up_times > MILLIS {
// stayed up
if last_status == Status::Reset1 {
log::info!("send Status::Reset2!");
tx.send(Status::Reset2).unwrap();
last_status = Status::Reset2;
}
}
}
} else {
if high {
high = false;
log::info!("=> GPIO8 LOW!");
if !pressed {
pressed = true;
log::info!("=> Button pressed!");
low_times = 0;
}
if !high {
if pressed {
low_times = low_times + 1;
if PAUSE * low_times > MILLIS {
// stayed held down
tx.send(Status::Reset1).unwrap();
if last_status == Status::Reset2 {
log::info!("send Status::Reset3!");
tx.send(Status::Reset3).unwrap();
last_status = Status::Reset3;
} else if last_status != Status::Reset1 && last_status != Status::Reset3 {
log::info!("send Status::Reset1!");
tx.send(Status::Reset1).unwrap();
last_status = Status::Reset1;
}
}
}
}

View File

@@ -27,14 +27,16 @@ fn states() -> BTreeMap<Status, (Color, Time)> {
s.insert(Status::Connected, (0x000101, 400)); // Cyan
s.insert(Status::Signing, (0x111111, 100)); // White
s.insert(Status::Ota, (0xffa500, 100)); // Orange
s.insert(Status::Reset1, (0x017700, 100)); // yellow?
s.insert(Status::Reset2, (0xffa500, 100)); // orange?
s.insert(Status::Reset3, (0x010000, 100)); // Red
s
}
pub fn led_control_loop(gpio0: gpio::Gpio0, channel0: rmt::CHANNEL0, rx: mpsc::Receiver<Status>) {
let led = gpio0;
let config = TransmitConfig::new().clock_divider(1);
let transmit = Arc::new(Mutex::new(
TxRmtDriver::new(channel0, led, &config).unwrap(),
TxRmtDriver::new(channel0, gpio0, &config).unwrap(),
));
thread::spawn(move || {
let mut led = Led::new(0x000001, 100);