ESPHome  2024.12.2
ektf2232.cpp
Go to the documentation of this file.
1 #include "ektf2232.h"
2 #include "esphome/core/helpers.h"
3 #include "esphome/core/log.h"
4 
5 #include <vector>
6 
7 namespace esphome {
8 namespace ektf2232 {
9 
10 static const char *const TAG = "ektf2232";
11 
12 static const uint8_t SOFT_RESET_CMD[4] = {0x77, 0x77, 0x77, 0x77};
13 static const uint8_t HELLO[4] = {0x55, 0x55, 0x55, 0x55};
14 static const uint8_t GET_X_RES[4] = {0x53, 0x60, 0x00, 0x00};
15 static const uint8_t GET_Y_RES[4] = {0x53, 0x63, 0x00, 0x00};
16 static const uint8_t GET_POWER_STATE_CMD[4] = {0x53, 0x50, 0x00, 0x01};
17 
19  ESP_LOGCONFIG(TAG, "Setting up EKT2232 Touchscreen...");
21  this->interrupt_pin_->setup();
22 
24 
25  this->rts_pin_->setup();
26 
27  this->hard_reset_();
28  if (!this->soft_reset_()) {
29  ESP_LOGE(TAG, "Failed to soft reset EKT2232!");
31  this->mark_failed();
32  return;
33  }
34 
35  // Get touch resolution
36  uint8_t received[4];
37  if (this->x_raw_max_ == this->x_raw_min_) {
38  this->write(GET_X_RES, 4);
39  if (this->read(received, 4)) {
40  ESP_LOGE(TAG, "Failed to read X resolution!");
42  this->mark_failed();
43  return;
44  }
45  this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
46  }
47 
48  if (this->y_raw_max_ == this->y_raw_min_) {
49  this->write(GET_Y_RES, 4);
50  if (this->read(received, 4)) {
51  ESP_LOGE(TAG, "Failed to read Y resolution!");
53  this->mark_failed();
54  return;
55  }
56  this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
57  }
58  this->set_power_state(true);
59 }
60 
62  uint8_t touch_count = 0;
63  int16_t x_raw, y_raw;
64 
65  uint8_t raw[8];
66  this->read(raw, 8);
67  for (int i = 0; i < 8; i++) {
68  if (raw[7] & (1 << i))
69  touch_count++;
70  }
71 
72  touch_count = std::min<uint8_t>(touch_count, 2);
73 
74  ESP_LOGV(TAG, "Touch count: %d", touch_count);
75 
76  for (int i = 0; i < touch_count; i++) {
77  uint8_t *d = raw + 1 + (i * 3);
78  x_raw = (d[0] & 0xF0) << 4 | d[1];
79  y_raw = (d[0] & 0x0F) << 8 | d[2];
80  this->add_raw_touch_position_(i, x_raw, y_raw);
81  }
82 }
83 
85  uint8_t data[] = {0x54, 0x50, 0x00, 0x01};
86  data[1] |= (enable << 3);
87  this->write(data, 4);
88 }
89 
91  uint8_t received[4];
92  this->write(GET_POWER_STATE_CMD, 4);
93  this->store_.touched = false;
94  this->read(received, 4);
95  return (received[1] >> 3) & 1;
96 }
97 
99  this->rts_pin_->digital_write(false);
100  delay(15);
101  this->rts_pin_->digital_write(true);
102  delay(15);
103 }
104 
106  auto err = this->write(SOFT_RESET_CMD, 4);
107  if (err != i2c::ERROR_OK)
108  return false;
109 
110  uint8_t received[4];
111  uint16_t timeout = 1000;
112  while (!this->store_.touched && timeout > 0) {
113  delay(1);
114  timeout--;
115  }
116  if (timeout > 0)
117  this->store_.touched = true;
118  this->read(received, 4);
119  this->store_.touched = false;
120 
121  return !memcmp(received, HELLO, 4);
122 }
123 
125  ESP_LOGCONFIG(TAG, "EKT2232 Touchscreen:");
126  LOG_I2C_DEVICE(this);
127  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
128  LOG_PIN(" RTS Pin: ", this->rts_pin_);
129 }
130 
131 } // namespace ektf2232
132 } // namespace esphome
virtual void digital_write(bool value)=0
uint8_t raw[35]
Definition: bl0939.h:19
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
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.
Definition: touchscreen.cpp:12
No error found during execution of method.
Definition: i2c_bus.h:13
TouchscreenInterrupt store_
Definition: touchscreen.h:109
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
Definition: touchscreen.cpp:74
virtual void detach_interrupt() const =0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26