-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCounter.cpp
32 lines (27 loc) · 960 Bytes
/
Counter.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
#include "Counter.h"
#include <Arduino.h>
Counter::Counter(const char* name, duration queryInterval, duration updateInterval, float multiplier): Sensor(name), state(0), multiplier(multiplier), queryInterval(queryInterval), updateInterval(updateInterval) {
}
void Counter::update() {
++state;
}
void Counter::printStatus0(Stream& stream, int value) {
stream.print(name);
stream.print('=');
stream.println(value * multiplier);
}
bool changedEnough(const int value, const int lastValue) {
const float diff = abs(value - lastValue);
return diff > 0.1 && diff >= lastValue * 1.1;
}
void Counter::printStatus(Stream& stream) {
unsigned long now = millis();
unsigned int duration = now-lastUpdate;
int value = state;
if(duration >= updateInterval || duration > 1000 && changedEnough(value, lastValue)) {
lastUpdate = now;
printStatus0(stream, value);
state = 0;
lastValue = value;
}
}