muwerk mupplet Core Library
muwerk applets; mupplets: functional units that support specific hardware or reusable applications
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
ustd::FrequencyCounter Class Reference

#include <mup_frequency_counter.h>

Public Types

enum  InterruptMode { IM_RISING , IM_FALLING , IM_CHANGE }
 
enum  MeasureMode {
  LOWFREQUENCY_FAST , LOWFREQUENCY_MEDIUM , LOWFREQUENCY_LONGTERM , HIGHFREQUENCY_FAST ,
  HIGHFREQUENCY_MEDIUM , HIGHFREQUENCY_LONGTERM
}
 

Public Member Functions

 FrequencyCounter (String name, uint8_t pin_input, int8_t interruptIndex_input, MeasureMode measureMode=HIGHFREQUENCY_MEDIUM, InterruptMode irqMode=InterruptMode::IM_FALLING)
 
void setMeasureMode (MeasureMode mode, bool silent=false)
 
bool begin (Scheduler *_pSched, uint32_t scheduleUs=2000000L)
 

Detailed Description

Interrupt driven frequency counter.

On an ESP32, measurement of frequencies at a GPIO are possible in ranges of 0-250kHz. There are six measurement modes: LOWFREQUENCY_FAST (good for measurements <50Hz, not much filtering) LOWFREQUENCY_MEDIUM (good for sub-Hertz enabled measurements, e.g. Geiger-counters) LOWFREQUENCY_LONGTERM (good for <50Hz, little deviation, high precision)

The HIGHFREQUENCY modes automatically reset the filter, if the input signal drops to 0Hz. HIGHFREQUENCY_FAST (no filtering) HIGHFREQUENCY_MEDIUM (some filtering) HIGHFREQUENCY_LONGTERM (strong filtering, good for precision measurement of signals with little deviation)

Precision <80kHz is better than 0.0005%. Interrupt load > 80kHz impacts the performance of the ESP32 severely and error goes up to 2-5%.

Messages

Messages sent by the switch mupplet:

topic message body comment
<mupplet-name>/sensor/frequency 53.001 Frequency in Hz encoded as String
<mupplet-name>/sensor/mode [LOW,HIGH]FREQUENCY_[FAST,MEDIUM,LONGTERM] Sample mode, e.g. LOWFREQUENCY_MEDIUM as string

Message received by the switch mupplet:

topic message body comment
<mupplet-name>/mode/set [LOW,HIGH]FREQUENCY_[FAST,MEDIUM,LONGTERM] Set the sample mode, e.g. LOWFREQUENCY_FAST, string encoded
<mupplet-name>/state/get Causes current frequency to be sent with topic <mupplet-name>/sensor/frequency
<mupplet-name>/frequency/get Causes current frequency to be sent with topic <mupplet-name>/sensor/frequency
<mupplet-name>/mode/get Causes current sample mode to be sent with topic <mupplet-name>/sensor/mode

Member Enumeration Documentation

◆ InterruptMode

Interrupt mode that triggers the counter

Enumerator
IM_RISING 

trigger on rising signal (LOW->HIGH)

IM_FALLING 

trigger on falling signal

IM_CHANGE 

trigger on both rising and falling signal

◆ MeasureMode

Sampling mode

Enumerator
LOWFREQUENCY_FAST 

for measurements <50Hz, not much filtering

LOWFREQUENCY_MEDIUM 

sub-Hertz enabled measurements, e.g. Geiger-counters

LOWFREQUENCY_LONGTERM 

for <50Hz, little deviation, high precision, long-time averaging

HIGHFREQUENCY_FAST 

no filtering

HIGHFREQUENCY_MEDIUM 

some filtering

HIGHFREQUENCY_LONGTERM 

strong filtering, good for precision measurement of signals with little deviation

Constructor & Destructor Documentation

◆ FrequencyCounter()

ustd::FrequencyCounter::FrequencyCounter ( String  name,
uint8_t  pin_input,
int8_t  interruptIndex_input,
MeasureMode  measureMode = HIGHFREQUENCY_MEDIUM,
InterruptMode  irqMode = InterruptMode::IM_FALLING 
)
inline

Create a frequency counter

Parameters
nameName of mupplet, used in message topics
pin_inputGPIO of input signal
interruptIndex_inputa system wide unique index for the interrupt to be used [0...9]
measureModeMeasureMode, e.g. LOWFREQUENCY_MEDIUM.
irqModeInterruptMode, e.g. IM_RISING.

Member Function Documentation

◆ begin()

bool ustd::FrequencyCounter::begin ( Scheduler *  _pSched,
uint32_t  scheduleUs = 2000000L 
)
inline

Enable interrupts and start counter

Parameters
_pSchedPointer to scheduler
scheduleUsMeasurement schedule in microseconds
Returns
true if successful

◆ setMeasureMode()

void ustd::FrequencyCounter::setMeasureMode ( MeasureMode  mode,
bool  silent = false 
)
inline

Change sample mode

Parameters
modeMeasureMode, e.g. LOWFREQUENCY_MEDIUM.
silenton true, no message is sent.

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