Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
time_utils.h
Go to the documentation of this file.
1
5#ifndef TIME_UTILS_HPP
6#define TIME_UTILS_HPP
7
8#include <cstdint>
9
11
12namespace lampda {
13
16{
17public:
18 uint32_t mPrevTrigger;
19 uint32_t mPeriod;
20 bool isInit;
21
23 {
24 isInit = false;
25 reset();
26 mPeriod = 1;
27 };
28 CEveryNMillis(uint32_t period)
29 {
30 isInit = false;
31 reset();
32 setPeriod(period);
33 };
34 void setPeriod(uint32_t period) { mPeriod = period; };
35 uint32_t getTime() const { return platform::time_ms(); };
36 uint32_t getPeriod() const { return mPeriod; };
37 uint32_t getElapsed() const { return getTime() - mPrevTrigger; }
38 uint32_t getRemaining() const { return mPeriod - getElapsed(); }
39 uint32_t getLastTriggerTime() const { return mPrevTrigger; }
40 bool ready()
41 {
42 bool isReady = not isInit or (getElapsed() >= mPeriod);
43 if (isReady)
44 {
45 isInit = true;
46 reset();
47 }
48 return isReady;
49 }
50 void reset() { mPrevTrigger = getTime(); };
51 void trigger() { mPrevTrigger = getTime() - mPeriod; };
52
53 operator bool() { return ready(); }
54};
55
56#define EVERY_N_MILLIS_I(NAME, N) \
57 static CEveryNMillis NAME(N); \
58 if (NAME)
59
60#define EVERY_N_MILLIS_REFRESH_I(NAME, N) \
61 static CEveryNMillis NAME(N); \
62 NAME.setPeriod(N); \
63 if (NAME)
64
65#define EVERY_N_MILLIS_WITH_COND_I(NAME, COND, N) \
66 static CEveryNMillis NAME(N); \
67 if (COND || NAME)
68
69// call the following block of code every N milliseconds
70#define EVERY_N_MILLIS(N) EVERY_N_MILLIS_I(PER##__COUNTER__, N)
71// call the following block of code every N milliseconds, with a possible update to the delay
72#define EVERY_N_MILLIS_REFRESH(N) EVERY_N_MILLIS_REFRESH_I(PER##__COUNTER__, N)
73// call the following block of code every N milliseconds, with an alternative trigge boolean
74#define EVERY_N_MILLIS_COND(N, COND) EVERY_N_MILLIS_WITH_COND_I(PER##__COUNTER__, COND, N)
75
76} // namespace lampda
77
78#endif
define time routines
Definition: time_utils.h:16
uint32_t time_ms(void)
Returns the number of milliseconds since the Arduino board began running the current program....
Definition: time.cpp:16
Program scope.
Definition: control_fixed_modes.hpp:12
Define all time related platform functions.