diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index b89fcae5c6..889d9f3209 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -145,7 +145,6 @@ class EngineStateBlinkingTask : public PeriodicTimerController { } void PeriodicTask() override { - counter++; #if EFI_SHAFT_POSITION_INPUT bool is_running = engine->rpmCalculator.isRunning(); #else @@ -154,14 +153,12 @@ class EngineStateBlinkingTask : public PeriodicTimerController { if (is_running) { // blink in running mode - enginePins.runningLedPin.setValue(counter % 2); + enginePins.runningLedPin.toggle(); } else { int is_cranking = engine->rpmCalculator.isCranking(); enginePins.runningLedPin.setValue(is_cranking); } } -private: - int counter = 0; }; static EngineStateBlinkingTask engineStateBlinkingTask; diff --git a/firmware/controllers/system/thread_controller.h b/firmware/controllers/system/thread_controller.h index a7325f23a5..9703f0dc90 100644 --- a/firmware/controllers/system/thread_controller.h +++ b/firmware/controllers/system/thread_controller.h @@ -7,8 +7,6 @@ #pragma once -#include "global.h" - /** * @brief A base class for a controller that requires its own thread. * @@ -17,53 +15,35 @@ * allocate the stack at compile time, it has to be a template parameter instead of a normal parameter) */ template -class ThreadController +class ThreadController : public chibios_rt::BaseStaticThread { private: - THD_WORKING_AREA(m_threadstack, TStackSize); - const tprio_t m_prio; - bool m_isStarted = false; - - /** - * The OS can only call a function with a single void* param. We have - * to convert back to an instance of ThreadController, and call the task to run. - */ - static void StaticThreadTaskAdapter(void* thread) - { - ThreadController* t = static_cast(thread); - - // Run our thread task - t->ThreadTask(); - } + const tprio_t m_prio; protected: - /** - * Override this function to implement your controller's thread's behavior. - */ - virtual void ThreadTask() = 0; - thread_t* m_thread; + // Override this function to implement your controller's thread's behavior. + virtual void ThreadTask() = 0; + + void main() override { + this->setName(m_name); + + ThreadTask(); + } const char* const m_name; public: - ThreadController(const char* name, tprio_t priority) + ThreadController(const char* name, tprio_t priority) : m_prio(priority) , m_name(name) - { - } + { + } - /** - * @brief Start the thread. - */ - void start() - { - if (m_isStarted) { - warning(CUSTOM_OBD_6003, "Tried to start thread %s but it was already running", m_name); - return; - } - - m_thread = chThdCreateStatic(m_threadstack, sizeof(m_threadstack), m_prio, StaticThreadTaskAdapter, this); - m_thread->name = m_name; - m_isStarted = true; - } + /** + * @brief Start the thread. + */ + void start() + { + chibios_rt::BaseStaticThread::start(m_prio); + } }; diff --git a/unit_tests/chibios-mock/mock-threads.h b/unit_tests/chibios-mock/mock-threads.h index 4f7f3ef4a0..bb814fb4bf 100644 --- a/unit_tests/chibios-mock/mock-threads.h +++ b/unit_tests/chibios-mock/mock-threads.h @@ -23,3 +23,12 @@ thread_t *chThdCreateStatic(void *wsp, size_t size, tprio_t prio, tfunc_t pf, void *arg); #define PAL_MODE_OUTPUT_PUSHPULL 0 + +namespace chibios_rt { + template + struct BaseStaticThread { + void start(tprio_t) { } + virtual void main() = 0; + void setName(const char*) { } + }; +}