From 414a5bb7498470c97d27421b9da0ea68a9b8b9bd Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Fri, 5 Jul 2024 11:01:19 -0400 Subject: [PATCH] Pushing up a preliminary version of GULLSManager --- include/Action.h | 12 +++- include/GULLSManager.h | 41 +++++++++++++ include/GlobalSettings.h | 9 +++ include/StreamEvent.h | 5 +- src/GULLSManager.cpp | 127 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 include/GULLSManager.h create mode 100644 include/GlobalSettings.h create mode 100644 src/GULLSManager.cpp diff --git a/include/Action.h b/include/Action.h index d9373ed..7ec83fa 100644 --- a/include/Action.h +++ b/include/Action.h @@ -8,20 +8,26 @@ template class Action { public: - Action(function _funcEventMatchesAction, function _funcInvokeAction) : - funcEventMatchesAction(_funcEventMatchesAction), funcInvokeAction(_funcInvokeAction) {} + Action(function _funcEventMatchesAction, function _funcInvokeAction, + function _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 funcEventMatchesAction; function funcInvokeAction; + function funcDescribeAction; }; #endif \ No newline at end of file diff --git a/include/GULLSManager.h b/include/GULLSManager.h new file mode 100644 index 0000000..39875c2 --- /dev/null +++ b/include/GULLSManager.h @@ -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* action); + + bool addAnimationBuilderAction(Action* action); + + void update(); + private: + uint16_t + maxHALs, + maxAnimations, + maxResponseActions, + maxAnimationBuilderActions; + Stream* + stream; + LEDHAL** + hals; + AnimationBase** + animations; + Action** + responseActions; + Action** + animationBuilderActions; +}; + +#endif \ No newline at end of file diff --git a/include/GlobalSettings.h b/include/GlobalSettings.h new file mode 100644 index 0000000..8c08453 --- /dev/null +++ b/include/GlobalSettings.h @@ -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 \ No newline at end of file diff --git a/include/StreamEvent.h b/include/StreamEvent.h index 412286c..3d8431a 100644 --- a/include/StreamEvent.h +++ b/include/StreamEvent.h @@ -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; diff --git a/src/GULLSManager.cpp b/src/GULLSManager.cpp new file mode 100644 index 0000000..99a7588 --- /dev/null +++ b/src/GULLSManager.cpp @@ -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*[maxResponseActions]; + + for(uint16_t i = 0; i < maxResponseActions; i++) { + responseActions[i] = nullptr; + } + + animationBuilderActions = new Action*[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* action) { + for(uint16_t i = 0; i < maxResponseActions; i++) { + if(responseActions[i] == nullptr) { + responseActions[i] = action; + return true; + } + } + + return false; +} + +bool GULLSManager::addAnimationBuilderAction(Action* 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(); + } + } +} \ No newline at end of file