ESPHome 2026.12.0-dev
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 // The configuration register reads back, so use it to check the device is there.
14 uint8_t config;
15 if (!this->setup_check(this->read_byte(TCA8418_REG_CFG, &config),
16 LOG_STR("Failed to read from device - check wiring / address")))
17 return;
18
19 if (!this->setup_check(this->configure_pins_(), LOG_STR("Failed to configure pins")))
20 return;
21
22 // Report key events, and keep the interrupt asserted while events remain queued.
23 if (!this->setup_check(this->write_byte(TCA8418_REG_CFG, TCA8418_CFG_KEY_INT_EN | TCA8418_CFG_INT_CFG),
24 LOG_STR("Failed to write the configuration register")))
25 return;
26
27 // The interrupt output is active low, so a falling edge means an event was queued.
29
30 // Discard anything the device queued before it was configured, and clear
31 // every interrupt flag: leaving one set holds the interrupt output asserted.
32 if (!this->setup_check(this->drain_events_(false), LOG_STR("Failed to clear the event queue")))
33 return;
34
35 // With an interrupt pin there is nothing to do until it fires. Stay running
36 // if the device still has something queued, since that produces no new edge.
37 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read())
38 this->disable_loop();
39}
40
41bool TCA8418Component::configure_pins_() {
42 // Each of the three registers covers a group of pins: ROW0-7, COL0-7, COL8-9.
43 uint8_t matrix[3] = {0, 0, 0};
44 if (this->rows_ > 0) {
45 matrix[0] = static_cast<uint8_t>((1U << this->rows_) - 1U);
46 const uint8_t low_columns = this->columns_ > 8 ? 8 : this->columns_;
47 matrix[1] = static_cast<uint8_t>((1U << low_columns) - 1U);
48 if (this->columns_ > 8)
49 matrix[2] = static_cast<uint8_t>((1U << (this->columns_ - 8U)) - 1U);
50 }
51
52 // Pins outside the matrix report individually when they are in event mode.
53 // The device ignores these bits for pins that belong to the matrix.
54 const uint8_t gpi[3] = {
55 static_cast<uint8_t>(this->gpi_events_ ? ~matrix[0] : 0),
56 static_cast<uint8_t>(this->gpi_events_ ? ~matrix[1] : 0),
57 static_cast<uint8_t>(this->gpi_events_ ? (0x03 & ~matrix[2]) : 0),
58 };
59
60 for (uint8_t i = 0; i < 3; i++) {
61 // Enable the internal pull-ups (a 0 bit enables the pull-up) so that
62 // buttons can simply switch their pin to ground.
63 if (!this->write_byte(TCA8418_REG_GPIO_PULL1 + i, 0x00) || !this->write_byte(TCA8418_REG_KP_GPIO1 + i, matrix[i]) ||
64 !this->write_byte(TCA8418_REG_GPI_EM1 + i, gpi[i]) || !this->write_byte(TCA8418_REG_GPIO_INT_EN1 + i, gpi[i])) {
65 return false;
66 }
67 }
68 return true;
69}
70
73 if (now - this->last_poll_ < POLL_INTERVAL_MS)
74 return;
75 this->last_poll_ = now;
76
77 if (!this->status_comm_result(this->drain_events_(true)))
78 return;
79
80 // Events that arrive during a read keep INT asserted without a new edge, so only stop once it is clear
81 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read())
82 this->disable_loop();
83}
84
86 uint8_t count;
87 if (!this->read_byte(TCA8418_REG_KEY_LCK_EC, &count))
88 return false;
89 count &= TCA8418_EVENT_COUNT_MASK;
90 // An idle poll has no flags to clear, unless INT is still asserted from a failed clear
91 if (count == 0 && dispatch && (this->interrupt_pin_ == nullptr || this->interrupt_pin_->digital_read()))
92 return true;
93 // The overflow flag stays clear with a full queue, so check the count
94 if (dispatch && count >= TCA8418_FIFO_DEPTH) {
95 ESP_LOGW(TAG, "Event queue is full - some key presses may have been lost");
96 }
97
98 uint8_t event;
99 for (uint8_t i = 0; i < count; i++) {
100 if (!this->read_byte(TCA8418_REG_KEY_EVENT_A, &event))
101 return false;
102 if (dispatch)
103 this->dispatch_(event & TCA8418_KEY_CODE_MASK, (event & TCA8418_KEY_PRESSED) != 0);
104 }
105 return this->write_byte(TCA8418_REG_INT_STAT, TCA8418_INT_STAT_ALL);
106}
107
108uint8_t TCA8418Component::key_char_(uint8_t key) const {
109 if (key == 0 || key > this->keys_size_)
110 return 0;
111 return progmem_read_byte(&this->keys_[key - 1]);
112}
113
114void TCA8418Component::dispatch_(uint8_t key, bool pressed) {
115 ESP_LOGV(TAG, "Key %u %s", key, pressed ? LOG_STR_LITERAL("pressed") : LOG_STR_LITERAL("released"));
116
117#ifdef USE_TCA8418_KEY_PUBLISHER
118 if (this->key_publisher_ != nullptr)
119 this->key_publisher_(key, pressed);
120#endif
121
122 if (!pressed)
123 return;
124
125 // Key collectors and automations receive the mapped character when a key map
126 // is configured, and the key number otherwise.
127 const uint8_t key_char = this->key_char_(key);
128 this->send_key_(key_char != 0 ? key_char : key);
129}
130
132 ESP_LOGCONFIG(TAG,
133 "TCA8418:\n"
134 " Individual inputs: %s",
135 YESNO(this->gpi_events_));
136 if (this->rows_ > 0) {
137 ESP_LOGCONFIG(TAG, " Key matrix: %u rows x %u columns", this->rows_, this->columns_);
138 }
139 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
140 LOG_I2C_DEVICE(this);
141}
142
143} // 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.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void disable_loop()
Disable this component's loop.
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
InternalGPIOPin * interrupt_pin_
Definition tca8418.h:71
HUB_PUBLISHER(key, uint8_t, bool) protected bool drain_events_(bool dispatch)
Read every queued key event, dispatching them if asked, then clear the interrupt flags.
Definition tca8418.cpp:85
uint8_t key_char_(uint8_t key) const
The character mapped to a matrix key, or 0 when it has none.
Definition tca8418.cpp:108
void dispatch_(uint8_t key, bool pressed)
Definition tca8418.cpp:114
const char * key
unsigned count
Definition gif.cpp:51
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
ESPHOME_LOG_TAG(TAG, "tca8418")
bool setup_wake_interrupt(GPIOPin *pin, Component *component, gpio::InterruptType type)
Set up pin and attach the wake interrupt when it is a GPIO of the chip itself.
Definition gpio.h:60
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