-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioModule.cpp
66 lines (55 loc) · 1.21 KB
/
RadioModule.cpp
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
#include <Arduino.h>
#define DEFAULT_SAMPLES_PER_AVERAGE 10
#define HIGH_TRIGGERED_THRESHOLD 100
class RadioModule
{
public:
RadioModule(int analogPin = 0, int samplesPerAverage = DEFAULT_SAMPLES_PER_AVERAGE)
: pin(analogPin),
samplesPerAverage(samplesPerAverage)
{
phase = 0;
currentAnalogValue = -1;
values = new int[samplesPerAverage];
}
void update()
{
values[phase] = analogRead(pin);
// update phase
phase = (phase + 1) % samplesPerAverage;
// update avg value
calculateAverageValue();
if (currentAnalogValue == 0)
triggered = true;
else
triggered = false;
}
int getAverageValue() const
{
return currentAnalogValue;
}
bool isTriggered() const
{
return triggered;
}
private:
int pin;
int samplesPerAverage;
int *values;
int phase;
int currentAnalogValue;
bool triggered;
void calculateAverageValue()
{
int accumulator = 0;
for (int i = 0; i < samplesPerAverage; ++i)
{
accumulator += values[i];
}
currentAnalogValue = round(double(accumulator) / samplesPerAverage);
}
void setSamplesPerAverage(int samplesPerAverage)
{
this->samplesPerAverage = samplesPerAverage;
}
};