From 39bc516defbe4d477cf0af6aeaa97a1ed17c93af Mon Sep 17 00:00:00 2001 From: wellenvogel Date: Sat, 27 Nov 2021 15:45:21 +0100 Subject: [PATCH] move user task handling into separate lib --- lib/usercode/GwUserCode.cpp | 89 +++++++++++++++++++++++++++++++++++++ lib/usercode/GwUserCode.h | 14 ++++++ src/main.cpp | 36 +++------------ 3 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 lib/usercode/GwUserCode.cpp create mode 100644 lib/usercode/GwUserCode.h diff --git a/lib/usercode/GwUserCode.cpp b/lib/usercode/GwUserCode.cpp new file mode 100644 index 0000000..e1249e3 --- /dev/null +++ b/lib/usercode/GwUserCode.cpp @@ -0,0 +1,89 @@ +#include "GwUserCode.h" +#include +#include +//user task handling +class UserTask{ + public: + String name; + TaskFunction_t task; + UserTask(String name,TaskFunction_t task){ + this->name=name; + this->task=task; + } +}; +std::vector userTasks; + +void registerUserTask(TaskFunction_t task,String name){ + userTasks.push_back(UserTask(name,task)); +} + +class GwUserTask{ + public: + GwUserTask(TaskFunction_t task,String name){ + registerUserTask(task,name); + } +}; +#define DECLARE_USERTASK(task) GwUserTask __##task##__(task,#task); +#include "GwUserTasks.h" +#include "GwApi.h" +class TaskApi : public GwApi +{ + GwApi *api; + int sourceId; + +public: + TaskApi(GwApi *api, int sourceId) + { + this->sourceId = sourceId; + this->api = api; + } + virtual GwRequestQueue *getQueue() + { + return api->getQueue(); + } + virtual void sendN2kMessage(const tN2kMsg &msg) + { + api->sendN2kMessage(msg); + } + virtual void sendNMEA0183Message(const tNMEA0183Msg &msg, int sourceId) + { + api->sendNMEA0183Message(msg, sourceId); + } + virtual int getSourceId() + { + return sourceId; + }; + virtual GwConfigHandler *getConfig() + { + return api->getConfig(); + } + virtual GwLog *getLogger() + { + return api->getLogger(); + } + virtual GwBoatData *getBoatData() + { + return api->getBoatData(); + } +}; + +GwUserCode::GwUserCode(GwApi *api){ + this->logger=api->getLogger(); + this->api=api; +} +static void startAddOnTask(GwApi *api,TaskFunction_t task,int sourceId){ + TaskApi* taskApi=new TaskApi(api,sourceId); + xTaskCreate(task,"user",2000,taskApi,3,NULL); +} +void GwUserCode::startUserTasks(int baseId){ + LOG_DEBUG(GwLog::DEBUG,"starting %d user tasks",userTasks.size()); + for (auto it=userTasks.begin();it != userTasks.end();it++){ + LOG_DEBUG(GwLog::LOG,"starting user task %s with id %d",it->name.c_str(),baseId); + startAddOnTask(api,it->task,baseId); + baseId++; + } +} +void GwUserCode::startAddonTask(String name, TaskFunction_t task, int id){ + LOG_DEBUG(GwLog::LOG,"starting addon task %s with id %d",name.c_str(),id); + startAddOnTask(api,task,id); +} \ No newline at end of file diff --git a/lib/usercode/GwUserCode.h b/lib/usercode/GwUserCode.h new file mode 100644 index 0000000..f8adbe9 --- /dev/null +++ b/lib/usercode/GwUserCode.h @@ -0,0 +1,14 @@ +#ifndef _GWUSERCODE_H +#define _GWUSERCODE_H +#include +class GwLog; +class GwApi; +class GwUserCode{ + GwLog *logger; + GwApi *api; + public: + GwUserCode(GwApi *api); + void startUserTasks(int baseId); + void startAddonTask(String name,TaskFunction_t task, int id); +}; +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8c483d8..b993498 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,22 +31,6 @@ // #define FALLBACK_SERIAL const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting #include -//user task handling -std::vector userTasks; - -void registerUserTask(TaskFunction_t task){ - //logWriter.write("register user task\n"); - userTasks.push_back(task); -} - -class GwUserTask{ - public: - GwUserTask(TaskFunction_t task){ - registerUserTask(task); - } -}; -#define DECLARE_USERTASK(task) GwUserTask __##task##__(task); -//#include "GwUserTasks.h" #include "GwHardware.h" #include // This will automatically choose right CAN library and create suitable NMEA2000 object @@ -78,6 +62,7 @@ class GwUserTask{ #include "GwLeds.h" #include "GwCounter.h" #include "GwXDRMappings.h" +#include "GwUserCode.h" #include "GwApi.h" @@ -281,12 +266,6 @@ bool delayedRestart(){ } - -void startAddOnTask(TaskFunction_t task,int sourceId){ - ApiImpl* api=new ApiImpl(sourceId); - xTaskCreate(task,"user",2000,api,3,NULL); -} - #define JSON_OK "{\"status\":\"OK\"}" //WebServer requests that should @@ -713,15 +692,12 @@ void setup() { NMEA2000.Open(); logger.logDebug(GwLog::LOG,"starting addon tasks"); logger.flush(); - startAddOnTask(handleButtons,100); + GwUserCode userHandler(new ApiImpl(200)); + userHandler.startAddonTask(F("handleButtons"),handleButtons,100); setLedMode(LED_GREEN); - startAddOnTask(handleLeds,101); - int userTaskId=200; - for (auto it=userTasks.begin();it != userTasks.end();it++){ - logger.logDebug(GwLog::LOG,"starting user task with id %d",userTaskId); - startAddOnTask(*it,userTaskId); - userTaskId++; - } + userHandler.startAddonTask(F("handleLeds"),handleLeds,101); + userHandler.startUserTasks(200); + logger.logDebug(GwLog::LOG,"setup done"); } //*****************************************************************************