mirror of
https://github.com/esphome/esphome.git
synced 2026-05-30 23:54:04 +08:00
[shtcx] Code clean-up (#15261)
This commit is contained in:
@@ -2,16 +2,15 @@
|
|||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome::shtcx {
|
||||||
namespace shtcx {
|
|
||||||
|
|
||||||
static const char *const TAG = "shtcx";
|
static const char *const TAG = "shtcx";
|
||||||
|
|
||||||
static const uint16_t SHTCX_COMMAND_SLEEP = 0xB098;
|
static constexpr uint16_t SHTCX_COMMAND_SLEEP = 0xB098;
|
||||||
static const uint16_t SHTCX_COMMAND_WAKEUP = 0x3517;
|
static constexpr uint16_t SHTCX_COMMAND_WAKEUP = 0x3517;
|
||||||
static const uint16_t SHTCX_COMMAND_READ_ID_REGISTER = 0xEFC8;
|
static constexpr uint16_t SHTCX_COMMAND_READ_ID_REGISTER = 0xEFC8;
|
||||||
static const uint16_t SHTCX_COMMAND_SOFT_RESET = 0x805D;
|
static constexpr uint16_t SHTCX_COMMAND_SOFT_RESET = 0x805D;
|
||||||
static const uint16_t SHTCX_COMMAND_POLLING_H = 0x7866;
|
static constexpr uint16_t SHTCX_COMMAND_POLLING_H = 0x7866;
|
||||||
|
|
||||||
static const LogString *shtcx_type_to_string(SHTCXType type) {
|
static const LogString *shtcx_type_to_string(SHTCXType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -91,8 +90,6 @@ void SHTCXComponent::update() {
|
|||||||
} else {
|
} else {
|
||||||
temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
|
temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
|
||||||
humidity = 100.0f * float(raw_data[1]) / 65536.0f;
|
humidity = 100.0f * float(raw_data[1]) / 65536.0f;
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Temperature=%.2f°C Humidity=%.2f%%", temperature, humidity);
|
|
||||||
}
|
}
|
||||||
if (this->temperature_sensor_ != nullptr)
|
if (this->temperature_sensor_ != nullptr)
|
||||||
this->temperature_sensor_->publish_state(temperature);
|
this->temperature_sensor_->publish_state(temperature);
|
||||||
@@ -117,5 +114,4 @@ void SHTCXComponent::wake_up() {
|
|||||||
delayMicroseconds(200);
|
delayMicroseconds(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace shtcx
|
} // namespace esphome::shtcx
|
||||||
} // namespace esphome
|
|
||||||
|
|||||||
@@ -4,10 +4,13 @@
|
|||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/sensirion_common/i2c_sensirion.h"
|
#include "esphome/components/sensirion_common/i2c_sensirion.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome::shtcx {
|
||||||
namespace shtcx {
|
|
||||||
|
|
||||||
enum SHTCXType { SHTCX_TYPE_SHTC3 = 0, SHTCX_TYPE_SHTC1, SHTCX_TYPE_UNKNOWN };
|
enum SHTCXType : uint8_t {
|
||||||
|
SHTCX_TYPE_SHTC3 = 0,
|
||||||
|
SHTCX_TYPE_SHTC1,
|
||||||
|
SHTCX_TYPE_UNKNOWN,
|
||||||
|
};
|
||||||
|
|
||||||
/// This class implements support for the SHT3x-DIS family of temperature+humidity i2c sensors.
|
/// This class implements support for the SHT3x-DIS family of temperature+humidity i2c sensors.
|
||||||
class SHTCXComponent : public PollingComponent, public sensirion_common::SensirionI2CDevice {
|
class SHTCXComponent : public PollingComponent, public sensirion_common::SensirionI2CDevice {
|
||||||
@@ -23,11 +26,10 @@ class SHTCXComponent : public PollingComponent, public sensirion_common::Sensiri
|
|||||||
void wake_up();
|
void wake_up();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SHTCXType type_;
|
|
||||||
uint16_t sensor_id_;
|
|
||||||
sensor::Sensor *temperature_sensor_{nullptr};
|
sensor::Sensor *temperature_sensor_{nullptr};
|
||||||
sensor::Sensor *humidity_sensor_{nullptr};
|
sensor::Sensor *humidity_sensor_{nullptr};
|
||||||
|
uint16_t sensor_id_;
|
||||||
|
SHTCXType type_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace shtcx
|
} // namespace esphome::shtcx
|
||||||
} // namespace esphome
|
|
||||||
|
|||||||
Reference in New Issue
Block a user