ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
tt21100.cpp
Go to the documentation of this file.
1#include "tt21100.h"
4namespace esphome {
5namespace tt21100 {
7static const char *const TAG = "tt21100";
9static const uint8_t MAX_BUTTONS = 4;
10static const uint8_t MAX_TOUCH_POINTS = 5;
11static const uint8_t MAX_DATA_LEN = (7 + MAX_TOUCH_POINTS * 10); // 7 Header + (Points * 10 data bytes)
12
13struct TT21100ButtonReport {
14 uint16_t length; // Always 14 (0x000E)
15 uint8_t report_id; // Always 0x03
16 uint16_t timestamp; // Number in units of 100 us
17 uint8_t btn_value; // Only use bit 0..3
18 uint16_t btn_signal[MAX_BUTTONS];
19} __attribute__((packed));
20
21struct TT21100TouchRecord {
22 uint8_t : 5;
23 uint8_t touch_type : 3;
24 uint8_t tip : 1;
25 uint8_t event_id : 2;
26 uint8_t touch_id : 5;
27 uint16_t x;
28 uint16_t y;
29 uint8_t pressure;
30 uint16_t major_axis_length;
31 uint8_t orientation;
32} __attribute__((packed));
33
34struct TT21100TouchReport {
35 uint16_t length;
36 uint8_t report_id;
37 uint16_t timestamp;
38 uint8_t : 2;
39 uint8_t large_object : 1;
40 uint8_t record_num : 5;
41 uint8_t report_counter : 2;
42 uint8_t : 3;
43 uint8_t noise_effect : 3;
44 TT21100TouchRecord touch_record[MAX_TOUCH_POINTS];
45} __attribute__((packed));
46
48
50 ESP_LOGCONFIG(TAG, "Setting up TT21100 Touchscreen...");
51
52 // Register interrupt pin
53 if (this->interrupt_pin_ != nullptr) {
55 this->interrupt_pin_->setup();
57 }
58
59 // Perform reset if necessary
60 if (this->reset_pin_ != nullptr) {
61 this->reset_pin_->setup();
62 this->reset_();
63 }
64
65 // Update display dimensions if they were updated during display setup
66 if (this->display_ != nullptr) {
67 if (this->x_raw_max_ == this->x_raw_min_) {
68 this->x_raw_max_ = this->display_->get_native_width();
69 }
70 if (this->y_raw_max_ == this->y_raw_min_) {
71 this->y_raw_max_ = this->display_->get_native_height();
72 }
73 }
74
75 // Trigger initial read to activate the interrupt
76 this->store_.touched = true;
77}
78
80 // Read report length
81 uint16_t data_len;
82 this->read((uint8_t *) &data_len, sizeof(data_len));
83
84 // Read report data
85 uint8_t data[MAX_DATA_LEN];
86 if (data_len > 0 && data_len < sizeof(data)) {
87 this->read(data, data_len);
88
89 if (data_len == 14) {
90 // Button event
91 auto *report = (TT21100ButtonReport *) data;
92
93 ESP_LOGV(TAG, "Button report: Len=%d, ID=%d, Time=%5u, Value=[%u], Signal=[%04X][%04X][%04X][%04X]",
94 report->length, report->report_id, report->timestamp, report->btn_value, report->btn_signal[0],
95 report->btn_signal[1], report->btn_signal[2], report->btn_signal[3]);
96
97 for (uint8_t i = 0; i < 4; i++) {
98 for (auto *listener : this->button_listeners_)
99 listener->update_button(i, report->btn_signal[i]);
100 }
101
102 } else if (data_len >= 7) {
103 // Touch point event
104 auto *report = (TT21100TouchReport *) data;
105
106 ESP_LOGV(TAG,
107 "Touch report: Len=%d, ID=%d, Time=%5u, LargeObject=%u, RecordNum=%u, RecordCounter=%u, NoiseEffect=%u",
108 report->length, report->report_id, report->timestamp, report->large_object, report->record_num,
109 report->report_counter, report->noise_effect);
110
111 uint8_t touch_count = (data_len - (sizeof(*report) - sizeof(report->touch_record))) / sizeof(TT21100TouchRecord);
112
113 for (int i = 0; i < touch_count; i++) {
114 auto *touch = &report->touch_record[i];
115
116 ESP_LOGV(TAG,
117 "Touch %d: Type=%u, Tip=%u, EventId=%u, TouchId=%u, X=%u, Y=%u, Pressure=%u, MajorAxisLen=%u, "
118 "Orientation=%u",
119 i, touch->touch_type, touch->tip, touch->event_id, touch->touch_id, touch->x, touch->y,
120 touch->pressure, touch->major_axis_length, touch->orientation);
121
122 this->add_raw_touch_position_(touch->tip, touch->x, touch->y, touch->pressure);
123 }
124 }
125 }
126}
127
129 if (this->reset_pin_ != nullptr) {
130 this->reset_pin_->digital_write(false);
131 delay(10);
132 this->reset_pin_->digital_write(true);
133 delay(10);
134 }
135}
136
138 ESP_LOGCONFIG(TAG, "TT21100 Touchscreen:");
139 LOG_I2C_DEVICE(this);
140 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
141 LOG_PIN(" Reset Pin: ", this->reset_pin_);
142}
143
144} // namespace tt21100
145} // namespace esphome
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:221
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:223
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
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:47
std::vector< TT21100ButtonListener * > button_listeners_
Definition tt21100.h:39
InternalGPIOPin * interrupt_pin_
Definition tt21100.h:36
struct @67::@68 __attribute__
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29