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

A class to make analog measurements with the Serial Wombat. More...

#include <SerialWombatAnalogInput.h>

Inheritance diagram for SerialWombatAnalogInput:
SerialWombatPin SerialWombatAnalogInput_18AB

Public Member Functions

 SerialWombatAnalogInput (SerialWombatChip &SerialWombatChip)
 Constructor for the SerialWombatAnalogInput class. More...
 
int16_t begin (uint8_t pin, uint16_t averageSamples=64, uint16_t filterConstant=0xFF80, AnalogInputPublicDataOutput output=AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Raw)
 Initialize an analog input on a given pin. More...
 
uint16_t readVoltage_mV ()
 Retreive a raw A/D measurement and convert it to mV. More...
 
uint16_t readCounts ()
 Retreive a raw A/D measurement. More...
 
uint16_t readFiltered_mV ()
 Retreive a filtered A/D measurement and convert it to mV. More...
 
uint16_t readFilteredCounts ()
 Retreive a filtered A/D measurement. More...
 
uint16_t readAveraged_mV ()
 Retreive an averaged A/D measurement and convert it to mV. More...
 
uint16_t readAveragedCounts ()
 Retreive an averaged A/D measurement. More...
 
uint16_t updateSupplyVoltage_mV ()
 Provides a wrapper around the readSupplyVoltage_mV() method for the SerialWombat chip hosting this pin mode. More...
 
uint16_t readMaximum_mV (bool resetAfterRead=false)
 Retreive the maximum single sample A/D value in mV. More...
 
uint16_t readMaximumCounts (bool resetAfterRead=false)
 
uint16_t readMinimum_mV (bool resetAfterRead=false)
 
uint16_t readMinimumCounts (bool resetAfterRead=false)
 Retreive the maximum single sample A/D value in counts. More...
 
- 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...
 

Additional Inherited Members

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

Detailed Description

A class to make analog measurements with the Serial Wombat.

The SerialWombatAnalogInput class is used to make measurements on a given pin.

Any analog-capable pin may be used to make a measurement.

Averaging of samples and first order IIR filtering (1 kHz sampling) of input are available.

For a good explanation of 1st order IIR filter calculations, see: https://www.monocilindro.com/2017/04/08/how-to-implement-a-1st-order-iir-filter-in-5-minutes/

Some filter cut-off (3dB down) frequency and constant values:

Filtering adds lag. The higher the filter constant value, the longer it takes for the filter to settle when given a steady input.

Declare and initialize a SerialWombatAnalogInput instance for each pin being used as an analog input.

The SW4B_ard_analogInput example included with the Arduino library shows how to use this class.

A Tutorial video is also avaialble:

https://youtu.be/_EKlrEVaEhg

Constructor & Destructor Documentation

◆ SerialWombatAnalogInput()

SerialWombatAnalogInput::SerialWombatAnalogInput ( SerialWombatChip SerialWombatChip)
inline

Constructor for the SerialWombatAnalogInput class.

Parameters
SerialWombatChipa reference to the Serial Wombat chip on which the Analog Input will be measured

Member Function Documentation

◆ begin()

int16_t SerialWombatAnalogInput::begin ( uint8_t  pin,
uint16_t  averageSamples = 64,
uint16_t  filterConstant = 0xFF80,
AnalogInputPublicDataOutput  output = AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Raw 
)
inline

Initialize an analog input on a given pin.

Parameters
pinThe Serial Wombat pin to set. Valid values for SW4A: 0-3 SW4B: 1-3 SW18AB: 0-4 and 16-19
averageSamplesNumber of samples to average. Valid values for SW4A and SW4B are 0 or 64. All non-zero values will be treated as 64 on these platforms. Default is 64.
filterConstantFirst Order IIR filter constant, expressed as 1/65536ths . Default is 65508 Values closer to 65536 give heavier filtering. Sample frequency is 1kHz.
publicDataOutputWhat to output as pin public data. Default is raw.
Returns
Returns a negative error code if initialization failed.

◆ readAveraged_mV()

uint16_t SerialWombatAnalogInput::readAveraged_mV ( )
inline

Retreive an averaged A/D measurement and convert it to mV.

Conversion is based on the most recent averaged A/D result taken by the Serial Wombat A/D at the command time and the last reference measurement made on the Serial Wombat chip using the SerialWombatChip.readSupplyVoltage_mV() method.

Returns
A 16 bit unsigned value indicating measurement in mV

◆ readAveragedCounts()

uint16_t SerialWombatAnalogInput::readAveragedCounts ( )
inline

