|
-
-
-
-
- #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, uint8_t priority=128) {
- bool irq = disableInterrupts();
- detachNoInterrupts();
- _function = function;
- _type = EventTypeYield;
- enableInterrupts(irq);
- }
-
-
-
-
- void attachImmediate(EventResponderFunction function) {
- bool irq = disableInterrupts();
- detachNoInterrupts();
- _function = function;
- _type = EventTypeImmediate;
- enableInterrupts(irq);
- }
-
-
-
-
-
-
- void attachInterrupt(EventResponderFunction function, uint8_t priority=128) {
- bool irq = disableInterrupts();
- detachNoInterrupts();
- _function = function;
- _type = EventTypeInterrupt;
- SCB_SHPR3 |= 0x00FF0000;
- enableInterrupts(irq);
- }
-
-
-
- void attachThread(EventResponderFunction function, void *param=nullptr) {
- attach(function);
- }
-
-
-
- void detach() {
- bool irq = disableInterrupts();
- detachNoInterrupts();
- enableInterrupts(irq);
- }
-
-
-
-
- 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() {
- if (!firstYield) return;
-
-
- uint32_t ipsr;
- __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
- if (ipsr != 0) return;
-
- bool irq = disableInterrupts();
- EventResponder *first = firstYield;
- if (first == nullptr) {
- enableInterrupts(irq);
- return;
- }
-
-
-
- if (runningFromYield) {
- enableInterrupts(irq);
- return;
- }
-
- runningFromYield = true;
- firstYield = first->_next;
- if (firstYield) {
- firstYield->_prev = nullptr;
- } else {
- lastYield = nullptr;
- }
- enableInterrupts(irq);
- first->_triggered = false;
- (*(first->_function))(*first);
- runningFromYield = false;
- }
- static void runFromInterrupt();
- operator bool() { return _triggered; }
- protected:
- void triggerEventNotImmediate();
- void detachNoInterrupts();
- int _status = 0;
- EventResponderFunction _function = nullptr;
- void *_data = nullptr;
- void *_context = nullptr;
- EventResponder *_next = nullptr;
- EventResponder *_prev = nullptr;
- EventType _type = EventTypeDetached;
- bool _triggered = false;
- static EventResponder *firstYield;
- static EventResponder *lastYield;
- static EventResponder *firstInterrupt;
- static EventResponder *lastInterrupt;
- static bool runningFromYield;
- private:
- static bool disableInterrupts() {
- uint32_t primask;
- __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
- __disable_irq();
- return (primask == 0) ? true : false;
- }
- static void enableInterrupts(bool doit) {
- if (doit) __enable_irq();
- }
- };
-
- class MillisTimer
- {
- public:
- constexpr MillisTimer() {
- }
- ~MillisTimer() {
- end();
- }
- void begin(unsigned long milliseconds, EventResponderRef event);
- void beginRepeating(unsigned long milliseconds, EventResponderRef event);
- void end();
- static void runFromTimer();
- private:
- void addToWaitingList();
- void addToActiveList();
- unsigned long _ms = 0;
- unsigned long _reload = 0;
- MillisTimer *_next = nullptr;
- MillisTimer *_prev = nullptr;
- EventResponder *_event = nullptr;
- enum TimerStateType {
- TimerOff = 0,
- TimerWaiting,
- TimerActive
- };
- volatile TimerStateType _state = TimerOff;
- static MillisTimer *listWaiting;
- static MillisTimer *listActive;
- static bool disableTimerInterrupt() {
- uint32_t primask;
- __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
- __disable_irq();
- return (primask == 0) ? true : false;
- }
- static void enableTimerInterrupt(bool doit) {
- if (doit) __enable_irq();
- }
- };
-
- #endif
|