mirror of
https://github.com/stakwork/sphinx-key.git
synced 2025-12-18 15:54:31 +01:00
button status sender
This commit is contained in:
@@ -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`
|
`espflash target/riscv32imc-esp-espidf/debug/sphinx-key --monitor`
|
||||||
|
|
||||||
|
### button test
|
||||||
|
|
||||||
|
`espflash target/riscv32imc-esp-espidf/debug/btn`
|
||||||
|
|
||||||
|
`espmonitor $PORT`
|
||||||
|
|
||||||
## dependencies
|
## dependencies
|
||||||
|
|
||||||
##### cargo nightly:
|
##### cargo nightly:
|
||||||
|
|||||||
@@ -10,8 +10,16 @@ use esp_idf_hal::peripherals::Peripherals;
|
|||||||
use esp_idf_svc::nvs::EspNvs;
|
use esp_idf_svc::nvs::EspNvs;
|
||||||
use esp_idf_svc::nvs::*;
|
use esp_idf_svc::nvs::*;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
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 peripherals = Peripherals::take().unwrap();
|
||||||
let pins = peripherals.pins;
|
let pins = peripherals.pins;
|
||||||
|
|
||||||
@@ -22,5 +30,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
// BUTTON thread
|
// BUTTON thread
|
||||||
periph::button::button_loop(pins.gpio8, led_tx.clone());
|
periph::button::button_loop(pins.gpio8, led_tx.clone());
|
||||||
|
|
||||||
Ok(())
|
loop {
|
||||||
|
thread::sleep(Duration::from_millis(1000));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,41 +7,55 @@ use std::time::Duration;
|
|||||||
|
|
||||||
const MILLIS: u16 = 10_000;
|
const MILLIS: u16 = 10_000;
|
||||||
|
|
||||||
const PAUSE: u16 = 15;
|
const PAUSE: u16 = 50;
|
||||||
|
|
||||||
pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender<Status>) {
|
pub fn button_loop(gpio8: gpio::Gpio8, tx: mpsc::Sender<Status>) {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut button = PinDriver::input(gpio8).unwrap();
|
let mut button = PinDriver::input(gpio8).unwrap();
|
||||||
button.set_pull(Pull::Up).unwrap();
|
button.set_pull(Pull::Up).unwrap();
|
||||||
let mut high = false;
|
let mut pressed = false;
|
||||||
let mut high_times = 0;
|
let mut up_times = 0;
|
||||||
let mut low_times = 0;
|
let mut low_times = 0;
|
||||||
|
let mut last_status = Status::Starting;
|
||||||
loop {
|
loop {
|
||||||
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
||||||
thread::sleep(Duration::from_millis(PAUSE.into()));
|
thread::sleep(Duration::from_millis(PAUSE.into()));
|
||||||
if button.is_high() {
|
if button.is_high() {
|
||||||
if !high {
|
if pressed {
|
||||||
high = true;
|
pressed = false;
|
||||||
log::info!("=> GPIO8 HIGH!");
|
log::info!("=> Button let up!");
|
||||||
high_times = 0;
|
up_times = 0;
|
||||||
}
|
}
|
||||||
if high {
|
if !pressed {
|
||||||
high_times = high_times + 1;
|
up_times = up_times + 1;
|
||||||
if PAUSE * high_times > MILLIS {
|
if PAUSE * up_times > MILLIS {
|
||||||
// stayed not held down
|
// stayed up
|
||||||
|
if last_status == Status::Reset1 {
|
||||||
|
log::info!("send Status::Reset2!");
|
||||||
|
tx.send(Status::Reset2).unwrap();
|
||||||
|
last_status = Status::Reset2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if high {
|
if !pressed {
|
||||||
high = false;
|
pressed = true;
|
||||||
log::info!("=> GPIO8 LOW!");
|
log::info!("=> Button pressed!");
|
||||||
low_times = 0;
|
low_times = 0;
|
||||||
}
|
}
|
||||||
if !high {
|
if pressed {
|
||||||
low_times = low_times + 1;
|
low_times = low_times + 1;
|
||||||
if PAUSE * low_times > MILLIS {
|
if PAUSE * low_times > MILLIS {
|
||||||
// stayed held down
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,14 +27,16 @@ fn states() -> BTreeMap<Status, (Color, Time)> {
|
|||||||
s.insert(Status::Connected, (0x000101, 400)); // Cyan
|
s.insert(Status::Connected, (0x000101, 400)); // Cyan
|
||||||
s.insert(Status::Signing, (0x111111, 100)); // White
|
s.insert(Status::Signing, (0x111111, 100)); // White
|
||||||
s.insert(Status::Ota, (0xffa500, 100)); // Orange
|
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
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn led_control_loop(gpio0: gpio::Gpio0, channel0: rmt::CHANNEL0, rx: mpsc::Receiver<Status>) {
|
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 config = TransmitConfig::new().clock_divider(1);
|
||||||
let transmit = Arc::new(Mutex::new(
|
let transmit = Arc::new(Mutex::new(
|
||||||
TxRmtDriver::new(channel0, led, &config).unwrap(),
|
TxRmtDriver::new(channel0, gpio0, &config).unwrap(),
|
||||||
));
|
));
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut led = Led::new(0x000001, 100);
|
let mut led = Led::new(0x000001, 100);
|
||||||
|
|||||||
Reference in New Issue
Block a user