ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
ld24xx.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <memory>
7#include <span>
8
9#ifdef USE_SENSOR
11
12#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \
13 protected: \
14 ld24xx::SensorWithDedup<dedup_type> *name##_sensor_{nullptr}; \
15\
16 public: \
17 void set_##name##_sensor(sensor::Sensor *sensor) { \
18 this->name##_sensor_ = new ld24xx::SensorWithDedup<dedup_type>(sensor); \
19 }
20#endif
21
22#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \
23 if ((sensor) != nullptr) { \
24 LOG_SENSOR(tag, name, (sensor)->sens); \
25 }
26
27#define SAFE_PUBLISH_SENSOR(sensor, value) \
28 if ((sensor) != nullptr) { \
29 (sensor)->publish_state_if_not_dup(value); \
30 }
31
32#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) \
33 if ((sensor) != nullptr) { \
34 (sensor)->publish_state_unknown(); \
35 }
36
37#define highbyte(val) (uint8_t)((val) >> 8)
38#define lowbyte(val) (uint8_t)((val) &0xff)
39
40namespace esphome::ld24xx {
41
42static const char *const UNKNOWN_MAC = "unknown";
43static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X";
44
45// Helper function to format MAC address with stack allocation
46// Returns pointer to UNKNOWN_MAC constant or formatted buffer
47// Buffer must be exactly 18 bytes (17 for "XX:XX:XX:XX:XX:XX" + null terminator)
48inline const char *format_mac_str(const uint8_t *mac_address, std::span<char, 18> buffer) {
49 if (mac_address_is_valid(mac_address)) {
50 format_mac_addr_upper(mac_address, buffer.data());
51 return buffer.data();
52 }
53 return UNKNOWN_MAC;
54}
55
56// Helper function to format firmware version with stack allocation
57// Buffer must be exactly 20 bytes (format: "x.xxXXXXXX" fits in 11 + null terminator, 20 for safety)
58inline void format_version_str(const uint8_t *version, std::span<char, 20> buffer) {
59 snprintf(buffer.data(), buffer.size(), VERSION_FMT, version[1], version[0], version[5], version[4], version[3],
60 version[2]);
61}
62
63#ifdef USE_SENSOR
64// Helper class to store a sensor with a deduplicator & publish state only when the value changes
65template<typename T> class SensorWithDedup {
66 public:
68
70 if (this->publish_dedup.next(state)) {
71 this->sens->publish_state(static_cast<float>(state));
72 }
73 }
74
76 if (this->publish_dedup.next_unknown()) {
77 this->sens->publish_state(NAN);
78 }
79 }
80
83};
84#endif
85} // namespace esphome::ld24xx
Helper class to deduplicate items in a series of values.
Definition helpers.h:1074
Deduplicator< T > publish_dedup
Definition ld24xx.h:82
SensorWithDedup(sensor::Sensor *sens)
Definition ld24xx.h:67
void publish_state_if_not_dup(T state)
Definition ld24xx.h:69
Base-class for all sensors.
Definition sensor.h:43
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
bool state
Definition fan.h:0
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
Definition ld24xx.h:58
const char * format_mac_str(const uint8_t *mac_address, std::span< char, 18 > buffer)
Definition ld24xx.h:48
void format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:765
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:721