Pushing up a preliminary version of GULLSManager
This commit is contained in:
127
src/GULLSManager.cpp
Normal file
127
src/GULLSManager.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
#include "GULLSManager.h"
|
||||
|
||||
GULLSManager::GULLSManager(Stream* _stream, uint16_t _maxHALs, uint16_t _maxAnimations,
|
||||
uint16_t _maxResponseActions, uint16_t _maxAnimationBuilderActions) {
|
||||
|
||||
stream = _stream;
|
||||
maxHALs = _maxHALs;
|
||||
maxAnimations = _maxAnimations;
|
||||
maxResponseActions = _maxResponseActions;
|
||||
maxAnimationBuilderActions = _maxAnimationBuilderActions;
|
||||
|
||||
hals = new LEDHAL*[maxHALs];
|
||||
|
||||
for(uint16_t i = 0; i < maxHALs; i++) {
|
||||
hals[i] = nullptr;
|
||||
}
|
||||
|
||||
animations = new AnimationBase*[maxAnimations];
|
||||
|
||||
for(uint16_t i = 0; i < maxAnimations; i++) {
|
||||
animations[i] = nullptr;
|
||||
}
|
||||
|
||||
responseActions = new Action<char>*[maxResponseActions];
|
||||
|
||||
for(uint16_t i = 0; i < maxResponseActions; i++) {
|
||||
responseActions[i] = nullptr;
|
||||
}
|
||||
|
||||
animationBuilderActions = new Action<AnimationBase>*[maxAnimationBuilderActions];
|
||||
|
||||
for(uint16_t i = 0; i < maxAnimationBuilderActions; i++) {
|
||||
animationBuilderActions[i] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool GULLSManager::addLEDHAL(LEDHAL* hal) {
|
||||
for(uint16_t i = 0; i < maxHALs; i++) {
|
||||
if(hals[i] == nullptr) {
|
||||
hals[i] = hal;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GULLSManager::addAnimation(AnimationBase* animation) {
|
||||
for(uint16_t i = 0; i < maxAnimations; i++) {
|
||||
if(animations[i] == nullptr) {
|
||||
animations[i] = animation;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GULLSManager::addResponseAction(Action<char>* action) {
|
||||
for(uint16_t i = 0; i < maxResponseActions; i++) {
|
||||
if(responseActions[i] == nullptr) {
|
||||
responseActions[i] = action;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GULLSManager::addAnimationBuilderAction(Action<AnimationBase>* action) {
|
||||
for(uint16_t i = 0; i < maxAnimationBuilderActions; i++) {
|
||||
if(animationBuilderActions[i] == nullptr) {
|
||||
animationBuilderActions[i] = action;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void GULLSManager::update() {
|
||||
if(stream->available()) {
|
||||
StreamEvent event(stream);
|
||||
|
||||
for(uint16_t i = 0; i < maxResponseActions; i++) {
|
||||
if(responseActions[i] != nullptr) {
|
||||
if(responseActions[i]->eventMatchesAction(&event)) {
|
||||
stream->write(responseActions[i]->invokeAction(&event));
|
||||
|
||||
event.markHandled();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!event.isHandled()) {
|
||||
for(uint16_t i = 0; i < maxAnimationBuilderActions; i++) {
|
||||
if(animationBuilderActions[i] != nullptr) {
|
||||
if(animationBuilderActions[i]->eventMatchesAction(&event)) {
|
||||
this->addAnimation(animationBuilderActions[i]->invokeAction(&event));
|
||||
|
||||
event.markHandled();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!event.isHandled()) {
|
||||
stream->print(F("ERROR: An event with code "));
|
||||
stream->print(event.getCode());
|
||||
stream->println(F(" was registered but went unprocessed by any actions!"));
|
||||
}
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < maxAnimations; i++) {
|
||||
if(animations[i] != nullptr) {
|
||||
animations[i]->update();
|
||||
}
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < maxHALs; i++) {
|
||||
if(hals[i] != nullptr) {
|
||||
hals[i]->show();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user