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