DueTimer and static method

Hi everyone,

I have a question for you. Sorry for my bad English.
I started to work with Arduino Due and I see that this board have a eight/nine timers. I found online the Timer library for Arduino Due named DueTimer (https://github.com/ivanseidel/DueTimer).

So I created a little C++ Class:

#include <Test.h>

Test::Test() //constructor
{
	Serial.begin(115200);
}

void Test::setTxPin(uint8_t pin){
	TxPin = pin;
}
	
void Test::handler_Tx(){
	Serial.println(TxPin);
}

Header file:

#ifndef TEST_H
#define TEST_H


#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
  #include <pins_arduino.h>
#endif

static uint8_t TxPin;

class Test
{
	public:
		Test();
	 	 void setTxPin(uint8_t pin); 
	 	 static void handler_Tx();
	 	 
};
#endif

and .ino file

#include <Test.h>
#include <DueTimer.h>


void setup() {
  Serial.begin(115200);
  Test c1 = Test();
  Test c2 = Test();
  c1.setTxPin(1);
  c2.setTxPin(3); 
  DueTimer tx_Timer1 = DueTimer(1);
  DueTimer tx_Timer2 = DueTimer(2);
  tx_Timer1.attachInterrupt(c1.handler_Tx).setFrequency(5).start();
  delay(10);
  tx_Timer2.attachInterrupt(c2.handler_Tx).setFrequency(5).start();
}

void loop() {
  // put your main code here, to run repeatedly:

}

This code working correctly but I have a problem because to use the interrupt is necessary defined a static handler function. So for this reason is impossible creating different instances of Test Class because each instance will have a shared memory, in fact with my code I have two differents objects with two different Arduino pins number but in the output I read only the last pin setting.

3
3
3
3
3
3
3
3
3
3
3

I am not a good programmer, so my question is this:

Is it possible modify my code or the DueTimer Library to create many different instances of Test Class?

Thanks a lot :slight_smile:

Test::Test() //constructor
{
    Serial.begin(115200);
}

Have you ever considered why you need to call Serial.begin() in setup()? Do you think that it might have something to do with the hardware not being ready when the Serial instance is created? It STILL may not be ready when your constructor is called.

Given that you call Serial.begin() in setup(), it does not make sense to have the class call it, too, with a hardcoded value.

Is it possible modify my code or the DueTimer Library to create many different instances of Test Class?

The library? No. Your code? Yes, of course.

Exactly how will depend on the answers to some questions.

The timer firing is like the UPS guy delivering a package to your house, and ringing the doorbell. The doorbell ringing is the interrupt.

Now, which instance of your class cares? Which instance of the class should answer the door and receive the package?

If the answer is anything other than ALL OF THEM, then your class needs to keep track of all instances, and tell the appropriate one which one to deal with the interrupt. You have to decide which is the appropriate one.

If the answer is ALL OF THEM, then your class needs to keep track of all instances, and tell each of them that the interrupt happened, so that they can do what they need to do.