MIDI Control Surface library for Arduino
|
Go to the documentation of this file.
6 #include <AH/Filters/EMA.hpp>
12 #include <AH/STL/type_traits>
13 #include <AH/STL/utility>
22 template <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)) {}
66 void reset(AnalogType value = 0) {
108 AnalogType input = getRawValue();
109 input = filter.filter(input);
110 input = mapFnHelper(input);
111 return hysteresis.update(input);
125 AnalogType
getValue()
const {
return hysteresis.getValue(); }
135 return getValue() * (1.0f / (ldexpf(1.0f, Precision) - 1.0f));
151 return (1UL << (
ADC_BITS + IncRes)) - 1;
162 #if HAS_ANALOG_READ_RESOLUTION
171 template <
typename M = MappingFunction>
172 typename std::enable_if<std::is_constructible<bool, M>::value,
175 return bool(mapFn) ? mapFn(input) : input;
182 template <
typename M = MappingFunction>
183 typename std::enable_if<!std::is_constructible<bool, M>::value,
194 ADC_BITS + IncRes + FilterShiftFactor <=
sizeof(FilterType) * CHAR_BIT,
195 "Error: FilterType is not wide enough to hold the maximum value");
197 ADC_BITS + IncRes <=
sizeof(AnalogType) * CHAR_BIT,
198 "Error: AnalogType is not wide enough to hold the maximum value");
201 "Error: Precision is larger than the increased ADC precision");
241 template <uint8_t Precision = 10,
245 FilterShiftFactor, FilterType, AnalogType>::value>
248 FilterShiftFactor, FilterType, AnalogType,
261 FilterShiftFactor, FilterType, AnalogType,
262 IncRes>(analogPin, nullptr, initial) {}
276 constexpr AnalogType maxval = FilteredAnalog::getMaxRawValue();
277 this->map([](AnalogType val) -> AnalogType {
return maxval - val; });
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.
const MappingFunction & getMappingFunction() const
Get a reference to the mapping function.
Hysteresis< ADC_BITS+IncRes - Precision, AnalogType, AnalogType > hysteresis
static void setupADC()
Select the configured ADC resolution.
T_out increaseBitDepth(T_in in)
Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide,...
analog_t(*)(analog_t) MappingFunction
A function pointer to a mapping function to map analog values.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Helper to determine how many of the remaining bits of the filter data types can be used to achieve hi...
FilteredAnalog base class with generic MappingFunction.
constexpr uint8_t ANALOG_FILTER_SHIFT_FACTOR
The factor for the analog filter: Difference equation: where .
float getFloatValue() const
Get the filtered value of the analog input with the mapping function applied as a floating point numb...
constexpr auto min(const T &a, const U &b) -> decltype(b< a ? b :a)
Return the smaller of two numbers/objects.
#define AH_DIAGNOSTIC_POP()
A class that reads and filters an analog input.
EMA< FilterShiftFactor, FilterType > filter
AnalogType getRawValue() const
Read the raw value of the analog input without any filtering or mapping applied, but with its bit dep...
AnalogType getValue() const
Get the filtered value of the analog input (with the mapping function applied).
constexpr uint8_t ADC_BITS
The bit depth to use for the ADC (Analog to Digital Converter).
bool update()
Read the analog input value, apply the mapping function, and update the average.
analog_t analogRead(pin_t pin)
An ExtIO version of the Arduino function.
A class for single-pole infinite impulse response filters or exponential moving average filters.
void map(MappingFunction fn)
Specify a mapping function/functor that is applied to the analog value after filtering and before app...
GenericFilteredAnalog(pin_t analogPin, MappingFunction mapFn, AnalogType initial=0)
Construct a new GenericFilteredAnalog object.
A class for applying hysteresis to a given input.
MappingFunction & getMappingFunction()
Get a reference to the mapping function.
FilteredAnalog(pin_t analogPin, AnalogType initial=0)
Construct a new FilteredAnalog object.
uint16_t analog_t
The type returned from analogRead and similar functions.
uint16_t ANALOG_FILTER_TYPE
The unsigned integer type to use for analog inputs during filtering.
constexpr static AnalogType getMaxRawValue()
Get the maximum value that can be returned from getRawValue.
void invert()
Invert the analog value.
#define AH_DIAGNOSTIC_WERROR()
#define BEGIN_AH_NAMESPACE
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.
analog_t(*)(analog_t) MappingFunction