From 5e18cd11719c6c3a535c446a1df3ddd0127ed8b9 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 06:14:49 -0800 Subject: [PATCH] If user presses button for 3 secs, discard all lora settings --- main/main.ino | 40 ++++++++++++++++++++++++++++++++-------- main/ttn.ino | 9 +++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/main/main.ino b/main/main.ino index 40b54a9..63654a2 100644 --- a/main/main.ino +++ b/main/main.ino @@ -127,16 +127,19 @@ void doDeepSleep(uint64_t msecToWake) void sleep() { #if SLEEP_BETWEEN_MESSAGES - // Show the going to sleep message on the screen - char buffer[20]; - snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0)); - screen_print(buffer); + // If the user has a screen, tell them we are about to sleep + if(ssd1306_found) { + // Show the going to sleep message on the screen + char buffer[20]; + snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0)); + screen_print(buffer); - // Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep - delay(MESSAGE_TO_SLEEP_DELAY); + // Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep + delay(MESSAGE_TO_SLEEP_DELAY); - // Turn off screen - screen_off(); + // Turn off screen + screen_off(); + } // Set the user button to wake the board sleep_interrupt(BUTTON_PIN, LOW); @@ -368,6 +371,27 @@ void loop() { sleep(); } + // if user presses button for more than 3 secs, discard our network prefs and reboot (FIXME, use a debounce lib instead of this boilerplate) + static bool wasPressed = false; + static uint32_t minPressMs; // what tick should we call this press long enough + if(!digitalRead(BUTTON_PIN)) { + if(!wasPressed) { // just started a new press + Serial.println("pressing"); + wasPressed = true; + minPressMs = millis() + 3000; + } + } else if(wasPressed) { + // we just did a release + wasPressed = false; + if(millis() > minPressMs) { + // held long enough + screen_print("Erasing prefs"); + ttn_erase_prefs(); + delay(5000); // Give some time to read the screen + ESP.restart(); + } + } + // Send every SEND_INTERVAL millis static uint32_t last = 0; static bool first = true; diff --git a/main/ttn.ino b/main/ttn.ino index b047df8..b760e1d 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -386,6 +386,15 @@ static void ttn_set_cnt() { } } +/// Blow away our prefs (i.e. to rejoin from scratch) +void ttn_erase_prefs() { + Preferences p; + if(p.begin("lora", false)) { + p.clear(); + p.end(); + } +} + void ttn_send(uint8_t * data, uint8_t data_size, uint8_t port, bool confirmed){ ttn_set_cnt(); // we are about to send using the current packet count