Refactor SEN0590Component setup and update methods for improved readability and maintainability, while preserving functionality.
This commit is contained in:
1 parent
ec18b8c223
commit
966ce1f25d
1 file changed
+5
-8
@@ -14,9 +14,7 @@ static const uint8_t DATA_REG_HIGH = 0x01;
|
|||||||
static const uint8_t DATA_REG_LOW = 0x02;
|
static const uint8_t DATA_REG_LOW = 0x02;
|
||||||
static const uint8_t MEASURE_COMMAND = 0xB0;
|
static const uint8_t MEASURE_COMMAND = 0xB0;
|
||||||
|
|
||||||
class SEN0590Component : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
void SEN0590Component::setup() {
|
||||||
public:
|
|
||||||
void setup() override {
|
|
||||||
ESP_LOGD(TAG, "Setting up SEN0590 laser distance sensor...");
|
ESP_LOGD(TAG, "Setting up SEN0590 laser distance sensor...");
|
||||||
if (!this->write_byte(COMMAND_REG, 0x00)) {
|
if (!this->write_byte(COMMAND_REG, 0x00)) {
|
||||||
ESP_LOGE(TAG, "SEN0590 not responding. Check wiring and address.");
|
ESP_LOGE(TAG, "SEN0590 not responding. Check wiring and address.");
|
||||||
@@ -24,9 +22,9 @@ class SEN0590Component : public sensor::Sensor, public PollingComponent, public
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "SEN0590 Setup complete.");
|
ESP_LOGD(TAG, "SEN0590 Setup complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() override {
|
void SEN0590Component::update() {
|
||||||
if (!this->write_byte(COMMAND_REG, MEASURE_COMMAND)) {
|
if (!this->write_byte(COMMAND_REG, MEASURE_COMMAND)) {
|
||||||
ESP_LOGW(TAG, "Failed to send measurement command");
|
ESP_LOGW(TAG, "Failed to send measurement command");
|
||||||
this->status_set_warning();
|
this->status_set_warning();
|
||||||
@@ -55,10 +53,9 @@ class SEN0590Component : public sensor::Sensor, public PollingComponent, public
|
|||||||
ESP_LOGD(TAG, "Distance: %.1f cm (Raw: %d mm)", distance_cm, distance_mm);
|
ESP_LOGD(TAG, "Distance: %.1f cm (Raw: %d mm)", distance_cm, distance_mm);
|
||||||
this->publish_state(distance_cm);
|
this->publish_state(distance_cm);
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
}
|
}
|
||||||
|
|
||||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
float SEN0590Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace sen0590
|
} // namespace sen0590
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
Reference in new issue
Block a user