Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
BufferedSampler Class Reference

Ringbuffer-based sampler for analog values. More...

#include <BufferedSampler.h>

Inheritance diagram for BufferedSampler:
Inheritance graph

Public Member Functions

 BufferedSampler (const uint8_t pin, const uint16_t periodMs=10, const uint8_t samples=10)
 
 ~BufferedSampler ()
 
virtual void begin ()
 
virtual void update ()
 
virtual int16_t minimum ()
 The minimum value in the sample set.
 
virtual int16_t maximum ()
 The maximum value in the sample set.
 
virtual float average ()
 Get the mean value in the sample set.
 
virtual uint8_t count ()
 
virtual int16_t last ()
 Get the most recent sample value.
 
- Public Member Functions inherited from AbstractSampler
 AbstractSampler (const uint8_t pin, const uint16_t periodMs)
 
virtual ~AbstractSampler ()
 Destructor.
 
uint8_t pin ()
 Accessor for the pin which is geting read from.
 

Protected Member Functions

virtual void calculate ()
 calculate avg, min, max if required
 

Protected Attributes

uint8_t _samples
 max number of samples to keep in buffer
 
uint8_t _idx
 index ptr (ring buffer)
 
uint8_t _count
 number of samples in buffer
 
uint32_t _lastUpdated
 when last sample taken
 
bool _updated
 flag to re-calulate avg or not
 
int16_t * _sampleData
 ring buffer for data
 
int16_t _minimum
 most recently calculated minimum value
 
int16_t _maximum
 most recently calculated maximum value
 
float _mean
 most recently calculated mean value
 
- Protected Attributes inherited from AbstractSampler
const uint8_t _pin
 pin to read data from
 
const uint16_t _periodMs
 minimum ms
 

Detailed Description

Ringbuffer-based sampler for analog values.

This class keeps a fixed number of samples in a ring buffer, enabling calculation of minimum(), maximum(), average() (mean) values.

Advantages:

Disadvantages:

Definition at line 19 of file BufferedSampler.h.

Constructor & Destructor Documentation

◆ BufferedSampler()

BufferedSampler::BufferedSampler ( const uint8_t  pin,
const uint16_t  periodMs = 10,
const uint8_t  samples = 10 
)

Constructor

Parameters
pinthe analog input pin to read samples from
periodMsthe number of milliseconds between samples. If update() is called before periodMs has passed since the last update(), no sample will be taken.
samplesthe number of samples to keep in the ring buffer used to calculate average(), minimum() and maximum().

Note: this class dynamically allocated memory for the ring buffer.

Definition at line 6 of file BufferedSampler.cpp.

◆ ~BufferedSampler()

BufferedSampler::~BufferedSampler ( )

Destructor

Destroy the BufferedSampler, and free memory used for the ring buffer.

Definition at line 21 of file BufferedSampler.cpp.

Member Function Documentation

◆ begin()

void BufferedSampler::begin ( )
virtual

Initialization Should be called before the object is used, and may also be called at any other time to clear the ring buffer. Immediately after begin() is called, average(), minimum() and maximum() will return 0.

Reimplemented from AbstractSampler.

Definition at line 29 of file BufferedSampler.cpp.

◆ count()

virtual uint8_t BufferedSampler::count ( )
inlinevirtual

Sample count The number of samples currently held in the ring buffer which are being used to calculate the average, minimum, maximum values. This will be 0 after the BufferedSampler has been constructed or begin() called, and will rise by 1 each time a sample is taken until the maximum number of samples is reached.

Definition at line 69 of file BufferedSampler.h.

◆ update()

void BufferedSampler::update ( )
virtual

Time slice allocation This method should be called every time a sample is to be taken. If the BufferedSampler object was constucted with periodMs > 0, this method will only add a new sample to the ring buffer if at least periodMs milliseconds has elapsed since the last call to update().

Implements AbstractSampler.

Definition at line 41 of file BufferedSampler.cpp.


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