ESPHome  2023.11.6
tt21100.cpp
Go to the documentation of this file.
1 #include "tt21100.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace tt21100 {
6 
7 static const char *const TAG = "tt21100";
8 
9 static const uint8_t MAX_BUTTONS = 4;
10 static const uint8_t MAX_TOUCH_POINTS = 5;
11 static const uint8_t MAX_DATA_LEN = (7 + MAX_TOUCH_POINTS * 10); // 7 Header + (Points * 10 data bytes)
12 
13 struct 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 
21 struct 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 
34 struct 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 
52  ESP_LOGCONFIG(TAG, "Setting up TT21100 Touchscreen...");
53 
54  // Register interrupt pin
55  this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
56  this->interrupt_pin_->setup();
57  this->store_.pin = this->interrupt_pin_->to_isr();
58  this->interrupt_pin_->attach_interrupt(TT21100TouchscreenStore::gpio_intr, &this->store_,
60 
61  // Perform reset if necessary
62  if (this->reset_pin_ != nullptr) {
63  this->reset_pin_->setup();
64  this->reset_();
65  }
66 
67  // Update display dimensions if they were updated during display setup
68  this->display_width_ = this->display_->get_width();
69  this->display_height_ = this->display_->get_height();
70  this->rotation_ = static_cast<TouchRotation>(this->display_->get_rotation());
71 
72  // Trigger initial read to activate the interrupt
73  this->store_.touch = true;
74 }
75 
77  if (!this->store_.touch)
78  return;
79  this->store_.touch = false;
80 
81  // Read report length
82  uint16_t data_len;
83  this->read((uint8_t *) &data_len, sizeof(data_len));
84 
85  // Read report data
86  uint8_t data[MAX_DATA_LEN];
87  if (data_len > 0 && data_len < sizeof(data)) {
88  this->read(data, data_len);
89 
90  if (data_len == 14) {
91  // Button event
92  auto *report = (TT21100ButtonReport *) data;
93 
94  ESP_LOGV(TAG, "Button report: Len=%d, ID=%d, Time=%5u, Value=[%u], Signal=[%04X][%04X][%04X][%04X]",
95  report->length, report->report_id, report->timestamp, report->btn_value, report->btn_signal[0],
96  report->btn_signal[1], report->btn_signal[2], report->btn_signal[3]);
97 
98  for (uint8_t i = 0; i < 4; i++) {
99  for (auto *listener : this->button_listeners_)
100  listener->update_button(i, report->btn_signal[i]);
101  }
102 
103  } else if (data_len >= 7) {
104  // Touch point event
105  auto *report = (TT21100TouchReport *) data;
106 
107  ESP_LOGV(TAG,
108  "Touch report: Len=%d, ID=%d, Time=%5u, LargeObject=%u, RecordNum=%u, RecordCounter=%u, NoiseEffect=%u",
109  report->length, report->report_id, report->timestamp, report->large_object, report->record_num,
110  report->report_counter, report->noise_effect);
111 
112  uint8_t touch_count = (data_len - (sizeof(*report) - sizeof(report->touch_record))) / sizeof(TT21100TouchRecord);
113 
114  if (touch_count == 0) {
115  for (auto *listener : this->touch_listeners_)
116  listener->release();
117  return;
118  }
119 
120  for (int i = 0; i < touch_count; i++) {
121  auto *touch = &report->touch_record[i];
122 
123  ESP_LOGV(TAG,
124  "Touch %d: Type=%u, Tip=%u, EventId=%u, TouchId=%u, X=%u, Y=%u, Pressure=%u, MajorAxisLen=%u, "
125  "Orientation=%u",
126  i, touch->touch_type, touch->tip, touch->event_id, touch->touch_id, touch->x, touch->y,
127  touch->pressure, touch->major_axis_length, touch->orientation);
128 
129  TouchPoint tp;
130  switch (this->rotation_) {
131  case ROTATE_0_DEGREES:
132  // Origin is top right, so mirror X by default
133  tp.x = this->display_width_ - touch->x;
134  tp.y = touch->y;
135  break;
136  case ROTATE_90_DEGREES:
137  tp.x = touch->y;
138  tp.y = touch->x;
139  break;
140  case ROTATE_180_DEGREES:
141  tp.x = touch->x;
142  tp.y = this->display_height_ - touch->y;
143  break;
144  case ROTATE_270_DEGREES:
145  tp.x = this->display_height_ - touch->y;
146  tp.y = this->display_width_ - touch->x;
147  break;
148  }
149  tp.id = touch->tip;
150  tp.state = touch->pressure;
151 
152  this->defer([this, tp]() { this->send_touch_(tp); });
153  }
154  }
155  }
156 }
157 
159  if (this->reset_pin_ != nullptr) {
160  this->reset_pin_->digital_write(false);
161  delay(10);
162  this->reset_pin_->digital_write(true);
163  delay(10);
164  }
165 }
166 
168  ESP_LOGCONFIG(TAG, "TT21100 Touchscreen:");
169  LOG_I2C_DEVICE(this);
170  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
171  LOG_PIN(" Reset Pin: ", this->reset_pin_);
172 }
173 
174 } // namespace tt21100
175 } // namespace esphome
uint8_t touch_id
Definition: tt21100.cpp:16
uint8_t tip
Definition: tt21100.cpp:14
uint8_t pressure
Definition: tt21100.cpp:19
uint16_t x
Definition: tt21100.cpp:17
uint8_t orientation
Definition: tt21100.cpp:21
struct esphome::tt21100::TT21100TouchscreenStore __attribute__
uint16_t btn_signal[MAX_BUTTONS]
Definition: tt21100.cpp:16
uint8_t btn_value
Definition: tt21100.cpp:15
uint16_t y
Definition: tt21100.cpp:18
uint16_t major_axis_length
Definition: tt21100.cpp:20
const char *const TAG
Definition: spi.cpp:8
uint8_t noise_effect
Definition: tt21100.cpp:20
uint16_t timestamp
Definition: tt21100.cpp:14
uint8_t record_num
Definition: tt21100.cpp:17
float get_setup_priority() const override
Definition: tt21100.cpp:49
uint8_t touch_type
Definition: tt21100.cpp:13
TT21100TouchRecord touch_record[MAX_TOUCH_POINTS]
Definition: tt21100.cpp:21
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
uint8_t report_counter
Definition: tt21100.cpp:18
uint8_t report_id
Definition: tt21100.cpp:13
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t event_id
Definition: tt21100.cpp:15
static void gpio_intr(TT21100TouchscreenStore *store)
Definition: tt21100.cpp:47
uint8_t large_object
Definition: tt21100.cpp:16
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26