RSeries astromech firmware
PinInterruptHandler.h
Go to the documentation of this file.
1 #ifndef PinInterruptHandler_h
2 #define PinInterruptHandler_h
3 
4 #include "ReelTwo.h"
5 
7 {
8 public:
9  void detachInterrupt(unsigned pin)
10  {
11  unsigned inum = digitalPinToInterrupt(pin);
12  ::detachInterrupt(inum);
13  handlerToISR(inum) = nullptr;
14  }
15 
16  bool attachInterrupt(unsigned pin)
17  {
18  uint8_t inum = digitalPinToInterrupt(pin);
19  // Serial.println("attachInterrupt pin="+String(pin)+" interrupt="+String(inum));
20  handlerToISR(inum) = this;
21  switch (inum) {
22  #define ATTACH_INTTERUPT(num) \
23  case num: \
24  ::attachInterrupt(inum, ISR_##num, CHANGE); \
25  break;
26  #ifdef __AVR__
47  #elif defined(ESP32)
72  #else
73  #error Not supported yet
74  #endif
75 
76  #undef ATTACH_INTTERUPT
77  default:
78  return false;
79  }
80  return true;
81  }
82 
83 protected:
84  virtual void interrupt() = 0;
85 
86 private:
87 #ifdef ESP32
88  #define MAX_ISR_COUNT 40
89 #else
90  #define MAX_ISR_COUNT 20
91 #endif
92  static PinInterruptHandler*& handlerToISR(int isr)
93  {
94  static PinInterruptHandler* sHandlers[MAX_ISR_COUNT+1];
95  return sHandlers[(isr >= 0 && isr < MAX_ISR_COUNT) ? isr+1 : 0];
96  }
97 
98  #define DEFINE_ISR_HANDLER(n) \
99  static void ISR_##n() \
100  { \
101  PinInterruptHandler* handler = handlerToISR(n); \
102  if (handler != nullptr) \
103  handler->interrupt(); \
104  }
105 
106 #ifdef __AVR__
127 #elif defined(ESP32)
152 #else
153  #error Not supported yet
154 #endif
155 #undef MAX_ISR_COUNT
156 };
157 #endif
ATTACH_INTTERUPT
#define ATTACH_INTTERUPT(num)
ReelTwo.h
PinInterruptHandler::detachInterrupt
void detachInterrupt(unsigned pin)
Definition: PinInterruptHandler.h:9
MAX_ISR_COUNT
#define MAX_ISR_COUNT
Definition: PinInterruptHandler.h:90
PinInterruptHandler::attachInterrupt
bool attachInterrupt(unsigned pin)
Definition: PinInterruptHandler.h:16
PinInterruptHandler
Definition: PinInterruptHandler.h:6
DEFINE_ISR_HANDLER
#define DEFINE_ISR_HANDLER(n)
Definition: PinInterruptHandler.h:98
PinInterruptHandler::interrupt
virtual void interrupt()=0