I have this code:
#include <Arduino.h>
#include "esp_timer.h"
//volatile int iCounter; <-- Uncommenting this line makes iCounter update in myTestFn()
class Test {
public:
Test(void) {
esp_timer_handle_t testTimer;
esp_timer_create_args_t timerConfig;
timerConfig.arg = this;
timerConfig.callback = reinterpret_cast<esp_timer_cb_t>( testFn );
timerConfig.dispatch_method = ESP_TIMER_TASK;
timerConfig.name = "Test_Timer";
esp_timer_create( &timerConfig, &testTimer );
esp_timer_start_periodic( testTimer, 1000000 );
}
static void testFn(void *arg) {
Test *obj = (Test *)arg;
obj->myTestFn();
}
void myTestFn(void) {
iCounter = iCounter +1;
Serial.printf("Test succeeded! Counter: %d Time elapsed: %lu.%03lu sec\n", iCounter, millis() / 1000, millis() % 1000);
}
private:
volatile int iCounter; // <-- This does not work, iCounter does not update in myTestFn()
};
void setup() {
Serial.begin(115200);
while(!Serial);
Test justATestObject;
}
void loop() {
}
I expected the iCounter variable to be indexed with each call to myTestFn(), but the value remains 1.
When I move the declaration of iCounter outside the class Test{} it does update (which seems odd to me). How do I get "Test" class member variables to update inside myTestFn()?