mirror of
https://github.com/lora-aprs/LoRa_APRS_iGate.git
synced 2026-04-06 15:05:53 +00:00
restructure code
This commit is contained in:
parent
8c2c217acc
commit
8f25ceddcf
57 changed files with 63 additions and 74 deletions
48
src/System/System.cpp
Normal file
48
src/System/System.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
|
||||
#include "System.h"
|
||||
|
||||
System::System() : _boardConfig(0), _userConfig(0), _isEthConnected(false), _isWifiConnected(false) {
|
||||
}
|
||||
|
||||
System::~System() {
|
||||
}
|
||||
|
||||
void System::setBoardConfig(BoardConfig const *const boardConfig) {
|
||||
_boardConfig = boardConfig;
|
||||
}
|
||||
|
||||
void System::setUserConfig(Configuration const *const userConfig) {
|
||||
_userConfig = userConfig;
|
||||
}
|
||||
|
||||
BoardConfig const *const System::getBoardConfig() const {
|
||||
return _boardConfig;
|
||||
}
|
||||
|
||||
Configuration const *const System::getUserConfig() const {
|
||||
return _userConfig;
|
||||
}
|
||||
|
||||
TaskManager &System::getTaskManager() {
|
||||
return _taskManager;
|
||||
}
|
||||
|
||||
Display &System::getDisplay() {
|
||||
return _display;
|
||||
}
|
||||
|
||||
bool System::isWifiOrEthConnected() const {
|
||||
return _isEthConnected || _isWifiConnected;
|
||||
}
|
||||
|
||||
void System::connectedViaEth(bool status) {
|
||||
_isEthConnected = status;
|
||||
}
|
||||
|
||||
void System::connectedViaWifi(bool status) {
|
||||
_isWifiConnected = status;
|
||||
}
|
||||
|
||||
logging::Logger &System::getLogger() {
|
||||
return _logger;
|
||||
}
|
||||
39
src/System/System.h
Normal file
39
src/System/System.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#ifndef SYSTEM_H_
|
||||
#define SYSTEM_H_
|
||||
|
||||
#include <logger.h>
|
||||
#include <memory>
|
||||
|
||||
#include "BoardFinder/BoardFinder.h"
|
||||
#include "ConfigurationManagement/configuration.h"
|
||||
#include "Display/Display.h"
|
||||
#include "TaskManager.h"
|
||||
|
||||
class System {
|
||||
public:
|
||||
System();
|
||||
~System();
|
||||
|
||||
void setBoardConfig(BoardConfig const *const boardConfig);
|
||||
void setUserConfig(Configuration const *const userConfig);
|
||||
|
||||
BoardConfig const *const getBoardConfig() const;
|
||||
Configuration const *const getUserConfig() const;
|
||||
TaskManager &getTaskManager();
|
||||
Display &getDisplay();
|
||||
bool isWifiOrEthConnected() const;
|
||||
void connectedViaEth(bool status);
|
||||
void connectedViaWifi(bool status);
|
||||
logging::Logger &getLogger();
|
||||
|
||||
private:
|
||||
BoardConfig const *_boardConfig;
|
||||
Configuration const *_userConfig;
|
||||
TaskManager _taskManager;
|
||||
Display _display;
|
||||
bool _isEthConnected;
|
||||
bool _isWifiConnected;
|
||||
logging::Logger _logger;
|
||||
};
|
||||
|
||||
#endif
|
||||
73
src/System/TaskManager.cpp
Normal file
73
src/System/TaskManager.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include "TaskManager.h"
|
||||
#include "Display/FontConfig.h"
|
||||
#include <logger.h>
|
||||
|
||||
#define MODULE_NAME "TaskManager"
|
||||
|
||||
TaskManager::TaskManager() {
|
||||
}
|
||||
|
||||
void TaskManager::addTask(Task *task) {
|
||||
_tasks.push_back(task);
|
||||
}
|
||||
|
||||
void TaskManager::addAlwaysRunTask(Task *task) {
|
||||
_alwaysRunTasks.push_back(task);
|
||||
}
|
||||
|
||||
std::list<Task *> TaskManager::getTasks() {
|
||||
std::list<Task *> tasks = _alwaysRunTasks;
|
||||
std::copy(_tasks.begin(), _tasks.end(), std::back_inserter(tasks));
|
||||
return tasks;
|
||||
}
|
||||
|
||||
bool TaskManager::setup(System &system) {
|
||||
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "will setup all tasks...");
|
||||
for (Task *elem : _alwaysRunTasks) {
|
||||
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "call setup for %s", elem->getName().c_str());
|
||||
elem->setup(system);
|
||||
}
|
||||
for (Task *elem : _tasks) {
|
||||
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "call setup for %s", elem->getName().c_str());
|
||||
elem->setup(system);
|
||||
}
|
||||
_nextTask = _tasks.begin();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TaskManager::loop(System &system) {
|
||||
for (Task *elem : _alwaysRunTasks) {
|
||||
elem->loop(system);
|
||||
}
|
||||
|
||||
if (_nextTask == _tasks.end()) {
|
||||
_nextTask = _tasks.begin();
|
||||
}
|
||||
bool ret = (*_nextTask)->loop(system);
|
||||
++_nextTask;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// cppcheck-suppress unusedFunction
|
||||
void StatusFrame::drawStatusPage(Bitmap &bitmap) {
|
||||
int y = 0;
|
||||
for (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;
|
||||
}
|
||||
}
|
||||
90
src/System/TaskManager.h
Normal file
90
src/System/TaskManager.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
#ifndef TASK_MANAGER_H_
|
||||
#define TASK_MANAGER_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
|
||||
#include "BoardFinder/BoardFinder.h"
|
||||
#include "ConfigurationManagement/configuration.h"
|
||||
#include "Display/Display.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(System &system) = 0;
|
||||
virtual bool loop(System &system) = 0;
|
||||
|
||||
protected:
|
||||
TaskDisplayState _state;
|
||||
String _stateInfo;
|
||||
|
||||
private:
|
||||
String _name;
|
||||
int _taskId;
|
||||
};
|
||||
|
||||
class TaskManager {
|
||||
public:
|
||||
TaskManager();
|
||||
~TaskManager() {
|
||||
}
|
||||
|
||||
void addTask(Task *task);
|
||||
void addAlwaysRunTask(Task *task);
|
||||
std::list<Task *> getTasks();
|
||||
|
||||
bool setup(System &system);
|
||||
bool loop(System &system);
|
||||
|
||||
private:
|
||||
std::list<Task *> _tasks;
|
||||
std::list<Task *>::iterator _nextTask;
|
||||
std::list<Task *> _alwaysRunTasks;
|
||||
};
|
||||
|
||||
class StatusFrame : public DisplayFrame {
|
||||
public:
|
||||
explicit StatusFrame(const std::list<Task *> &tasks) : _tasks(tasks) {
|
||||
}
|
||||
virtual ~StatusFrame() {
|
||||
}
|
||||
void drawStatusPage(Bitmap &bitmap) override;
|
||||
|
||||
private:
|
||||
std::list<Task *> _tasks;
|
||||
};
|
||||
|
||||
#include "System.h"
|
||||
|
||||
#endif
|
||||
29
src/System/TaskQueue.h
Normal file
29
src/System/TaskQueue.h
Normal 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
|
||||
32
src/System/Timer.cpp
Normal file
32
src/System/Timer.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "Timer.h"
|
||||
|
||||
Timer::Timer() : _timeout_ms(0), _nextTimeout(0) {
|
||||
}
|
||||
|
||||
void Timer::setTimeout(const uint32_t timeout_ms) {
|
||||
_timeout_ms = timeout_ms;
|
||||
}
|
||||
|
||||
uint32_t Timer::getTriggerTimeInSec() const {
|
||||
return (_nextTimeout - millis()) / 1000;
|
||||
}
|
||||
|
||||
// cppcheck-suppress unusedFunction
|
||||
bool Timer::isActive() const {
|
||||
return _nextTimeout != 0;
|
||||
}
|
||||
|
||||
// cppcheck-suppress unusedFunction
|
||||
void Timer::reset() {
|
||||
_nextTimeout = 0;
|
||||
}
|
||||
|
||||
bool Timer::check() {
|
||||
return millis() > _nextTimeout;
|
||||
}
|
||||
|
||||
void Timer::start() {
|
||||
_nextTimeout = millis() + _timeout_ms;
|
||||
}
|
||||
23
src/System/Timer.h
Normal file
23
src/System/Timer.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
Timer();
|
||||
|
||||
void setTimeout(const uint32_t timeout_ms);
|
||||
uint32_t getTriggerTimeInSec() const;
|
||||
|
||||
bool isActive() const;
|
||||
|
||||
void reset();
|
||||
|
||||
bool check();
|
||||
void start();
|
||||
|
||||
private:
|
||||
uint32_t _timeout_ms;
|
||||
uint32_t _nextTimeout;
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue