From 213d914915a6dc8b8f45f86fc58874a4d3fb4831 Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Sun, 20 Sep 2020 22:15:16 +0200 Subject: [PATCH] add better timer support and show next beaconing --- README.md | 2 +- src/LoRa_APRS_iGate.cpp | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3408cd9..28af9f4 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Feel free to add a link to your iGate here: ## Future plans -[ ] show time until next beaconing +[x] show time until next beaconing [ ] show login issues from IS server [ ] add better OLED library to support multiple different OLEDs [ ] add support to turn OLED on, off and dimming diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 691e487..d00795f 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -23,6 +23,10 @@ PowerManagement powerManagement; #endif LoRa_APRS lora_aprs; +portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; +hw_timer_t * timer = NULL; +volatile bool timerTick = false; + int next_update = -1; void load_config(); @@ -31,6 +35,7 @@ void setup_ota(); void setup_lora(); void setup_ntp(); void setup_aprs_is(); +void setup_timer(); String BeaconMsg; @@ -64,6 +69,7 @@ void setup() setup_lora(); setup_ntp(); setup_aprs_is(); + setup_timer(); delay(500); } @@ -71,6 +77,13 @@ void setup() // cppcheck-suppress unusedFunction void loop() { + if(timerTick) + { + portENTER_CRITICAL(&timerMux); + timerTick = false; + portEXIT_CRITICAL(&timerMux); + next_update--; + } timeClient.update(); ArduinoOTA.handle(); if(WiFiMulti.run() != WL_CONNECTED) @@ -97,13 +110,13 @@ void loop() } Serial.println("[INFO] Connected to server!"); } - if(next_update == timeClient.getMinutes() || next_update == -1) + if(next_update < 0) { show_display(Config->getIsCall(), "Beacon to Server..."); Serial.print("[" + timeClient.getFormattedTime() + "] "); Serial.print(BeaconMsg); aprs_is->sendMessage(BeaconMsg); - next_update = (timeClient.getMinutes() + Config->getBeaconTimeout()) % 60; + next_update = Config->getBeaconTimeout() * 60; } if(aprs_is->available() > 0) { @@ -120,7 +133,7 @@ void loop() { std::shared_ptr msg = lora_aprs.getMessage(); - show_display(Config->getIsCall(), timeClient.getFormattedTime() + " LoRa", "RSSI: " + String(lora_aprs.getMessageRssi()) + ", SNR: " + String(lora_aprs.getMessageSnr()), msg->toString(), 0); + show_display(Config->getIsCall(), timeClient.getFormattedTime() + " LoRa", "RSSI: " + String(lora_aprs.getMessageRssi()) + ", SNR: " + String(lora_aprs.getMessageSnr()), msg->toString()); Serial.print("[" + timeClient.getFormattedTime() + "] "); Serial.print(" Received packet '"); Serial.print(msg->toString()); @@ -131,6 +144,11 @@ void loop() aprs_is->sendMessage(msg->encode()); } + static int _next_update = 0; + if(next_update != _next_update) + { + show_display(Config->getIsCall(), "Time to next beaconing: " + String(next_update)); + } } void load_config() @@ -241,3 +259,18 @@ void setup_aprs_is() msg.getAPRSBody()->setData(String("=") + Config->getBeaconPosLat() + "I" + Config->getBeaconPosLong() + "&" + Config->getBeaconMessage()); BeaconMsg = msg.encode(); } + +void IRAM_ATTR onTimer() +{ + portENTER_CRITICAL_ISR(&timerMux); + timerTick = true; + portEXIT_CRITICAL_ISR(&timerMux); +} + +void setup_timer() +{ + timer = timerBegin(0, 80, true); + timerAlarmWrite(timer, 1000000, true); + timerAttachInterrupt(timer, &onTimer, true); + timerAlarmEnable(timer); +}