Create ultrasonic_sensor.cpp

This commit is contained in:
Masterz69 2024-04-02 18:09:55 +03:00 committed by GitHub
parent e24c2332d6
commit d149aaa2d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,28 @@
#include "ultrasonic_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ultrasonic_uart {
static const char *const TAG = "ultrasonic_uart";
void UltrasonicSensorComponent_UART::setup() {
ESP_LOGCONFIG(TAG, "Setting up Ultrasonic Sensor...");
}
void UltrasonicSensorComponent_UART::update() {
this->write(0x55);
while (this->available() == 4) {
auto frame = *this->read_array<4>();
if ((frame[0] == 0xFF) && (((frame[0] + frame[1] + frame[2]) & 0x00FF) == frame[3])) {
float value = ((frame[1] << 8) + frame[2]) / 10.0;
this->publish_state(value);
}
}
}
void UltrasonicSensorComponent_UART::dump_config() {
LOG_SENSOR("", "Ultrasonic Sensor", this);
LOG_UPDATE_INTERVAL(this);
}
float UltrasonicSensorComponent_UART::get_setup_priority() const { return setup_priority::DATA; }
} // namespace ultrasonic_uart
} // namespace esphome