Reads input pulse width asynchronously. More...

#include <PwmReader.h>

Collaboration diagram for MHZ19::PwmReader:

Classes

union  Pulse
 Used internally to measure a high/low pulse pair. More...
 

Public Types

using Callback = Delegate< void(uint16_t ppm)>
 Callback for regular readings. More...
 

Public Member Functions

 ~PwmReader ()
 
void begin (uint8_t pin, DetectionRange range)
 Start the PWM reader. More...
 
void end ()
 Stop the PWM reader. More...
 
uint16_t getMeasurement () const
 Obtain the most recent measurement. More...
 
void setCallback (Callback callback)
 Set a callback to be invoked whenever a valid reading is obtained. More...
 
bool suspend ()
 Temporarily suspend readings and disable interrupts on the PWM pin. More...
 
bool resume ()
 Resume reading after a call to suspend() More...
 

Static Public Attributes

static constexpr uint16_t CYCLE_MS {1004}
 
static constexpr uint16_t CYCLE_TOLERANCE {250}
 

Detailed Description

Reads input pulse width asynchronously.

The ESP8266 lacks any timing capture hardware but as the pulse output from the MHZ19 is very slow it can be decoded using interrupts with high accuracy.

Once started, the PwmReader runs continuously and the last value can be obtained by calling getMeasurement.

Member Typedef Documentation

◆ Callback

using MHZ19::PwmReader::Callback = Delegate<void(uint16_t ppm)>

Callback for regular readings.

Constructor & Destructor Documentation

◆ ~PwmReader()

MHZ19::PwmReader::~PwmReader ( )
inline

Member Function Documentation

◆ begin()

void MHZ19::PwmReader::begin ( uint8_t  pin,
DetectionRange  range 
)

Start the PWM reader.

Parameters
pinGPIO to read PWM signal on
rangeConfigured device range

Runs continuously in background until end() is called.

◆ end()

void MHZ19::PwmReader::end ( )

Stop the PWM reader.

◆ getMeasurement()

uint16_t MHZ19::PwmReader::getMeasurement ( ) const

Obtain the most recent measurement.

◆ resume()

bool MHZ19::PwmReader::resume ( )

Resume reading after a call to suspend()

Return values
booltrue on success, false if reader not initialised

◆ setCallback()

void MHZ19::PwmReader::setCallback ( Callback  callback)
inline

Set a callback to be invoked whenever a valid reading is obtained.

◆ suspend()

bool MHZ19::PwmReader::suspend ( )

Temporarily suspend readings and disable interrupts on the PWM pin.

Return values
booltrue on success, false if reader not initialised

Member Data Documentation

◆ CYCLE_MS

constexpr uint16_t MHZ19::PwmReader::CYCLE_MS {1004}
staticconstexpr

◆ CYCLE_TOLERANCE

constexpr uint16_t MHZ19::PwmReader::CYCLE_TOLERANCE {250}
staticconstexpr

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