GY-63_MS5611/libraries/SGP30/SGP30.h

125 lines
3.2 KiB
C
Raw Normal View History

2021-06-27 07:04:39 -04:00
#pragma once
//
// FILE: SGP30.h
// AUTHOR: Rob Tillaart
2024-01-01 13:08:59 -05:00
// VERSION: 0.3.1
2021-06-27 07:04:39 -04:00
// DATE: 2021-06-24
2023-01-27 14:36:02 -05:00
// PURPOSE: Arduino library for SGP30 environment sensor.
2021-06-27 07:04:39 -04:00
// URL: https://github.com/RobTillaart/SGP30
// https://www.adafruit.com/product/3709
#include "Arduino.h"
#include "Wire.h"
2024-01-01 13:08:59 -05:00
#define SGP30_LIB_VERSION (F("0.3.1"))
2021-06-27 07:04:39 -04:00
2021-12-28 05:10:52 -05:00
#define SGP30_OK 0x00
#define SGP30_ERROR_CRC 0xFF
#define SGP30_ERROR_I2C 0xFE
2021-06-27 07:04:39 -04:00
class SGP30
{
public:
explicit SGP30(TwoWire *wire = &Wire);
bool begin();
bool isConnected();
// WARNING resets all I2C devices that support this call !!
void GenericReset();
2022-11-24 05:22:37 -05:00
// META
2021-06-27 07:04:39 -04:00
bool getID();
uint16_t getFeatureSet();
2023-01-27 14:36:02 -05:00
bool measureTest(); // need a better name
2021-06-27 07:04:39 -04:00
// MEASUREMENTS
// lastMeasurement works only with measure();
// not with async calls.
uint32_t lastMeasurement() { return _lastTime; };
2022-11-24 05:22:37 -05:00
// all == false ==> only TVOC and eCO2
// blocks 15 - 40 milliseconds (depends on parameter all).
2021-06-27 07:04:39 -04:00
bool measure(bool all = false);
2023-01-27 14:36:02 -05:00
2022-11-24 05:22:37 -05:00
// async interface
2021-06-27 07:04:39 -04:00
void request();
bool read();
void requestRaw();
bool readRaw();
2022-11-24 05:22:37 -05:00
// get the data // UNITS
uint16_t getTVOC() { return _tvoc; }; // PPB
uint16_t getCO2() { return _co2; }; // PPM
uint16_t getH2_raw() { return _h2; }; // UNKNOWN
uint16_t getEthanol_raw() { return _ethanol; }; // UNKNOWN
float getH2(); // experimental // PPM
float getEthanol(); // experimental // PPM
// CALIBRATION - read datasheet
// T in °C
// RH == 0..100
float setRelHumidity(float T, float RH); // P10
// Absolute humidity in grams / m3
// set Abs Hum to 0 to disables it...
2021-12-28 05:10:52 -05:00
void setAbsHumidity(float absoluteHumidity);
2021-06-27 07:04:39 -04:00
void setBaseline(uint16_t CO2, uint16_t TVOC);
bool getBaseline(uint16_t *CO2, uint16_t *TVOC);
2023-01-27 14:36:02 -05:00
2022-11-24 05:22:37 -05:00
// See Inceptive Baseline for TVOC measurements in data sheet
2021-06-27 07:04:39 -04:00
void setTVOCBaseline(uint16_t TVOC);
bool getTVOCBaseline(uint16_t *TVOC);
2022-11-24 05:22:37 -05:00
// EXPERIMENTAL
// 13119 = average raw measured outside 22°C (example)
2021-06-27 07:04:39 -04:00
void setSrefH2(uint16_t s = 13119) { _srefH2 = s; };
uint16_t getSrefH2() { return _srefH2; };
2022-11-24 05:22:37 -05:00
// 18472 = average raw measured outside 22°C (example)
2021-06-27 07:04:39 -04:00
void setSrefEthanol(uint16_t s = 18472) { _srefEth = s; };
uint16_t getSrefEthanol() { return _srefEth; };
2023-01-27 14:36:02 -05:00
// MISCELLANEOUS
int lastError();
2021-06-27 07:04:39 -04:00
2023-01-27 14:36:02 -05:00
// need an access function to prevent writing _id[]
// for now it is OK. TODO private?
uint8_t _id[6];
2021-06-27 07:04:39 -04:00
private:
uint8_t _address;
int _error;
2024-01-01 13:08:59 -05:00
uint32_t _lastTime;
uint32_t _lastRequest;
2021-06-27 07:04:39 -04:00
2022-11-24 05:22:37 -05:00
// TODO improve?
2021-06-27 07:04:39 -04:00
int _command(uint16_t cmd);
int _command(uint16_t cmd, uint16_t v1);
int _command(uint16_t cmd, uint16_t v1, uint16_t v2);
2024-01-01 13:08:59 -05:00
2021-06-27 07:04:39 -04:00
uint8_t _CRC8(uint16_t val);
void _init();
uint16_t _tvoc;
uint16_t _co2;
uint16_t _h2;
uint16_t _ethanol;
2023-01-27 14:36:02 -05:00
2022-11-24 05:22:37 -05:00
// experimental
2023-01-27 14:36:02 -05:00
// average raw values measured outside 22°C
2021-06-27 07:04:39 -04:00
uint16_t _srefH2 = 13119;
uint16_t _srefEth = 18472;
TwoWire* _wire;
};
2021-12-28 05:10:52 -05:00
2023-01-27 14:36:02 -05:00
// -- END OF FILE --
2021-12-28 05:10:52 -05:00