ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tca8418.cpp
Go to the documentation of this file.
1#include "tca8418.h"
3#include "esphome/core/log.h"
4
5namespace esphome::tca8418 {
6
7ESPHOME_LOG_TAG(TAG, "tca8418");
8
9// Minimum gap between event queue reads; the ten entry queue holds anything arriving sooner.
10static constexpr uint32_t POLL_INTERVAL_MS = 50;
11
13
15 // The configuration register reads back, so use it to check the device is there.
16 uint8_t config;
17 if (!this->read_byte(TCA8418_REG_CFG, &config)) {
18 this->mark_failed(LOG_STR("Failed to read from device - check wiring / address"));
19 return;
20 }
21
22 if (!this->configure_pins_()) {
23 this->mark_failed(LOG_STR("Failed to configure pins"));
24 return;
25 }
26
27 // Report key events, and keep the interrupt asserted while events remain queued.
28 if (!this->write_byte(TCA8418_REG_CFG, TCA8418_CFG_KEY_INT_EN | TCA8418_CFG_INT_CFG)) {
29 this->mark_failed(LOG_STR("Failed to write the configuration register"));
30 return;
31 }
32
33 if (this->interrupt_pin_ != nullptr) {
34 this->interrupt_pin_->setup();
35 // The interrupt output is active low, so a falling edge means an event was
36 // queued. The handler only wakes the loop; the queue is read from there.
38 }
39
40 // Discard anything the device queued before it was configured, and clear
41 // every interrupt flag: leaving one set holds the interrupt output asserted.
42 if (!this->drain_events_(false)) {
43 this->mark_failed(LOG_STR("Failed to clear the event queue"));
44 return;
45 }
46
47 // With an interrupt pin there is nothing to do until it fires. Stay running
48 // if the device still has something queued, since that produces no new edge.
49 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read())
50 this->disable_loop();
51}
52
54 // Each of the three registers covers a group of pins: ROW0-7, COL0-7, COL8-9.
55 uint8_t matrix[3] = {0, 0, 0};
56 if (this->rows_ > 0) {
57 matrix[0] = static_cast<uint8_t>((1U << this->rows_) - 1U);
58 const uint8_t low_columns = this->columns_ > 8 ? 8 : this->columns_;
59 matrix[1] = static_cast<uint8_t>((1U << low_columns) - 1U);
60 if (this->columns_ > 8)
61 matrix[2] = static_cast<uint8_t>((1U << (this->columns_ - 8U)) - 1U);
62 }
63
64 // Pins outside the matrix report individually when they are in event mode.
65 // The device ignores these bits for pins that belong to the matrix.
66 const uint8_t gpi[3] = {
67 static_cast<uint8_t>(this->gpi_events_ ? ~matrix[0] : 0),
68 static_cast<uint8_t>(this->gpi_events_ ? ~matrix[1] : 0),
69 static_cast<uint8_t>(this->gpi_events_ ? (0x03 & ~matrix[2]) : 0),
70 };
71
72 for (uint8_t i = 0; i < 3; i++) {
73 // Enable the internal pull-ups (a 0 bit enables the pull-up) so that
74 // buttons can simply switch their pin to ground.
75 if (!this->write_byte(TCA8418_REG_GPIO_PULL1 + i, 0x00) || !this->write_byte(TCA8418_REG_KP_GPIO1 + i, matrix[i]) ||
76 !this->write_byte(TCA8418_REG_GPI_EM1 + i, gpi[i]) || !this->write_byte(TCA8418_REG_GPIO_INT_EN1 + i, gpi[i])) {
77 return false;
78 }
79 }
80 return true;
81}
82
85 if (now - this->last_poll_ < POLL_INTERVAL_MS)
86 return;
87 this->last_poll_ = now;
88
89 if (!this->status_comm_result(this->drain_events_(true)))
90 return;
91
92 // Events that arrive during a read keep INT asserted without a new edge, so only stop once it is clear
93 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read())
94 this->disable_loop();
95}
96
98 uint8_t count;
99 if (!this->read_byte(TCA8418_REG_KEY_LCK_EC, &count))
100 return false;
101 count &= TCA8418_EVENT_COUNT_MASK;
102 // An idle poll has no flags to clear, unless INT is still asserted from a failed clear
103 if (count == 0 && dispatch && (this->interrupt_pin_ == nullptr || this->interrupt_pin_->digital_read()))
104 return true;
105 // The overflow flag stays clear with a full queue, so check the count
106 if (dispatch && count >= TCA8418_FIFO_DEPTH) {
107 ESP_LOGW(TAG, "Event queue is full - some key presses may have been lost");
108 }
109
110 uint8_t event;
111 for (uint8_t i = 0; i < count; i++) {
112 if (!this->read_byte(TCA8418_REG_KEY_EVENT_A, &event))
113 return false;
114 if (dispatch)
115 this->dispatch_(event & TCA8418_KEY_CODE_MASK, (event & TCA8418_KEY_PRESSED) != 0);
116 }
117 return this->write_byte(TCA8418_REG_INT_STAT, TCA8418_INT_STAT_ALL);
118}
119
120uint8_t TCA8418Component::key_char_(uint8_t key) const {
121 if (key == 0 || key > this->keys_size_)
122 return 0;
123 return progmem_read_byte(&this->keys_[key - 1]);
124}
125
126void TCA8418Component::dispatch_(uint8_t key, bool pressed) {
127 ESP_LOGV(TAG, "Key %u %s", key, pressed ? LOG_STR_LITERAL("pressed") : LOG_STR_LITERAL("released"));
128
129#ifdef TCA8418_LISTENER_COUNT
130 for (auto *listener : this->listeners_) {
131 if (pressed) {
132 listener->key_pressed(key);
133 } else {
134 listener->key_released(key);
135 }
136 }
137#endif
138
139 if (!pressed)
140 return;
141
142 // Key collectors and automations receive the mapped character when a key map
143 // is configured, and the key number otherwise.
144 const uint8_t key_char = this->key_char_(key);
145 this->send_key_(key_char != 0 ? key_char : key);
146}
147
149 ESP_LOGCONFIG(TAG,
150 "TCA8418:\n"
151 " Individual inputs: %s",
152 YESNO(this->gpi_events_));
153 if (this->rows_ > 0) {
154 ESP_LOGCONFIG(TAG, " Key matrix: %u rows x %u columns", this->rows_, this->columns_);
155 }
156 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
157 LOG_I2C_DEVICE(this);
158}
159
160} // namespace esphome::tca8418
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:29
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
static void interrupt_handler(TCA8418Component *arg)
Wakes the loop so queued events are read. Runs in interrupt context.
Definition tca8418.cpp:12
bool configure_pins_()
Write the matrix / individual-input pin configuration. Returns false on I2C failure.
Definition tca8418.cpp:53
InternalGPIOPin * interrupt_pin_
Definition tca8418.h:74
StaticVector< TCA8418Listener *, TCA8418_LISTENER_COUNT > listeners_
Definition tca8418.h:77
bool drain_events_(bool dispatch)
Read every queued key event, dispatching them if asked, then clear the interrupt flags.
Definition tca8418.cpp:97
uint8_t key_char_(uint8_t key) const
The character mapped to a matrix key, or 0 when it has none.
Definition tca8418.cpp:120
void dispatch_(uint8_t key, bool pressed)
Definition tca8418.cpp:126
const char * key
unsigned count
Definition gif.cpp:51
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
ESPHOME_LOG_TAG(TAG, "tca8418")
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t