renderbug-cpp/lib/Figments/MainLoop.cpp

109 lines
2.8 KiB
C++
Raw Normal View History

2019-05-10 05:17:29 +00:00
#include "./MainLoop.h"
#include "./Input.h"
#include "./Figment.h"
2021-03-29 08:10:55 +00:00
#include <ArduinoLog.h>
2019-05-10 05:17:29 +00:00
void
MainLoop::dispatch(const InputEvent& evt)
{
if (evt.intent == InputEvent::None) {
return;
}
m_eventBuf.insert(evt);
}
//#ifdef BOARD_ESP32
__NOINIT_ATTR const char* s_lastTaskName;
//#endif
2019-05-10 05:17:29 +00:00
void
MainLoop::dispatchSync(const InputEvent& evt)
2019-05-10 05:17:29 +00:00
{
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();
2019-05-10 05:17:29 +00:00
}
}
}
}
2019-05-10 05:17:29 +00:00
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)) {
dispatchSync(evt);
2019-05-10 05:17:29 +00:00
}
2021-04-10 18:10:25 +00:00
unsigned int slowest = 0;
unsigned int frameSpeed = 0;
unsigned int frameStart = millis();
unsigned int taskCount = 0;
Task* slowestTask = NULL;
2019-05-10 05:17:29 +00:00
for(Task* task : scheduler) {
2021-04-10 18:10:25 +00:00
//unsigned int start = millis();
#if defined(BOARD_ESP32) or defined(BOARD_ESP8266)
2021-04-10 18:10:25 +00:00
unsigned int start = ESP.getCycleCount();
#else
unsigned int start = millis();
#endif
Log.verbose("Running %s", task->name);
s_lastTaskName = task->name;
2019-05-10 05:17:29 +00:00
task->loop();
#if defined(BOARD_ESP32) or defined(BOARD_ESP8266)
unsigned int runtime = (ESP.getCycleCount() - start) / 160000;
#else
unsigned int runtime = millis() - start;
#endif
2021-04-10 18:10:25 +00:00
frameSpeed += runtime;
taskCount++;
if (runtime > slowest) {
slowest = runtime;
slowestTask = task;
}
}
frameSpeed = millis() - frameStart;
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);
2019-05-10 05:17:29 +00:00
}
}
void
MainLoop::start()
{
s_instance = this;
Log.notice("Welcome to 🌕 Figment 0.3.0");
2021-03-29 08:10:55 +00:00
Log.notice("*** Starting %d tasks...", scheduler.tasks.size());
2019-05-10 05:17:29 +00:00
Serial.flush();
for(auto task: scheduler) {
2021-03-29 08:10:55 +00:00
Log.notice("** Starting %s", task->name);
2019-05-10 05:17:29 +00:00
task->start();
}
loop();
dispatch(InputEvent::ReadyToRoll);
2019-05-10 05:17:29 +00:00
}
MainLoop* MainLoop::s_instance;