Working on preliminary version of ActionEngine
This commit is contained in:
27
include/Action.h
Normal file
27
include/Action.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef ACTION_H
|
||||
#define ACTION_H
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "functional"
|
||||
#include "StreamEvent.h"
|
||||
|
||||
template <typename T>
|
||||
class Action {
|
||||
public:
|
||||
Action(function<bool(StreamEvent*)> _funcEventMatchesAction, function<T*(StreamEvent*)> _funcInvokeAction) :
|
||||
funcEventMatchesAction(_funcEventMatchesAction), funcInvokeAction(_funcInvokeAction) {}
|
||||
|
||||
bool eventMatchesAction(StreamEvent* event) {
|
||||
return funcEventMatchesAction(event);
|
||||
}
|
||||
|
||||
T* invokeAction(StreamEvent* event) {
|
||||
return funcInvokeAction(event);
|
||||
}
|
||||
|
||||
private:
|
||||
function<bool(StreamEvent*)> funcEventMatchesAction;
|
||||
function<T*(StreamEvent*)> funcInvokeAction;
|
||||
};
|
||||
|
||||
#endif
|
||||
33
include/Point2D.h
Normal file
33
include/Point2D.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef POINT2D_H
|
||||
#define POINT2D_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
class Point2D {
|
||||
public:
|
||||
Point2D() : x(0), y(0) {}
|
||||
Point2D(uint16_t _x, uint16_t _y) : x(_x), y(_y) {}
|
||||
|
||||
virtual ~Point2D() {}
|
||||
|
||||
void setX(uint16_t _x) { x = _x; }
|
||||
void setY(uint16_t _y) { y = _y; }
|
||||
uint16_t getX() { return x; }
|
||||
uint16_t getY() { return y; }
|
||||
bool isOrigin() { return x == 0 && y == 0; }
|
||||
|
||||
bool operator==(const Point2D& other) {
|
||||
return this->x == other.x && this->y == other.y;
|
||||
}
|
||||
|
||||
bool operator!=(const Point2D other) {
|
||||
return this->x != other.x || this->y != other.y;
|
||||
}
|
||||
|
||||
private:
|
||||
uint16_t
|
||||
x,
|
||||
y;
|
||||
};
|
||||
|
||||
#endif
|
||||
58
include/StreamEvent.h
Normal file
58
include/StreamEvent.h
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user