template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t IncRes = MaximumFilteredAnalogIncRes< FilterShiftFactor, FilterType, AnalogType>::value>
class FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, IncRes >
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.
- Template Parameters
-
| 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 272 of file FilteredAnalog.hpp.
|
| | FilteredAnalog (pin_t analogPin, AnalogType initial=0) |
| | Construct a new FilteredAnalog object.
|
| | FilteredAnalog (ArduinoPin_t analogPin, AnalogType initial=0) |
| | FilteredAnalog () |
| | Construct a new FilteredAnalog object.
|
| void | invert () |
| | Invert the analog value.
|
| void | reset (AnalogType value=0) |
| | Reset the filter to the given value.
|
| void | resetToCurrentValue () |
| | Reset the filtered value to the value that's currently being measured at the analog input.
|
| void | map (MappingFunction fn) |
| | Specify a mapping function/functor that is applied to the analog value after filtering and before applying hysteresis.
|
| MappingFunction & | getMappingFunction () |
| | Get a reference to the mapping function.
|
| const MappingFunction & | getMappingFunction () const |
| | Get a reference to the mapping function.
|
| bool | update () |
| | Read the analog input value, apply the mapping function, and update the average.
|
| AnalogType | getValue () const |
| | Get the filtered value of the analog input (with the mapping function applied).
|
| 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.
|
| AnalogType | getRawValue () const |
| | Read the raw value of the analog input without any filtering or mapping applied, but with its bit depth increased by IncRes.
|
|
| template<typename M = MappingFunction> |
| std::enable_if< std::is_constructible< bool, M >::value, AnalogType >::type | mapFnHelper (AnalogType input) |
| | Helper function that applies the mapping function if it's enabled.
|
| template<typename M = MappingFunction> |
| std::enable_if<!std::is_constructible< bool, M >::value, AnalogType >::type | mapFnHelper (AnalogType input) |
| | Helper function that applies the mapping function without checking if it's enabled.
|
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t IncRes = MaximumFilteredAnalogIncRes< FilterShiftFactor, FilterType, AnalogType>::value>
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t IncRes = MaximumFilteredAnalogIncRes< FilterShiftFactor, FilterType, AnalogType>::value>
Construct a new FilteredAnalog object.
- Parameters
-
| analogPin | The analog pin to read from. |
| initial | The initial value of the filter. |
Definition at line 285 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t IncRes = MaximumFilteredAnalogIncRes< FilterShiftFactor, FilterType, AnalogType>::value>
Construct a new FilteredAnalog object.
This constructor should not be used.
It is just a way to easily create arrays of FilteredAnalog objects, and initializing them later. Trying to update a default-constructed or uninitialized FilteredAnalog object will result in a fatal runtime error.
Definition at line 302 of file FilteredAnalog.hpp.
template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t IncRes = MaximumFilteredAnalogIncRes< FilterShiftFactor, FilterType, AnalogType>::value>
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.
- Note
- This overrides the mapping function set by the map method.
Definition at line 315 of file FilteredAnalog.hpp.