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,6 +127,8 @@ void doDeepSleep(uint64_t msecToWake)
void sleep() {
#if SLEEP_BETWEEN_MESSAGES
// 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));
@ -137,6 +139,7 @@ void sleep() {
// 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;

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