Serial Wombat Arduino Library
Public Member Functions | Public Attributes | List of all members
SerialWombatPulseTimer Class Reference

A Class which uses a Serial Wombat pin to measure the length of a pulse high and low time. More...

#include <SerialWombatPulseTimer.h>

Inheritance diagram for SerialWombatPulseTimer:
SerialWombatPin SerialWombatPulseTimer_18AB

Public Member Functions

 SerialWombatPulseTimer (SerialWombatChip &serialWombat)
 Class constructor for SerialWombatPulseTimer. More...
 
int16_t begin (uint8_t pin, SerialWombatPulseTimerUnits units=SW_PULSETIMER_uS, bool pullUpEnabled=false)
 Initialization routine for SerialWombatPulseTimer. More...
 
void refresh ()
 Retreive the latest values for HighCounts, LowCounts, Pulses, and MeasurementOverflowOccured. More...
 
void refreshHighCountsLowCounts ()
 Retreive the High and Low counts from the Serial Wombat chip in a single transaction. More...
 
void refreshHighCountsPulses ()
 
uint16_t readHighCounts ()
 
uint16_t readLowCounts ()
 
uint16_t readPulses ()
 
- Public Member Functions inherited from SerialWombatPin
 SerialWombatPin (SerialWombatChip &serialWombatChip)
 Instantiates a Serial Wombat Pin. More...
 
 SerialWombatPin (SerialWombatChip &serialWombatChip, uint8_t pin)
 Instantiates a Serial Wombat Pin. More...
 
uint16_t readPublicData ()
 Read the 16 Bit public data associated with this pin. More...
 
void pinMode (uint8_t mode, bool pullDown=false, bool openDrain=false)
 Set pin to INPUT or OUTPUT, with options for pull Ups and open Drain settings. More...
 
void digitalWrite (uint8_t val)
 Set output pin High or Low. More...
 
int digitalRead ()
 Reads the state of the Pin. More...
 
uint16_t writePublicData (uint16_t value)
 Write a 16 bit value to this pin. More...
 
uint8_t pin ()
 Returns the current SW pin number. Used primarily for virtual calls by derived classes. More...
 
uint8_t swPinModeNumber ()
 Returns the Mode number. Used primarily by derived classes to populate packet data. More...
 
int16_t initPacketNoResponse (uint8_t packetNumber, uint8_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55, uint8_t param4=0x55)
 
int16_t initPacketNoResponse (uint8_t packetNumber, uint16_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55)
 
int16_t initPacketNoResponse (uint8_t packetNumber, uint16_t param0, uint16_t param1, uint8_t param2=0x55)
 
int16_t disable ()
 Disables the pin mode (if applicable) More...
 

Public Attributes

uint16_t HighCounts = 0
 
uint16_t LowCounts = 0
 
uint16_t Pulses = 0
 
bool MeasurementOverflowOccurred = false
 

Additional Inherited Members

- Protected Attributes inherited from SerialWombatPin
uint8_t _pin = 255
 
SerialWombatChip_sw
 
uint8_t _pinMode = 0
 

Detailed Description

A Class which uses a Serial Wombat pin to measure the length of a pulse high and low time.

This class is used to measure the length of pulses in mS or in uS. On the Serial Wombat 4A/B the precision is about 8uS in uS mode, with an accuracy of +/- 2% because of variation in the Serial Wombat chip's internal oscillator.

One SerialWombatPulseTimer should be declared per Serial Wombat pin in this mode.

A video Tutorial on this pin mode is available:

https://youtu.be/YtQWUub9gYw

This pinmode is useful for measuring RC Servo outputs, PWM frequency and duty cycle, or any other signal where the pulse width is relevant. It is designed to measure repetitive signals. It is not intended to read a number of differing independent pulse widths that come one after another, such as WS2812 or similar output.

Warning
Care must be taken when using this pinmode with high frequency ( > 5 kHz) signals or pins that may be left floating on the Serial Wombat 4A / 4B because the Serial Wombat 4A and 4B chip uses an interrupt internally to capture transition timestamps. Excessively frequent pin transitions may cause the interrupt handler to starve the main processing loop, impacting function of all pin modes and communications.

The Serial Wombat chip can be queried for overflow frames. If overflow frames are occuring, then the system is overloaded.

The Serial Wombat 4A and 4B chips can measure a maxium of 8 transitions per mS across all pulse input pins. More frequent transitions than this may result in incorrect measurements. The MeasurementOverflowOccurred flag is set if at any time more than 8 transitions occured without processing.

The Serial Wombat 18AB chip uses a DMA based sampling scheme with samples taken at 57600 Hz. It has lower resolution than the Serial Wombat 4A/B (17 uS resolution), but can measure up to 18 simultaneous pulse trains and is not subject to being overloaded by faster signals.

The number of pulses over time can be used for rough frequency measurements.

Consecutive High and low periods can be used to calculate duty cycle, although there is no indication whether high or low was most recent.

Constructor & Destructor Documentation

◆ SerialWombatPulseTimer()

SerialWombatPulseTimer::SerialWombatPulseTimer ( SerialWombatChip serialWombat)
inline

Class constructor for SerialWombatPulseTimer.

Parameters
serialWombatThe Serial Wombat chip on which the SerialWombatPulseTimer pinmode will be run

Member Function Documentation

◆ begin()

int16_t SerialWombatPulseTimer::begin ( uint8_t  pin,
SerialWombatPulseTimerUnits  units = SW_PULSETIMER_uS,
bool  pullUpEnabled = false 
)
inline

Initialization routine for SerialWombatPulseTimer.

Parameters
pinThe Serial Wombat pin used for pulse measurments. All 4 pins on the SW4A/SW4B and all 20 pins on the SW18AB may be used.
unitsSW_PULSETIMER_uS or SW_PULSETIMER_mS. Default uS
pullUpEnabledTRUE = Pull Up Enabled, FALSE = Pull Up Disabled. Default disabled.

◆ readHighCounts()

uint16_t SerialWombatPulseTimer::readHighCounts ( )
inline

◆ readLowCounts()

uint16_t SerialWombatPulseTimer::readLowCounts ( )
inline

◆ readPulses()

uint16_t SerialWombatPulseTimer::readPulses ( )
inline

◆ refresh()

void SerialWombatPulseTimer::refresh ( )
inline

Retreive the latest values for HighCounts, LowCounts, Pulses, and MeasurementOverflowOccured.

This happens in two packets to the Serial Wombat chip, so data may not be coherent

◆ refreshHighCountsLowCounts()

void SerialWombatPulseTimer::refreshHighCountsLowCounts ( )
inline

Retreive the High and Low counts from the Serial Wombat chip in a single transaction.

This command will retreive consecutive high and low periods from the Serial Wombat chip. It it not guaranteed which is the most recent.

◆ refreshHighCountsPulses()

void SerialWombatPulseTimer::refreshHighCountsPulses ( )
inline

Member Data Documentation

◆ HighCounts

uint16_t SerialWombatPulseTimer::HighCounts = 0

◆ LowCounts

uint16_t SerialWombatPulseTimer::LowCounts = 0

◆ MeasurementOverflowOccurred

bool SerialWombatPulseTimer::MeasurementOverflowOccurred = false

◆ Pulses

uint16_t SerialWombatPulseTimer::Pulses = 0