ESPHome 2026.12.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
34// A template on the sensor type keeps binary_sensor.h out of this hub header
35template<typename T> inline void publish_key(T *sensor, uint8_t key, uint8_t sensor_key, bool pressed) {
36 if (key == sensor_key)
37 sensor->publish_state(pressed);
38}
39
41 public:
42 void setup() override;
43 void loop() override;
44 void dump_config() override;
45
46 void set_rows(uint8_t rows) { this->rows_ = rows; }
47 void set_columns(uint8_t columns) { this->columns_ = columns; }
48 void set_gpi_events(bool gpi_events) { this->gpi_events_ = gpi_events; }
49 void set_keys(const uint8_t *keys, uint8_t size) {
50 this->keys_ = keys;
51 this->keys_size_ = size;
52 }
54
55#ifdef USE_TCA8418_KEY_PUBLISHER
56 // key is 1..80 for the matrix, 97..114 for individual inputs
57 HUB_PUBLISHER(key, uint8_t, bool)
58#endif
59
60 protected:
62 bool configure_pins_();
65 bool drain_events_(bool dispatch);
66 void dispatch_(uint8_t key, bool pressed);
68 uint8_t key_char_(uint8_t key) const;
69
70 const uint8_t *keys_{nullptr}; // PROGMEM, indexed by key number - 1
73 uint8_t rows_{0};
74 uint8_t columns_{0};
75 uint8_t keys_size_{0};
76 bool gpi_events_{true};
77};
78
79} // namespace esphome::tca8418
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
interface for components that provide keypresses
Definition key_provider.h:9
void set_columns(uint8_t columns)
Definition tca8418.h:47
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
void set_rows(uint8_t rows)
Definition tca8418.h:46
void set_gpi_events(bool gpi_events)
Definition tca8418.h:48
void set_interrupt_pin(InternalGPIOPin *pin)
Definition tca8418.h:53
void set_keys(const uint8_t *keys, uint8_t size)
Definition tca8418.h:49
const char * key
void publish_key(T *sensor, uint8_t key, uint8_t sensor_key, bool pressed)
Definition tca8418.h:35
uint16_t size
Definition helpers.cpp:44
static void uint32_t