Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
src
EMASampler.cpp
1
#include <Arduino.h>
2
#include "MutilaDebug.h"
3
#include "EMASampler.h"
4
#include "Millis.h"
5
6
EMASampler::EMASampler
(
const
uint8_t pin,
const
uint16_t periodMs,
const
float
alpha) :
7
AbstractSampler
(pin, periodMs),
8
_alpha(alpha),
9
_lastUpdated(0),
10
_movingAverage(0.),
11
_lastSample(0)
12
{
13
}
14
15
void
EMASampler::begin
()
16
{
17
_DBLN(F(
"EMASampler::begin"
));
18
// Note: no need to set pinMode for analog inputs
19
_movingAverage
= analogRead(
_pin
);
20
_lastSample
=
_movingAverage
;
21
_lastUpdated
= Millis();
22
}
23
24
void
EMASampler::update
()
25
{
26
if
(
_periodMs
== 0 || MillisSince(
_lastUpdated
) >=
_periodMs
) {
27
_lastSample
= analogRead(
_pin
);
28
_DB(F(
"EMASampler::update sample="
));
29
_DBLN(
_lastSample
);
30
_movingAverage
= (
_alpha
*
_lastSample
) + ((1-
_alpha
)*
_movingAverage
);
31
_lastUpdated
= Millis();
32
}
33
}
34
35
36
EMASampler::_alpha
float _alpha
alpha value used in EMA calculation
Definition:
EMASampler.h:70
AbstractSampler::_pin
const uint8_t _pin
pin to read data from
Definition:
AbstractSampler.h:43
AbstractSampler::_periodMs
const uint16_t _periodMs
minimum ms
Definition:
AbstractSampler.h:44
EMASampler::_movingAverage
float _movingAverage
most recently calculated mean value
Definition:
EMASampler.h:72
EMASampler::_lastUpdated
uint32_t _lastUpdated
when last sample taken
Definition:
EMASampler.h:71
EMASampler::EMASampler
EMASampler(const uint8_t pin, const uint16_t periodMs=10, const float alpha=0.5)
Definition:
EMASampler.cpp:6
EMASampler::_lastSample
int16_t _lastSample
keep the most recent sample
Definition:
EMASampler.h:73
EMASampler::begin
virtual void begin()
Definition:
EMASampler.cpp:15
EMASampler::update
virtual void update()
Definition:
EMASampler.cpp:24
AbstractSampler
Base class for analog pin sampling classes.
Definition:
AbstractSampler.h:12
Generated by
1.8.15