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 b8d6d6bf6c
commit 24142f8330
3 changed files with 30 additions and 44 deletions

View File

@ -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;

View File

@ -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 <int TStackSize>
class ThreadController
class ThreadController : public chibios_rt::BaseStaticThread<TStackSize>
{
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<ThreadController*>(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<TStackSize>::start(m_prio);
}
};

View File

@ -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 <int N>
struct BaseStaticThread {
void start(tprio_t) { }
virtual void main() = 0;
void setName(const char*) { }
};
}