Created
July 4, 2020 05:11
-
-
Save gsuberland/9c163f5a61da01f3866ad7aa10f00c6b to your computer and use it in GitHub Desktop.
Arduino interrupt template magic thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __INCLUDE_INTERRUPTS_H__ | |
#define __INCLUDE_INTERRUPTS_H__ | |
#include "Arduino.h" | |
#include <algorithm> | |
typedef void (*ISRFunctionPtr)(); | |
template <class C, int N> | |
class InterruptHandlers; | |
class InterruptHandler | |
{ | |
private: | |
public: | |
static ISRFunctionPtr GetISR(int n); | |
static void HandleInterrupt(int pin) | |
{ | |
Serial.print("Interrupt on pin "); | |
Serial.print(pin); | |
Serial.println(); | |
} | |
}; | |
template <class C, int N> | |
class InterruptHandlers | |
{ | |
public: | |
InterruptHandlers() | |
{ | |
memset(&_interruptHandlers, sizeof(_interruptHandlers), 0); | |
Init(int_<N>()); | |
} | |
ISRFunctionPtr GetISR(int n) | |
{ | |
return _interruptHandlers[n]; | |
} | |
private: | |
template<int offset> | |
static void ICACHE_RAM_ATTR isr() | |
{ | |
InterruptHandler::HandleInterrupt(offset); | |
} | |
void (*_interruptHandlers[N])(); | |
template <int M> | |
struct int_ {}; | |
template <int M> | |
void Init(int_<M>) | |
{ | |
Init(int_<M - 1>()); | |
_interruptHandlers[M - 1] = &InterruptHandlers<C, N>::template isr<M>; | |
Serial.print("Initializing ISR"); | |
Serial.print(M - 1); | |
Serial.println("...\n"); | |
} | |
void Init(int_<0>) {} | |
}; | |
static InterruptHandlers<int, 32> GlobalInterruptHandlers; | |
ISRFunctionPtr InterruptHandler::GetISR(int n) | |
{ | |
return GlobalInterruptHandlers.GetISR(n); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment