6#include <AH/Filters/EMA.hpp>
12#include <AH/STL/type_traits>
13#include <AH/STL/utility>
22template <u
int8_t FilterShiftFactor,
class FilterType,
class AnalogType>
24 constexpr static uint8_t value =
25 min(
sizeof(FilterType) * CHAR_BIT -
ADC_BITS - FilterShiftFactor,
26 sizeof(AnalogType) * CHAR_BIT -
ADC_BITS);
38 FilterShiftFactor, FilterType, AnalogType>::value>
52 AnalogType initial = 0)
55 AnalogType>(initial)) {}
59 AnalogType initial = 0)
73 void reset(AnalogType value = 0) {
75 AnalogType, AnalogType>(value);
76 filter.reset(widevalue);
77 hysteresis.setValue(widevalue);
87 AnalogType widevalue = getRawValue();
88 filter.reset(widevalue);
89 hysteresis.setValue(widevalue);
129 AnalogType input = getRawValue();
130 input = filter.filter(input);
131 input = mapFnHelper(input);
132 return hysteresis.update(input);
146 AnalogType
getValue()
const {
return hysteresis.getValue(); }
156 return getValue() * (1.0f / (ldexpf(1.0f, Precision) - 1.0f));
169 return increaseBitDepth<ADC_BITS + IncRes, ADC_BITS, AnalogType>(value);
176 return (1ul << (
ADC_BITS + IncRes)) - 1ul;
187#if HAS_ANALOG_READ_RESOLUTION
196 template <
typename M = MappingFunction>
197 typename std::enable_if<std::is_constructible<bool, M>::value,
200 return bool(mapFn) ? mapFn(input) : input;
207 template <
typename M = MappingFunction>
208 typename std::enable_if<!std::is_constructible<bool, M>::value,
221 ADC_BITS + IncRes + FilterShiftFactor <=
sizeof(FilterType) * CHAR_BIT,
222 "Error: FilterType is not wide enough to hold the maximum value");
224 ADC_BITS + IncRes <=
sizeof(AnalogType) * CHAR_BIT,
225 "Error: AnalogType is not wide enough to hold the maximum value");
228 "Error: Precision is larger than the increased ADC precision");
229 static_assert(EMA_t::supports_range(AnalogType(0), getMaxRawValue()),
230 "Error: EMA filter type doesn't support full ADC range");
270template <uint8_t Precision = 10,
274 FilterShiftFactor, FilterType, AnalogType>::value>
277 FilterShiftFactor, FilterType, AnalogType,
290 FilterShiftFactor, FilterType, AnalogType,
291 IncRes>(analogPin, nullptr, initial) {}
319 constexpr AnalogType maxval = FilteredAnalog::getMaxRawValue();
320 this->map([](AnalogType val) -> AnalogType {
return maxval - val; });
#define BEGIN_AH_NAMESPACE
AH::function_traits< decltype(::digitalWrite)>::argument_t< 0 > ArduinoPin_t
analog_t(*)(analog_t) MappingFunction
#define AH_DIAGNOSTIC_POP()
#define AH_DIAGNOSTIC_WERROR()
A class that reads and filters an analog input.
FilteredAnalog(pin_t analogPin, AnalogType initial=0)
Construct a new FilteredAnalog object.
FilteredAnalog(ArduinoPin_t analogPin, AnalogType initial=0)
Construct a new FilteredAnalog object.
FilteredAnalog()
Construct a new FilteredAnalog object.
void invert()
Invert the analog value.
AnalogType(*)(AnalogType) MappingFunction
A function pointer to a mapping function to map analog values.
FilteredAnalog base class with generic MappingFunction.
GenericFilteredAnalog(pin_t analogPin, MappingFunction mapFn, AnalogType initial=0)
Construct a new GenericFilteredAnalog object.
void resetToCurrentValue()
Reset the filtered value to the value that's currently being measured at the analog input.
static void setupADC()
Select the configured ADC resolution.
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.
void map(MappingFunction fn)
Specify a mapping function/functor that is applied to the analog value after filtering and before app...
AnalogType getRawValue() const
Read the raw value of the analog input without any filtering or mapping applied, but with its bit dep...
void reset(AnalogType value=0)
Reset the filter to the given value.
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.
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 numb...
bool update()
Read the analog input value, apply the mapping function, and update the average.
MappingFunction & getMappingFunction()
Get a reference to the mapping function.
GenericFilteredAnalog(ArduinoPin_t analogPin, MappingFunction mapFn, AnalogType initial=0)
Construct a new GenericFilteredAnalog object.
Hysteresis< ADC_BITS+IncRes - Precision, AnalogType, AnalogType > hysteresis
const MappingFunction & getMappingFunction() const
Get a reference to the mapping function.
static constexpr AnalogType getMaxRawValue()
Get the maximum value that can be returned from getRawValue.
Exponential moving average filter.
A class for applying hysteresis to a given input.
analog_t analogRead(pin_t pin)
An ExtIO version of the Arduino function.
constexpr auto min(const T &a, const U &b) -> decltype(b< a ? b :a)
Return the smaller of two numbers/objects.
T_out increaseBitDepth(T_in in)
Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide,...
uint16_t analog_t
The type returned from analogRead and similar functions.
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
constexpr uint8_t ANALOG_FILTER_SHIFT_FACTOR
The factor for the analog filter: Difference equation: where .
constexpr uint8_t ADC_BITS
The bit depth to use for the ADC (Analog to Digital Converter).
uint16_t ANALOG_FILTER_TYPE
The unsigned integer type to use for analog inputs during filtering.
Type for storing pin numbers of Extended Input/Output elements.
Helper to determine how many of the remaining bits of the filter data types can be used to achieve hi...