A class that reads and filters an analog input. More...
#include <AH/Hardware/FilteredAnalog.hpp>
Public Types | |
using | MappingFunction = AnalogType(*)(AnalogType) |
A function pointer to a mapping function to map analog values. More... | |
Public Member Functions | |
FilteredAnalog (pin_t analogPin) | |
Construct a new FilteredAnalog object. More... | |
void | map (MappingFunction fn) |
Specify a mapping function that is applied to the raw analog value before filtering. More... | |
void | invert () |
Invert the analog value. More... | |
bool | update () |
Read the analog input value, apply the mapping function, and update the average. More... | |
AnalogType | getValue () const |
Get the filtered value of the analog input (with the mapping function applied). More... | |
float | getFloatValue () const |
Get the filtered value of the analog input with the mapping function applied as a floating point number from 0.0 to 1.0. More... | |
AnalogType | getRawValue () const |
Read the raw value of the analog input any filtering or mapping applied, but with its bit depth increased by IncRes . More... | |
Static Public Member Functions | |
static void | setupADC () |
Select the configured ADC resolution. More... | |
Private Attributes | |
const pin_t | analogPin |
MappingFunction | mapFn = nullptr |
EMA< FilterShiftFactor, FilterType > | filter |
Hysteresis< ADC_BITS+IncRes - Precision, AnalogType, AnalogType > | hysteresis |
A class that reads and filters an analog input.
A map function can be applied to the analog value (e.g. to compensate for logarithmic taper potentiometers or to calibrate the range). The analog input value is filtered using an exponential moving average filter. The default settings for this filter can be changed in Settings.hpp.
After filtering, hysteresis is applied to prevent flipping back and forth between two values when the input is not changing.
Precision | The number of bits of precision the output should have. |
FilterShiftFactor | The number of bits used for the EMA filter. The pole location is \( 1 - \left(\frac{1}{2}\right)^{\text{FilterShiftFactor}} \). A lower shift factor means less filtering ( \(0\) is no filtering), and a higher shift factor means more filtering (and more latency). |
FilterType | The type to use for the intermediate types of the filter. Should be at least \( \text{ADC_BITS} + \text{IncRes} + \text{FilterShiftFactor} \) bits wide. |
AnalogType | The type to use for the analog values. Should be at least \( \text{ADC_BITS} + \text{IncRes} \) bits wide. |
IncRes | The number of bits to increase the resolution of the analog reading by. |
Definition at line 55 of file FilteredAnalog.hpp.
using MappingFunction = AnalogType (*)(AnalogType) |
A function pointer to a mapping function to map analog values.
Definition at line 67 of file FilteredAnalog.hpp.
|
inline |
Construct a new FilteredAnalog object.
analogPin | The analog pin to read from. |
Definition at line 63 of file FilteredAnalog.hpp.
|
inline |
Specify a mapping function that is applied to the raw analog value before filtering.
fn | A function pointer to the mapping function. This function should take the filtered value (of ADC_BITS + IncRes bits wide) as a parameter, and should return a value of ADC_BITS + IncRes bits wide. |
Definition at line 86 of file FilteredAnalog.hpp.
|
inline |
Invert the analog value.
For example, if the precision is 10 bits, when the analog input measures 1023, the output will be 0, and when the analog input measures 0, the output will be 1023.
map
method. Definition at line 95 of file FilteredAnalog.hpp.
|
inline |
Read the analog input value, apply the mapping function, and update the average.
true | The value changed since last time it was updated. |
false | The value is still the same. |
Definition at line 109 of file FilteredAnalog.hpp.
|
inline |
Get the filtered value of the analog input (with the mapping function applied).
Precision
bits wide. Definition at line 128 of file FilteredAnalog.hpp.
|
inline |
Get the filtered value of the analog input with the mapping function applied as a floating point number from 0.0 to 1.0.
Definition at line 137 of file FilteredAnalog.hpp.
|
inline |
Read the raw value of the analog input any filtering or mapping applied, but with its bit depth increased by IncRes
.
Definition at line 145 of file FilteredAnalog.hpp.
|
inlinestatic |
Select the configured ADC resolution.
By default, it is set to the maximum resolution supported by the hardware.
Definition at line 157 of file FilteredAnalog.hpp.
|
private |
Definition at line 164 of file FilteredAnalog.hpp.
|
private |
Definition at line 166 of file FilteredAnalog.hpp.
|
private |
Definition at line 170 of file FilteredAnalog.hpp.
|
private |
Definition at line 180 of file FilteredAnalog.hpp.