figments: mainloop: add debugging tool that records last task prior to reset

This commit is contained in:
Torrie Fischer 2023-02-19 18:41:25 +01:00
parent 1426b1524b
commit de5d3e836a
2 changed files with 40 additions and 20 deletions

View File

@ -13,31 +13,48 @@ MainLoop::dispatch(const InputEvent& evt)
m_eventBuf.insert(evt);
}
//#ifdef BOARD_ESP32
__NOINIT_ATTR const char* s_lastTaskName;
//#endif
void
MainLoop::dispatchSync(const InputEvent& evt)
{
if (evt.intent == InputEvent::StartThing || evt.intent == InputEvent::StopThing) {
const bool jobState = (evt.intent == InputEvent::StartThing);
for(auto figmentJob: scheduler.tasks) {
if (!strcmp(figmentJob->name, evt.asString())) {
if (jobState) {
Log.trace("** Starting %s", figmentJob->name);
figmentJob->start();
} else {
Log.trace("** Stopping %s", figmentJob->name);
figmentJob->stop();
}
}
}
}
for(Task* task : scheduler) {
Log.verbose("** Eventing %s", task->name);
s_lastTaskName = task->name;
task->handleEvent(evt);
}
}
const char*
MainLoop::lastTaskName()
{
return s_lastTaskName;
}
void
MainLoop::loop()
{
s_instance = this;
InputEvent evt;
while (m_eventBuf.take(evt)) {
if (evt.intent == InputEvent::StartThing || evt.intent == InputEvent::StopThing) {
const bool jobState = (evt.intent == InputEvent::StartThing);
for(auto figmentJob: scheduler.tasks) {
if (!strcmp(figmentJob->name, evt.asString())) {
if (jobState) {
Log.trace("** Starting %s", figmentJob->name);
figmentJob->start();
} else {
Log.trace("** Stopping %s", figmentJob->name);
figmentJob->stop();
}
}
}
}
for(Task* task : scheduler) {
Log.verbose("** Eventing %s", task->name);
task->handleEvent(evt);
}
dispatchSync(evt);
}
unsigned int slowest = 0;
unsigned int frameSpeed = 0;
@ -52,6 +69,7 @@ MainLoop::loop()
unsigned int start = millis();
#endif
Log.verbose("Running %s", task->name);
s_lastTaskName = task->name;
task->loop();
#if defined(BOARD_ESP32) or defined(BOARD_ESP8266)
unsigned int runtime = (ESP.getCycleCount() - start) / 160000;
@ -66,7 +84,7 @@ MainLoop::loop()
}
}
frameSpeed = millis() - frameStart;
if (frameSpeed >= 23) {
if (frameSpeed >= 23) { // TODO: Configure max frame time at build
const char* slowestName = (slowestTask->name ? slowestTask->name : "(Unnamed)");
Log.warning("Slow frame: %dms, %d tasks, longest task %s was %dms", frameSpeed, taskCount, slowestTask->name, slowest);
}

View File

@ -60,7 +60,9 @@ struct MainLoop {
void start();
void loop();
void dispatch(const InputEvent& event);
void dispatchSync(const InputEvent& event);
static MainLoop* instance() { return s_instance; }
static const char* lastTaskName();
private:
Ringbuf<InputEvent, 32> m_eventBuf;