Pushing up a preliminary version of GULLSManager

This commit is contained in:
Bradley Bickford 2024-07-05 11:01:19 -04:00
parent 126cc58c67
commit 414a5bb749
5 changed files with 188 additions and 6 deletions

View File

@ -8,20 +8,26 @@
template <typename T>
class Action {
public:
Action(function<bool(StreamEvent*)> _funcEventMatchesAction, function<T*(StreamEvent*)> _funcInvokeAction) :
funcEventMatchesAction(_funcEventMatchesAction), funcInvokeAction(_funcInvokeAction) {}
Action(function<bool(StreamEvent*)> _funcEventMatchesAction, function<T*(StreamEvent*)> _funcInvokeAction,
function<void(Print*)> _funcDescribeAction) : funcEventMatchesAction(_funcEventMatchesAction),
funcInvokeAction(_funcInvokeAction), funcDescribeAction(_funcDescribeAction) {}
bool eventMatchesAction(StreamEvent* event) {
return funcEventMatchesAction(event);
}
void describeAction(Print* printStream) {
funcDescribeAction(printStream);
}
T* invokeAction(StreamEvent* event) {
return funcInvokeAction(event);
}
private:
function<bool(StreamEvent*)> funcEventMatchesAction;
function<T*(StreamEvent*)> funcInvokeAction;
function<void(Print*)> funcDescribeAction;
};
#endif

41
include/GULLSManager.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef GULLSMANAGER_H
#define GULLSMANAGER_H
#include "Arduino.h"
#include "Action.h"
#include "LEDHAL.h"
#include "AnimationBase.h"
class GULLSManager {
public:
GULLSManager(Stream* _stream, uint16_t _maxHALs, uint16_t _maxAnimations,
uint16_t _maxResponseActions, uint16_t _maxAnimationBuilderActions);
bool addLEDHAL(LEDHAL* hal);
bool addAnimation(AnimationBase* animation);
bool addResponseAction(Action<char>* action);
bool addAnimationBuilderAction(Action<AnimationBase>* action);
void update();
private:
uint16_t
maxHALs,
maxAnimations,
maxResponseActions,
maxAnimationBuilderActions;
Stream*
stream;
LEDHAL**
hals;
AnimationBase**
animations;
Action<char>**
responseActions;
Action<AnimationBase>**
animationBuilderActions;
};
#endif

9
include/GlobalSettings.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef GLOBALSETTINGS_H
#define GLOBALSETTINGS_H
#define MAX_RESPONSE_ACTIONS 15
#define MAX_ANIMATION_BUILDER_ACTIONS 30
#define MAX_ANIMATIONS 15
#define MAX_HALS 15
#endif

View File

@ -10,7 +10,6 @@ class StreamEvent
handled = false;
code = _readFrom->read();
subCode = _readFrom->read();
flags = _readFrom->read();
if (flags & 0x01 == 1) {
@ -29,8 +28,9 @@ class StreamEvent
virtual ~StreamEvent() { delete[] payload; }
void markHandled() { handled = true; }
uint8_t getCode() { return code; }
uint8_t getSubCode() { return subCode; }
uint16_t getPayloadSize() { return payloadSize; }
@ -49,7 +49,6 @@ class StreamEvent
payloadSize;
uint8_t
code,
subCode,
flags;
bool
handled;

127
src/GULLSManager.cpp Normal file
View 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();
}
}
}