ESPHome 2026.2.1
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
42// Helper to find index of value in constexpr array
43template<size_t N> optional<size_t> find_index(const uint32_t (&arr)[N], uint32_t value) {
44 for (size_t i = 0; i < N; i++) {
45 if (arr[i] == value)
46 return i;
47 }
48 return {};
49}
50
51static const char *const UNKNOWN_MAC = "unknown";
52static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X";
53
54// Helper function to format MAC address with stack allocation
55// Returns pointer to UNKNOWN_MAC constant or formatted buffer
56// Buffer must be exactly 18 bytes (17 for "XX:XX:XX:XX:XX:XX" + null terminator)
57inline const char *format_mac_str(const uint8_t *mac_address, std::span<char, 18> buffer) {
58 if (mac_address_is_valid(mac_address)) {
59 format_mac_addr_upper(mac_address, buffer.data());
60 return buffer.data();
61 }
62 return UNKNOWN_MAC;
63}
64
65// Helper function to format firmware version with stack allocation
66// Buffer must be exactly 20 bytes (format: "x.xxXXXXXX" fits in 11 + null terminator, 20 for safety)
67inline void format_version_str(const uint8_t *version, std::span<char, 20> buffer) {
68 snprintf(buffer.data(), buffer.size(), VERSION_FMT, version[1], version[0], version[5], version[4], version[3],
69 version[2]);
70}
71
72#ifdef USE_SENSOR
73// Helper class to store a sensor with a deduplicator & publish state only when the value changes
74template<typename T> class SensorWithDedup {
75 public:
77
79 if (this->publish_dedup.next(state)) {
80 this->sens->publish_state(static_cast<float>(state));
81 }
82 }
83
85 if (this->publish_dedup.next_unknown()) {
86 this->sens->publish_state(NAN);
87 }
88 }
89
92};
93#endif
94} // namespace esphome::ld24xx
Helper class to deduplicate items in a series of values.
Definition helpers.h:1443
Deduplicator< T > publish_dedup
Definition ld24xx.h:91
SensorWithDedup(sensor::Sensor *sens)
Definition ld24xx.h:76
void publish_state_if_not_dup(T state)
Definition ld24xx.h:78
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:65
bool state
Definition fan.h:2
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
Definition ld24xx.h:67
const char * format_mac_str(const uint8_t *mac_address, std::span< char, 18 > buffer)
Definition ld24xx.h:57
optional< size_t > find_index(const uint32_t(&arr)[N], uint32_t value)
Definition ld24xx.h:43
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:830
char * 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:1045