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

182 lines
5.7 KiB
C
Raw Normal View History

2022-10-07 05:41:02 -04:00
#pragma once
//
// FILE: CHT8305.h
// AUTHOR: Rob Tillaart
2023-12-05 14:46:08 -05:00
// VERSION: 0.2.0
2022-10-07 05:41:02 -04:00
// PURPOSE: Arduino library for CHT8305 temperature and humidity sensor
2023-02-01 13:29:55 -05:00
// URL: https://github.com/RobTillaart/CHT8305
2022-10-07 05:41:02 -04:00
//
#include "Arduino.h"
#include "Wire.h"
2023-12-05 14:46:08 -05:00
#define CHT8305_LIB_VERSION (F("0.2.0"))
2022-10-16 05:14:15 -04:00
// DEFAULT ADDRESS
#ifndef CHT8305_DEFAULT_ADDRESS
#define CHT8305_DEFAULT_ADDRESS 0x40
#endif
2022-10-07 05:41:02 -04:00
2022-10-09 10:31:16 -04:00
// ERRORS
2022-10-07 05:41:02 -04:00
#define CHT8305_OK 0
#define CHT8305_ERROR_ADDR -10
#define CHT8305_ERROR_I2C -11
#define CHT8305_ERROR_CONNECT -12
#define CHT8305_ERROR_LASTREAD -20
2022-10-09 10:31:16 -04:00
// REGISTERS
#define CHT8305_REG_TEMPERATURE 0x00
#define CHT8305_REG_HUMIDITY 0x01
#define CHT8305_REG_CONFIG 0x02
#define CHT8305_REG_ALERT 0x03
#define CHT8305_REG_VOLTAGE 0x04
#define CHT8305_REG_MANUFACTURER 0xFE
#define CHT8305_REG_VERSION 0xFF
// REGISTER MASKS
#define CHT8305_CFG_SOFT_RESET 0x8000
#define CHT8305_CFG_CLOCK_STRETCH 0x4000
#define CHT8305_CFG_HEATER 0x2000
#define CHT8305_CFG_MODE 0x1000
#define CHT8305_CFG_VCCS 0x0800
#define CHT8305_CFG_TEMP_RES 0x0400
#define CHT8305_CFG_HUMI_RES 0x0300
#define CHT8305_CFG_ALERT_MODE 0x00C0
#define CHT8305_CFG_ALERT_PENDING 0x0020
#define CHT8305_CFG_ALERT_HUMI 0x0010
#define CHT8305_CFG_ALERT_TEMP 0x0008
#define CHT8305_CFG_VCC_ENABLE 0x0004
#define CHT8305_CFG_VCC_RESERVED 0x0003
2022-10-07 05:41:02 -04:00
class CHT8305
{
public:
2023-12-05 14:46:08 -05:00
// default address = AD0 to GND.
CHT8305(const uint8_t address = CHT8305_DEFAULT_ADDRESS, TwoWire *wire = &Wire);
2022-10-07 05:41:02 -04:00
2023-12-05 14:46:08 -05:00
int begin();
2022-10-07 05:41:02 -04:00
bool isConnected();
2022-10-29 12:55:26 -04:00
uint8_t getAddress();
2022-10-07 05:41:02 -04:00
2022-10-29 12:55:26 -04:00
// read both the temperature and humidity.
2022-10-07 05:41:02 -04:00
int read();
2022-10-29 12:55:26 -04:00
// read only temperature (slightly faster)
int readTemperature();
// read only humidity (slightly faster)
int readHumidity();
2022-10-07 05:41:02 -04:00
// lastRead is in MilliSeconds since start sketch
2022-10-09 14:22:05 -04:00
uint32_t lastRead();
float getHumidity();
float getTemperature();
2022-10-13 10:26:41 -04:00
void setConversionDelay(uint8_t cd = 14);
uint8_t getConversionDelay();
2022-10-07 05:41:02 -04:00
2022-10-29 12:55:26 -04:00
2022-10-07 05:41:02 -04:00
// adding offsets works well in normal range
2023-12-05 14:46:08 -05:00
void setHumidityOffset(float offset);
2023-09-22 13:47:40 -04:00
// might introduce under- or overflow at the ends of the sensor range
2023-12-05 14:46:08 -05:00
void setTemperatureOffset(float offset);
float getHumidityOffset();
float getTemperatureOffset();
2022-10-07 05:41:02 -04:00
2022-10-29 12:55:26 -04:00
2022-10-07 05:41:02 -04:00
// CONFIG REGISTER
void setConfigRegister(uint16_t bitmask);
uint16_t getConfigRegister();
2022-10-09 10:31:16 -04:00
//
// | bit | mask | name | description |
// |:-----:|:------:|:----------------|:--------------|
// | 15 | 0x8000 | soft reset | 1 = reboot the sensor to default
// | 14 | 0x4000 | clock stretch | 1 = ON, 0 = OFF (default)
// | 13 | 0x2000 | heater | 1 = ON, 0 = OFF (default) ==> user is responsible for timing!
// | 12 | 0x1000 | mode | 1 = read both (default), 0 = read T or RH
// | 11 | 0x0800 | vccs | 1 = >2.8V, 0 = <2.8V
// | 10 | 0x0400 | T-RES | 1 = 11 bit, 0 = 14 bit (default)
// | 9-8 | 0x0300 | H-RES | 10 = 8 bit, 01 = 11 bit, 00 = 14 bit (default)
// | 7-6 | 0x00C0 | ALTM | Alert Mode (datasheet)
// | 5 | 0x0020 | APS | Alert pending status
// | 4 | 0x0010 | H-ALT | Humidity Alert status
2022-10-13 10:26:41 -04:00
// | 3 | 0x0008 | T-ALT | Temperature Alert status
// | 2 | 0x0004 | VCC enable | 1 = enable VCC measurement (default), 0 = disable
2022-10-09 10:31:16 -04:00
// | 1-0 | 0x0003 | reserved. | do not change.
//
2022-10-16 05:14:15 -04:00
// specific configuration functions. See datasheet!
2022-10-09 14:22:05 -04:00
//
2022-10-07 05:41:02 -04:00
void softReset();
2022-10-09 10:31:16 -04:00
void setI2CClockStretch(bool on = false);
bool getI2CClockStretch();
2022-10-29 12:55:26 -04:00
// WARNING: user is responsible for timing! WARNING!
void setHeaterOn(bool on = false);
2022-10-09 10:31:16 -04:00
bool getHeater();
void setMeasurementMode(bool both = true);
bool getMeasurementMode();
bool getVCCstatus();
2022-10-29 12:55:26 -04:00
// 1 = 11 bit, other = 14 bit
void setTemperatureResolution(uint8_t res = 0);
2022-10-09 14:22:05 -04:00
uint8_t getTemperatureResolution();
2022-10-09 10:31:16 -04:00
2022-10-29 12:55:26 -04:00
// 2 = 8 bit, 1 = 11 bit, other = 14 bit
void setHumidityResolution(uint8_t res = 0);
uint8_t getHumidityResolution();
2022-10-09 10:31:16 -04:00
2022-10-13 10:26:41 -04:00
void setVCCenable(bool enable = true);
2022-10-09 10:31:16 -04:00
bool getVCCenable();
// ALERT FUNCTIONS
// mode trigger
// 0 T or H (default)
// 1 T
// 2 H
// 3 T and H
2022-10-29 12:55:26 -04:00
bool setAlertTriggerMode(uint8_t mode = 0);
2022-10-09 10:31:16 -04:00
uint8_t getAlertTriggerMode();
bool getAlertPendingStatus();
bool getAlertHumidityStatus();
bool getAlertTemperatureStatus();
2022-10-09 14:22:05 -04:00
// it is mandatory to set both values.
// optionally use 125.0 for temperature and 100.0 for humidity
2022-10-09 10:31:16 -04:00
bool setAlertLevels(float temperature, float humidity);
float getAlertLevelTemperature();
float getAlertLevelHumidity();
2022-10-07 05:41:02 -04:00
// VOLTAGE
float getVoltage();
// META DATA
uint16_t getManufacturer(); // expect 0x5959
uint16_t getVersionID(); // may vary
2022-10-29 12:55:26 -04:00
2022-10-07 05:41:02 -04:00
private:
2022-10-13 10:26:41 -04:00
float _humOffset = 0.0;
float _tempOffset = 0.0;
float _humidity = 0.0;
float _temperature = 0.0;
uint32_t _lastRead = 0;
uint8_t _conversionDelay = 14;
2022-10-07 05:41:02 -04:00
TwoWire* _wire;
2022-10-16 05:14:15 -04:00
uint8_t _address = CHT8305_DEFAULT_ADDRESS;
2022-10-07 05:41:02 -04:00
2022-10-09 10:31:16 -04:00
int _readRegister(uint8_t reg, uint8_t * buf, uint8_t size);
int _writeRegister(uint8_t reg, uint8_t * buf, uint8_t size);
void _setConfigMask(uint16_t mask);
void _clrConfigMask(uint16_t mask);
2022-10-07 05:41:02 -04:00
};
2023-12-05 14:46:08 -05:00
// -- END OF FILE --
2022-10-07 05:41:02 -04:00