46#include <sfTk/sfTkII2C.h>
47#include <sfTk/sfToolkit.h>
201 sfTkError_t
begin(sfTkIBus *theBus =
nullptr);
235 bool probeADC(
bool assumeUnconfigured =
false);
Platform-independent driver for the SparkFun Qwiic MEMS gas sensor breakouts.
Definition sfDevGMX02B.h:172
sfTkError_t getAlertStatus(bool &underRange, bool &overRange)
Read the raw alert status flags.
Definition sfDevGMX02B.cpp:631
static const uint8_t kCleanAirSamples
Default readings averaged for R0.
Definition sfDevGMX02B.h:947
static constexpr float kSupplyVoltageDefault
Qwiic 3.3 V rail powers ADC and sensing loop.
Definition sfDevGMX02B.h:942
float getRatedConcentration_ppm(void)
Get the concentration at which getRatedSensitivity() is specified.
Definition sfDevGMX02B.cpp:299
sfTkError_t readValueRegister(uint8_t reg, uint16_t &counts)
Read a 10-bit value register (value stored in bits 11:2).
Definition sfDevGMX02B.cpp:773
sfTkError_t enableAlertFlag(bool enable)
Enable or disable the alert flag in the conversion result register.
Definition sfDevGMX02B.cpp:611
static const uint8_t kRegLowestConversion
Lowest conversion seen (16-bit).
Definition sfDevGMX02B.h:905
const sfe_gmx02b_part_t * getPartInfo(void)
Get the descriptor for the current sensor variant.
Definition sfDevGMX02B.cpp:259
bool isConnected(void)
Check whether the board's ADC is connected and responding.
Definition sfDevGMX02B.cpp:185
sfTkError_t setAlertHysteresis(uint16_t counts)
Set the alert hysteresis.
Definition sfDevGMX02B.cpp:582
float _loadResistance
Load resistor RL (Ohm). Set from the part descriptor.
Definition sfDevGMX02B.h:889
sfTkError_t getRatio(float &ratio)
Get the raw Rs/R0 ratio from the most recent readMeasurement().
Definition sfDevGMX02B.cpp:348
sfTkError_t getLowestConversion(uint16_t &counts)
Get the lowest conversion result recorded in automatic conversion mode.
Definition sfDevGMX02B.cpp:696
sfTkError_t getAlertLowLimit(uint16_t &counts)
Get the alert low limit.
Definition sfDevGMX02B.cpp:567
const sfe_gmx02b_part_t * _part
Descriptor for _sensorType. Never null after construction.
Definition sfDevGMX02B.h:881
sfTkError_t getAlertHysteresis_mV(float &millivolts)
Get the alert hysteresis as a sensor-output voltage band.
Definition sfDevGMX02B.cpp:598
sfTkError_t clearLowestConversion(void)
Reset the lowest conversion register (to full scale).
Definition sfDevGMX02B.cpp:706
sfTkError_t responseToCounts(float response, uint16_t &counts)
Convert a gas response ratio into the equivalent 10-bit ADC counts.
Definition sfDevGMX02B.cpp:445
static const uint8_t kConfigAlertHold
Alert hold enable (D4).
Definition sfDevGMX02B.h:926
static const uint8_t kConfigPolarity
ALERT pin polarity: 1 = active high (D0).
Definition sfDevGMX02B.h:930
sfDevGMX02B(sfe_gmx02b_sensor_t sensor)
Construct a driver for a specific family member.
Definition sfDevGMX02B.h:186
float getRangeMin_ppm(void)
Get the low end of the part's detection range.
Definition sfDevGMX02B.cpp:284
static const uint8_t kConfigAlertFlagEnable
Conversion result alert flag enable (D3).
Definition sfDevGMX02B.h:927
static const uint8_t kRegHighLimit
Alert high limit (16-bit).
Definition sfDevGMX02B.h:903
void setCleanAirResistance_Ohm(float ohms)
Set the clean-air resistance (R0) directly.
Definition sfDevGMX02B.cpp:494
uint16_t voltageToCounts(float volts)
Convert a voltage into the equivalent 10-bit ADC counts.
Definition sfDevGMX02B.cpp:412
sfTkIBus * _theBus
Pointer to the communication bus device.
Definition sfDevGMX02B.h:877
static const uint8_t kAlertStatusUnderRange
Under-range alert flag.
Definition sfDevGMX02B.h:920
bool getAlertTriggered(void)
Get the ADC alert flag from the most recent readMeasurement().
Definition sfDevGMX02B.cpp:385
sfTkError_t getHighestConversion(uint16_t &counts)
Get the highest conversion result recorded in automatic conversion mode.
Definition sfDevGMX02B.cpp:701
sfTkError_t writeRegister8(uint8_t reg, uint8_t value)
Write an 8-bit register on the ADC.
Definition sfDevGMX02B.cpp:733
sfTkError_t setAlertHighLimit(uint16_t counts)
Set the alert high limit (over-range threshold).
Definition sfDevGMX02B.cpp:572
sfTkError_t getPeakGasVoltage_V(float &volts)
Get the peak gas conversion as a sensor output voltage.
Definition sfDevGMX02B.cpp:678
bool isInRange(float ppm)
Check whether a concentration falls inside the part's rated detection range.
Definition sfDevGMX02B.cpp:530
sfTkError_t readRegister8(uint8_t reg, uint8_t &value)
Read an 8-bit register from the ADC.
Definition sfDevGMX02B.cpp:720
float _curveA
Multiplier; 0 = no curve set.
Definition sfDevGMX02B.h:893
sfTkError_t getAlertHysteresis(uint16_t &counts)
Get the alert hysteresis.
Definition sfDevGMX02B.cpp:587
sfTkError_t clearHighestConversion(void)
Reset the highest conversion register (to zero).
Definition sfDevGMX02B.cpp:712
static const uint8_t kRegLowLimit
Alert low limit (16-bit).
Definition sfDevGMX02B.h:902
sfTkError_t readMeasurement(void)
Read one conversion from the ADC and cache the result.
Definition sfDevGMX02B.cpp:306
float countsToVoltage_V(uint16_t counts)
Convert 10-bit ADC counts into the equivalent sensor output voltage.
Definition sfDevGMX02B.cpp:425
static const uint8_t kConfigAlertPinEnable
ALERT pin enable (D2).
Definition sfDevGMX02B.h:928
sfTkError_t begin(sfTkIBus *theBus=nullptr)
Initialize the device driver with the given bus.
Definition sfDevGMX02B.cpp:171
sfTkError_t writeRegister16(uint8_t reg, uint16_t value)
Write a 16-bit register on the ADC (most significant byte first).
Definition sfDevGMX02B.cpp:763
bool setSensorType(sfe_gmx02b_sensor_t sensor)
Declare which family member is installed.
Definition sfDevGMX02B.cpp:237
sfTkError_t getGasResponse(float &response)
Get the direction-normalized gas response from the most recent readMeasurement().
Definition sfDevGMX02B.cpp:364
uint16_t resistanceToCounts(float ohms)
Convert a sensor resistance into the equivalent 10-bit ADC counts.
Definition sfDevGMX02B.cpp:431
sfTkError_t getGasAlert(bool &gasDetected)
Read the hardware alert status as a single "gas detected" flag.
Definition sfDevGMX02B.cpp:549
void setSupplyVoltage_V(float volts)
Set the sensing circuit supply voltage used by getVoltage_V(), voltageToCounts() and countsToVoltage_...
Definition sfDevGMX02B.cpp:392
float _cleanAirResistance
Stored clean-air sensor resistance R0 (Ohm); 0 = not set.
Definition sfDevGMX02B.h:890
static const uint8_t kRegHighestConversion
Highest conversion seen (16-bit).
Definition sfDevGMX02B.h:906
bool probeADC(bool assumeUnconfigured=false)
Check that the responding device looks like an ADC101C021.
Definition sfDevGMX02B.cpp:192
static const uint16_t kValueMask
Mask for the 10-bit value (after shifting).
Definition sfDevGMX02B.h:913
sfTkError_t setCycleTime(sfe_gmx02b_cycle_time_t cycleTime)
Set the automatic conversion cycle time (enables / disables automatic mode).
Definition sfDevGMX02B.cpp:656
sfTkError_t updateConfiguration(uint8_t mask, uint8_t value)
Read-modify-write bits of the 8-bit configuration register.
Definition sfDevGMX02B.cpp:792
static const uint16_t kAlertFlagMask
Alert flag (D15) in the conversion result.
Definition sfDevGMX02B.h:914
sfTkError_t enableAlertHold(bool enable)
Enable or disable alert hold.
Definition sfDevGMX02B.cpp:626
float getLoadResistance_Ohm(void)
Get the configured load resistance.
Definition sfDevGMX02B.cpp:407
float getRatedSensitivity(void)
Get the datasheet response ratio bound for this part.
Definition sfDevGMX02B.cpp:294
static const uint16_t kCleanAirSampleDelayMs
Default delay between R0 readings (ms).
Definition sfDevGMX02B.h:948
float getCleanAirResistance_Ohm(void)
Get the stored clean-air resistance (R0).
Definition sfDevGMX02B.cpp:499
static constexpr float kAdcFullScaleInv
Precomputed 1 / full scale.
Definition sfDevGMX02B.h:937
static const uint8_t kAlertStatusOverRange
Over-range alert flag.
Definition sfDevGMX02B.h:921
static const uint8_t kConfigReserved
Reserved, reads zero (D1).
Definition sfDevGMX02B.h:929
bool _alertFlag
Alert flag (bit D15) from the conversion result.
Definition sfDevGMX02B.h:885
static const uint8_t kRegAlertStatus
Alert status flags (8-bit).
Definition sfDevGMX02B.h:900
sfTkError_t getConcentration_ppm(float &ppm)
Get the estimated target gas concentration.
Definition sfDevGMX02B.cpp:512
sfTkError_t enableAlertPin(bool enable)
Enable or disable the ADC's ALERT output pin.
Definition sfDevGMX02B.cpp:616
static const uint8_t kRegConfiguration
Configuration (8-bit).
Definition sfDevGMX02B.h:901
void setLoadResistance_Ohm(float ohms)
Override the load resistance (RL) used by getSensorResistance_Ohm().
Definition sfDevGMX02B.cpp:402
static const uint8_t kConfigCycleTimeMask
Cycle time field (D7:5).
Definition sfDevGMX02B.h:924
sfTkError_t setAlertPinActiveHigh(bool activeHigh)
Set the active polarity of the ALERT output pin.
Definition sfDevGMX02B.cpp:621
float getSupplyVoltage_V(void)
Get the configured sensing circuit supply voltage.
Definition sfDevGMX02B.cpp:397
const char * getTargetGas(void)
Get a printable target gas description for the current sensor variant.
Definition sfDevGMX02B.cpp:269
sfTkError_t setAlertLowLimit(uint16_t counts)
Set the alert low limit (under-range threshold).
Definition sfDevGMX02B.cpp:562
static const uint16_t kValueReservedMask
D15:D12 and D1:D0.
Definition sfDevGMX02B.h:917
static const uint16_t kAdcFullScale
10-bit ADC full-scale count (1 LSB = VA / 1024).
Definition sfDevGMX02B.h:935
sfe_gmx02b_sensor_t _sensorType
Declared sensor variant.
Definition sfDevGMX02B.h:880
uint16_t _counts
Raw 10-bit ADC value.
Definition sfDevGMX02B.h:884
sfTkError_t getAlertHighLimit(uint16_t &counts)
Get the alert high limit.
Definition sfDevGMX02B.cpp:577
sfTkError_t readRegister16(uint8_t reg, uint16_t &value)
Read a 16-bit register from the ADC (most significant byte first).
Definition sfDevGMX02B.cpp:743
static const uint8_t kConfigCycleTimeShift
Cycle time field bit position.
Definition sfDevGMX02B.h:925
sfTkError_t clearAlertStatus(void)
Clear both alert status flags.
Definition sfDevGMX02B.cpp:647
sfTkError_t getPeakGasCounts(uint16_t &counts)
Get the peak GAS conversion recorded in automatic conversion mode.
Definition sfDevGMX02B.cpp:672
float getRangeMax_ppm(void)
Get the high end of the part's detection range.
Definition sfDevGMX02B.cpp:289
sfTkError_t getSensorResistance_Ohm(float &ohms)
Get the sensing element resistance (Rs) from the most recent readMeasurement().
Definition sfDevGMX02B.cpp:334
sfTkError_t writeValueRegister(uint8_t reg, uint16_t counts)
Write a 10-bit value register (value stored in bits 11:2).
Definition sfDevGMX02B.cpp:784
sfe_gmx02b_sensor_t getSensorType(void)
Get the declared sensor variant.
Definition sfDevGMX02B.cpp:254
sfTkError_t getCycleTime(sfe_gmx02b_cycle_time_t &cycleTime)
Get the automatic conversion cycle time.
Definition sfDevGMX02B.cpp:661
uint16_t getRaw(void)
Get the raw 10-bit ADC value from the most recent readMeasurement().
Definition sfDevGMX02B.cpp:324
uint8_t getDefaultAddress(void)
Get the factory-default I2C address for the current sensor variant.
Definition sfDevGMX02B.cpp:274
static const uint8_t kValueShift
Bit position of the 10-bit value.
Definition sfDevGMX02B.h:912
const char * getSensorName(void)
Get a printable name for the current sensor variant.
Definition sfDevGMX02B.cpp:264
bool rsRisesWithGas(void)
Report whether Rs rises or falls with target gas concentration.
Definition sfDevGMX02B.cpp:279
sfTkError_t setAlertHysteresis_mV(float millivolts)
Set the alert hysteresis as a sensor-output voltage band.
Definition sfDevGMX02B.cpp:592
static const uint8_t kRegHysteresis
Alert hysteresis (16-bit).
Definition sfDevGMX02B.h:904
sfTkError_t clearPeakGasCounts(void)
Reset the peak gas tracking register for this part.
Definition sfDevGMX02B.cpp:691
static const uint8_t kRegConversionResult
Conversion result (16-bit, read only).
Definition sfDevGMX02B.h:899
float getVoltage_V(void)
Get the sensor output voltage from the most recent readMeasurement().
Definition sfDevGMX02B.cpp:329
float _curveB
Exponent.
Definition sfDevGMX02B.h:894
float _supplyVoltage
ADC reference / sensing loop supply (V).
Definition sfDevGMX02B.h:888
void setResponseCurve(float a, float b)
Set a power-law curve that converts gas response into a concentration.
Definition sfDevGMX02B.cpp:506
static const uint16_t kAdcMaxCounts
Maximum 10-bit ADC value.
Definition sfDevGMX02B.h:936
sfTkError_t setGasAlertResponse(float response)
Set a hardware gas threshold in gas response units.
Definition sfDevGMX02B.cpp:537
sfTkError_t measureCleanAirResistance(float &r0, uint8_t samples=kCleanAirSamples, uint16_t sampleDelayMs=kCleanAirSampleDelayMs)
Measure and store the clean-air sensor resistance (R0).
Definition sfDevGMX02B.cpp:462
sfe_gmx02b_sensor_t
The Winsen GM-x02B sensing elements carried by the SparkFun Qwiic gas sensor boards.
Definition sfDevGMX02B.h:63
@ GM_PART_COUNT
Number of parts in the family. Not a valid sensor.
Definition sfDevGMX02B.h:73
@ GM_302B
GM-302B - alcohol and ethanol vapor.
Definition sfDevGMX02B.h:66
@ GM_102B
GM-102B - nitrogen dioxide (NO2). Rs RISES with gas.
Definition sfDevGMX02B.h:64
@ GM_602B
GM-602B - hydrogen sulfide (H2S) and benzene.
Definition sfDevGMX02B.h:70
@ GM_702B
GM-702B - carbon monoxide (CO) and hydrogen.
Definition sfDevGMX02B.h:71
@ GM_202B
GM-202B - ethanol and smoke.
Definition sfDevGMX02B.h:65
@ GM_402B
GM-402B - methane and propane (combustibles).
Definition sfDevGMX02B.h:67
@ GM_502B
GM-502B - VOCs (ethanol, formaldehyde, toluene).
Definition sfDevGMX02B.h:68
@ GM_512B
GM-512B - breath odor (H2S, ethanol, acetone).
Definition sfDevGMX02B.h:69
@ GM_UNKNOWN
No sensor declared or detected.
Definition sfDevGMX02B.h:74
@ GM_802B
GM-802B - ammonia (NH3).
Definition sfDevGMX02B.h:72
const sfe_gmx02b_part_t * sfe_gmx02b_partInfo(sfe_gmx02b_sensor_t sensor)
Get the descriptor for a family member.
Definition sfDevGMX02B.cpp:103
const char * sfe_gmx02b_partName(sfe_gmx02b_sensor_t sensor)
Get a printable part name.
Definition sfDevGMX02B.cpp:115
sfe_gmx02b_cycle_time_t
Cycle time settings for the ADC101C021's automatic conversion mode.
Definition sfDevGMX02B.h:91
@ GM_CYCLE_TIME_512
Convert every Tconvert x 512 (~1.7 ksps).
Definition sfDevGMX02B.h:97
@ GM_CYCLE_DISABLED
Automatic conversion mode off (normal mode, default).
Definition sfDevGMX02B.h:92
@ GM_CYCLE_TIME_256
Convert every Tconvert x 256 (~3.4 ksps).
Definition sfDevGMX02B.h:96
@ GM_CYCLE_TIME_1024
Convert every Tconvert x 1024 (~0.9 ksps).
Definition sfDevGMX02B.h:98
@ GM_CYCLE_TIME_128
Convert every Tconvert x 128 (~6.7 ksps).
Definition sfDevGMX02B.h:95
@ GM_CYCLE_TIME_2048
Convert every Tconvert x 2048 (~0.4 ksps).
Definition sfDevGMX02B.h:99
@ GM_CYCLE_TIME_64
Convert every Tconvert x 64 (~13.5 ksps).
Definition sfDevGMX02B.h:94
@ GM_CYCLE_TIME_32
Convert every Tconvert x 32 (~27 ksps).
Definition sfDevGMX02B.h:93
const char * sfe_gmx02b_targetGas(sfe_gmx02b_sensor_t sensor)
Get a printable target gas description.
Definition sfDevGMX02B.cpp:142
Per-part parameters for one GM-x02B variant and its board.
Definition sfDevGMX02B.h:116
uint8_t defaultAddress
7-bit I2C address with factory ADR1 / ADR0 jumpers.
Definition sfDevGMX02B.h:117
float ratedSensitivity
Datasheet bound on the response ratio at ratedConcentration.
Definition sfDevGMX02B.h:122
bool rsRisesWithGas
true if Rs increases with target gas (GM-102B only).
Definition sfDevGMX02B.h:118
float rsMax
Datasheet Rs upper bound at the reference concentration (Ohm).
Definition sfDevGMX02B.h:121
float loadResistance
Load resistor RL fitted on this board variant (Ohm).
Definition sfDevGMX02B.h:119
float ratedConcentration
Concentration at which ratedSensitivity is specified (ppm).
Definition sfDevGMX02B.h:123
float rangeMax
High end of the detection range (ppm).
Definition sfDevGMX02B.h:125
float rsMin
Datasheet Rs lower bound at the reference concentration (Ohm).
Definition sfDevGMX02B.h:120
float rangeMin
Low end of the detection range (ppm).
Definition sfDevGMX02B.h:124