mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 17:27:59 +01:00
Create ultrasonic_sensor.cpp
This commit is contained in:
parent
e24c2332d6
commit
d149aaa2d3
1 changed files with 28 additions and 0 deletions
28
esphome/components/ultrasonic_uart/ultrasonic_sensor.cpp
Normal file
28
esphome/components/ultrasonic_uart/ultrasonic_sensor.cpp
Normal 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
|
Loading…
Reference in a new issue