Skip to content

Instantly share code, notes, and snippets.

@bfueldner
Last active August 29, 2015 14:10
Show Gist options
  • Save bfueldner/80b1201b153575aedabe to your computer and use it in GitHub Desktop.
Save bfueldner/80b1201b153575aedabe to your computer and use it in GitHub Desktop.
C++ interrupt service routine using templates
#include <iostream>
using namespace std;
typedef void (*callback)(void);
struct timer1_tag {};
struct timer2_tag {};
static callback isr_vector[8];
template<typename T>
void timer_isr(void);
template<typename T>
struct timer
{
const int base;
static timer<T> *self;
timer(const int base, const int index):
base(base)
{
self = this;
isr_vector[index] = timer_isr<T>;
}
void isr(void)
{
cout << base << endl;
}
};
template<typename T>
void timer_isr(void)
{
timer<T>::self->isr();
}
template<typename T>
timer<T> *timer<T>::self;
int main(int argc, char *argv[])
{
cout << "create" << endl;
timer<timer1_tag> timer1(5, 0);
timer<timer2_tag> timer2(12, 1);
cout << "call" << endl;
isr_vector[0]();
isr_vector[1]();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment