A simple event driven system for microcontroller in C++.
- define your event id
#define ADC_START 1
#define ADC_END 2
#define LED 3
#define DEBUG_PRINT 4
- implement
uint32_t us_ticker_read()
which return microseconds
uint32_t us_ticker_read(){
return micros();
}
#include "EventDriven.h"
#include "TimerEv.h"
EventDriven eventloop;
TimerEv timerev(eventloop);
// define your events
#define A 1
#define B 2
#define C 3
// ...
int main(){
timerev.add(C, 50000, 1, 1); // add repeat C timer (50 ms)
eventloop.push(A); // first call A (emit A event)
while(1){
timerev.tick();
switch(eventloop.get()){
case A:
// do somthing
// ...
timerev.add(B, 10*1000, 0, 1); // emit B event 10 ms later
break;
case B:
// do somthing
// ...
eventloop.push(A); // emit A event right now
break;
// this do every 50 ms
case C:
led = !led;
break;
}
}
return 0;
}
[TOC]
creat a event loop system.
push a event into event loop.
return next event in event loop.
clear all event in event loop.
return is event in event loop or not.
return the event count in event loop.
return is event loop full or not.
creat a timer system base on event loop.
add a timer event to timer slots.
enable a timer event in timer slots.
disable a timer event in timer slots.
remove timer event in timer slots.
clear all timer event in timer slots.
call this for emit event.
return the timer event count in timer slots.
return is timer slots full or not.