This commit is contained in:
sredfern committed 2025-05-05 22:50:17 +10:00
1 parent 966ce1f25d
commit b548ea6984
1 file changed
+12 -11
+12 -11
View File
@@ -9,14 +9,14 @@ namespace esphome {
namespace sen0590 { namespace sen0590 {
static const char *const TAG = "sen0590"; static const char *const TAG = "sen0590";
static const uint8_t COMMAND_REG = 0x00; static const uint8_t COMMAND_REG = 0x10;
static const uint8_t DATA_REG_HIGH = 0x01; static const uint8_t DATA_REG = 0x02;
static const uint8_t DATA_REG_LOW = 0x02;
static const uint8_t MEASURE_COMMAND = 0xB0; static const uint8_t MEASURE_COMMAND = 0xB0;
void SEN0590Component::setup() { void SEN0590Component::setup() {
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)) { uint8_t temp_buffer;
if (this->read_register(DATA_REG, &temp_buffer, 1) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "SEN0590 not responding. Check wiring and address."); ESP_LOGE(TAG, "SEN0590 not responding. Check wiring and address.");
this->mark_failed(); this->mark_failed();
return; return;
@@ -26,7 +26,7 @@ void SEN0590Component::setup() {
void SEN0590Component::update() { 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 to reg 0x%02X", COMMAND_REG);
this->status_set_warning(); this->status_set_warning();
return; return;
} }
@@ -34,16 +34,17 @@ void SEN0590Component::update() {
delay(50); delay(50);
uint8_t buffer[2]; uint8_t buffer[2];
if (this->read_register(DATA_REG_HIGH, &buffer[0], 1) != i2c::ERROR_OK || if (this->read_register(DATA_REG, buffer, 2) != i2c::ERROR_OK) {
this->read_register(DATA_REG_LOW, &buffer[1], 1) != i2c::ERROR_OK) { ESP_LOGW(TAG, "Failed to read distance data from reg 0x%02X", DATA_REG);
ESP_LOGW(TAG, "Failed to read distance data");
this->status_set_warning(); this->status_set_warning();
return; return;
} }
int distance_mm = (buffer[0] << 8) | buffer[1]; int distance_mm = ((buffer[0] << 8) | buffer[1]) + 10;
if (distance_mm == 0xFFFF) {
ESP_LOGW(TAG, "Received invalid distance data (0xFFFF)"); if (((buffer[0] << 8) | buffer[1]) == 0xFFFF) {
ESP_LOGW(TAG, "Received invalid distance data (raw 0xFFFF)");
this->publish_state(NAN);
this->status_set_warning(); this->status_set_warning();
return; return;
} }