Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

TimeTrap - Code example
[TimeTrap - A process manager over time]

 #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));
 }
 
 // This procedure is executed every 1s
 void event_1()
 {
     static U32  cnt = 1;    // Event counter
     static F64  acc = 0;    // Total amount of events period as ms
     acc += (F64) events.elapsedTime(0) / 1000.0;
     print(cnt, 0, acc);
     // Emulate CPU load
     DelayMs(50);
 }
 
 // This procedure is executed every 2s
 void event_2()
 {
     static U32  cnt = 1;    // Event counter
     static F64  acc = 0;    // Total amount of events period as ms
     acc += (F64) events.elapsedTime(1) / 1000.0;
     print(cnt, 1, acc);
     // Emulate CPU load
     DelayMs(100);
 }
 
 // This procedure is executed every 3s
 void event_3()
 {
     static U32  cnt = 1;    // Event counter
     static F64  acc = 0;    // Total amount of events period as ms
     acc += (F64) events.elapsedTime(2) / 1000.0;
     print(cnt, 2, acc);
     // Emulate CPU load
     DelayMs(150);
 }
 
 // This procedure is executed every 4s
 void event_4()
 {
     static U32  cnt = 1;    // Event counter
     static F64  acc = 0;    // Total amount of events period as ms
     acc += (F64) events.elapsedTime(3) / 1000.0;
     print(cnt, 3, acc);
     // Emulate CPU load
     DelayMs(200);
 }
 
 void setup()
 {
        UartEnableSignals(UART0);
        UartSetBaud(UART0, 115200);
        setStdout(4096, UART0); // printf output is now directed on 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();  // Synchronization
     
     //events.sync(4, 10, 20, 50, 100);
 }
 
 void loop()
 {
                events.process();
 }
footer
otStudio - Library Reference - (C) 2020-23 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0