diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 25178e7..c3d8ed7 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -18,6 +18,7 @@ target_sources(app src/SensorDataProcessor.hpp src/SensorSampler.cpp src/SensorSampler.hpp + src/ThreadWrapper.hpp ) # The code below locates the git index file for this repository and adds it as a dependency for diff --git a/app/src/FirmwareManager.cpp b/app/src/FirmwareManager.cpp index 0fcbdfb..51f6073 100644 --- a/app/src/FirmwareManager.cpp +++ b/app/src/FirmwareManager.cpp @@ -1,9 +1,11 @@ #include "FirmwareManager.hpp" #include -void app::FirmwareManager::entryPoint(void *, void *, void *) { - while (true) { - printk("app::FirmwareManager::entryPoint - Running...\n"); - k_sleep(K_SECONDS(5)); - } +void app::FirmwareManager::init() {} + +void app::FirmwareManager::run() { + printk("app::FirmwareManager::run - Running...\n"); + k_sleep(K_SECONDS(5)); } + +void app::FirmwareManager::finalize() {} diff --git a/app/src/FirmwareManager.hpp b/app/src/FirmwareManager.hpp index 903976a..90a02e4 100644 --- a/app/src/FirmwareManager.hpp +++ b/app/src/FirmwareManager.hpp @@ -1,14 +1,20 @@ #ifndef FIRMWARE_MANAGER_HPP #define FIRMWARE_MANAGER_HPP +#include "ThreadWrapper.hpp" + namespace app { -class FirmwareManager { +class FirmwareManager : public ThreadWrapper { public: FirmwareManager() = default; ~FirmwareManager() = default; - static void entryPoint(void *, void *, void *); + void init(); + + void run(); + + void finalize(); private: }; diff --git a/app/src/SensorDataProcessor.cpp b/app/src/SensorDataProcessor.cpp index f32a11e..b16f89f 100644 --- a/app/src/SensorDataProcessor.cpp +++ b/app/src/SensorDataProcessor.cpp @@ -1,9 +1,11 @@ #include "SensorDataProcessor.hpp" #include -void app::SensorDataProcessor::entryPoint(void *, void *, void *) { - while (true) { - printk("app::SensorDataProcessor::entryPoint - Running...\n"); - k_sleep(K_SECONDS(5)); - } +void app::SensorDataProcessor::init() {} + +void app::SensorDataProcessor::run() { + printk("app::SensorDataProcessor::run - Running...\n"); + k_sleep(K_SECONDS(5)); } + +void app::SensorDataProcessor::finalize() {} diff --git a/app/src/SensorDataProcessor.hpp b/app/src/SensorDataProcessor.hpp index ae936d1..c047da2 100644 --- a/app/src/SensorDataProcessor.hpp +++ b/app/src/SensorDataProcessor.hpp @@ -1,15 +1,20 @@ #ifndef SENSOR_DATA_PROCESSOR_HPP #define SENSOR_DATA_PROCESSOR_HPP +#include "ThreadWrapper.hpp" + namespace app { -class SensorDataProcessor { +class SensorDataProcessor : public ThreadWrapper { public: SensorDataProcessor() = default; ~SensorDataProcessor() = default; - // Thread entry - static void entryPoint(void *, void *, void *); + void init(); + + void run(); + + void finalize(); private: }; diff --git a/app/src/SensorSampler.cpp b/app/src/SensorSampler.cpp index 3cc8471..16a48a0 100644 --- a/app/src/SensorSampler.cpp +++ b/app/src/SensorSampler.cpp @@ -1,9 +1,11 @@ #include "SensorSampler.hpp" #include -void app::SensorSampler::entryPoint(void *, void *, void *) { - while (true) { - printk("app::SensorSampler::entryPoint - Running...\n"); - k_sleep(K_SECONDS(5)); - } +void app::SensorSampler::init() {} + +void app::SensorSampler::run() { + printk("app::SensorSampler::run - Running...\n"); + k_sleep(K_SECONDS(5)); } + +void app::SensorSampler::finalize() {} diff --git a/app/src/SensorSampler.hpp b/app/src/SensorSampler.hpp index 606687e..42ceb60 100644 --- a/app/src/SensorSampler.hpp +++ b/app/src/SensorSampler.hpp @@ -1,15 +1,20 @@ #ifndef SENSOR_SAMPLER_HPP #define SENSOR_SAMPLER_HPP +#include "ThreadWrapper.hpp" + namespace app { -class SensorSampler { +class SensorSampler : public ThreadWrapper { public: SensorSampler() = default; ~SensorSampler() = default; - // Thread entry - static void entryPoint(void *, void *, void *); + void init(); + + void run(); + + void finalize(); private: }; diff --git a/app/src/ThreadWrapper.hpp b/app/src/ThreadWrapper.hpp new file mode 100644 index 0000000..3721391 --- /dev/null +++ b/app/src/ThreadWrapper.hpp @@ -0,0 +1,36 @@ +#ifndef THREAD_WRAPPER_HPP +#define THREAD_WRAPPER_HPP + +#include + +namespace app { + +template class ThreadWrapper { +public: + /** + * @brief Entry point to thread. Must define init(), run(), and finalize() for + * thread type. + * + */ + inline static void entryPoint(void *, void *, void *) { + auto thread = ThreadType(); + thread.init(); + + while (true) { + thread.run(); + thread.incrementRunCount(); + } + + // NOTE: Maybe one day I'll add logic to be able to finalize a thread... + thread.finalize(); + } + +private: + inline void incrementRunCount() { ++runCounter_; } + + std::size_t runCounter_ = 0; +}; + +} // namespace app + +#endif