Control Surface pin-t-adl
MIDI Control Surface library for Arduino
FilteredAnalog.hpp
Go to the documentation of this file.
1#pragma once
2
4AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5
6#include <AH/Filters/EMA.hpp>
11#include <AH/Math/MinMaxFix.hpp>
12#include <AH/STL/type_traits> // std::enable_if, std::is_constructible
13#include <AH/STL/utility> // std::forward
15
17
22template <uint8_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);
27};
28
34template <class MappingFunction, uint8_t Precision = 10,
35 uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR,
36 class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t,
37 uint8_t IncRes = MaximumFilteredAnalogIncRes<
38 FilterShiftFactor, FilterType, AnalogType>::value>
40 public:
52 AnalogType initial = 0)
53 : analogPin(analogPin), mapFn(std::forward<MappingFunction>(mapFn)),
54 filter(increaseBitDepth<ADC_BITS + IncRes, Precision, AnalogType,
55 AnalogType>(initial)) {}
56
59 AnalogType initial = 0)
60 : GenericFilteredAnalog(pin_t(analogPin),
61 std::forward<MappingFunction>(mapFn), initial) {
62 }
63
73 void reset(AnalogType value = 0) {
74 AnalogType widevalue = increaseBitDepth<ADC_BITS + IncRes, Precision,
75 AnalogType, AnalogType>(value);
76 filter.reset(widevalue);
77 hysteresis.setValue(widevalue);
78 }
79
87 AnalogType widevalue = getRawValue();
88 filter.reset(widevalue);
89 hysteresis.setValue(widevalue);
90 }
91
108 void map(MappingFunction fn) { mapFn = std::forward<MappingFunction>(fn); }
109
117 const MappingFunction &getMappingFunction() const { return mapFn; }
118
128 bool update() {
129 AnalogType input = getRawValue(); // read the raw analog input value
130 input = filter.filter(input); // apply a low-pass EMA filter
131 input = mapFnHelper(input); // apply the mapping function
132 return hysteresis.update(input); // apply hysteresis, and return true
133 // if the value changed since last time
134 }
135
146 AnalogType getValue() const { return hysteresis.getValue(); }
147
155 float getFloatValue() const {
156 return getValue() * (1.0f / (ldexpf(1.0f, Precision) - 1.0f));
157 }
158
163 AnalogType getRawValue() const {
164 AnalogType value = ExtIO::analogRead(analogPin);
165#ifdef ESP8266
166 if (value > 1023)
167 value = 1023;
168#endif
169 return increaseBitDepth<ADC_BITS + IncRes, ADC_BITS, AnalogType>(value);
170 }
171
175 constexpr static AnalogType getMaxRawValue() {
176 return (1ul << (ADC_BITS + IncRes)) - 1ul;
177 }
178
186 static void setupADC() {
187#if HAS_ANALOG_READ_RESOLUTION
188 analogReadResolution(ADC_BITS);
189#endif
190 }
191
192 private:
196 template <typename M = MappingFunction>
197 typename std::enable_if<std::is_constructible<bool, M>::value,
198 AnalogType>::type
199 mapFnHelper(AnalogType input) {
200 return bool(mapFn) ? mapFn(input) : input;
201 }
202
207 template <typename M = MappingFunction>
208 typename std::enable_if<!std::is_constructible<bool, M>::value,
209 AnalogType>::type
210 mapFnHelper(AnalogType input) {
211 return mapFn(input);
212 }
213
214 private:
217
219
220 static_assert(
221 ADC_BITS + IncRes + FilterShiftFactor <= sizeof(FilterType) * CHAR_BIT,
222 "Error: FilterType is not wide enough to hold the maximum value");
223 static_assert(
224 ADC_BITS + IncRes <= sizeof(AnalogType) * CHAR_BIT,
225 "Error: AnalogType is not wide enough to hold the maximum value");
226 static_assert(
227 Precision <= ADC_BITS + IncRes,
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");
231
233 Hysteresis<ADC_BITS + IncRes - Precision, AnalogType, AnalogType>
235};
236
270template <uint8_t Precision = 10,
271 uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR,
272 class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t,
273 uint8_t IncRes = MaximumFilteredAnalogIncRes<
274 FilterShiftFactor, FilterType, AnalogType>::value>
276 : public GenericFilteredAnalog<AnalogType (*)(AnalogType), Precision,
277 FilterShiftFactor, FilterType, AnalogType,
278 IncRes> {
279 public:
288 FilteredAnalog(pin_t analogPin, AnalogType initial = 0)
289 : GenericFilteredAnalog<AnalogType (*)(AnalogType), Precision,
290 FilterShiftFactor, FilterType, AnalogType,
291 IncRes>(analogPin, nullptr, initial) {}
292
294 FilteredAnalog(ArduinoPin_t analogPin, AnalogType initial = 0)
295 : FilteredAnalog(pin_t(analogPin), initial) {}
296
306
309 using MappingFunction = AnalogType (*)(AnalogType);
310
318 void invert() {
319 constexpr AnalogType maxval = FilteredAnalog::getMaxRawValue();
320 this->map([](AnalogType val) -> AnalogType { return maxval - val; });
321 }
322};
323
325
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
AH::function_traits< decltype(::digitalWrite)>::argument_t< 0 > ArduinoPin_t
analog_t(*)(analog_t) MappingFunction
Definition: Def.hpp:23
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
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.
Definition: EMA.hpp:58
A class for applying hysteresis to a given input.
Definition: Hysteresis.hpp:36
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.
Definition: MinMaxFix.hpp:15
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...