big changes:

- task manager working now in round robbin and returning after every task to arduino
- adding a system class which is managing all bigger things
This commit is contained in:
Peter Buchegger 2021-03-21 22:29:31 +01:00
parent 5a1a091fc7
commit 52d41dd9f6
27 changed files with 216 additions and 121 deletions

24
lib/System/System.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "System.h"
System::System(std::shared_ptr<BoardConfig> boardConfig, std::shared_ptr<Configuration> userConfig) : _boardConfig(boardConfig), _userConfig(userConfig) {
}
System::~System() {
}
std::shared_ptr<BoardConfig> System::getBoardConfig() const {
return _boardConfig;
}
std::shared_ptr<Configuration> System::getUserConfig() const {
return _userConfig;
}
TaskManager &System::getTaskManager() {
return _taskManager;
}
Display &System::getDisplay() {
return _display;
}

28
lib/System/System.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef SYSTEM_H_
#define SYSTEM_H_
#include <memory>
#include "TaskManager.h"
#include <BoardFinder.h>
#include <Display.h>
#include <configuration.h>
class System {
public:
System(std::shared_ptr<BoardConfig> boardConfig, std::shared_ptr<Configuration> userConfig);
~System();
std::shared_ptr<BoardConfig> getBoardConfig() const;
std::shared_ptr<Configuration> getUserConfig() const;
TaskManager & getTaskManager();
Display & getDisplay();
private:
std::shared_ptr<BoardConfig> _boardConfig;
std::shared_ptr<Configuration> _userConfig;
TaskManager _taskManager;
Display _display;
};
#endif

View file

@ -0,0 +1,83 @@
#include "TaskManager.h"
#include <FontConfig.h>
#include <logger.h>
TaskManager::TaskManager() {
}
void TaskManager::addTask(std::shared_ptr<Task> task) {
_tasks.push_back(task);
}
std::shared_ptr<Task> TaskManager::getTask(const char *name) {
std::_List_iterator<std::shared_ptr<Task>> elem = std::find_if(_tasks.begin(), _tasks.end(), [&](std::shared_ptr<Task> task) {
return task->getName() == name;
});
if (elem == _tasks.end()) {
return 0;
}
return *elem;
}
std::list<std::shared_ptr<Task>> TaskManager::getTasks() {
return _tasks;
}
bool TaskManager::setup(std::shared_ptr<System> system) {
logPrintlnV("will setup all tasks...");
for (std::shared_ptr<Task> &elem : _tasks) {
logPrintW("call setup from ");
logPrintlnW(elem->getName());
elem->setup(system);
}
_nextTask = _tasks.begin();
return true;
}
bool TaskManager::loop(std::shared_ptr<System> system) {
// logPrintlnD("will loop all tasks...");
if (_nextTask == _tasks.end()) {
_nextTask = _tasks.begin();
}
bool ret = (*_nextTask)->loop(system);
_nextTask++;
return ret;
for (std::shared_ptr<Task> &elem : _tasks) {
// logPrintD("call loop from ");
// logPrintlnD(elem->getName());
if (!elem->loop(system)) {
return false;
}
}
return true;
}
void StatusFrame::drawStatusPage(Bitmap &bitmap) {
int y = 0;
for (std::shared_ptr<Task> task : _tasks) {
int x = bitmap.drawString(0, y, (task->getName()).substring(0, task->getName().indexOf("Task")));
x = bitmap.drawString(x, y, ": ");
if (task->getStateInfo() == "") {
switch (task->getState()) {
case Error:
bitmap.drawString(x, y, "Error");
break;
case Warning:
bitmap.drawString(x, y, "Warning");
default:
break;
}
bitmap.drawString(x, y, "Okay");
} else {
bitmap.drawString(x, y, task->getStateInfo());
}
y += getSystemFont()->heightInPixel;
}
}
bool StatusFrame::isPrio() const {
return std::any_of(_tasks.begin(), _tasks.end(), [](std::shared_ptr<Task> task) {
return task->getState() != Okay;
});
}

92
lib/System/TaskManager.h Normal file
View file

@ -0,0 +1,92 @@
#ifndef TASK_MANAGER_H_
#define TASK_MANAGER_H_
#include <Arduino.h>
#include <list>
#include <memory>
#include <BoardFinder.h>
#include <Display.h>
#include <configuration.h>
#include "TaskQueue.h"
class System;
enum TaskDisplayState
{
Error,
Warning,
Okay,
};
class Task {
public:
Task(String &name, int taskId) : _state(Okay), _stateInfo("Booting"), _name(name), _taskId(taskId) {
}
Task(const char *name, int taskId) : _state(Okay), _stateInfo("Booting"), _name(name), _taskId(taskId) {
}
virtual ~Task() {
}
String getName() const {
return _name;
}
int getTaskId() const {
return _taskId;
}
TaskDisplayState getState() const {
return _state;
}
String getStateInfo() const {
return _stateInfo;
}
virtual bool setup(std::shared_ptr<System> system) = 0;
virtual bool loop(std::shared_ptr<System> system) = 0;
protected:
TaskDisplayState _state;
String _stateInfo;
private:
String _name;
int _taskId;
};
class TaskManager {
public:
TaskManager();
~TaskManager() {
}
void addTask(std::shared_ptr<Task> task);
std::shared_ptr<Task> getTask(const char *name);
std::list<std::shared_ptr<Task>> getTasks();
bool setup(std::shared_ptr<System> system);
bool loop(std::shared_ptr<System> system);
private:
std::list<std::shared_ptr<Task>> _tasks;
std::list<std::shared_ptr<Task>>::iterator _nextTask;
};
class StatusFrame : public DisplayFrame {
public:
explicit StatusFrame(const std::list<std::shared_ptr<Task>> &tasks) : _tasks(tasks) {
}
virtual ~StatusFrame() {
}
void drawStatusPage(Bitmap &bitmap) override;
bool isPrio() const;
private:
std::list<std::shared_ptr<Task>> _tasks;
};
#include "System.h"
#endif

29
lib/System/TaskQueue.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef TASK_QUEUE_H_
#define TASK_QUEUE_H_
#include <list>
template <typename T> 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<T> _elements;
};
#endif

28
lib/System/Timer.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "Timer.h"
Timer::Timer() : _timeout_sec(0), _timeout(0) {
}
void Timer::setTimeout(const time_t timeout_sec) {
_timeout_sec = timeout_sec;
}
time_t Timer::getTriggerTime() const {
return _timeout;
}
bool Timer::isActive() const {
return _timeout != 0;
}
void Timer::reset() {
_timeout = 0;
}
bool Timer::check() {
return now() > _timeout;
}
void Timer::start() {
_timeout = now() + _timeout_sec;
}

25
lib/System/Timer.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef TIMER_H_
#define TIMER_H_
#include <TimeLib.h>
class Timer {
public:
Timer();
void setTimeout(const time_t timeout_sec);
time_t getTriggerTime() const;
bool isActive() const;
void reset();
bool check();
void start();
private:
time_t _timeout_sec;
time_t _timeout;
};
#endif