renderbug/lib/Figments/MainLoop.cpp

76 lines
2.1 KiB
C++

#include "./MainLoop.h"
#include "./Input.h"
#include "./Figment.h"
#include <ArduinoLog.h>
void
MainLoop::dispatch(const InputEvent& evt)
{
if (evt.intent == InputEvent::None) {
return;
}
m_eventBuf.insert(evt);
}
void
MainLoop::loop()
{
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.notice("Starting %s", figmentJob->name);
figmentJob->start();
} else {
//Log.notice("Stopping %s", figmentJob->name);
figmentJob->stop();
}
}
}
}
for(Task* task : scheduler) {
task->handleEvent(evt);
}
}
unsigned int slowest = 0;
unsigned int frameSpeed = 0;
unsigned int frameStart = millis();
unsigned int taskCount = 0;
Task* slowestTask = NULL;
for(Task* task : scheduler) {
//unsigned int start = millis();
unsigned int start = ESP.getCycleCount();
task->loop();
//unsigned int runtime = millis() - start;
unsigned int runtime = ESP.getCycleCount() - start;
frameSpeed += runtime;
taskCount++;
if (runtime > slowest) {
slowest = runtime;
slowestTask = task;
}
}
frameSpeed = millis() - frameStart;
if (frameSpeed >= 23) {
Log.notice("Slow frame: %dms, %d tasks, longest task %s was %dms", frameSpeed, taskCount, slowestTask->name, slowest/160000);
}
}
void
MainLoop::start()
{
Log.notice("*** Starting %d tasks...", scheduler.tasks.size());
Serial.flush();
for(auto task: scheduler) {
Log.notice("** Starting %s", task->name);
task->start();
}
}
MainLoop* MainLoop::s_instance;