ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tt21100.cpp
Go to the documentation of this file.
1#include "tt21100.h"
4namespace esphome::tt21100 {
6ESPHOME_LOG_TAG(TAG, "tt21100");
8static const uint8_t MAX_BUTTONS = 4;
9static const uint8_t MAX_TOUCH_POINTS = 5;
10static const uint8_t MAX_DATA_LEN = (7 + MAX_TOUCH_POINTS * 10); // 7 Header + (Points * 10 data bytes)
11
12struct TT21100ButtonReport {
13 uint16_t length; // Always 14 (0x000E)
14 uint8_t report_id; // Always 0x03
15 uint16_t timestamp; // Number in units of 100 us
16 uint8_t btn_value; // Only use bit 0..3
17 uint16_t btn_signal[MAX_BUTTONS];
18} __attribute__((packed));
19
20struct TT21100TouchRecord {
21 uint8_t : 5;
22 uint8_t touch_type : 3;
23 uint8_t tip : 1;
24 uint8_t event_id : 2;
25 uint8_t touch_id : 5;
26 uint16_t x;
27 uint16_t y;
28 uint8_t pressure;
29 uint16_t major_axis_length;
30 uint8_t orientation;
31} __attribute__((packed));
32
33struct TT21100TouchReport {
34 uint16_t length;
35 uint8_t report_id;
36 uint16_t timestamp;
37 uint8_t : 2;
38 uint8_t large_object : 1;
39 uint8_t record_num : 5;
40 uint8_t report_counter : 2;
41 uint8_t : 3;
42 uint8_t noise_effect : 3;
43 TT21100TouchRecord touch_record[MAX_TOUCH_POINTS];
44} __attribute__((packed));
45
47
49 // Register interrupt pin
50 if (this->interrupt_pin_ != nullptr) {
52 this->interrupt_pin_->setup();
54 }
55
56 // Perform reset if necessary
57 pulse_reset(this->reset_pin_, {.low_ms = 10, .high_after_ms = 10});
58
59 // Update display dimensions if they were updated during display setup
60 if (this->display_ != nullptr) {
61 if (this->x_raw_max_ == this->x_raw_min_) {
62 this->x_raw_max_ = this->display_->get_native_width();
63 }
64 if (this->y_raw_max_ == this->y_raw_min_) {
65 this->y_raw_max_ = this->display_->get_native_height();
66 }
67 }
68
69 // Trigger initial read to activate the interrupt
70 this->store_.touched = true;
71}
72
74 // Read report length
75 uint16_t data_len;
76 this->read((uint8_t *) &data_len, sizeof(data_len));
77
78 // Read report data
79 uint8_t data[MAX_DATA_LEN];
80 if (data_len > 0 && data_len < sizeof(data)) {
81 this->read(data, data_len);
82
83 if (data_len == 14) {
84 // Button event
85 auto *report = (TT21100ButtonReport *) data;
86
87 ESP_LOGV(TAG, "Button report: Len=%d, ID=%d, Time=%5u, Value=[%u], Signal=[%04X][%04X][%04X][%04X]",
88 report->length, report->report_id, report->timestamp, report->btn_value, report->btn_signal[0],
89 report->btn_signal[1], report->btn_signal[2], report->btn_signal[3]);
90
91 for (uint8_t i = 0; i < 4; i++) {
92 for (auto *listener : this->button_listeners_)
93 listener->update_button(i, report->btn_signal[i]);
94 }
95
96 } else if (data_len >= 7) {
97 // Touch point event
98 auto *report = (TT21100TouchReport *) data;
99
100 ESP_LOGV(TAG,
101 "Touch report: Len=%d, ID=%d, Time=%5u, LargeObject=%u, RecordNum=%u, RecordCounter=%u, NoiseEffect=%u",
102 report->length, report->report_id, report->timestamp, report->large_object, report->record_num,
103 report->report_counter, report->noise_effect);
104
105 uint8_t touch_count = (data_len - (sizeof(*report) - sizeof(report->touch_record))) / sizeof(TT21100TouchRecord);
106
107 for (int i = 0; i < touch_count; i++) {
108 auto *touch = &report->touch_record[i];
109
110 ESP_LOGV(TAG,
111 "Touch %d: Type=%u, Tip=%u, EventId=%u, TouchId=%u, X=%u, Y=%u, Pressure=%u, MajorAxisLen=%u, "
112 "Orientation=%u",
113 i, touch->touch_type, touch->tip, touch->event_id, touch->touch_id, touch->x, touch->y,
114 touch->pressure, touch->major_axis_length, touch->orientation);
115
116 this->add_raw_touch_position_(touch->tip, touch->x, touch->y, touch->pressure);
117 }
118 }
119 }
120}
121
123 ESP_LOGCONFIG(TAG, "TT21100 Touchscreen:");
124 LOG_I2C_DEVICE(this);
125 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
126 LOG_PIN(" Reset Pin: ", this->reset_pin_);
127}
128
129} // namespace esphome::tt21100
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:341
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:343
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
void attach_interrupt_(InternalGPIOPin *irq_pin, esphome::gpio::InterruptType type)
Call this function to send touch points to the on_touch listener and the binary_sensors.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
float get_setup_priority() const override
Definition tt21100.cpp:46
std::vector< TT21100ButtonListener * > button_listeners_
Definition tt21100.h:36
InternalGPIOPin * interrupt_pin_
Definition tt21100.h:33
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
ESPHOME_LOG_TAG(TAG, "touchscreen")
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:163