1 | //macros for a non blocking wait, periodic or x-times repeated intruction block!
|
2 | //needs millis() or micros() system function
|
3 | //should be located inside a fast loop
|
4 |
|
5 | //usage:
|
6 | // WAITBLOCK(MSEC,T2B,100) {digitalWrite(LED_PIN, digitalRead(LED_PIN)^1); //...} //fast flashed led
|
7 | // PERIODIC(MSEC,T2B,500) {digitalWrite(LED_PIN, digitalRead(LED_PIN)^1); //...} //blinky led
|
8 | // XPERIODIC(MSEC,T2B,500,42) {digitalWrite(LED_PIN, digitalRead(LED_PIN)^1); //...} //42-times blinky led
|
9 |
|
10 | //hints:
|
11 | // change the data type in case of shorter time period
|
12 | // uint32_t micros() 4µs tick, rolls over after 2^32 /1000/1000/60 = 71.58 min
|
13 | // uint32_t millis() rolls over after 2^32 /1000/60/60/24 = 49.710 days
|
14 | // uint16_t micros() 4µs tick, rolls over after 2^16 /1000 = 65.535 ms
|
15 | // uint16_t millis() rolls over after 2^16 /1000 = 65.535 sec
|
16 | // refresh time stamp +=t, if a more exact repeated time period is needed
|
17 |
|
18 | //todo: -open,+done,!prio,?=questionable
|
19 | // ?postfix ul for t needed
|
20 | // ?refresh time stamp via +=t or =millis()/micros()
|
21 |
|
22 | #ifndef NONBL_DELAY_H
|
23 | #define NONBL_DELAY_H
|
24 |
|
25 | //options
|
26 | #define USEC micros() //timer tick base
|
27 | #define MSEC millis()
|
28 | #define T2B uint16_t //timer data type
|
29 | #define T4B uint32_t
|
30 |
|
31 | //create an unique name from given parameter and line number
|
32 | #define UNIQUE(name) (name ## __LINE__)
|
33 |
|
34 | //creates an unique timer for each instance
|
35 | #define WAITBLOCK(tb, dt, t) \
|
36 | static dt UNIQUE(tmr) = (dt) tb; \
|
37 | if (tb - UNIQUE(tmr) >= t)
|
38 |
|
39 | //creates an unique timer for each instance
|
40 | #define PERIODIC(tb, dt, t) \
|
41 | static dt UNIQUE(tmr) = (dt) tb; \
|
42 | if ((tb - UNIQUE(tmr) >= t) \
|
43 | && (UNIQUE(tmr) = tb, 1)) //(UNIQUE(tmr) += t, 1))
|