mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 18:56:38 +00:00
Some corrections based on feedback from the project leads contributors.
Improved runtime impact by replacing the sleep between the I2C operations with separate code triggered by timer events.
This commit is contained in:
parent
b7907aee1a
commit
b758699e39
@ -66,4 +66,4 @@ Index | Define | Driver | Device | Address(es) | Description
|
||||
42 | USE_DS1624 | xsns_59 | DS1624 | 0x48 - 0x4F | Temperature sensor
|
||||
43 | USE_AHT1x | xsns_63 | AHT10/15 | 0x38 | Temperature and humidity sensor
|
||||
44 | USE_WEMOS_MOTOR_V1 | xdrv_34 | | 0x2D - 0x30 | WEMOS motor shield v1.0.0 (6612FNG)
|
||||
92 | USE_HDC1080 | xsns_92 | HDC1080 | 0x40 | Temperature and Humidity sensor
|
||||
45 | USE_HDC1080 | xsns_65 | HDC1080 | 0x40 | Temperature and Humidity sensor
|
||||
|
@ -254,8 +254,8 @@ void GetFeatures(void)
|
||||
#ifdef USE_SHT
|
||||
feature_sns1 |= 0x00000100; // xsns_07_sht1x.ino
|
||||
#endif
|
||||
#if defined(USE_HTU) || defined(USE_HDC1080)
|
||||
feature_sns1 |= 0x00000200; // xsns_08_htu21.ino or xsns_92_hdc1080.ino
|
||||
#ifdef USE_HTU
|
||||
feature_sns1 |= 0x00000200; // xsns_08_htu21.ino
|
||||
#endif
|
||||
#ifdef USE_BMP
|
||||
feature_sns1 |= 0x00000400; // xsns_09_bmp.ino
|
||||
|
@ -182,7 +182,7 @@ extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack
|
||||
#define USE_DHT12 // Add I2C code for DHT12 temperature and humidity sensor (+0k7 code)
|
||||
#define USE_DS1624 // Add I2C code for DS1624, DS1621 sensor
|
||||
//#define USE_AHT1x // Enable AHT10/15 humidity and temperature sensor (I2C address 0x38) (+0k8 code)
|
||||
#define USE_HDC1080 // Enable HDC1080 temperature/humidity sensor
|
||||
//#define USE_HDC1080 // Enable HDC1080 temperature/humidity sensor
|
||||
|
||||
|
||||
#define USE_WEMOS_MOTOR_V1 // Enable Wemos motor driver V1 (I2C addresses 0x2D - 0x30) (+0k7 code)
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
#ifdef USE_I2C
|
||||
#ifdef USE_HDC1080
|
||||
|
||||
/*********************************************************************************************\
|
||||
* HDC1080 - Temperature and Humidy sensor
|
||||
*
|
||||
@ -27,8 +28,8 @@
|
||||
* I2C Address: 0x40
|
||||
\*********************************************************************************************/
|
||||
|
||||
#define XSNS_92 92
|
||||
#define XI2C_92 92 // See I2CDEVICES.md
|
||||
#define XSNS_65 65
|
||||
#define XI2C_45 45 // See I2CDEVICES.md
|
||||
|
||||
#define HDC1080_ADDR 0x40
|
||||
|
||||
@ -59,7 +60,7 @@
|
||||
|
||||
// Constants:
|
||||
|
||||
#define HDC1080_CONV_TIME 50 // Assume 6.50 + 6.35 ms + x of conversion delay for this device
|
||||
#define HDC1080_CONV_TIME 80 // Assume 6.50 + 6.35 ms + x of conversion delay for this device
|
||||
#define HDC1080_TEMP_MULT 0.0025177
|
||||
#define HDC1080_RH_MULT 0.0025177
|
||||
#define HDC1080_TEMP_OFFSET 40.0
|
||||
@ -74,6 +75,9 @@ float hdc_humidity = 0.0;
|
||||
|
||||
uint8_t hdc_valid = 0;
|
||||
|
||||
bool is_reading = false;
|
||||
uint32_t timer = millis() + HDC1080_CONV_TIME;
|
||||
|
||||
/**
|
||||
* Reads the device ID register.
|
||||
*
|
||||
@ -117,18 +121,32 @@ void HdcReset(void) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the single transaction read of the HDC1080, providing the
|
||||
* adequate delay for the acquisition.
|
||||
* Performs the write portion of the HDC1080 sensor transaction. This
|
||||
* action of writing to a register signals the beginning of the operation
|
||||
* (e.g. data acquisition).
|
||||
*
|
||||
* addr: the address of the I2C device we are talking to.
|
||||
* reg: the register where we are writing to.
|
||||
*
|
||||
* returns: 0 if the transmission was successfully completed, != 0 otherwise.
|
||||
*/
|
||||
int8_t HdcReadBuffer(uint8_t addr, uint8_t reg, uint8_t *reg_data, uint16_t len) {
|
||||
Wire.beginTransmission((uint8_t)addr);
|
||||
Wire.write((uint8_t)reg);
|
||||
Wire.endTransmission();
|
||||
int8_t HdcTransactionOpen(uint8_t addr, uint8_t reg) {
|
||||
Wire.beginTransmission((uint8_t) addr);
|
||||
Wire.write((uint8_t) reg);
|
||||
return Wire.endTransmission();
|
||||
}
|
||||
|
||||
delay(HDC1080_CONV_TIME);
|
||||
|
||||
if (len != Wire.requestFrom((uint8_t)addr, (uint8_t)len)) {
|
||||
/**
|
||||
* Performs the read portion of the HDC1080 sensor transaction.
|
||||
*
|
||||
* addr: the address of the I2C device we are talking to.
|
||||
* reg_data: the pointer to the memory location where we will place the bytes that were read from the device
|
||||
* len: the number of bytes we expect to read
|
||||
*
|
||||
* returns: if the read operation was successful. != 0 otherwise.
|
||||
*/
|
||||
int8_t HdcTransactionClose(uint8_t addr, uint8_t *reg_data, uint16_t len) {
|
||||
if (len != Wire.requestFrom((uint8_t) addr, (uint8_t) len)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -149,11 +167,31 @@ void HdcInit(void) {
|
||||
HdcConfig(HDC1080_MODE_ON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers the single transaction read of the T/RH sensor.
|
||||
*
|
||||
*/
|
||||
bool HdcTriggerRead(void) {
|
||||
int8_t status = HdcTransactionOpen(HDC1080_ADDR, HDC_REG_TEMP);
|
||||
|
||||
if(status) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("HdcTriggerRead: failed to open the transaction for HDC_REG_TEMP. Status = %d"), status);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
is_reading = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a temperature and humidity measurement, and calls
|
||||
* the conversion function providing the results in the correct
|
||||
* unit according to the device settings.
|
||||
*
|
||||
* returns: false if something failed during the read process.
|
||||
*
|
||||
*/
|
||||
bool HdcRead(void) {
|
||||
int8_t status = 0;
|
||||
@ -161,18 +199,20 @@ bool HdcRead(void) {
|
||||
uint16_t temp_data = 0;
|
||||
uint16_t rh_data = 0;
|
||||
|
||||
status = HdcReadBuffer(HDC1080_ADDR, HDC_REG_TEMP, sensor_data, 4);
|
||||
is_reading = false;
|
||||
|
||||
status = HdcTransactionClose(HDC1080_ADDR, sensor_data, 4);
|
||||
|
||||
if(status) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("HdcRead: failed to read HDC_REG_TEMP. Status = %d"), status);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
temp_data = (uint16_t) ((sensor_data[0] << 8) | sensor_data[1]);
|
||||
rh_data = (uint16_t) ((sensor_data[2] << 8) | sensor_data[3]);
|
||||
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("HdcRead: temperature raw data: 0x%04x; humidity raw data: 0x%04x"), temp_data, rh_data);
|
||||
|
||||
if(status != 0) {
|
||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("HdcRead: failed to read HDC_REG_TEMP. Status = %d"), status);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// read the temperature from the first 16 bits of the result
|
||||
|
||||
@ -190,7 +230,10 @@ bool HdcRead(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
/**
|
||||
* Performs the detection of the HTC1080 sensor.
|
||||
*
|
||||
*/
|
||||
|
||||
void HdcDetect(void) {
|
||||
if (I2cActive(HDC1080_ADDR)) {
|
||||
@ -210,14 +253,24 @@ void HdcDetect(void) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* As the name suggests, this function is called every second
|
||||
* for performing driver related logic.
|
||||
*
|
||||
*/
|
||||
void HdcEverySecond(void) {
|
||||
if (uptime &1) { // Every 2 seconds
|
||||
if (!HdcRead()) {
|
||||
if (!HdcTriggerRead()) {
|
||||
AddLogMissed((char*) hdc_type_name, hdc_valid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tasmota boilerplate for presenting the sensor data in the web UI, JSON for
|
||||
* the MQTT messages, and so on.
|
||||
*
|
||||
*/
|
||||
void HdcShow(bool json) {
|
||||
if (hdc_valid) {
|
||||
char temperature[33];
|
||||
@ -252,10 +305,10 @@ void HdcShow(bool json) {
|
||||
* Interface
|
||||
\*********************************************************************************************/
|
||||
|
||||
bool Xsns92(uint8_t function)
|
||||
bool Xsns65(uint8_t function)
|
||||
{
|
||||
if (!I2cEnabled(XI2C_92)) {
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("Xsns92: I2C driver not enabled for this device."));
|
||||
if (!I2cEnabled(XI2C_45)) {
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("Xsns65: I2C driver not enabled for this device."));
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -267,6 +320,14 @@ bool Xsns92(uint8_t function)
|
||||
}
|
||||
else if (hdc_device_id) {
|
||||
switch (function) {
|
||||
case FUNC_EVERY_50_MSECOND:
|
||||
if(is_reading && TimeReached(timer)) {
|
||||
if(!HdcRead()) {
|
||||
AddLogMissed((char*) hdc_type_name, hdc_valid);
|
||||
}
|
||||
timer = millis() + HDC1080_CONV_TIME;
|
||||
}
|
||||
break;
|
||||
case FUNC_EVERY_SECOND:
|
||||
HdcEverySecond();
|
||||
break;
|
@ -178,7 +178,7 @@ a_features = [[
|
||||
"USE_INA219","USE_SHT3X","USE_MHZ19","USE_TSL2561",
|
||||
"USE_SENSEAIR","USE_PMS5003","USE_MGS","USE_NOVA_SDS",
|
||||
"USE_SGP30","USE_SR04","USE_SDM120","USE_SI1145",
|
||||
"USE_SDM630","USE_LM75AD","USE_APDS9960","USE_TM1638", "USE_HDC1080"
|
||||
"USE_SDM630","USE_LM75AD","USE_APDS9960","USE_TM1638",
|
||||
],[
|
||||
"USE_MCP230xx","USE_MPR121","USE_CCS811","USE_MPU6050",
|
||||
"USE_MCP230xx_OUTPUT","USE_MCP230xx_DISPLAYOUTPUT","USE_HLW8012","USE_CSE7766",
|
||||
|
Loading…
x
Reference in New Issue
Block a user