fsfw-from-zero/start/main.cpp

86 lines
2.3 KiB
C++
Raw Normal View History

2022-09-02 09:07:20 +02:00
#include <iostream>
2022-09-28 18:35:15 +02:00
#include <thread>
2022-09-02 09:07:20 +02:00
2022-09-28 19:44:30 +02:00
#include "fsfw/tasks/ExecutableObjectIF.h"
#include "fsfw/tasks/PeriodicTaskIF.h"
#include "fsfw/tasks/TaskFactory.h"
2022-09-02 09:07:20 +02:00
using namespace std;
2022-09-28 19:18:21 +02:00
class MyExecutableObjectIF {
2022-09-28 18:35:15 +02:00
public:
2022-09-28 19:18:21 +02:00
virtual ~MyExecutableObjectIF() = default;
virtual void performOperation() = 0;
};
2022-09-28 18:35:15 +02:00
2022-09-28 19:44:30 +02:00
class MyExecutableObject0: public ExecutableObjectIF {
2022-09-28 19:18:21 +02:00
public:
MyExecutableObject0() = default;
2022-09-28 19:44:30 +02:00
ReturnValue_t performOperation(uint8_t opCode) override {
2022-09-28 19:18:21 +02:00
cout << "Task 0" << endl;
2022-09-28 19:44:30 +02:00
return returnvalue::OK;
2022-09-28 19:18:21 +02:00
}
private:
};
2022-09-28 19:44:30 +02:00
class MyExecutableObject1: public ExecutableObjectIF {
2022-09-28 19:18:21 +02:00
public:
MyExecutableObject1() = default;
2022-09-28 19:44:30 +02:00
ReturnValue_t performOperation(uint8_t opCode) override {
2022-09-28 19:18:21 +02:00
cout << "Task 1" << endl;
2022-09-28 19:44:30 +02:00
return returnvalue::OK;
2022-09-28 19:18:21 +02:00
}
private:
};
2022-09-28 19:44:30 +02:00
class MyExecutableObject2: public ExecutableObjectIF {
2022-09-28 19:18:21 +02:00
public:
MyExecutableObject2() = default;
2022-09-28 19:44:30 +02:00
ReturnValue_t performOperation(uint8_t opCode) override {
2022-09-28 19:18:21 +02:00
cout << "Task 2" << endl;
2022-09-28 19:44:30 +02:00
return returnvalue::OK;
2022-09-28 18:35:15 +02:00
}
2022-09-28 19:18:21 +02:00
private:
};
class MyPeriodicTask {
public:
MyPeriodicTask(MyExecutableObjectIF& executable, uint32_t taskFreqMs)
: executable(executable), taskFreqMs(taskFreqMs) {}
2022-09-28 18:35:15 +02:00
2022-09-28 19:18:21 +02:00
std::thread start() {
return std::thread(
MyPeriodicTask::executeTask,
std::reference_wrapper(*this));
2022-09-28 18:35:15 +02:00
}
private:
2022-09-28 19:18:21 +02:00
static void executeTask(MyPeriodicTask& self) {
while(true) {
self.executable.performOperation();
this_thread::sleep_for(std::chrono::milliseconds(self.taskFreqMs));
}
}
MyExecutableObjectIF& executable;
uint32_t taskFreqMs;
2022-09-28 18:35:15 +02:00
};
2022-09-02 09:07:20 +02:00
int main() {
2022-09-28 19:18:21 +02:00
MyExecutableObject0 myExecutableObject0;
MyExecutableObject1 myExecutableObject1;
MyExecutableObject2 myExecutableObject2;
2022-09-28 19:44:30 +02:00
auto* factory = TaskFactory::instance();
auto* periodicTask0 = factory->createPeriodicTask("TASK_0", 0, PeriodicTaskIF::MINIMUM_STACK_SIZE, 0.5, nullptr);
auto* periodicTask1 = factory->createPeriodicTask("TASK_1", 0, PeriodicTaskIF::MINIMUM_STACK_SIZE, 1.0, nullptr);
periodicTask0->addComponent(&myExecutableObject0);
periodicTask0->addComponent(&myExecutableObject1);
periodicTask1->addComponent(&myExecutableObject2);
periodicTask0->startTask();
periodicTask1->startTask();
while(true) {
this_thread::sleep_for(5000ms);
}
2022-09-28 14:35:09 +02:00
}