From 33e766a5ac889f237dfd9e07d73fc97e139f4d03 Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Mon, 4 Jan 2021 23:10:23 +0100 Subject: [PATCH] add TaskQueue and many other stuff :) --- lib/APRS-IS/APRS-IS.cpp | 8 +-- lib/APRS-IS/APRS-IS.h | 4 +- lib/LoRa_APRS/LoRa_APRS.cpp | 6 -- lib/LoRa_APRS/LoRa_APRS.h | 5 +- lib/SignalSlot/SignalSlot.h | 116 -------------------------------- lib/TaskManager/TaskManager.cpp | 6 +- lib/TaskManager/TaskManager.h | 21 ++++-- lib/TaskManager/TaskQueue.h | 33 +++++++++ platformio.ini | 2 +- src/LoRa_APRS_iGate.cpp | 21 +++--- src/Task.h | 12 ++++ src/TaskAprsIs.cpp | 11 ++- src/TaskAprsIs.h | 4 +- src/TaskEth.cpp | 5 +- src/TaskEth.h | 2 +- src/TaskFTP.cpp | 5 +- src/TaskFTP.h | 2 +- src/TaskLora.cpp | 25 +++++-- src/TaskLora.h | 3 +- src/TaskNTP.cpp | 5 +- src/TaskNTP.h | 2 +- src/TaskOTA.cpp | 5 +- src/TaskOTA.h | 2 +- src/TaskWifi.cpp | 5 +- src/TaskWifi.h | 2 +- src/display.h | 5 +- 26 files changed, 131 insertions(+), 186 deletions(-) delete mode 100644 lib/SignalSlot/SignalSlot.h create mode 100644 lib/TaskManager/TaskQueue.h create mode 100644 src/Task.h diff --git a/lib/APRS-IS/APRS-IS.cpp b/lib/APRS-IS/APRS-IS.cpp index 17c95cd..af34a7c 100644 --- a/lib/APRS-IS/APRS-IS.cpp +++ b/lib/APRS-IS/APRS-IS.cpp @@ -66,15 +66,10 @@ bool APRS_IS::sendMessage(const std::shared_ptr message) { return false; } - _client.println(message->encode()); + _client.println(message->encode() + "\n"); return true; } -void APRS_IS::action(std::shared_ptr elem, int rssi, float snr) -{ - sendMessage(elem); -} - int APRS_IS::available() { return _client.available(); @@ -108,6 +103,5 @@ std::shared_ptr APRS_IS::getAPRSMessage() } std::shared_ptr msg = std::shared_ptr(new APRSMessage()); msg->decode(line); - emit(msg); return msg; } diff --git a/lib/APRS-IS/APRS-IS.h b/lib/APRS-IS/APRS-IS.h index a7df11f..256df1e 100644 --- a/lib/APRS-IS/APRS-IS.h +++ b/lib/APRS-IS/APRS-IS.h @@ -2,11 +2,10 @@ #ifndef APRS_IS_Lib_h_ #define APRS_IS_Lib_h_ -#include #include #include -class APRS_IS : public Signal1>, public Slot3, int, float> +class APRS_IS { public: APRS_IS(const String & user, const String & passcode, const String & tool_name, const String & version); @@ -18,7 +17,6 @@ public: bool sendMessage(const String & message); bool sendMessage(const std::shared_ptr message); - void action(std::shared_ptr msg, int rssi, float snr) override; int available(); diff --git a/lib/LoRa_APRS/LoRa_APRS.cpp b/lib/LoRa_APRS/LoRa_APRS.cpp index 8f8a5a2..2deb45f 100644 --- a/lib/LoRa_APRS/LoRa_APRS.cpp +++ b/lib/LoRa_APRS/LoRa_APRS.cpp @@ -34,7 +34,6 @@ bool LoRa_APRS::checkMessage() } _LastReceivedMsg = std::shared_ptr(new APRSMessage()); _LastReceivedMsg->decode(str); - emit(_LastReceivedMsg, packetRssi(), packetSnr()); return true; } @@ -80,8 +79,3 @@ long LoRa_APRS::getTxFrequency() const { return _TxFrequency; } - -void LoRa_APRS::action(const std::shared_ptr elem) -{ - sendMessage(elem); -} diff --git a/lib/LoRa_APRS/LoRa_APRS.h b/lib/LoRa_APRS/LoRa_APRS.h index 71bad11..1991528 100644 --- a/lib/LoRa_APRS/LoRa_APRS.h +++ b/lib/LoRa_APRS/LoRa_APRS.h @@ -5,7 +5,6 @@ #include #include #include -#include #define LORA_RX_FREQUENCY (433775000) #define LORA_TX_FREQUENCY (433900000) @@ -13,7 +12,7 @@ #define LORA_SIGNAL_BANDWIDTH (125E3) #define LORA_CODING_RATE4 (5) -class LoRa_APRS : public LoRaClass, public Slot1>, public Signal3, int, float> +class LoRa_APRS : public LoRaClass { public: explicit LoRa_APRS(std::shared_ptr boardConfig); @@ -29,8 +28,6 @@ public: void setTxFrequency(long frequency); long getTxFrequency() const; - void action(const std::shared_ptr elem) override; - private: std::shared_ptr _LastReceivedMsg; long _RxFrequency; diff --git a/lib/SignalSlot/SignalSlot.h b/lib/SignalSlot/SignalSlot.h deleted file mode 100644 index 28c92b5..0000000 --- a/lib/SignalSlot/SignalSlot.h +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef SIGNAL_SLOT_H_ -#define SIGNAL_SLOT_H_ - -#include - -class Slot0 -{ -public: - virtual void action() = 0; -}; - -template -class Slot1 -{ -public: - virtual void action(T elem1) = 0; -}; - -template -class Slot2 -{ -public: - virtual void action(T elem1, H elem2) = 0; -}; - -template -class Slot3 -{ -public: - virtual void action(T elem1, H elem2, K elem3) = 0; -}; - -class Signal0 -{ -public: - void emit() - { - for(Slot0 * slot: _slots) - { - slot->action(); - } - } - - void connectSlot(Slot0 * slot) - { - _slots.push_back(slot); - } - -private: - std::list _slots; -}; - -template -class Signal1 -{ -public: - void emit(T elem1) - { - for(Slot1 * slot: _slots) - { - slot->action(elem1); - } - } - - void connectSlot(Slot1 * slot) - { - _slots.push_back(slot); - } - -private: - std::list *> _slots; -}; - -template -class Signal2 -{ -public: - void emit(T elem1, H elem2) - { - for(Slot2 * slot: _slots) - { - slot->action(elem1, elem2); - } - } - - void connectSlot(Slot2 * slot) - { - _slots.push_back(slot); - } - -private: - std::list *> _slots; -}; - -template -class Signal3 -{ -public: - void emit(T elem1, H elem2, K elem3) - { - for(Slot3 * slot: _slots) - { - slot->action(elem1, elem2, elem3); - } - } - - void connectSlot(Slot3 * slot) - { - _slots.push_back(slot); - } - -private: - std::list *> _slots; -}; - -#endif diff --git a/lib/TaskManager/TaskManager.cpp b/lib/TaskManager/TaskManager.cpp index 2043208..216712d 100644 --- a/lib/TaskManager/TaskManager.cpp +++ b/lib/TaskManager/TaskManager.cpp @@ -10,7 +10,7 @@ void TaskManager::addTask(std::shared_ptr task) _tasks.push_back(task); } -std::shared_ptr TaskManager::getTask(String & name) +std::shared_ptr TaskManager::getTask(const char * name) { for(std::shared_ptr & elem : _tasks) { @@ -22,14 +22,14 @@ std::shared_ptr TaskManager::getTask(String & name) return 0; } -bool TaskManager::setup(std::shared_ptr config) +bool TaskManager::setup(std::shared_ptr config, std::shared_ptr boardConfig) { logPrintlnV("will setup all tasks..."); for(std::shared_ptr & elem : _tasks) { logPrintW("call setup from "); logPrintlnW(elem->getName()); - if(!elem->setup(config)) + if(!elem->setup(config, boardConfig)) { return false; } diff --git a/lib/TaskManager/TaskManager.h b/lib/TaskManager/TaskManager.h index bbead2b..1894f9f 100644 --- a/lib/TaskManager/TaskManager.h +++ b/lib/TaskManager/TaskManager.h @@ -5,6 +5,9 @@ #include #include #include +#include + +#include "TaskQueue.h" class Task { @@ -15,7 +18,7 @@ public: String getName() const { return _name; } - virtual bool setup(std::shared_ptr config) = 0; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) = 0; virtual bool loop(std::shared_ptr config) = 0; private: @@ -25,17 +28,27 @@ private: class TaskManager { public: - TaskManager(); + + static TaskManager & instance() + { + static TaskManager _instance; + return _instance; + } + + ~TaskManager() {} void addTask(std::shared_ptr task); - std::shared_ptr getTask(String & name); + std::shared_ptr getTask(const char * name); - bool setup(std::shared_ptr config); + bool setup(std::shared_ptr config, std::shared_ptr boardConfig); bool loop(std::shared_ptr config); private: std::list> _tasks; + TaskManager(); + TaskManager(const TaskManager &); + TaskManager & operator = (const TaskManager &); }; #endif diff --git a/lib/TaskManager/TaskQueue.h b/lib/TaskManager/TaskQueue.h new file mode 100644 index 0000000..258e0de --- /dev/null +++ b/lib/TaskManager/TaskQueue.h @@ -0,0 +1,33 @@ +#ifndef TASK_QUEUE_H_ +#define TASK_QUEUE_H_ + +#include + +template +class TaskQueue +{ +public: + TaskQueue() {} + + void addElement(T elem) + { + _elements.push_back(elem); + } + + T getElement() + { + T elem = _elements.front(); + _elements.pop_front(); + return elem; + } + + bool empty() const + { + return _elements.empty(); + } + +private: + std::list _elements; +}; + +#endif diff --git a/platformio.ini b/platformio.ini index 03fd44c..7f00833 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,7 +8,7 @@ lib_deps = adafruit/Adafruit SSD1306 @ 2.4.0 bblanchon/ArduinoJson @ 6.17.0 lewisxhe/AXP202X_Library @ 1.1.2 - peterus/APRS-Decoder-Lib @ 0.0.5 + peterus/APRS-Decoder-Lib @ 0.0.6 peterus/esp-logger @ 0.0.1 peterus/ESP-FTP-Server-Lib @ 0.9.5 check_tool = cppcheck diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 2f57d00..ca69df7 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -24,7 +24,6 @@ String create_long_aprs(double lng); std::shared_ptr userConfig; std::shared_ptr boardConfig; -TaskManager taskManager; HardwareSerial Serial(0); // cppcheck-suppress unusedFunction @@ -92,20 +91,18 @@ void setup() load_config(boardConfig); - std::shared_ptr lora_task = std::shared_ptr(new LoraTask()); - lora_task->setup(userConfig, boardConfig); - taskManager.addTask(lora_task); + TaskManager::instance().addTask(std::shared_ptr(new LoraTask())); if(boardConfig->Type == eETH_BOARD) { - taskManager.addTask(std::shared_ptr(new EthTask())); + TaskManager::instance().addTask(std::shared_ptr(new EthTask())); } - taskManager.addTask(std::shared_ptr(new WifiTask())); - taskManager.addTask(std::shared_ptr(new OTATask())); - taskManager.addTask(std::shared_ptr(new NTPTask())); - taskManager.addTask(std::shared_ptr(new FTPTask())); - taskManager.addTask(std::shared_ptr(new AprsIsTask())); + TaskManager::instance().addTask(std::shared_ptr(new WifiTask())); + TaskManager::instance().addTask(std::shared_ptr(new OTATask())); + TaskManager::instance().addTask(std::shared_ptr(new NTPTask())); + TaskManager::instance().addTask(std::shared_ptr(new FTPTask())); + TaskManager::instance().addTask(std::shared_ptr(new AprsIsTask())); - taskManager.setup(userConfig); + TaskManager::instance().setup(userConfig, boardConfig); if(userConfig->display.overwritePin != 0) { @@ -120,7 +117,7 @@ void setup() // cppcheck-suppress unusedFunction void loop() { - taskManager.loop(userConfig); + TaskManager::instance().loop(userConfig); } String create_lat_aprs(double lat) diff --git a/src/Task.h b/src/Task.h new file mode 100644 index 0000000..770b84e --- /dev/null +++ b/src/Task.h @@ -0,0 +1,12 @@ +#ifndef TASK_H_ +#define TASK_H_ + +#define TASK_APRS_IS "AprsIsTask" +#define TASK_ETH "EthTask" +#define TASK_FTP "FTPTask" +#define TASK_LORA "LoraTask" +#define TASK_NTP "NTPTask" +#define TASK_OTA "OTATask" +#define TASK_WIFI "WifiTask" + +#endif diff --git a/src/TaskAprsIs.cpp b/src/TaskAprsIs.cpp index f962c78..2961358 100644 --- a/src/TaskAprsIs.cpp +++ b/src/TaskAprsIs.cpp @@ -2,12 +2,13 @@ #include #include "project_configuration.h" #include "TaskAprsIs.h" +#include "Task.h" String create_lat_aprs(double lat); String create_long_aprs(double lng); AprsIsTask::AprsIsTask() - : Task("AprsIsTask") + : Task(TASK_APRS_IS), _beacon_next_time(0) { } @@ -15,7 +16,7 @@ AprsIsTask::~AprsIsTask() { } -bool AprsIsTask::setup(std::shared_ptr config) +bool AprsIsTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { _aprs_is = std::shared_ptr(new APRS_IS(config->callsign, config->aprs_is.passcode , "ESP32-APRS-IS", "0.1")); connect(config); @@ -38,6 +39,12 @@ bool AprsIsTask::loop(std::shared_ptr config) } _aprs_is->getAPRSMessage(); + if(!inputQueue.empty()) + { + std::shared_ptr msg = inputQueue.getElement(); + _aprs_is->sendMessage(msg); + } + if(_beacon_next_time < now()) { //show_display(userConfig->callsign, "Beacon to APRS-IS Server..."); diff --git a/src/TaskAprsIs.h b/src/TaskAprsIs.h index cc9d413..3b393b4 100644 --- a/src/TaskAprsIs.h +++ b/src/TaskAprsIs.h @@ -11,9 +11,11 @@ public: AprsIsTask(); virtual ~AprsIsTask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; + TaskQueue> inputQueue; + private: std::shared_ptr _aprs_is; std::shared_ptr _beaconMsg; diff --git a/src/TaskEth.cpp b/src/TaskEth.cpp index ad6d231..201ff96 100644 --- a/src/TaskEth.cpp +++ b/src/TaskEth.cpp @@ -2,6 +2,7 @@ #include #include #include "TaskEth.h" +#include "Task.h" volatile bool eth_connected = false; @@ -42,7 +43,7 @@ static void WiFiEvent(WiFiEvent_t event) } EthTask::EthTask() - : Task("EthTask") + : Task(TASK_ETH) { } @@ -50,7 +51,7 @@ EthTask::~EthTask() { } -bool EthTask::setup(std::shared_ptr config) +bool EthTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { WiFi.onEvent(WiFiEvent); diff --git a/src/TaskEth.h b/src/TaskEth.h index ca684aa..a814495 100644 --- a/src/TaskEth.h +++ b/src/TaskEth.h @@ -9,7 +9,7 @@ public: EthTask(); virtual ~EthTask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; private: diff --git a/src/TaskFTP.cpp b/src/TaskFTP.cpp index b28e29e..0ddf27b 100644 --- a/src/TaskFTP.cpp +++ b/src/TaskFTP.cpp @@ -3,9 +3,10 @@ #include #include "project_configuration.h" #include "TaskFTP.h" +#include "Task.h" FTPTask::FTPTask() - : Task("FTPTask") + : Task(TASK_FTP) { } @@ -13,7 +14,7 @@ FTPTask::~FTPTask() { } -bool FTPTask::setup(std::shared_ptr config) +bool FTPTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { _ftpServer = std::shared_ptr(new FTPServer()); if(config->ftp.active) diff --git a/src/TaskFTP.h b/src/TaskFTP.h index 5db9edc..5bb83d2 100644 --- a/src/TaskFTP.h +++ b/src/TaskFTP.h @@ -10,7 +10,7 @@ public: FTPTask(); virtual ~FTPTask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; private: diff --git a/src/TaskLora.cpp b/src/TaskLora.cpp index 556c2ec..0f515c9 100644 --- a/src/TaskLora.cpp +++ b/src/TaskLora.cpp @@ -1,9 +1,12 @@ #include +#include #include "project_configuration.h" #include "TaskLora.h" +#include "TaskAprsIs.h" +#include "Task.h" LoraTask::LoraTask() - : Task("LoraTask") + : Task(TASK_LORA) { } @@ -11,11 +14,6 @@ LoraTask::~LoraTask() { } -bool LoraTask::setup(std::shared_ptr config) -{ - return true; -} - bool LoraTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { _lora_aprs = std::shared_ptr(new LoRa_APRS(boardConfig)); @@ -40,6 +38,19 @@ bool LoraTask::setup(std::shared_ptr config, std::shared_ptr config) { - _lora_aprs->checkMessage(); + if(_lora_aprs->checkMessage()) + { + std::shared_ptr msg = _lora_aprs->getMessage(); + //msg->getAPRSBody()->setData(msg->getAPRSBody()->getData() + " 123"); + logPrintD("[" + timeString() + "] "); + logPrintD("Received packet '"); + logPrintD(msg->toString()); + logPrintD("' with RSSI "); + logPrintD(String(_lora_aprs->packetRssi())); + logPrintD(" and SNR "); + logPrintlnD(String(_lora_aprs->packetSnr())); + std::shared_ptr is_thread = std::static_pointer_cast(TaskManager::instance().getTask(TASK_APRS_IS)); + is_thread->inputQueue.addElement(msg); + } return true; } diff --git a/src/TaskLora.h b/src/TaskLora.h index b6410eb..c0a02be 100644 --- a/src/TaskLora.h +++ b/src/TaskLora.h @@ -11,9 +11,8 @@ public: LoraTask(); virtual ~LoraTask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; - bool setup(std::shared_ptr config, std::shared_ptr boardConfig); private: std::shared_ptr _lora_aprs; diff --git a/src/TaskNTP.cpp b/src/TaskNTP.cpp index 294bceb..3da2af0 100644 --- a/src/TaskNTP.cpp +++ b/src/TaskNTP.cpp @@ -2,9 +2,10 @@ #include #include "project_configuration.h" #include "TaskNTP.h" +#include "Task.h" NTPTask::NTPTask() - : Task("NTPTask") + : Task(TASK_NTP) { } @@ -12,7 +13,7 @@ NTPTask::~NTPTask() { } -bool NTPTask::setup(std::shared_ptr config) +bool NTPTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { _ntpClient = std::shared_ptr(new NTPClient(config->ntpServer.c_str())); _ntpClient->begin(); diff --git a/src/TaskNTP.h b/src/TaskNTP.h index e05a89f..c5895b6 100644 --- a/src/TaskNTP.h +++ b/src/TaskNTP.h @@ -10,7 +10,7 @@ public: NTPTask(); virtual ~NTPTask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; private: diff --git a/src/TaskOTA.cpp b/src/TaskOTA.cpp index 5d96a62..f4b5b64 100644 --- a/src/TaskOTA.cpp +++ b/src/TaskOTA.cpp @@ -1,9 +1,10 @@ #include #include "project_configuration.h" #include "TaskOTA.h" +#include "Task.h" OTATask::OTATask() - : Task("OTATask") + : Task(TASK_OTA) { } @@ -11,7 +12,7 @@ OTATask::~OTATask() { } -bool OTATask::setup(std::shared_ptr config) +bool OTATask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { _ota = std::shared_ptr(new ArduinoOTAClass()); _ota->onStart([&]() diff --git a/src/TaskOTA.h b/src/TaskOTA.h index e36fb29..4700132 100644 --- a/src/TaskOTA.h +++ b/src/TaskOTA.h @@ -10,7 +10,7 @@ public: OTATask(); virtual ~OTATask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; private: diff --git a/src/TaskWifi.cpp b/src/TaskWifi.cpp index 297248c..1266074 100644 --- a/src/TaskWifi.cpp +++ b/src/TaskWifi.cpp @@ -2,9 +2,10 @@ #include #include "project_configuration.h" #include "TaskWifi.h" +#include "Task.h" WifiTask::WifiTask() - : Task("WifiTask") + : Task(TASK_WIFI) { } @@ -12,7 +13,7 @@ WifiTask::~WifiTask() { } -bool WifiTask::setup(std::shared_ptr config) +bool WifiTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { //WiFi.onEvent(WiFiEvent); //WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); diff --git a/src/TaskWifi.h b/src/TaskWifi.h index 15cd050..c6b67bf 100644 --- a/src/TaskWifi.h +++ b/src/TaskWifi.h @@ -10,7 +10,7 @@ public: WifiTask(); virtual ~WifiTask(); - virtual bool setup(std::shared_ptr config) override; + virtual bool setup(std::shared_ptr config, std::shared_ptr boardConfig) override; virtual bool loop(std::shared_ptr config) override; private: diff --git a/src/display.h b/src/display.h index 7ebfd92..4bd7530 100644 --- a/src/display.h +++ b/src/display.h @@ -13,11 +13,10 @@ void show_display(String header, String line1, String line2, String line3, int w //void show_display(String header, String line1, String line2, String line3, String line4, String line5, int wait = 0); #include -#include "SignalSlot.h" #include "TimeLib.h" #include "logger.h" -class PrintMessageToConsole : public Slot1>, public Slot3, int, float> +/*class PrintMessageToConsole : public Slot1>, public Slot3, int, float> { public: void action(std::shared_ptr msg, int rssi, float snr) override @@ -42,6 +41,6 @@ public: logPrintD(msg->toString()); logPrintD("'"); } -}; +};*/ #endif