Registering an interrupt service routine (ISR) is usually done by storing its address in a table (often called the "vector table"). The IntervalTimer.begin() function does this for you. If an interrupt wtih number n occurs, the processor basically looks up the address stored at position n of this table and jumps to this address. So, there is no direct way to pass information to the ISR.
Of course there are workarounds. The simplest is to write a relay function which you would attach to the interrupt (i.e., write its address to the vector table). This relay function can then call another function and pass the required information to it.
Say, you have this struct
C++:
struct BOB
{
int someData;
void doSomething()
{
Serial.println("called");
}
};
BOB bob{42}; // instance of the struct with someData = 42;
and want a callback to access its data member
C++:
void function(BOB* b)
{
Serial.println(b->someData);
}
You could define a relay function and attach it to the timer
C++:
void relay()
{
function(&bob);
}
Here the complete code:
C++:
struct BOB
{
int someData;
void doSomething(){
Serial.println("called");
}
};
//--------------------------------------
IntervalTimer t;
BOB bob {42};
void function(BOB* b)
{
Serial.println(b->someData);
}
void relay()
{
function(&bob);
}
void setup()
{
t.begin(relay,1'000'000);
}
void loop(){}
Using lambda expressions you can have the compiler generating the relay function for you:
C++:
IntervalTimer t;
BOB bob {42};
void function(BOB* b)
{
Serial.println(b->someData);
}
void setup()
{
t.begin([]{function(&bob);},1'000'000);
}
void loop(){}
And, if you only want e.g. print the value of someData, you can also dispose of the callback:
C++:
struct BOB
{
int someData;
void doSomething(){
Serial.println("called");
}
};
//--------------------------------------
IntervalTimer t;
BOB bob {42};
void setup()
{
t.begin([]{Serial.print(bob.someData);},1'000'000);
// or:
// t.begin([]{bob.doSomething();},1'000'000);
}
void loop(){}
Here some more information about this:
https://github.com/TeensyUser/doc/wiki/callbacks
Teensyduino 1.59 (currently in beta) will have a more modern callback interface which allows for more possibilites when attaching callbacks. Here some information about this:
https://github.com/TeensyUser/doc/wiki/Using-the-new-callback-interface