forked from clone45/EquationComposer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleInputSmooth.cpp
42 lines (32 loc) · 907 Bytes
/
ModuleInputSmooth.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
#include "Arduino.h"
#include "ModuleInputSmooth.h"
#include "defines.h"
ModuleInputSmooth::ModuleInputSmooth(Module *parent_module)
{
this->parent_module = parent_module;
this->value = 0;
index = 0; // the index of the current reading
total = 0; // the running total
average = 0; // the average
for (int i = 0; i < 8; i++)
{
readings[i] = 0;
}
}
uint16_t ModuleInputSmooth::compute()
{
// subtract the last reading:
total = total - readings[index];
// read from the input
readings[index] = parent_module->compute();
// add the reading to the total:
total = total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array, then wrap around to the beginning
if (index >= 8) index = 0;
// calculate the average:
// average = total / 8;
average = total >> 3;
return(average);
}