From 126cc58c6728d6767722d1799c39215d8a815f81 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Tue, 2 Jul 2024 21:32:37 -0400 Subject: [PATCH] Working on preliminary version of ActionEngine --- README.md | 2 +- include/Action.h | 27 ++++++++++++++++++ {src => include}/Point2D.h | 0 include/StreamEvent.h | 58 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 include/Action.h rename {src => include}/Point2D.h (100%) create mode 100644 include/StreamEvent.h diff --git a/README.md b/README.md index 570f0d7..d522d34 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ | TextMatrix | :heavy_check_mark: | | | | TripCyclingMatrix | | | | | TripCyclingStrip | | | | -| GIFMatrix | :heavy_check_mark: | | | +| GIFMatrix | :heavy_check_mark: | :heavy_check_mark: | | | FlameMatrix | :heavy_check_mark: | | | | RainfallMatrix | :heavy_check_mark: | | | | Conway'sMatrix | :heavy_check_mark: | | | diff --git a/include/Action.h b/include/Action.h new file mode 100644 index 0000000..d9373ed --- /dev/null +++ b/include/Action.h @@ -0,0 +1,27 @@ +#ifndef ACTION_H +#define ACTION_H + +#include "Arduino.h" +#include "functional" +#include "StreamEvent.h" + +template +class Action { + public: + Action(function _funcEventMatchesAction, function _funcInvokeAction) : + funcEventMatchesAction(_funcEventMatchesAction), funcInvokeAction(_funcInvokeAction) {} + + bool eventMatchesAction(StreamEvent* event) { + return funcEventMatchesAction(event); + } + + T* invokeAction(StreamEvent* event) { + return funcInvokeAction(event); + } + + private: + function funcEventMatchesAction; + function funcInvokeAction; +}; + +#endif \ No newline at end of file diff --git a/src/Point2D.h b/include/Point2D.h similarity index 100% rename from src/Point2D.h rename to include/Point2D.h diff --git a/include/StreamEvent.h b/include/StreamEvent.h new file mode 100644 index 0000000..412286c --- /dev/null +++ b/include/StreamEvent.h @@ -0,0 +1,58 @@ +#ifndef STREAMEVENT_H +#define STREAMEVENT_H + +#include "Arduino.h" + +class StreamEvent +{ + public: + StreamEvent(Stream* _readFrom) { + handled = false; + + code = _readFrom->read(); + subCode = _readFrom->read(); + flags = _readFrom->read(); + + if (flags & 0x01 == 1) { + uint16_t payloadSizeMSB = _readFrom->read() << 8; + + payloadSize == payloadSizeMSB | _readFrom->read(); + + payload = new byte[payloadSize]; + + _readFrom->readBytes(payload, payloadSize); + } else { + payloadSize = 0; + payload = NULL; + } + } + + virtual ~StreamEvent() { delete[] payload; } + + uint8_t getCode() { return code; } + uint8_t getSubCode() { return subCode; } + + uint16_t getPayloadSize() { return payloadSize; } + + byte* getPayload() { return payload; } + + bool isHandled() { return handled; } + bool isResponse() { return flags & 0x02 == 2; } + bool isDescribeRequest() { return flags & 0x04 == 4; } + + virtual ~StreamEvent() { delete[] payload; } + + private: + byte* + payload; + uint16_t + payloadSize; + uint8_t + code, + subCode, + flags; + bool + handled; +}; + +#endif \ No newline at end of file