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
+37
-40
@@ -14,51 +14,48 @@ static const uint8_t DATA_REG_HIGH = 0x01;
|
||||
static const uint8_t DATA_REG_LOW = 0x02;
|
||||
static const uint8_t MEASURE_COMMAND = 0xB0;
|
||||
|
||||
class SEN0590Component : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override {
|
||||
ESP_LOGD(TAG, "Setting up SEN0590 laser distance sensor...");
|
||||
if (!this->write_byte(COMMAND_REG, 0x00)) {
|
||||
ESP_LOGE(TAG, "SEN0590 not responding. Check wiring and address.");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(TAG, "SEN0590 Setup complete.");
|
||||
void SEN0590Component::setup() {
|
||||
ESP_LOGD(TAG, "Setting up SEN0590 laser distance sensor...");
|
||||
if (!this->write_byte(COMMAND_REG, 0x00)) {
|
||||
ESP_LOGE(TAG, "SEN0590 not responding. Check wiring and address.");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(TAG, "SEN0590 Setup complete.");
|
||||
}
|
||||
|
||||
void SEN0590Component::update() {
|
||||
if (!this->write_byte(COMMAND_REG, MEASURE_COMMAND)) {
|
||||
ESP_LOGW(TAG, "Failed to send measurement command");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
void update() override {
|
||||
if (!this->write_byte(COMMAND_REG, MEASURE_COMMAND)) {
|
||||
ESP_LOGW(TAG, "Failed to send measurement command");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(50);
|
||||
|
||||
delay(50);
|
||||
|
||||
uint8_t buffer[2];
|
||||
if (this->read_register(DATA_REG_HIGH, &buffer[0], 1) != i2c::ERROR_OK ||
|
||||
this->read_register(DATA_REG_LOW, &buffer[1], 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGW(TAG, "Failed to read distance data");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
int distance_mm = (buffer[0] << 8) | buffer[1];
|
||||
if (distance_mm == 0xFFFF) {
|
||||
ESP_LOGW(TAG, "Received invalid distance data (0xFFFF)");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
float distance_cm = distance_mm / 10.0f;
|
||||
|
||||
ESP_LOGD(TAG, "Distance: %.1f cm (Raw: %d mm)", distance_cm, distance_mm);
|
||||
this->publish_state(distance_cm);
|
||||
this->status_clear_warning();
|
||||
uint8_t buffer[2];
|
||||
if (this->read_register(DATA_REG_HIGH, &buffer[0], 1) != i2c::ERROR_OK ||
|
||||
this->read_register(DATA_REG_LOW, &buffer[1], 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGW(TAG, "Failed to read distance data");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
};
|
||||
int distance_mm = (buffer[0] << 8) | buffer[1];
|
||||
if (distance_mm == 0xFFFF) {
|
||||
ESP_LOGW(TAG, "Received invalid distance data (0xFFFF)");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
float distance_cm = distance_mm / 10.0f;
|
||||
|
||||
ESP_LOGD(TAG, "Distance: %.1f cm (Raw: %d mm)", distance_cm, distance_mm);
|
||||
this->publish_state(distance_cm);
|
||||
this->status_clear_warning();
|
||||
}
|
||||
|
||||
float SEN0590Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
} // namespace sen0590
|
||||
} // namespace esphome
|
||||
Reference in new issue
Block a user