#include "main.h"
#include <otSerial.h>
#include <otPrintf.h>
#include <otTimer.h>
#include <otTimeTrap.h>
#include <stdarg.h>
otTimeTrap events;
void print(U32 & cnt, U16 id, F64 acc)
{
U16 h, m, s;
events.time(h, m, s);
printf("[%02d:%02d:%02d] Event %ds > [%5u] : T = %10llu us, Tt = %10.3lf ms, Jit = %8lld us\n", h, m, s, id + 1, cnt++, events.elapsedTime(id), acc, events.jitter(id));
}
void event_1()
{
static U32 cnt = 1;
static F64 acc = 0;
acc += (F64) events.elapsedTime(0) / 1000.0;
print(cnt, 0, acc);
DelayMs(50);
}
void event_2()
{
static U32 cnt = 1;
static F64 acc = 0;
acc += (F64) events.elapsedTime(1) / 1000.0;
print(cnt, 1, acc);
DelayMs(100);
}
void event_3()
{
static U32 cnt = 1;
static F64 acc = 0;
acc += (F64) events.elapsedTime(2) / 1000.0;
print(cnt, 2, acc);
DelayMs(150);
}
void event_4()
{
static U32 cnt = 1;
static F64 acc = 0;
acc += (F64) events.elapsedTime(3) / 1000.0;
print(cnt, 3, acc);
DelayMs(200);
}
void setup()
{
UartEnableSignals(UART0);
UartSetBaud(UART0, 115200);
setStdout(4096, UART0);
DelayMs(1000);
if(!events.init(32, TIMER0))
{
printf("TimeTrap init failure!\n");
IDLE;
}
printf("ID = %u \n", events.addTrap(&event_1, 1, otTimeTrap::TB_S));
printf("ID = %u \n", events.addTrap(&event_2, 2, otTimeTrap::TB_S));
printf("ID = %u \n", events.addTrap(&event_3, 3, otTimeTrap::TB_S));
printf("ID = %u \n", events.addTrap(&event_4, 4, otTimeTrap::TB_S));
events.sync();
}
void loop()
{
events.process();
}