59 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.4 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);
 | |
|         }
 | |
|     }
 | |
|     for(Task* task : scheduler) {
 | |
|         //Log.notice("Running %s", task->name);
 | |
|         task->loop();
 | |
|         //Log.notice("next");
 | |
|     }
 | |
| }
 | |
| 
 | |
| 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;
 |