Retreive an averaged A/D measurement.

Conversion is based on the most recent averaged A/D value taken by the Serial Wombat A/D at the command time.

All Serial Wombat products will return a 16-bit value. However the SW4A and SW4B products only have 10-bit A/D converters. Averaging will potentially increase the effective resolution slightly for signals that have a small amount of randomly distributed noise.

Returns
A 16 bit unsigned value indicating the counts of the A/D conversion

◆ readCounts()

uint16_t SerialWombatAnalogInput::readCounts ( )
inline

Retreive a raw A/D measurement.

Conversion is based on the most recent A/D conversion taken by the Serial Wombat A/D at the command time.

All Serial Wombat products will return a 16-bit value. However the SW4A and SW4B products only have 10-bit A/D converters, so the returned value moves by 64 counts at a time, except for the topmost value. For all Serial Wombat products, the highest possible reading (0xFFC0 for the SW4A/SW4B, 0xFFF0 for the SW18AB) is changed to 0xFFFF to indicate maximum possible hardware value.

Returns
A 16 bit unsigned value indicating the counts of the A/D conversion

◆ readFiltered_mV()

uint16_t SerialWombatAnalogInput::readFiltered_mV ( )
inline

Retreive a filtered A/D measurement and convert it to mV.

Conversion is based on the most recent filtered A/D result taken by the Serial Wombat A/D at the command time and the last reference measurement made on the Serial Wombat chip using the SerialWombatChip.readSupplyVoltage_mV() method.

Returns
A 16 bit unsigned value indicating measurement in mV

◆ readFilteredCounts()

uint16_t SerialWombatAnalogInput::readFilteredCounts ( )
inline

Retreive a filtered A/D measurement.

Conversion is based on the most recent filtered A/D value taken by the Serial Wombat A/D at the command time.

Returns
A 16 bit unsigned value indicating the filtered A/D result

◆ readMaximum_mV()

uint16_t SerialWombatAnalogInput::readMaximum_mV ( bool  resetAfterRead = false)
inline

Retreive the maximum single sample A/D value in mV.

The maximum value the Serial Wombat chip has seen on that pin since last reset of Min/Max

Parameters
resetAfterReadIf True, maximum value is set to 0 after read so that subsequent values become maximum. Also resets minimum to next sample.
Returns
A 16 bit unsigned value indicating measurement in mV

◆ readMaximumCounts()

uint16_t SerialWombatAnalogInput::readMaximumCounts ( bool  resetAfterRead = false)
inline
\brief Retreive the maximum single sample A/D value in counts

The maximum value the Serial Wombat chip has seen on that pin since last reset of Min/Max

/

Parameters
resetAfterReadIf True, maximum value is set to 0 after read so that subsequent values become maximum. Also resets minimum to next sample.
Returns
A 16 bit unsigned value indicating maximum A/D Counts

◆ readMinimum_mV()

uint16_t SerialWombatAnalogInput::readMinimum_mV ( bool  resetAfterRead = false)
inline
\brief Retreive the minimum single sample A/D value in mV

The minimum value the Serial Wombat chip has seen on that pin since last reset of Min/Max

/

Parameters
resetAfterReadIf True, minimum value is set to 0 after read so that subsequent values become minimum. Also resets maximum to next sample.
Returns
A 16 bit unsigned value indicating measurement in mV

◆ readMinimumCounts()

uint16_t SerialWombatAnalogInput::readMinimumCounts ( bool  resetAfterRead = false)
inline

Retreive the maximum single sample A/D value in counts.

The maximum value the Serial Wombat chip has seen on that pin since last reset of Min/Max

Parameters
resetAfterReadIf True, maximum value is set to 0 after read so that subsequent values become maximum. Also resets minimum to next sample.
Returns
A 16 bit unsigned value indicating maximum A/D Counts

◆ readVoltage_mV()

uint16_t SerialWombatAnalogInput::readVoltage_mV ( )
inline

Retreive a raw A/D measurement and convert it to mV.

Conversion is based on the most recent A/D conversion taken by the Serial Wombat A/D at the command time and the last reference measurement made on the Serial Wombat chip using the SerialWombatChip.readSupplyVoltage_mV() method.

Returns
A 16 bit unsigned value indicating measurement in mV

◆ updateSupplyVoltage_mV()

uint16_t SerialWombatAnalogInput::updateSupplyVoltage_mV ( )
inline

Provides a wrapper around the readSupplyVoltage_mV() method for the SerialWombat chip hosting this pin mode.