Arduino Helpers master
Utility library for Arduino
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
GenericFilteredAnalog< MappingFunction, Precision, FilterShiftFactor, FilterType, AnalogType, IncRes > Class Template Reference

#include <AH/Hardware/FilteredAnalog.hpp>

Detailed Description

template<class MappingFunction, 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 GenericFilteredAnalog< MappingFunction, Precision, FilterShiftFactor, FilterType, AnalogType, IncRes >

FilteredAnalog base class with generic MappingFunction.

See also
FilteredAnalog

Definition at line 36 of file FilteredAnalog.hpp.

+ Collaboration diagram for GenericFilteredAnalog< MappingFunction, Precision, FilterShiftFactor, FilterType, AnalogType, IncRes >:

Public Member Functions

 GenericFilteredAnalog (pin_t analogPin, MappingFunction mapFn, AnalogType initial=0)
 Construct a new GenericFilteredAnalog object. More...
 
void reset (AnalogType value=0)
 Reset the filter to the given value. More...
 
void resetToCurrentValue ()
 Reset the filtered value to the value that's currently being measured at the analog input. More...
 
void map (MappingFunction fn)
 Specify a mapping function/functor that is applied to the analog value after filtering and before applying hysteresis. More...
 
MappingFunction & getMappingFunction ()
 Get a reference to the mapping function. More...
 
const MappingFunction & getMappingFunction () const
 Get a reference to the mapping function. 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 without any filtering or mapping applied, but with its bit depth increased by IncRes. More...
 

Static Public Member Functions

static constexpr AnalogType getMaxRawValue ()
 Get the maximum value that can be returned from getRawValue. More...
 
static void setupADC ()
 Select the configured ADC resolution. More...
 

Private Types

using EMA_t = EMA< FilterShiftFactor, AnalogType, FilterType >
 

Private Member Functions

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

Private Attributes

pin_t analogPin
 
MappingFunction mapFn
 
EMA_t filter
 
Hysteresis< ADC_BITS+IncRes - Precision, AnalogType, AnalogType > hysteresis
 

Member Typedef Documentation

◆ EMA_t

using EMA_t = EMA<FilterShiftFactor, AnalogType, FilterType>
private

Definition at line 208 of file FilteredAnalog.hpp.

Constructor & Destructor Documentation

◆ GenericFilteredAnalog()

GenericFilteredAnalog ( pin_t  analogPin,
MappingFunction  mapFn,
AnalogType  initial = 0 
)
inline

Construct a new GenericFilteredAnalog object.

Parameters
analogPinThe analog pin to read from.
mapFnThe mapping function
initialThe initial value of the filter.

Definition at line 48 of file FilteredAnalog.hpp.

Member Function Documentation

◆ reset()

void reset ( AnalogType  value = 0)
inline

Reset the filter to the given value.

Parameters
valueThe value to reset the filter state to.

Definition at line 63 of file FilteredAnalog.hpp.

◆ resetToCurrentValue()

void resetToCurrentValue ( )
inline

Reset the filtered value to the value that's currently being measured at the analog input.

This is useful to avoid transient effects upon initialization.

Definition at line 76 of file FilteredAnalog.hpp.

◆ map()

void map ( MappingFunction  fn)
inline

Specify a mapping function/functor that is applied to the analog value after filtering and before applying hysteresis.

Parameters
fnThis functor should have a call operator that takes the filtered value (of ADC_BITS + IncRes bits wide) as a parameter, and returns a value of ADC_BITS + IncRes 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 98 of file FilteredAnalog.hpp.

◆ getMappingFunction() [1/2]

MappingFunction & getMappingFunction ( )
inline

Get a reference to the mapping function.

Definition at line 103 of file FilteredAnalog.hpp.

◆ getMappingFunction() [2/2]

const MappingFunction & getMappingFunction ( ) const
inline

Get a reference to the mapping function.

Definition at line 107 of file FilteredAnalog.hpp.

◆ update()

bool 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 118 of file FilteredAnalog.hpp.

◆ getValue()

AnalogType getValue ( ) const
inline

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

Note
This function just returns the value from the last call to update, it doesn't read the analog input again.
Returns
The filtered value of the analog input, as a number of Precision bits wide.

Definition at line 136 of file FilteredAnalog.hpp.

◆ getFloatValue()

float 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 145 of file FilteredAnalog.hpp.

◆ getRawValue()

AnalogType getRawValue ( ) const
inline

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

Definition at line 153 of file FilteredAnalog.hpp.

◆ getMaxRawValue()

static constexpr AnalogType getMaxRawValue ( )
inlinestaticconstexpr

Get the maximum value that can be returned from getRawValue.

Definition at line 165 of file FilteredAnalog.hpp.

◆ setupADC()

static void setupADC ( )
inlinestatic

Select the configured ADC resolution.

By default, it is set to the maximum resolution supported by the hardware.

See also
ADC_BITS
ADCConfig.hpp

Definition at line 176 of file FilteredAnalog.hpp.

◆ mapFnHelper() [1/2]

std::enable_if< std::is_constructible< bool, M >::value, AnalogType >::type mapFnHelper ( AnalogType  input)
inlineprivate

Helper function that applies the mapping function if it's enabled.

This function is only enabled if MappingFunction is explicitly convertible to bool.

Definition at line 189 of file FilteredAnalog.hpp.

◆ mapFnHelper() [2/2]

std::enable_if<!std::is_constructible< bool, M >::value, AnalogType >::type mapFnHelper ( AnalogType  input)
inlineprivate

Helper function that applies the mapping function without checking if it's enabled.

This function is only enabled if MappingFunction is not convertible to bool.

Definition at line 200 of file FilteredAnalog.hpp.

Member Data Documentation

◆ analogPin

pin_t analogPin
private

Definition at line 205 of file FilteredAnalog.hpp.

◆ mapFn

MappingFunction mapFn
private

Definition at line 206 of file FilteredAnalog.hpp.

◆ filter

EMA_t filter
private

Definition at line 222 of file FilteredAnalog.hpp.

◆ hysteresis

Hysteresis<ADC_BITS + IncRes - Precision, AnalogType, AnalogType> hysteresis
private

Definition at line 224 of file FilteredAnalog.hpp.


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