ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
wiegand.cpp
Go to the documentation of this file.
1#include "wiegand.h"
2#include <cinttypes>
4#include "esphome/core/log.h"
5
6namespace esphome::wiegand {
7
8static const char *const TAG = "wiegand";
9static const char *const KEYS = "0123456789*#";
10
11void IRAM_ATTR HOT WiegandStore::d0_gpio_intr(WiegandStore *arg) {
12 if (arg->d0.digital_read())
13 return;
14 arg->count += 1;
15 arg->value <<= 1;
16 arg->last_bit_time = millis();
17 arg->done = false;
18}
19
20void IRAM_ATTR HOT WiegandStore::d1_gpio_intr(WiegandStore *arg) {
21 if (arg->d1.digital_read())
22 return;
23 arg->count += 1;
24 arg->value = (arg->value << 1) | 1;
25 arg->last_bit_time = millis();
26 arg->done = false;
27}
28
37
38bool check_eparity(uint64_t value, int start, int length) {
39 int parity = 0;
40 uint64_t mask = 1LL << start;
41 for (int i = 0; i < length; i++, mask <<= 1) {
42 if (value & mask)
43 parity++;
44 }
45 return !(parity & 1);
46}
47
48bool check_oparity(uint64_t value, int start, int length) {
49 int parity = 0;
50 uint64_t mask = 1LL << start;
51 for (int i = 0; i < length; i++, mask <<= 1) {
52 if (value & mask)
53 parity++;
54 }
55 return parity & 1;
56}
57
59 if (this->store_.done)
60 return;
61 if (millis() - this->store_.last_bit_time < 100)
62 return;
63 uint8_t count = this->store_.count;
64 uint64_t value = this->store_.value;
65 this->store_.count = 0;
66 this->store_.value = 0;
67 this->store_.done = true;
68 ESP_LOGV(TAG, "received %d-bit value: %llx", count, value);
69 for (auto *trigger : this->raw_triggers_)
70 trigger->trigger(count, value);
71 if (count == 26) {
72 char tag_buf[12]; // max 8 digits for 24-bit value + null
73 buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu32, static_cast<uint32_t>((value >> 1) & 0xffffff));
74 ESP_LOGD(TAG, "received 26-bit tag: %s", tag_buf);
75 if (!check_eparity(value, 13, 13) || !check_oparity(value, 0, 13)) {
76 ESP_LOGW(TAG, "invalid parity");
77 return;
78 }
79 for (auto *trigger : this->tag_triggers_)
80 trigger->trigger(tag_buf);
81 } else if (count == 34) {
82 char tag_buf[12]; // max 10 digits for 32-bit value + null
83 buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu32, static_cast<uint32_t>((value >> 1) & 0xffffffff));
84 ESP_LOGD(TAG, "received 34-bit tag: %s", tag_buf);
85 if (!check_eparity(value, 17, 17) || !check_oparity(value, 0, 17)) {
86 ESP_LOGW(TAG, "invalid parity");
87 return;
88 }
89 for (auto *trigger : this->tag_triggers_)
90 trigger->trigger(tag_buf);
91 } else if (count == 37) {
92 char tag_buf[12]; // max 11 digits for 35-bit value + null
93 buf_append_printf(tag_buf, sizeof(tag_buf), 0, "%" PRIu64, static_cast<uint64_t>((value >> 1) & 0x7ffffffff));
94 ESP_LOGD(TAG, "received 37-bit tag: %s", tag_buf);
95 if (!check_eparity(value, 18, 19) || !check_oparity(value, 0, 19)) {
96 ESP_LOGW(TAG, "invalid parity");
97 return;
98 }
99 for (auto *trigger : this->tag_triggers_)
100 trigger->trigger(tag_buf);
101 } else if (count == 4) {
102 for (auto *trigger : this->key_triggers_)
103 trigger->trigger(value);
104 if (value < 12) {
105 uint8_t key = KEYS[value];
106 this->send_key_(key);
107 }
108 } else if (count == 8) {
109 if ((value ^ 0xf0) >> 4 == (value & 0xf)) {
110 value &= 0xf;
111 for (auto *trigger : this->key_triggers_)
112 trigger->trigger(value);
113 if (value < 12) {
114 uint8_t key = KEYS[value];
115 this->send_key_(key);
116 }
117 }
118 } else {
119 ESP_LOGD(TAG, "received unknown %d-bit value: %llx", count, value);
120 }
121}
122
124 ESP_LOGCONFIG(TAG, "Wiegand reader:");
125 LOG_PIN(" D0 pin: ", this->d0_pin_);
126 LOG_PIN(" D1 pin: ", this->d1_pin_);
127}
128
129} // namespace esphome::wiegand
virtual void setup()=0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
virtual ISRInternalGPIOPin to_isr() const =0
InternalGPIOPin * d1_pin_
Definition wiegand.h:45
void loop() override
Definition wiegand.cpp:58
void dump_config() override
Definition wiegand.cpp:123
std::vector< WiegandKeyTrigger * > key_triggers_
Definition wiegand.h:49
InternalGPIOPin * d0_pin_
Definition wiegand.h:44
void setup() override
Definition wiegand.cpp:29
std::vector< WiegandTagTrigger * > tag_triggers_
Definition wiegand.h:47
std::vector< WiegandRawTrigger * > raw_triggers_
Definition wiegand.h:48
WiegandStore store_
Definition wiegand.h:46
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
bool check_eparity(uint64_t value, int start, int length)
Definition wiegand.cpp:38
bool check_oparity(uint64_t value, int start, int length)
Definition wiegand.cpp:48
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
ISRInternalGPIOPin d0
Definition wiegand.h:13
static void d0_gpio_intr(WiegandStore *arg)
Definition wiegand.cpp:11
static void d1_gpio_intr(WiegandStore *arg)
Definition wiegand.cpp:20
ISRInternalGPIOPin d1
Definition wiegand.h:14
volatile uint64_t value
Definition wiegand.h:15
volatile uint8_t count
Definition wiegand.h:18
volatile uint32_t last_bit_time
Definition wiegand.h:16
uint16_t length
Definition tt21100.cpp:0