move user task handling into separate lib
This commit is contained in:
parent
1e4285fe5d
commit
39bc516def
|
@ -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);
|
||||||
|
}
|
|
@ -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
|
36
src/main.cpp
36
src/main.cpp
|
@ -31,22 +31,6 @@
|
||||||
// #define FALLBACK_SERIAL
|
// #define FALLBACK_SERIAL
|
||||||
const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting
|
const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
//user task handling
|
|
||||||
std::vector<TaskFunction_t> 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 "GwHardware.h"
|
||||||
|
|
||||||
#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object
|
#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object
|
||||||
|
@ -78,6 +62,7 @@ class GwUserTask{
|
||||||
#include "GwLeds.h"
|
#include "GwLeds.h"
|
||||||
#include "GwCounter.h"
|
#include "GwCounter.h"
|
||||||
#include "GwXDRMappings.h"
|
#include "GwXDRMappings.h"
|
||||||
|
#include "GwUserCode.h"
|
||||||
|
|
||||||
|
|
||||||
#include "GwApi.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\"}"
|
#define JSON_OK "{\"status\":\"OK\"}"
|
||||||
|
|
||||||
//WebServer requests that should
|
//WebServer requests that should
|
||||||
|
@ -713,15 +692,12 @@ void setup() {
|
||||||
NMEA2000.Open();
|
NMEA2000.Open();
|
||||||
logger.logDebug(GwLog::LOG,"starting addon tasks");
|
logger.logDebug(GwLog::LOG,"starting addon tasks");
|
||||||
logger.flush();
|
logger.flush();
|
||||||
startAddOnTask(handleButtons,100);
|
GwUserCode userHandler(new ApiImpl(200));
|
||||||
|
userHandler.startAddonTask(F("handleButtons"),handleButtons,100);
|
||||||
setLedMode(LED_GREEN);
|
setLedMode(LED_GREEN);
|
||||||
startAddOnTask(handleLeds,101);
|
userHandler.startAddonTask(F("handleLeds"),handleLeds,101);
|
||||||
int userTaskId=200;
|
userHandler.startUserTasks(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++;
|
|
||||||
}
|
|
||||||
logger.logDebug(GwLog::LOG,"setup done");
|
logger.logDebug(GwLog::LOG,"setup done");
|
||||||
}
|
}
|
||||||
//*****************************************************************************
|
//*****************************************************************************
|
||||||
|
|
Loading…
Reference in New Issue