Simpler thread controller (#4327)

* s

* Start -> start

* maybe tests are happy

* s

* s
This commit is contained in:
Matthew Kennedy 2022-08-01 15:16:13 -07:00 committed by GitHub
parent ce7fc8e69d
commit 435fd9538b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 44 deletions

View File

@ -145,7 +145,6 @@ class EngineStateBlinkingTask : public PeriodicTimerController {
} }
void PeriodicTask() override { void PeriodicTask() override {
counter++;
#if EFI_SHAFT_POSITION_INPUT #if EFI_SHAFT_POSITION_INPUT
bool is_running = engine->rpmCalculator.isRunning(); bool is_running = engine->rpmCalculator.isRunning();
#else #else
@ -154,14 +153,12 @@ class EngineStateBlinkingTask : public PeriodicTimerController {
if (is_running) { if (is_running) {
// blink in running mode // blink in running mode
enginePins.runningLedPin.setValue(counter % 2); enginePins.runningLedPin.toggle();
} else { } else {
int is_cranking = engine->rpmCalculator.isCranking(); int is_cranking = engine->rpmCalculator.isCranking();
enginePins.runningLedPin.setValue(is_cranking); enginePins.runningLedPin.setValue(is_cranking);
} }
} }
private:
int counter = 0;
}; };
static EngineStateBlinkingTask engineStateBlinkingTask; static EngineStateBlinkingTask engineStateBlinkingTask;

View File

@ -7,8 +7,6 @@
#pragma once #pragma once
#include "global.h"
/** /**
* @brief A base class for a controller that requires its own thread. * @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) * allocate the stack at compile time, it has to be a template parameter instead of a normal parameter)
*/ */
template <int TStackSize> template <int TStackSize>
class ThreadController class ThreadController : public chibios_rt::BaseStaticThread<TStackSize>
{ {
private: private:
THD_WORKING_AREA(m_threadstack, TStackSize); const tprio_t m_prio;
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<ThreadController*>(thread);
// Run our thread task
t->ThreadTask();
}
protected: protected:
/** // Override this function to implement your controller's thread's behavior.
* Override this function to implement your controller's thread's behavior. virtual void ThreadTask() = 0;
*/
virtual void ThreadTask() = 0; void main() override {
thread_t* m_thread; this->setName(m_name);
ThreadTask();
}
const char* const m_name; const char* const m_name;
public: public:
ThreadController(const char* name, tprio_t priority) ThreadController(const char* name, tprio_t priority)
: m_prio(priority) : m_prio(priority)
, m_name(name) , m_name(name)
{ {
} }
/** /**
* @brief Start the thread. * @brief Start the thread.
*/ */
void start() void start()
{ {
if (m_isStarted) { chibios_rt::BaseStaticThread<TStackSize>::start(m_prio);
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;
}
}; };

View File

@ -23,3 +23,12 @@ thread_t *chThdCreateStatic(void *wsp, size_t size,
tprio_t prio, tfunc_t pf, void *arg); tprio_t prio, tfunc_t pf, void *arg);
#define PAL_MODE_OUTPUT_PUSHPULL 0 #define PAL_MODE_OUTPUT_PUSHPULL 0
namespace chibios_rt {
template <int N>
struct BaseStaticThread {
void start(tprio_t) { }
virtual void main() = 0;
void setName(const char*) { }
};
}