-
Notifications
You must be signed in to change notification settings - Fork 8
/
taskRunner.h
97 lines (80 loc) · 2.24 KB
/
taskRunner.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Flybrix Flight Controller -- Copyright 2018 Flying Selfie Inc. d/b/a Flybrix
*
* http://www.flybrix.com
*/
#ifndef task_runner_h
#define task_runner_h
#include <Arduino.h>
#include "utility/clock.h"
using TaskPtr = bool (*)();
constexpr uint32_t hzToMicros(float hz) {
return (uint32_t) (1000000.0f / hz);
}
struct StatTrack {
void log(uint32_t value) {
value_last = value;
value_min = std::min(value_min, value);
value_max = std::max(value_max, value);
value_sum += value;
}
void reset() {
value_last = 0;
value_min = 0xFFFFFFFF;
value_max = 0;
value_sum = 0;
}
uint32_t value_last{0};
uint32_t value_min{0xFFFFFFFF};
uint32_t value_max{0};
uint32_t value_sum{0};
};
class TaskRunner {
public:
TaskRunner(const char* _name, TaskPtr task, uint32_t desired_interval_us);
TaskRunner(const char* _name, TaskPtr task, uint32_t desired_interval_us, bool enabled);
TaskRunner(const char* _name, TaskPtr task, uint32_t desired_interval_us, bool enabled, bool always_log_stats);
~TaskRunner() {
free(name);
}
void setDesiredInterval(uint32_t value_usec, uint32_t disable_threshold_usec) {
desired_interval_us = value_usec;
if (value_usec < disable_threshold_usec){
if (!enabled)
last_update_us = ClockTime::now();
enabled = true;
}
else {
enabled = false;
}
}
bool process(uint32_t expected_time_until_next_attempt_usec);
bool isEnabled(){
return enabled;
}
void reset(ClockTime reset_time_us) {
last_update_us = reset_time_us;
}
void logExecution(uint32_t delay, uint32_t duration) {
delay_track.log(delay);
duration_track.log(duration);
++log_count;
}
void resetStats(){
delay_track.reset();
duration_track.reset();
log_count = 0;
}
char * name;
TaskPtr task;
uint32_t desired_interval_us;
ClockTime last_update_us;
StatTrack delay_track{0};
StatTrack duration_track{0};
uint32_t log_count{0};
uint32_t work_count{0};
private:
bool enabled;
bool always_log_stats;
};
#endif // task_runner_h