mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 08:41:59 +00:00
[wiegand] Replace heap-allocating to_string with stack buffers (#13294)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "wiegand.h"
|
||||
#include <cinttypes>
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
@@ -69,32 +70,35 @@ void Wiegand::loop() {
|
||||
for (auto *trigger : this->raw_triggers_)
|
||||
trigger->trigger(count, value);
|
||||
if (count == 26) {
|
||||
std::string tag = to_string((value >> 1) & 0xffffff);
|
||||
ESP_LOGD(TAG, "received 26-bit tag: %s", tag.c_str());
|
||||
char tag_buf[12]; // max 8 digits for 24-bit value + null
|
||||
buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu32, static_cast<uint32_t>((value >> 1) & 0xffffff));
|
||||
ESP_LOGD(TAG, "received 26-bit tag: %s", tag_buf);
|
||||
if (!check_eparity(value, 13, 13) || !check_oparity(value, 0, 13)) {
|
||||
ESP_LOGW(TAG, "invalid parity");
|
||||
return;
|
||||
}
|
||||
for (auto *trigger : this->tag_triggers_)
|
||||
trigger->trigger(tag);
|
||||
trigger->trigger(tag_buf);
|
||||
} else if (count == 34) {
|
||||
std::string tag = to_string((value >> 1) & 0xffffffff);
|
||||
ESP_LOGD(TAG, "received 34-bit tag: %s", tag.c_str());
|
||||
char tag_buf[12]; // max 10 digits for 32-bit value + null
|
||||
buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu32, static_cast<uint32_t>((value >> 1) & 0xffffffff));
|
||||
ESP_LOGD(TAG, "received 34-bit tag: %s", tag_buf);
|
||||
if (!check_eparity(value, 17, 17) || !check_oparity(value, 0, 17)) {
|
||||
ESP_LOGW(TAG, "invalid parity");
|
||||
return;
|
||||
}
|
||||
for (auto *trigger : this->tag_triggers_)
|
||||
trigger->trigger(tag);
|
||||
trigger->trigger(tag_buf);
|
||||
} else if (count == 37) {
|
||||
std::string tag = to_string((value >> 1) & 0x7ffffffff);
|
||||
ESP_LOGD(TAG, "received 37-bit tag: %s", tag.c_str());
|
||||
char tag_buf[12]; // max 11 digits for 35-bit value + null
|
||||
buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu64, static_cast<uint64_t>((value >> 1) & 0x7ffffffff));
|
||||
ESP_LOGD(TAG, "received 37-bit tag: %s", tag_buf);
|
||||
if (!check_eparity(value, 18, 19) || !check_oparity(value, 0, 19)) {
|
||||
ESP_LOGW(TAG, "invalid parity");
|
||||
return;
|
||||
}
|
||||
for (auto *trigger : this->tag_triggers_)
|
||||
trigger->trigger(tag);
|
||||
trigger->trigger(tag_buf);
|
||||
} else if (count == 4) {
|
||||
for (auto *trigger : this->key_triggers_)
|
||||
trigger->trigger(value);
|
||||
|
||||
Reference in New Issue
Block a user