|
-
-
-
-
- #if !defined(EventResponder_h) && defined(__cplusplus)
- #define EventResponder_h
-
- #include <Arduino.h>
-
-
-
- class EventResponder;
- typedef EventResponder& EventResponderRef;
- typedef void (*EventResponderFunction)(EventResponderRef);
- class EventResponder
- {
- public:
- constexpr EventResponder() {
- }
- ~EventResponder() {
- detach();
- }
- enum EventType {
- EventTypeDetached = 0,
- EventTypeYield,
- EventTypeImmediate,
- EventTypeInterrupt,
- EventTypeThread
- };
-
-
-
-
- void attach(EventResponderFunction function) {
- detach();
- _function = function;
- _type = EventTypeYield;
- }
-
-
-
-
- void attachImmediate(EventResponderFunction function) {
- detach();
- _function = function;
- _type = EventTypeImmediate;
- }
-
-
-
-
-
-
- void attachInterrupt(EventResponderFunction function) {
- detach();
- _function = function;
- _type = EventTypeInterrupt;
-
- }
-
-
-
- void attachThread(EventResponderFunction function, void *param=nullptr) {
- attach(function);
- }
-
-
-
- void detach();
-
-
-
-
- virtual void triggerEvent(int status=0, void *data=nullptr) {
- _status = status;
- _data = data;
- if (_type == EventTypeImmediate) {
- (*_function)(*this);
- } else {
- triggerEventNotImmediate();
- }
- }
-
-
- bool clearEvent();
-
-
-
-
-
-
- int getStatus() { return _status; }
-
-
-
-
- void * getData() { return _data; }
-
-
-
-
-
- void setContext(void *context) { _context = context; }
- void * getContext() { return _context; }
-
-
-
- bool waitForEvent(EventResponderRef event, int timeout);
- EventResponder * waitForEvent(EventResponder *list, int listsize, int timeout);
-
- static void runFromYield() {
- EventResponder *first = firstYield;
- if (first && !runningFromYield) {
- runningFromYield = true;
- firstYield = first->_next;
- if (firstYield) {
- firstYield->_prev = nullptr;
- } else {
- lastYield = nullptr;
- }
- first->_pending = false;
- (*(first->_function))(*first);
- runningFromYield = false;
- }
- }
- static void runFromInterrupt();
- operator bool() { return _pending; }
- protected:
- void triggerEventNotImmediate();
- int _status = 0;
- EventResponderFunction _function = nullptr;
- void *_data = nullptr;
- void *_context = nullptr;
- EventResponder *_next = nullptr;
- EventResponder *_prev = nullptr;
- EventType _type = EventTypeDetached;
- bool _pending = false;
- static EventResponder *firstYield;
- static EventResponder *lastYield;
- static EventResponder *firstInterrupt;
- static EventResponder *lastInterrupt;
- static bool runningFromYield;
- };
-
- class MillisTimer
- {
- public:
- constexpr MillisTimer() {
- }
- ~MillisTimer() {
- end();
- }
- void begin(unsigned long milliseconds, EventResponderRef event);
- void beginRepeat(unsigned long milliseconds, EventResponderRef event);
- void end();
- static void runFromTimer();
- private:
- void addToList();
- unsigned long _ms = 0;
- unsigned long _reload = 0;
- MillisTimer *_next = nullptr;
- MillisTimer *_prev = nullptr;
- EventResponder *_event = nullptr;
- bool isQueued = false;
- static MillisTimer *list;
- };
-
- #endif
|