From 0c59659da3533606b94ca164073bc228b41c0915 Mon Sep 17 00:00:00 2001 From: Tekk Date: Sat, 4 Dec 2021 04:06:38 +0100 Subject: [PATCH 1/6] Added support for multiple alternative messages/callsigns --- data/tracker.json | 63 ++++++++++++++++++------------------- src/LoRa_APRS_Tracker.cpp | 28 ++++++++++++----- src/configuration.cpp | 65 +++++++++++++++++++++++++++++---------- src/configuration.h | 34 ++++++++++++++------ 4 files changed, 124 insertions(+), 66 deletions(-) diff --git a/data/tracker.json b/data/tracker.json index f439a10..ab35abe 100644 --- a/data/tracker.json +++ b/data/tracker.json @@ -1,41 +1,42 @@ { - "callsign":"NOCALL-7", "debug": false, "enhance_precision": true, - "beacon": - { - "message":"LoRa Tracker", - "timeout": 1, - "button_tx": false, - "symbol": "[", - "overlay": "/" + "beacons": [ + { + "callsign": "NOCALL-7", + "message": "LoRa Tracker", + "timeout": 1, + "symbol": "[", + "overlay": "/" + } + ], + "button": { + "tx": true, + "alt_message": true }, - "smart_beacon": - { - "active":true, - "turn_min":25, - "slow_rate":300, - "slow_speed":10, - "fast_rate":60, - "fast_speed":100, - "min_tx_dist":100, - "min_bcn":5 + "smart_beacon": { + "active": true, + "turn_min": 25, + "slow_rate": 300, + "slow_speed": 10, + "fast_rate": 60, + "fast_speed": 100, + "min_tx_dist": 100, + "min_bcn": 5, }, - "lora": - { - "frequency_rx":433775000, - "frequency_tx":433775000, - "power":20, - "spreading_factor":12, - "signal_bandwidth":125000, - "coding_rate4":5 + "lora": { + "frequency_rx": 433775000, + "frequency_tx": 433775000, + "power": 20, + "spreading_factor": 12, + "signal_bandwidth": 125000, + "coding_rate4": 5 }, - "ptt_output": - { - "active":false, + "ptt_output": { + "active": false, "io_pin": 4, "start_delay": 0, "end_delay": 0, - "reverse":false + "reverse": false } -} +} \ No newline at end of file diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index 05d3761..9a7bf42 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -40,6 +40,11 @@ static void handle_tx_click() { send_update = true; } +static void handle_next_beacon() { + Configuration::Beacon beacon = Config.SetNextBeacon(); + show_display(beacon.callsign, beacon.message, 2000); +} + // cppcheck-suppress unusedFunction void setup() { Serial.begin(115200); @@ -76,11 +81,15 @@ void setup() { WiFi.mode(WIFI_OFF); btStop(); - if (Config.beacon.button_tx) { + if (Config.button.tx) { // attach TX action to user button (defined by BUTTON_PIN) userButton.attachClick(handle_tx_click); } + if (Config.button.alt_message) { + userButton.attachLongPressStart(handle_next_beacon); + } + logPrintlnI("Smart Beacon is " + getSmartBeaconState()); show_display("INFO", "Smart Beacon is " + getSmartBeaconState(), 1000); logPrintlnI("setup done..."); @@ -124,7 +133,7 @@ void loop() { rate_limit_message_text = 0; } else { // enforce message text every n's Config.beacon.timeout frame - if (Config.beacon.timeout * rate_limit_message_text > 30) { + if (Config.GetCurrentBeacon().timeout * rate_limit_message_text > 30) { rate_limit_message_text = 0; } } @@ -178,14 +187,16 @@ void loop() { if (send_update && gps_loc_update) { send_update = false; - nextBeaconTimeStamp = now() + (Config.smart_beacon.active ? Config.smart_beacon.slow_rate : (Config.beacon.timeout * SECS_PER_MIN)); + nextBeaconTimeStamp = now() + (Config.smart_beacon.active ? Config.smart_beacon.slow_rate : (Config.GetCurrentBeacon().timeout * SECS_PER_MIN)); APRSMessage msg; String lat; String lng; String dao; - msg.setSource(Config.callsign); + Configuration::Beacon beacon = Config.GetCurrentBeacon(); + + msg.setSource(beacon.callsign); msg.setDestination("APLT00-1"); if (!Config.enhance_precision) { @@ -232,13 +243,13 @@ void loop() { } String aprsmsg; - aprsmsg = "!" + lat + Config.beacon.overlay + lng + Config.beacon.symbol + course_and_speed + alt; + aprsmsg = "!" + lat + beacon.overlay + lng + beacon.symbol + course_and_speed + alt; // message_text every 10's packet (i.e. if we have beacon rate 1min at high // speed -> every 10min). May be enforced above (at expirey of smart beacon // rate (i.e. every 30min), or every third packet on static rate (i.e. // static rate 10 -> every third packet) if (!(rate_limit_message_text++ % 10)) { - aprsmsg += Config.beacon.message; + aprsmsg += beacon.message; } if (BatteryIsConnected) { aprsmsg += " - _Bat.: " + batteryVoltage + "V - Cur.: " + batteryChargeCurrent + "mA"; @@ -282,7 +293,8 @@ void loop() { } if (gps_time_update) { - show_display(Config.callsign, createDateString(now()) + " " + createTimeString(now()), String("Sats: ") + gps.satellites.value() + " HDOP: " + gps.hdop.hdop(), String("Nxt Bcn: ") + (Config.smart_beacon.active ? "~" : "") + createTimeString(nextBeaconTimeStamp), BatteryIsConnected ? (String("Bat: ") + batteryVoltage + "V, " + batteryChargeCurrent + "mA") : "Powered via USB", String("Smart Beacon: " + getSmartBeaconState())); + + show_display(Config.GetCurrentBeacon().callsign, createDateString(now()) + " " + createTimeString(now()), String("Sats: ") + gps.satellites.value() + " HDOP: " + gps.hdop.hdop(), String("Nxt Bcn: ") + (Config.smart_beacon.active ? "~" : "") + createTimeString(nextBeaconTimeStamp), BatteryIsConnected ? (String("Bat: ") + batteryVoltage + "V, " + batteryChargeCurrent + "mA") : "Powered via USB", String("Smart Beacon: " + getSmartBeaconState())); if (Config.smart_beacon.active) { // Change the Tx internal based on the current speed @@ -316,7 +328,7 @@ void loop() { void load_config() { ConfigurationManagement confmg("/tracker.json"); Config = confmg.readConfiguration(); - if (Config.callsign == "NOCALL-10") { + if (Config.GetCurrentBeacon().callsign == "NOCALL-10") { logPrintlnE("You have to change your settings in 'data/tracker.json' and " "upload it via \"Upload File System image\"!"); show_display("ERROR", "You have to change your settings in 'data/tracker.json' and " diff --git a/src/configuration.cpp b/src/configuration.cpp index 3534698..52945a5 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -33,19 +33,26 @@ Configuration ConfigurationManagement::readConfiguration() { file.close(); Configuration conf; - if (data.containsKey("callsign")) - conf.callsign = data["callsign"].as(); + conf.debug = data["debug"] | false; conf.enhance_precision = data["enhance_precision"] | false; - if (data.containsKey("beacon") && data["beacon"].containsKey("message")) - conf.beacon.message = data["beacon"]["message"].as(); - conf.beacon.timeout = data["beacon"]["timeout"] | 1; - if (data.containsKey("beacon") && data["beacon"].containsKey("symbol")) - conf.beacon.symbol = data["beacon"]["symbol"].as(); - if (data.containsKey("beacon") && data["beacon"].containsKey("overlay")) - conf.beacon.overlay = data["beacon"]["overlay"].as(); - if (data.containsKey("beacon") && data["beacon"].containsKey("button_tx")) - conf.beacon.button_tx = data["beacon"]["button_tx"] | false; + + JsonArray beacons = data["beacons"].as(); + for (JsonVariant v : beacons) { + Configuration::Beacon beacon; + + if (v.containsKey("callsign")) + beacon.callsign = v["callsign"].as(); + if (v.containsKey("message")) + beacon.message = v["message"].as(); + beacon.timeout = v["timeout"] | 1; + if (v.containsKey("symbol")) + beacon.symbol = v["symbol"].as(); + if (v.containsKey("overlay")) + beacon.overlay = v["overlay"].as(); + + conf.beacons.push_back(beacon); + } conf.smart_beacon.active = data["smart_beacon"]["active"] | false; conf.smart_beacon.turn_min = data["smart_beacon"]["turn_min"] | 25; @@ -55,6 +62,9 @@ Configuration ConfigurationManagement::readConfiguration() { conf.smart_beacon.fast_speed = data["smart_beacon"]["fast_speed"] | 100; conf.smart_beacon.min_tx_dist = data["smart_beacon"]["min_tx_dist"] | 100; conf.smart_beacon.min_bcn = data["smart_beacon"]["min_bcn"] | 5; + + conf.button.tx = data["button"]["tx"] | false; + conf.button.alt_message = data["button"]["alt_message"] | false; conf.lora.frequencyRx = data["lora"]["frequency_rx"] | 433775000; conf.lora.frequencyTx = data["lora"]["frequency_tx"] | 433775000; @@ -81,14 +91,22 @@ void ConfigurationManagement::writeConfiguration(Configuration conf) { } DynamicJsonDocument data(2048); - data["callsign"] = conf.callsign; + JsonArray beacons = data.createNestedArray("beacons"); + for (Configuration::Beacon beacon : conf.beacons) { + JsonObject v = beacons.createNestedObject(); + v["callsign"] = beacon.callsign; + v["message"] = beacon.message; + v["timeout"] = beacon.timeout; + v["symbol"] = beacon.symbol; + v["overlay"] = beacon.overlay; + } + data["debug"] = conf.debug; data["enhance_precision"] = conf.enhance_precision; - data["beacon"]["message"] = conf.beacon.message; - data["beacon"]["timeout"] = conf.beacon.timeout; - data["beacon"]["symbol"] = conf.beacon.symbol; - data["beacon"]["overlay"] = conf.beacon.overlay; - data["beacon"]["button_tx"] = conf.beacon.button_tx; + + data["button"]["tx"] = conf.button.tx; + data["button"]["alt_message"] = conf.button.alt_message; + data["smart_beacon"]["active"] = conf.smart_beacon.active; data["smart_beacon"]["turn_min"] = conf.smart_beacon.turn_min; data["smart_beacon"]["slow_rate"] = conf.smart_beacon.slow_rate; @@ -114,3 +132,16 @@ void ConfigurationManagement::writeConfiguration(Configuration conf) { serializeJson(data, file); file.close(); } + +Configuration::Beacon Configuration::GetCurrentBeacon() { + auto iterator = this->beacons.begin(); + std::advance(iterator, this->current_beacon_index); + return *iterator; +} + +Configuration::Beacon Configuration::SetNextBeacon() { + this->current_beacon_index++; + if (this->current_beacon_index >= this->beacons.size()) + this->current_beacon_index = 0; + return this->GetCurrentBeacon(); +} \ No newline at end of file diff --git a/src/configuration.h b/src/configuration.h index affa0cd..2aca0fe 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H_ #include +#include #include @@ -9,14 +10,14 @@ class Configuration { public: class Beacon { public: - Beacon() : message("LoRa Tracker, Info: github.com/lora-aprs/LoRa_APRS_Tracker"), timeout(1), symbol("["), overlay("/"), button_tx(false) { + Beacon() : callsign("NOCALL-10"), message("LoRa Tracker, Info: github.com/lora-aprs/LoRa_APRS_Tracker"), timeout(1), symbol("["), overlay("/") { } + String callsign; String message; int timeout; String symbol; String overlay; - bool button_tx; }; class Smart_Beacon { @@ -59,15 +60,28 @@ public: bool reverse; }; - Configuration() : callsign("NOCALL-10"), debug(false), enhance_precision(true){}; + class Button { + public: + Button() : tx(false), alt_message(false) { + } - String callsign; - bool debug; - bool enhance_precision; - Beacon beacon; - Smart_Beacon smart_beacon; - LoRa lora; - PTT ptt; + bool tx; + int alt_message; + }; + + Configuration() : debug(false), enhance_precision(true), current_beacon_index(0) {}; + + bool debug; + bool enhance_precision; + std::list beacons; + int current_beacon_index; + Smart_Beacon smart_beacon; + LoRa lora; + PTT ptt; + Button button; + + Beacon GetCurrentBeacon(); + Beacon SetNextBeacon(); }; class ConfigurationManagement { From df89fab51eb5fb15133b71b71b87340aedb31c3b Mon Sep 17 00:00:00 2001 From: Tekk Date: Sat, 4 Dec 2021 04:27:24 +0100 Subject: [PATCH 2/6] Cleanup code --- src/LoRa_APRS_Tracker.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index 9a7bf42..266133c 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -187,15 +187,15 @@ void loop() { if (send_update && gps_loc_update) { send_update = false; - nextBeaconTimeStamp = now() + (Config.smart_beacon.active ? Config.smart_beacon.slow_rate : (Config.GetCurrentBeacon().timeout * SECS_PER_MIN)); + Configuration::Beacon beacon = Config.GetCurrentBeacon(); + + nextBeaconTimeStamp = now() + (Config.smart_beacon.active ? Config.smart_beacon.slow_rate : (beacon.timeout * SECS_PER_MIN)); APRSMessage msg; String lat; String lng; String dao; - Configuration::Beacon beacon = Config.GetCurrentBeacon(); - msg.setSource(beacon.callsign); msg.setDestination("APLT00-1"); From 6f2fd7e95943d736257e41b89fa7e2a4a1806a31 Mon Sep 17 00:00:00 2001 From: Tekk Date: Sat, 4 Dec 2021 08:16:31 +0100 Subject: [PATCH 3/6] Fixed tracker.json indent --- data/tracker.json | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/data/tracker.json b/data/tracker.json index ab35abe..7cd83dd 100644 --- a/data/tracker.json +++ b/data/tracker.json @@ -1,7 +1,8 @@ { "debug": false, "enhance_precision": true, - "beacons": [ + "beacons": + [ { "callsign": "NOCALL-7", "message": "LoRa Tracker", @@ -10,11 +11,13 @@ "overlay": "/" } ], - "button": { + "button": + { "tx": true, "alt_message": true }, - "smart_beacon": { + "smart_beacon": + { "active": true, "turn_min": 25, "slow_rate": 300, @@ -22,9 +25,10 @@ "fast_rate": 60, "fast_speed": 100, "min_tx_dist": 100, - "min_bcn": 5, + "min_bcn": 5 }, - "lora": { + "lora": + { "frequency_rx": 433775000, "frequency_tx": 433775000, "power": 20, @@ -32,7 +36,8 @@ "signal_bandwidth": 125000, "coding_rate4": 5 }, - "ptt_output": { + "ptt_output": + { "active": false, "io_pin": 4, "start_delay": 0, From a87c3ddb766b8b8b87549489f4e29e5a307ae768 Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Sun, 19 Dec 2021 22:27:29 +0100 Subject: [PATCH 4/6] add path --- data/tracker.json | 18 +++++++----------- src/LoRa_APRS_Tracker.cpp | 3 ++- src/configuration.cpp | 19 +++++++++++-------- src/configuration.h | 23 ++++++++++++----------- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/data/tracker.json b/data/tracker.json index 7cd83dd..87a2715 100644 --- a/data/tracker.json +++ b/data/tracker.json @@ -1,23 +1,21 @@ { "debug": false, "enhance_precision": true, - "beacons": - [ + "beacons": [ { "callsign": "NOCALL-7", + "path": "WIDE1-1", "message": "LoRa Tracker", "timeout": 1, "symbol": "[", "overlay": "/" } ], - "button": - { + "button": { "tx": true, "alt_message": true }, - "smart_beacon": - { + "smart_beacon": { "active": true, "turn_min": 25, "slow_rate": 300, @@ -27,8 +25,7 @@ "min_tx_dist": 100, "min_bcn": 5 }, - "lora": - { + "lora": { "frequency_rx": 433775000, "frequency_tx": 433775000, "power": 20, @@ -36,12 +33,11 @@ "signal_bandwidth": 125000, "coding_rate4": 5 }, - "ptt_output": - { + "ptt_output": { "active": false, "io_pin": 4, "start_delay": 0, "end_delay": 0, "reverse": false } -} \ No newline at end of file +} diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index 266133c..9a265ed 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -186,7 +186,7 @@ void loop() { } if (send_update && gps_loc_update) { - send_update = false; + send_update = false; Configuration::Beacon beacon = Config.GetCurrentBeacon(); nextBeaconTimeStamp = now() + (Config.smart_beacon.active ? Config.smart_beacon.slow_rate : (beacon.timeout * SECS_PER_MIN)); @@ -197,6 +197,7 @@ void loop() { String dao; msg.setSource(beacon.callsign); + msg.setPath(beacon.path); msg.setDestination("APLT00-1"); if (!Config.enhance_precision) { diff --git a/src/configuration.cpp b/src/configuration.cpp index 52945a5..660c18f 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -43,6 +43,8 @@ Configuration ConfigurationManagement::readConfiguration() { if (v.containsKey("callsign")) beacon.callsign = v["callsign"].as(); + if (v.containsKey("path")) + beacon.path = v["path"].as(); if (v.containsKey("message")) beacon.message = v["message"].as(); beacon.timeout = v["timeout"] | 1; @@ -62,9 +64,9 @@ Configuration ConfigurationManagement::readConfiguration() { conf.smart_beacon.fast_speed = data["smart_beacon"]["fast_speed"] | 100; conf.smart_beacon.min_tx_dist = data["smart_beacon"]["min_tx_dist"] | 100; conf.smart_beacon.min_bcn = data["smart_beacon"]["min_bcn"] | 5; - - conf.button.tx = data["button"]["tx"] | false; - conf.button.alt_message = data["button"]["alt_message"] | false; + + conf.button.tx = data["button"]["tx"] | false; + conf.button.alt_message = data["button"]["alt_message"] | false; conf.lora.frequencyRx = data["lora"]["frequency_rx"] | 433775000; conf.lora.frequencyTx = data["lora"]["frequency_tx"] | 433775000; @@ -95,17 +97,18 @@ void ConfigurationManagement::writeConfiguration(Configuration conf) { for (Configuration::Beacon beacon : conf.beacons) { JsonObject v = beacons.createNestedObject(); v["callsign"] = beacon.callsign; + v["path"] = beacon.path; v["message"] = beacon.message; v["timeout"] = beacon.timeout; v["symbol"] = beacon.symbol; v["overlay"] = beacon.overlay; } - data["debug"] = conf.debug; - data["enhance_precision"] = conf.enhance_precision; + data["debug"] = conf.debug; + data["enhance_precision"] = conf.enhance_precision; - data["button"]["tx"] = conf.button.tx; - data["button"]["alt_message"] = conf.button.alt_message; + data["button"]["tx"] = conf.button.tx; + data["button"]["alt_message"] = conf.button.alt_message; data["smart_beacon"]["active"] = conf.smart_beacon.active; data["smart_beacon"]["turn_min"] = conf.smart_beacon.turn_min; @@ -144,4 +147,4 @@ Configuration::Beacon Configuration::SetNextBeacon() { if (this->current_beacon_index >= this->beacons.size()) this->current_beacon_index = 0; return this->GetCurrentBeacon(); -} \ No newline at end of file +} diff --git a/src/configuration.h b/src/configuration.h index 2aca0fe..1653cb0 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -1,8 +1,8 @@ #ifndef CONFIGURATION_H_ #define CONFIGURATION_H_ -#include #include +#include #include @@ -10,10 +10,11 @@ class Configuration { public: class Beacon { public: - Beacon() : callsign("NOCALL-10"), message("LoRa Tracker, Info: github.com/lora-aprs/LoRa_APRS_Tracker"), timeout(1), symbol("["), overlay("/") { + Beacon() : callsign("NOCALL-10"), path("WIDE1-1"), message("LoRa Tracker"), timeout(1), symbol("["), overlay("/") { } String callsign; + String path; String message; int timeout; String symbol; @@ -69,16 +70,16 @@ public: int alt_message; }; - Configuration() : debug(false), enhance_precision(true), current_beacon_index(0) {}; + Configuration() : debug(false), enhance_precision(true), current_beacon_index(0){}; - bool debug; - bool enhance_precision; - std::list beacons; - int current_beacon_index; - Smart_Beacon smart_beacon; - LoRa lora; - PTT ptt; - Button button; + bool debug; + bool enhance_precision; + std::list beacons; + int current_beacon_index; + Smart_Beacon smart_beacon; + LoRa lora; + PTT ptt; + Button button; Beacon GetCurrentBeacon(); Beacon SetNextBeacon(); From 7120010a83c00f0d916e4ef2d84806748e82825d Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Sun, 19 Dec 2021 23:41:10 +0100 Subject: [PATCH 5/6] update tracker config --- data/tracker.json | 24 ++++++++-------- src/BeaconManager.cpp | 23 +++++++++++++++ src/BeaconManager.h | 20 +++++++++++++ src/LoRa_APRS_Tracker.cpp | 55 ++++++++++++++++++------------------ src/configuration.cpp | 59 ++++++++++++++++----------------------- src/configuration.h | 56 +++++++++++++++++-------------------- 6 files changed, 133 insertions(+), 104 deletions(-) create mode 100644 src/BeaconManager.cpp create mode 100644 src/BeaconManager.h diff --git a/data/tracker.json b/data/tracker.json index 87a2715..174af42 100644 --- a/data/tracker.json +++ b/data/tracker.json @@ -1,6 +1,5 @@ { "debug": false, - "enhance_precision": true, "beacons": [ { "callsign": "NOCALL-7", @@ -8,23 +7,24 @@ "message": "LoRa Tracker", "timeout": 1, "symbol": "[", - "overlay": "/" + "overlay": "/", + "smart_beacon": { + "active": true, + "turn_min": 25, + "slow_rate": 300, + "slow_speed": 10, + "fast_rate": 60, + "fast_speed": 100, + "min_tx_dist": 100, + "min_bcn": 5 + }, + "enhance_precision": true } ], "button": { "tx": true, "alt_message": true }, - "smart_beacon": { - "active": true, - "turn_min": 25, - "slow_rate": 300, - "slow_speed": 10, - "fast_rate": 60, - "fast_speed": 100, - "min_tx_dist": 100, - "min_bcn": 5 - }, "lora": { "frequency_rx": 433775000, "frequency_tx": 433775000, diff --git a/src/BeaconManager.cpp b/src/BeaconManager.cpp new file mode 100644 index 0000000..97d537b --- /dev/null +++ b/src/BeaconManager.cpp @@ -0,0 +1,23 @@ +#include "BeaconManager.h" + +BeaconManager::BeaconManager() : _currentBeaconConfig(_beacon_config.end()) { +} + +// cppcheck-suppress unusedFunction +void BeaconManager::loadConfig(const std::list &beacon_config) { + _beacon_config = beacon_config; + _currentBeaconConfig = _beacon_config.begin(); +} + +// cppcheck-suppress unusedFunction +std::list::iterator BeaconManager::getCurrentBeaconConfig() const { + return _currentBeaconConfig; +} + +// cppcheck-suppress unusedFunction +void BeaconManager::loadNextBeacon() { + ++_currentBeaconConfig; + if (_currentBeaconConfig == _beacon_config.end()) { + _currentBeaconConfig = _beacon_config.begin(); + } +} diff --git a/src/BeaconManager.h b/src/BeaconManager.h new file mode 100644 index 0000000..702f499 --- /dev/null +++ b/src/BeaconManager.h @@ -0,0 +1,20 @@ +#ifndef BEACON_MANAGER_H_ +#define BEACON_MANAGER_H_ + +#include "configuration.h" + +class BeaconManager { +public: + BeaconManager(); + + void loadConfig(const std::list &beacon_config); + + std::list::iterator getCurrentBeaconConfig() const; + void loadNextBeacon(); + +private: + std::list _beacon_config; + std::list::iterator _currentBeaconConfig; +}; + +#endif diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index 8acedf4..361b82a 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -7,12 +7,14 @@ #include #include +#include "BeaconManager.h" #include "configuration.h" #include "display.h" #include "pins.h" #include "power_management.h" Configuration Config; +BeaconManager BeaconMan; PowerManagement powerManagement; OneButton userButton = OneButton(BUTTON_PIN, true, true); @@ -41,8 +43,8 @@ static void handle_tx_click() { } static void handle_next_beacon() { - Configuration::Beacon beacon = Config.SetNextBeacon(); - show_display(beacon.callsign, beacon.message, 2000); + BeaconMan.loadNextBeacon(); + show_display(BeaconMan.getCurrentBeaconConfig()->callsign, BeaconMan.getCurrentBeaconConfig()->message, 2000); } // cppcheck-suppress unusedFunction @@ -127,13 +129,13 @@ void loop() { if (gps_loc_update && nextBeaconTimeStamp <= now()) { send_update = true; - if (Config.smart_beacon.active) { + if (BeaconMan.getCurrentBeaconConfig()->smart_beacon.active) { currentHeading = gps.course.deg(); // enforce message text on slowest Config.smart_beacon.slow_rate rate_limit_message_text = 0; } else { // enforce message text every n's Config.beacon.timeout frame - if (Config.GetCurrentBeacon().timeout * rate_limit_message_text > 30) { + if (BeaconMan.getCurrentBeaconConfig()->timeout * rate_limit_message_text > 30) { rate_limit_message_text = 0; } } @@ -160,7 +162,7 @@ void loop() { } #endif - if (!send_update && gps_loc_update && Config.smart_beacon.active) { + if (!send_update && gps_loc_update && BeaconMan.getCurrentBeaconConfig()->smart_beacon.active) { uint32_t lastTx = millis() - lastTxTime; currentHeading = gps.course.deg(); lastTxdistance = TinyGPSPlus::distanceBetween(gps.location.lat(), gps.location.lng(), lastTxLat, lastTxLng); @@ -176,9 +178,9 @@ void loop() { // Get headings and heading delta double headingDelta = abs(previousHeading - currentHeading); - if (lastTx > Config.smart_beacon.min_bcn * 1000) { + if (lastTx > BeaconMan.getCurrentBeaconConfig()->smart_beacon.min_bcn * 1000) { // Check for heading more than 25 degrees - if (headingDelta > Config.smart_beacon.turn_min && lastTxdistance > Config.smart_beacon.min_tx_dist) { + if (headingDelta > BeaconMan.getCurrentBeaconConfig()->smart_beacon.turn_min && lastTxdistance > BeaconMan.getCurrentBeaconConfig()->smart_beacon.min_tx_dist) { send_update = true; } } @@ -186,22 +188,20 @@ void loop() { } if (send_update && gps_loc_update) { - send_update = false; - Configuration::Beacon beacon = Config.GetCurrentBeacon(); + send_update = false; - nextBeaconTimeStamp = now() + (Config.smart_beacon.active ? Config.smart_beacon.slow_rate : (beacon.timeout * SECS_PER_MIN)); + nextBeaconTimeStamp = now() + (BeaconMan.getCurrentBeaconConfig()->smart_beacon.active ? BeaconMan.getCurrentBeaconConfig()->smart_beacon.slow_rate : (BeaconMan.getCurrentBeaconConfig()->timeout * SECS_PER_MIN)); APRSMessage msg; String lat; String lng; String dao; - msg.setSource(beacon.callsign); - msg.setPath(beacon.path); + msg.setSource(BeaconMan.getCurrentBeaconConfig()->callsign); + msg.setPath(BeaconMan.getCurrentBeaconConfig()->path); msg.setDestination("APLT00-1"); - msg.setPath(Config.beacon.path); - if (!Config.enhance_precision) { + if (!BeaconMan.getCurrentBeaconConfig()->enhance_precision) { lat = create_lat_aprs(gps.location.rawLat()); lng = create_long_aprs(gps.location.rawLng()); } else { @@ -245,19 +245,19 @@ void loop() { } String aprsmsg; - aprsmsg = "!" + lat + beacon.overlay + lng + beacon.symbol + course_and_speed + alt; + aprsmsg = "!" + lat + BeaconMan.getCurrentBeaconConfig()->overlay + lng + BeaconMan.getCurrentBeaconConfig()->symbol + course_and_speed + alt; // message_text every 10's packet (i.e. if we have beacon rate 1min at high // speed -> every 10min). May be enforced above (at expirey of smart beacon // rate (i.e. every 30min), or every third packet on static rate (i.e. // static rate 10 -> every third packet) if (!(rate_limit_message_text++ % 10)) { - aprsmsg += beacon.message; + aprsmsg += BeaconMan.getCurrentBeaconConfig()->message; } if (BatteryIsConnected) { aprsmsg += " - _Bat.: " + batteryVoltage + "V - Cur.: " + batteryChargeCurrent + "mA"; } - if (Config.enhance_precision) { + if (BeaconMan.getCurrentBeaconConfig()->enhance_precision) { aprsmsg += " " + dao; } @@ -280,7 +280,7 @@ void loop() { LoRa.write((const uint8_t *)data.c_str(), data.length()); LoRa.endPacket(); - if (Config.smart_beacon.active) { + if (BeaconMan.getCurrentBeaconConfig()->smart_beacon.active) { lastTxLat = gps.location.lat(); lastTxLng = gps.location.lng(); previousHeading = currentHeading; @@ -296,15 +296,15 @@ void loop() { if (gps_time_update) { - show_display(Config.GetCurrentBeacon().callsign, createDateString(now()) + " " + createTimeString(now()), String("Sats: ") + gps.satellites.value() + " HDOP: " + gps.hdop.hdop(), String("Nxt Bcn: ") + (Config.smart_beacon.active ? "~" : "") + createTimeString(nextBeaconTimeStamp), BatteryIsConnected ? (String("Bat: ") + batteryVoltage + "V, " + batteryChargeCurrent + "mA") : "Powered via USB", String("Smart Beacon: " + getSmartBeaconState())); + show_display(BeaconMan.getCurrentBeaconConfig()->callsign, createDateString(now()) + " " + createTimeString(now()), String("Sats: ") + gps.satellites.value() + " HDOP: " + gps.hdop.hdop(), String("Nxt Bcn: ") + (BeaconMan.getCurrentBeaconConfig()->smart_beacon.active ? "~" : "") + createTimeString(nextBeaconTimeStamp), BatteryIsConnected ? (String("Bat: ") + batteryVoltage + "V, " + batteryChargeCurrent + "mA") : "Powered via USB", String("Smart Beacon: " + getSmartBeaconState())); - if (Config.smart_beacon.active) { + if (BeaconMan.getCurrentBeaconConfig()->smart_beacon.active) { // Change the Tx internal based on the current speed int curr_speed = (int)gps.speed.kmph(); - if (curr_speed < Config.smart_beacon.slow_speed) { - txInterval = Config.smart_beacon.slow_rate * 1000; - } else if (curr_speed > Config.smart_beacon.fast_speed) { - txInterval = Config.smart_beacon.fast_rate * 1000; + if (curr_speed < BeaconMan.getCurrentBeaconConfig()->smart_beacon.slow_speed) { + txInterval = BeaconMan.getCurrentBeaconConfig()->smart_beacon.slow_rate * 1000; + } else if (curr_speed > BeaconMan.getCurrentBeaconConfig()->smart_beacon.fast_speed) { + txInterval = BeaconMan.getCurrentBeaconConfig()->smart_beacon.fast_rate * 1000; } else { /* Interval inbetween low and high speed min(slow_rate, ..) because: if slow rate is 300s at slow speed <= @@ -316,7 +316,7 @@ void loop() { would lead to decrease of beacon rate in between 5 to 20 km/h. what is even below the slow speed rate. */ - txInterval = min(Config.smart_beacon.slow_rate, Config.smart_beacon.fast_speed * Config.smart_beacon.fast_rate / curr_speed) * 1000; + txInterval = min(BeaconMan.getCurrentBeaconConfig()->smart_beacon.slow_rate, BeaconMan.getCurrentBeaconConfig()->smart_beacon.fast_speed * BeaconMan.getCurrentBeaconConfig()->smart_beacon.fast_rate / curr_speed) * 1000; } } } @@ -331,7 +331,8 @@ void loop() { void load_config() { ConfigurationManagement confmg("/tracker.json"); Config = confmg.readConfiguration(); - if (Config.GetCurrentBeacon().callsign == "NOCALL-10") { + BeaconMan.loadConfig(Config.beacons); + if (BeaconMan.getCurrentBeaconConfig()->callsign == "NOCALL-10") { logPrintlnE("You have to change your settings in 'data/tracker.json' and " "upload it via \"Upload File System image\"!"); show_display("ERROR", "You have to change your settings in 'data/tracker.json' and " @@ -476,7 +477,7 @@ String createTimeString(time_t t) { } String getSmartBeaconState() { - if (Config.smart_beacon.active) { + if (BeaconMan.getCurrentBeaconConfig()->smart_beacon.active) { return "On"; } return "Off"; diff --git a/src/configuration.cpp b/src/configuration.cpp index 660c18f..e69d263 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -34,8 +34,7 @@ Configuration ConfigurationManagement::readConfiguration() { Configuration conf; - conf.debug = data["debug"] | false; - conf.enhance_precision = data["enhance_precision"] | false; + conf.debug = data["debug"] | false; JsonArray beacons = data["beacons"].as(); for (JsonVariant v : beacons) { @@ -53,18 +52,20 @@ Configuration ConfigurationManagement::readConfiguration() { if (v.containsKey("overlay")) beacon.overlay = v["overlay"].as(); + beacon.smart_beacon.active = v["smart_beacon"]["active"] | false; + beacon.smart_beacon.turn_min = v["smart_beacon"]["turn_min"] | 25; + beacon.smart_beacon.slow_rate = v["smart_beacon"]["slow_rate"] | 300; + beacon.smart_beacon.slow_speed = v["smart_beacon"]["slow_speed"] | 10; + beacon.smart_beacon.fast_rate = v["smart_beacon"]["fast_rate"] | 60; + beacon.smart_beacon.fast_speed = v["smart_beacon"]["fast_speed"] | 100; + beacon.smart_beacon.min_tx_dist = v["smart_beacon"]["min_tx_dist"] | 100; + beacon.smart_beacon.min_bcn = v["smart_beacon"]["min_bcn"] | 5; + + beacon.enhance_precision = v["enhance_precision"] | false; + conf.beacons.push_back(beacon); } - conf.smart_beacon.active = data["smart_beacon"]["active"] | false; - conf.smart_beacon.turn_min = data["smart_beacon"]["turn_min"] | 25; - conf.smart_beacon.slow_rate = data["smart_beacon"]["slow_rate"] | 300; - conf.smart_beacon.slow_speed = data["smart_beacon"]["slow_speed"] | 10; - conf.smart_beacon.fast_rate = data["smart_beacon"]["fast_rate"] | 60; - conf.smart_beacon.fast_speed = data["smart_beacon"]["fast_speed"] | 100; - conf.smart_beacon.min_tx_dist = data["smart_beacon"]["min_tx_dist"] | 100; - conf.smart_beacon.min_bcn = data["smart_beacon"]["min_bcn"] | 5; - conf.button.tx = data["button"]["tx"] | false; conf.button.alt_message = data["button"]["alt_message"] | false; @@ -102,23 +103,24 @@ void ConfigurationManagement::writeConfiguration(Configuration conf) { v["timeout"] = beacon.timeout; v["symbol"] = beacon.symbol; v["overlay"] = beacon.overlay; + + v["smart_beacon"]["active"] = beacon.smart_beacon.active; + v["smart_beacon"]["turn_min"] = beacon.smart_beacon.turn_min; + v["smart_beacon"]["slow_rate"] = beacon.smart_beacon.slow_rate; + v["smart_beacon"]["slow_speed"] = beacon.smart_beacon.slow_speed; + v["smart_beacon"]["fast_rate"] = beacon.smart_beacon.fast_rate; + v["smart_beacon"]["fast_speed"] = beacon.smart_beacon.fast_speed; + v["smart_beacon"]["min_tx_dist"] = beacon.smart_beacon.min_tx_dist; + v["smart_beacon"]["min_bcn"] = beacon.smart_beacon.min_bcn; + + v["enhance_precision"] = beacon.enhance_precision; } - data["debug"] = conf.debug; - data["enhance_precision"] = conf.enhance_precision; + data["debug"] = conf.debug; data["button"]["tx"] = conf.button.tx; data["button"]["alt_message"] = conf.button.alt_message; - data["smart_beacon"]["active"] = conf.smart_beacon.active; - data["smart_beacon"]["turn_min"] = conf.smart_beacon.turn_min; - data["smart_beacon"]["slow_rate"] = conf.smart_beacon.slow_rate; - data["smart_beacon"]["slow_speed"] = conf.smart_beacon.slow_speed; - data["smart_beacon"]["fast_rate"] = conf.smart_beacon.fast_rate; - data["smart_beacon"]["fast_speed"] = conf.smart_beacon.fast_speed; - data["smart_beacon"]["min_tx_dist"] = conf.smart_beacon.min_tx_dist; - data["smart_beacon"]["min_bcn"] = conf.smart_beacon.min_bcn; - data["lora"]["frequency_rx"] = conf.lora.frequencyRx; data["lora"]["frequency_tx"] = conf.lora.frequencyTx; data["lora"]["power"] = conf.lora.power; @@ -135,16 +137,3 @@ void ConfigurationManagement::writeConfiguration(Configuration conf) { serializeJson(data, file); file.close(); } - -Configuration::Beacon Configuration::GetCurrentBeacon() { - auto iterator = this->beacons.begin(); - std::advance(iterator, this->current_beacon_index); - return *iterator; -} - -Configuration::Beacon Configuration::SetNextBeacon() { - this->current_beacon_index++; - if (this->current_beacon_index >= this->beacons.size()) - this->current_beacon_index = 0; - return this->GetCurrentBeacon(); -} diff --git a/src/configuration.h b/src/configuration.h index c334578..c7dae47 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -10,31 +10,32 @@ class Configuration { public: class Beacon { public: - Beacon() : callsign("NOCALL-10"), path("WIDE1-1"), message("LoRa Tracker"), timeout(1), symbol("["), overlay("/") { + class Smart_Beacon { + public: + Smart_Beacon() : active(false), turn_min(25), slow_rate(300), slow_speed(10), fast_rate(60), fast_speed(100), min_tx_dist(100), min_bcn(5) { + } + + bool active; + int turn_min; + int slow_rate; + int slow_speed; + int fast_rate; + int fast_speed; + int min_tx_dist; + int min_bcn; + }; + + Beacon() : callsign("NOCALL-10"), path("WIDE1-1"), message("LoRa Tracker"), timeout(1), symbol("["), overlay("/"), enhance_precision(true) { } - String callsign; - String path; - String message; - String path; - int timeout; - String symbol; - String overlay; - }; - - class Smart_Beacon { - public: - Smart_Beacon() : active(false), turn_min(25), slow_rate(300), slow_speed(10), fast_rate(60), fast_speed(100), min_tx_dist(100), min_bcn(5) { - } - - bool active; - int turn_min; - int slow_rate; - int slow_speed; - int fast_rate; - int fast_speed; - int min_tx_dist; - int min_bcn; + String callsign; + String path; + String message; + int timeout; + String symbol; + String overlay; + Smart_Beacon smart_beacon; + bool enhance_precision; }; class LoRa { @@ -71,19 +72,14 @@ public: int alt_message; }; - Configuration() : debug(false), enhance_precision(true), current_beacon_index(0){}; + Configuration() : debug(false) { + } bool debug; - bool enhance_precision; std::list beacons; - int current_beacon_index; - Smart_Beacon smart_beacon; LoRa lora; PTT ptt; Button button; - - Beacon GetCurrentBeacon(); - Beacon SetNextBeacon(); }; class ConfigurationManagement { From 2dc2cb202a2300ea9434f7d535e4dd375fce3c88 Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Sun, 19 Dec 2021 23:41:30 +0100 Subject: [PATCH 6/6] ignore vscode file and set t-beam v1 as default --- .gitignore | 1 + platformio.ini | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 89cc49c..fb3ca12 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +.vscode/settings.json diff --git a/platformio.ini b/platformio.ini index a1880c0..b761817 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,3 +1,5 @@ +[platformio] +default_envs = ttgo-t-beam-v1 [env] platform = espressif32 @ 3.0.0