This is very cool looking @luni!
Here is the example I just wrote - with simple change to newest example:
Code:
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
void callback(uint32_t& someInt) // this callback has context, i.e. parameter
{
Serial.print("loop()/sec=");
Serial.print(someInt);
Serial.print(" us=");
Serial.println(micros());
someInt = 0;
}
//==============================================================
Timer t;
static uint32_t loopCnt = 0;
void setup()
{
t.beginPeriodic([] { callback(loopCnt); }, 1'000'000);
}
void loop()
{
loopCnt++; // change every second
}
Results in this good looking SerMon output - showing it is being called in a timely fashion - down to the microsecond with no other code holding it up. And giving a loop() per second about where expected:
Code:
loop()/sec=5881751 us=269300004
loop()/sec=5881752 us=270300004
loop()/sec=5881752 us=271300004
loop()/sec=5881751 us=272300004
Writing it olde Style is a bit faster with added loop() clutter and timer management:
Code:
static uint32_t loopCnt = 0;
elapsedMicros tWait = 0;
void setup()
{
while ( !Serial);
tWait = 0;
}
void loop()
{
loopCnt++; // change every second
if ( tWait >= 1000000 ) {
Serial.print("loop()/sec=");
Serial.print(loopCnt);
Serial.print(" us=");
Serial.println(micros());
loopCnt = 0;
tWait -= 1000000;
}
}
gives this:
Code:
loop()/sec=7316314 us=15417001
loop()/sec=7316313 us=16417001
loop()/sec=7316313 us=17417001
loop()/sec=7316314 us=18417001
And adding "void yield(){}" to prior code gives:
Code:
loop()/sec=13042097 us=99407001
loop()/sec=13042098 us=100407001
loop()/sec=13042096 us=101407001
loop()/sec=13042097 us=102407001