ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
tca8418.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/hal.h"
8
9namespace esphome::tca8418 {
10
11// Register map (see TI datasheet SCPS231)
12static constexpr uint8_t TCA8418_REG_CFG = 0x01;
13static constexpr uint8_t TCA8418_REG_INT_STAT = 0x02;
14static constexpr uint8_t TCA8418_REG_KEY_LCK_EC = 0x03;
15static constexpr uint8_t TCA8418_REG_KEY_EVENT_A = 0x04; // top of the event FIFO
16static constexpr uint8_t TCA8418_REG_GPIO_INT_EN1 = 0x1A;
17static constexpr uint8_t TCA8418_REG_KP_GPIO1 = 0x1D;
18static constexpr uint8_t TCA8418_REG_GPI_EM1 = 0x20;
19static constexpr uint8_t TCA8418_REG_GPIO_PULL1 = 0x2C;
20
21// CFG
22static constexpr uint8_t TCA8418_CFG_KEY_INT_EN = 0x01; // interrupt on key events
23static constexpr uint8_t TCA8418_CFG_INT_CFG = 0x10; // re-assert INT while events remain
24// INT_STAT
25static constexpr uint8_t TCA8418_INT_STAT_ALL = 0x1F; // every interrupt flag
26// KEY_LCK_EC: the low nibble is the number of queued events
27static constexpr uint8_t TCA8418_EVENT_COUNT_MASK = 0x0F;
28// Key events: bit 7 set = pressed, clear = released; bits 6-0 are the key number
29static constexpr uint8_t TCA8418_KEY_PRESSED = 0x80;
30static constexpr uint8_t TCA8418_KEY_CODE_MASK = 0x7F;
31// The event queue holds ten entries, which bounds how many can be read at once.
32static constexpr uint8_t TCA8418_FIFO_DEPTH = 10;
33
35 public:
37 virtual void key_pressed(uint8_t key) {}
38 virtual void key_released(uint8_t key) {}
39};
40
42 public:
43 void setup() override;
44 void loop() override;
45 void dump_config() override;
46
47 void set_rows(uint8_t rows) { this->rows_ = rows; }
48 void set_columns(uint8_t columns) { this->columns_ = columns; }
49 void set_gpi_events(bool gpi_events) { this->gpi_events_ = gpi_events; }
50 void set_keys(const uint8_t *keys, uint8_t size) {
51 this->keys_ = keys;
52 this->keys_size_ = size;
53 }
55
56#ifdef TCA8418_LISTENER_COUNT
57 void register_listener(TCA8418Listener *listener) { this->listeners_.push_back(listener); }
58#endif
59
61 static void IRAM_ATTR interrupt_handler(TCA8418Component *arg);
62
63 protected:
65 bool configure_pins_();
68 bool drain_events_(bool dispatch);
69 void dispatch_(uint8_t key, bool pressed);
71 uint8_t key_char_(uint8_t key) const;
72
73 const uint8_t *keys_{nullptr}; // PROGMEM, indexed by key number - 1
76#ifdef TCA8418_LISTENER_COUNT
78#endif
79 uint8_t rows_{0};
80 uint8_t columns_{0};
81 uint8_t keys_size_{0};
82 bool gpi_events_{true};
83};
84
85} // namespace esphome::tca8418
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:286
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
interface for components that provide keypresses
Definition key_provider.h:9
static void IRAM_ATTR 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:57
void set_columns(uint8_t columns)
Definition tca8418.h:48
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:104
uint8_t key_char_(uint8_t key) const
The character mapped to a matrix key, or 0 when it has none.
Definition tca8418.cpp:127
void dispatch_(uint8_t key, bool pressed)
Definition tca8418.cpp:133
void register_listener(TCA8418Listener *listener)
Definition tca8418.h:57
void set_rows(uint8_t rows)
Definition tca8418.h:47
void set_gpi_events(bool gpi_events)
Definition tca8418.h:49
void set_interrupt_pin(InternalGPIOPin *pin)
Definition tca8418.h:54
void set_keys(const uint8_t *keys, uint8_t size)
Definition tca8418.h:50
virtual void key_pressed(uint8_t key)
Called with the key number reported by the device (1..80 matrix, 97..114 individual inputs).
Definition tca8418.h:37
virtual void key_released(uint8_t key)
Definition tca8418.h:38
uint16_t size
Definition helpers.cpp:34
static void uint32_t