If user presses button for 3 secs, discard all lora settings

This commit is contained in:
geeksville 2020-02-01 06:14:49 -08:00
parent f655882cfa
commit 5e18cd1171
2 changed files with 41 additions and 8 deletions

View File

@ -127,16 +127,19 @@ void doDeepSleep(uint64_t msecToWake)
void sleep() { void sleep() {
#if SLEEP_BETWEEN_MESSAGES #if SLEEP_BETWEEN_MESSAGES
// Show the going to sleep message on the screen // If the user has a screen, tell them we are about to sleep
char buffer[20]; if(ssd1306_found) {
snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0)); // Show the going to sleep message on the screen
screen_print(buffer); 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 // Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep
delay(MESSAGE_TO_SLEEP_DELAY); delay(MESSAGE_TO_SLEEP_DELAY);
// Turn off screen // Turn off screen
screen_off(); screen_off();
}
// Set the user button to wake the board // Set the user button to wake the board
sleep_interrupt(BUTTON_PIN, LOW); sleep_interrupt(BUTTON_PIN, LOW);
@ -368,6 +371,27 @@ void loop() {
sleep(); 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 // Send every SEND_INTERVAL millis
static uint32_t last = 0; static uint32_t last = 0;
static bool first = true; static bool first = true;

View File

@ -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){ 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 ttn_set_cnt(); // we are about to send using the current packet count