13 namespace hrxl_maxsonar_wr {
15 static const char *
const TAG =
"hrxl.maxsonar.wr.sensor";
16 static const uint8_t ASCII_CR = 0x0D;
17 static const uint8_t ASCII_NBSP = 0xFF;
18 static const int MAX_DATA_LENGTH_BYTES = 6;
38 if (this->
buffer_.back() ==
static_cast<char>(ASCII_NBSP)) {
45 if (this->
buffer_.back() ==
static_cast<char>(ASCII_CR) || this->
buffer_.length() >= MAX_DATA_LENGTH_BYTES) {
46 ESP_LOGV(TAG,
"Read from serial: %s", this->
buffer_.c_str());
48 size_t rpos = this->
buffer_.find(static_cast<char>(ASCII_CR));
50 if (this->
buffer_.length() <= MAX_DATA_LENGTH_BYTES && this->
buffer_[0] ==
'R' && rpos != std::string::npos) {
51 std::string distance = this->buffer_.substr(1, rpos - 1);
52 int millimeters = parse_number<int>(distance).value_or(0);
55 if (distance.length() == 3) {
56 millimeters = millimeters * 10;
59 float meters = float(millimeters) / 1000.0;
60 ESP_LOGV(TAG,
"Distance from sensor: %d mm, %f m", millimeters, meters);
63 ESP_LOGW(TAG,
"Invalid data read from sensor: %s", this->buffer_.c_str());
65 this->buffer_.clear();
70 ESP_LOGCONFIG(TAG,
"HRXL MaxSonar WR Sensor:");
71 LOG_SENSOR(
" ",
"Distance",
this);
void dump_config() override
void loop() override
HRXL sensors output the format "R1234\r" at 6Hz The 1234 means 1234mm XL sensors output the format "R...
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
bool read_byte(uint8_t *data)
void publish_state(float state)
Publish a new state to the front-end.
Implementation of SPI Controller mode.