ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ds1603l.cpp
Go to the documentation of this file.
1#include "ds1603l.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome::ds1603l {
7
8ESPHOME_LOG_TAG(TAG, "ds1603l.sensor");
9
11 this->read_available([this](uint8_t byte) {
12 const uint8_t *frame = this->reader_.feed(byte);
13 if (frame == nullptr) {
14 if (this->reader_.length() == 0) {
15 ESP_LOGV(TAG, "Skipping byte 0x%02X while looking for header", byte);
16 }
17 return;
18 }
19 if (!this->parse_data_(frame)) {
20 // The header byte was part of the payload of a misaligned frame, so realign instead of dropping everything
21 this->reader_.resync();
22 }
23 });
24}
25
26void DS1603L::dump_config() { LOG_SENSOR("", "DS1603L", this); }
27
28bool DS1603L::parse_data_(const uint8_t *frame) {
29 uint8_t header = frame[0];
30 uint8_t data_h = frame[1];
31 uint8_t data_l = frame[2];
32 uint8_t checksum = frame[3];
33
34 uint8_t computed_checksum = (header + data_h + data_l) & 0xFF;
35
36 ESP_LOGV(TAG, "Data: Header=0x%02X, Data_H=0x%02X, Data_L=0x%02X, Checksum=0x%02X", header, data_h, data_l, checksum);
37
38 if (checksum != computed_checksum) {
39 ESP_LOGW(TAG, "Checksum mismatch: received 0x%02X, expected 0x%02X", checksum, computed_checksum);
40 return false;
41 }
42
43 this->publish_state(encode_uint16(data_h, data_l));
44 return true;
45}
46
47} // namespace esphome::ds1603l
uint8_t checksum
Definition bl0906.h:3
size_t length() const
Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
const uint8_t * feed(uint8_t c)
Feed one byte; returns the completed frame, header included, valid until the next call,...
void resync()
For a frame the caller rejected (bad checksum): drop its first byte and realign on a header in the re...
bool parse_data_(const uint8_t *frame)
Definition ds1603l.cpp:28
FrameReader< FRAME_SIZE, HEADER_BYTE > reader_
Definition ds1603l.h:25
void dump_config() override
Definition ds1603l.cpp:26
void loop() override
Definition ds1603l.cpp:10
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
ESPHOME_LOG_TAG(TAG, "ds1603l.sensor")
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043