mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
move user task handling into separate lib
This commit is contained in:
89
lib/usercode/GwUserCode.cpp
Normal file
89
lib/usercode/GwUserCode.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "GwUserCode.h"
|
||||
#include <Arduino.h>
|
||||
#include <vector>
|
||||
//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<UserTask> 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);
|
||||
}
|
||||
14
lib/usercode/GwUserCode.h
Normal file
14
lib/usercode/GwUserCode.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef _GWUSERCODE_H
|
||||
#define _GWUSERCODE_H
|
||||
#include <Arduino.h>
|
||||
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
|
||||
Reference in New Issue
Block a user