-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChangesDetector.h
78 lines (57 loc) · 1.62 KB
/
ChangesDetector.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
#pragma once
typedef void (*ChangesDetectedCallbackType)(void);
typedef void (*GetValuesCallbackType)(float *);
template <int SZ>
class ChangesDetector {
const float THRESHOLD = 0.2;
private:
float values [SZ] = {0}; // Remembered values
ChangesDetectedCallbackType changesDetectedCallback = [](){};
GetValuesCallbackType getValuesCallback = [](float *){};
public:
ChangesDetector();
void loop();
void setChangesDetectedCallback(void callback(void));
void setGetValuesCallback(void callback(float[]));
void remember();
};
template <int SZ>
ChangesDetector<SZ>::ChangesDetector()
{
}
template <int SZ>
void ChangesDetector<SZ>::setChangesDetectedCallback(void callback(void))
{
this->changesDetectedCallback = callback;
}
template <int SZ>
void ChangesDetector<SZ>::setGetValuesCallback(void callback(float *))
{
this->getValuesCallback = callback;
}
template <int SZ>
void ChangesDetector<SZ>::loop()
{
float buf[SZ] = {0};
this->getValuesCallback(buf);
bool detected = false;
// check each value if changed enough
for(int i = 0; i < SZ; i++){
if(std::abs(buf[i] - this->values[i]) >= THRESHOLD){
detected = true;
}
}
if(detected){
this->changesDetectedCallback();
this->remember();
}
}
template <int SZ>
void ChangesDetector<SZ>::remember()
{
float buf[SZ] = {0};
this->getValuesCallback(buf);
for(int i = 0; i < SZ; i++){
this->values[i] = buf[i];
}
}