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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-03-29 08:10:55 +00:00
|
|
|
if (!strcmp(figmentJob->name, evt.asString())) {
|
2019-05-10 05:17:29 +00:00
|
|
|
if (jobState) {
|
2021-03-29 08:10:55 +00:00
|
|
|
//Log.notice("Starting %s", figmentJob->name);
|
2019-05-10 05:17:29 +00:00
|
|
|
figmentJob->start();
|
|
|
|
} else {
|
2021-03-29 08:10:55 +00:00
|
|
|
//Log.notice("Stopping %s", figmentJob->name);
|
2019-05-10 05:17:29 +00:00
|
|
|
figmentJob->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(Task* task : scheduler) {
|
|
|
|
task->handleEvent(evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(Task* task : scheduler) {
|
2021-03-29 08:10:55 +00:00
|
|
|
//Log.notice("Running %s", task->name);
|
2019-05-10 05:17:29 +00:00
|
|
|
task->loop();
|
2021-03-29 08:10:55 +00:00
|
|
|
//Log.notice("next");
|
2019-05-10 05:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainLoop::start()
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop* MainLoop::s_instance;
|