Control Surface  1.1.0
MIDI Control Surface library for Arduino
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample > Class Template Reference

A class that reads and filters an analog input. More...

#include <FilteredAnalog.hpp>

Collaboration diagram for AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >:

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 Upsample. More...
 

Static Public Member Functions

static void setupADC ()
 

Private Attributes

const pin_t analogPin
 
MappingFunction mapFn = nullptr
 
EMA< FilterShiftFactor, FilterType > filter
 
Hysteresis< ADC_BITS+Upsample - Precision, AnalogType, AnalogType > hysteresis
 

Detailed Description

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
class AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >

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
PrecisionThe number of bits of precision the output should have.
FilterShiftFactorThe number of bits used for the EMA filter. The pole location is \( 1 - \left(\frac{1}{2}\right)^{\mathrm{FilterShiftFactor}} \).
A lower shift factor means less filtering ( \(0\) is no filtering), and a higher shift factor means more filtering (and more latency).
FilterTypeThe type to use for the intermediate types of the filter.
Should be at least \( \mathrm{ADC_BITS} + \mathrm{Upsample} + \mathrm{FilterShiftFactor} \) bits wide.
AnalogTypeThe type to use for the analog values.
Should be at least \( \mathrm{ADC_BITS} + \mathrm{Upsample} \) bits wide.
UpsampleThe number of bits to upsample the analog reading by.

Definition at line 54 of file FilteredAnalog.hpp.

Member Typedef Documentation

◆ MappingFunction

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
using AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::MappingFunction = AnalogType (*)(AnalogType)

A function pointer to a mapping function to map analog values.

See also
map()

Definition at line 66 of file FilteredAnalog.hpp.

Constructor & Destructor Documentation

◆ FilteredAnalog()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::FilteredAnalog ( pin_t  analogPin)
inline

Construct a new FilteredAnalog object.

Parameters
analogPinThe analog pin to read from.

Definition at line 62 of file FilteredAnalog.hpp.

Member Function Documentation

◆ map()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
void AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::map ( MappingFunction  fn)
inline

Specify a mapping function that is applied to the raw analog value before filtering.

Parameters
fnA function pointer to the mapping function. This function should take the filtered value (of ADC_BITS + Upsample bits wide) as a parameter, and should return a value of ADC_BITS + Upsample bits wide.
Note
Applying the mapping function before filtering could result in the noise being amplified to such an extent that filtering it afterwards would be ineffective.
Applying it after hysteresis would result in a lower resolution.
That's why the mapping function is applied after filtering and before hysteresis.

Definition at line 85 of file FilteredAnalog.hpp.

◆ invert()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
void AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::invert ( )
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.

Note
This overrides the mapping function set by the map method.

Definition at line 94 of file FilteredAnalog.hpp.

◆ update()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
bool AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::update ( )
inline

Read the analog input value, apply the mapping function, and update the average.

Return values
trueThe value changed since last time it was updated.
falseThe value is still the same.

Definition at line 108 of file FilteredAnalog.hpp.

◆ getValue()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
AnalogType AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::getValue ( ) const
inline

Get the filtered value of the analog input with the mapping function applied.

Returns
The filtered value of the analog input, as a number of Precision bits wide.

Definition at line 124 of file FilteredAnalog.hpp.

◆ getFloatValue()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
float AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::getFloatValue ( ) const
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.

Returns
The filtered value of the analog input, as a number from 0.0 to 1.0.

Definition at line 133 of file FilteredAnalog.hpp.

◆ getRawValue()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
AnalogType AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::getRawValue ( ) const
inline

Read the raw value of the analog input any filtering or mapping applied, but with its bit depth increased by Upsample.

Definition at line 141 of file FilteredAnalog.hpp.

◆ setupADC()

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
static void AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::setupADC ( )
inlinestatic

Definition at line 146 of file FilteredAnalog.hpp.

Member Data Documentation

◆ analogPin

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
const pin_t AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::analogPin
private

Definition at line 153 of file FilteredAnalog.hpp.

◆ mapFn

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
MappingFunction AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::mapFn = nullptr
private

Definition at line 155 of file FilteredAnalog.hpp.

◆ filter

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
EMA<FilterShiftFactor, FilterType> AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::filter
private

Definition at line 160 of file FilteredAnalog.hpp.

◆ hysteresis

template<uint8_t Precision = 10, uint8_t FilterShiftFactor = ANALOG_FILTER_SHIFT_FACTOR, class FilterType = ANALOG_FILTER_TYPE, class AnalogType = analog_t, uint8_t Upsample = min(sizeof(FilterType) * CHAR_BIT - ADC_BITS - FilterShiftFactor, sizeof(AnalogType) * CHAR_BIT - ADC_BITS)>
Hysteresis<ADC_BITS + Upsample - Precision, AnalogType, AnalogType> AH::FilteredAnalog< Precision, FilterShiftFactor, FilterType, AnalogType, Upsample >::hysteresis
private

Definition at line 170 of file FilteredAnalog.hpp.


The documentation for this class was generated from the following file: