ESPHome  2024.12.2
ft5x06_touchscreen.cpp
Go to the documentation of this file.
1 #include "ft5x06_touchscreen.h"
2 
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace ft5x06 {
7 
8 static const char *const TAG = "ft5x06.touchscreen";
9 
11  ESP_LOGCONFIG(TAG, "Setting up FT5x06 Touchscreen...");
12  if (this->interrupt_pin_ != nullptr) {
13  this->interrupt_pin_->setup();
15  this->interrupt_pin_->setup();
17  }
18 
19  // wait 200ms after reset.
20  this->set_timeout(200, [this] { this->continue_setup_(); });
21 }
22 
24  uint8_t data[4];
25  if (!this->set_mode_(FT5X06_OP_MODE))
26  return;
27 
28  if (!this->err_check_(this->read_register(FT5X06_VENDOR_ID_REG, data, 1), "Read Vendor ID"))
29  return;
30  switch (data[0]) {
31  case FT5X06_ID_1:
32  case FT5X06_ID_2:
33  case FT5X06_ID_3:
34  this->vendor_id_ = (VendorId) data[0];
35  ESP_LOGD(TAG, "Read vendor ID 0x%X", data[0]);
36  break;
37 
38  default:
39  ESP_LOGE(TAG, "Unknown vendor ID 0x%X", data[0]);
40  this->mark_failed();
41  return;
42  }
43  // reading the chip registers to get max x/y does not seem to work.
44  if (this->display_ != nullptr) {
45  if (this->x_raw_max_ == this->x_raw_min_) {
46  this->x_raw_max_ = this->display_->get_native_width();
47  }
48  if (this->y_raw_max_ == this->y_raw_min_) {
49  this->y_raw_max_ = this->display_->get_native_height();
50  }
51  }
52  ESP_LOGCONFIG(TAG, "FT5x06 Touchscreen setup complete");
53 }
54 
56  uint8_t touch_cnt;
57  uint8_t data[MAX_TOUCHES][6];
58 
59  if (!this->read_byte(FT5X06_TD_STATUS, &touch_cnt) || touch_cnt > MAX_TOUCHES) {
60  ESP_LOGW(TAG, "Failed to read status");
61  return;
62  }
63  if (touch_cnt == 0)
64  return;
65 
66  if (!this->read_bytes(FT5X06_TOUCH_DATA, (uint8_t *) data, touch_cnt * 6)) {
67  ESP_LOGW(TAG, "Failed to read touch data");
68  return;
69  }
70  for (uint8_t i = 0; i != touch_cnt; i++) {
71  uint8_t status = data[i][0] >> 6;
72  uint8_t id = data[i][2] >> 3;
73  uint16_t x = encode_uint16(data[i][0] & 0x0F, data[i][1]);
74  uint16_t y = encode_uint16(data[i][2] & 0xF, data[i][3]);
75 
76  ESP_LOGD(TAG, "Read %X status, id: %d, pos %d/%d", status, id, x, y);
77  if (status == 0 || status == 2) {
78  this->add_raw_touch_position_(id, x, y);
79  }
80  }
81 }
82 
84  ESP_LOGCONFIG(TAG, "FT5x06 Touchscreen:");
85  ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
86  ESP_LOGCONFIG(TAG, " Vendor ID: 0x%X", (int) this->vendor_id_);
87 }
88 
89 bool FT5x06Touchscreen::err_check_(i2c::ErrorCode err, const char *msg) {
90  if (err != i2c::ERROR_OK) {
91  this->mark_failed();
92  ESP_LOGE(TAG, "%s failed - err 0x%X", msg, err);
93  return false;
94  }
95  return true;
96 }
98  return this->err_check_(this->write_register(FT5X06_MODE_REG, (uint8_t *) &mode, 1), "Set mode");
99 }
100 
101 } // namespace ft5x06
102 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:10
bool err_check_(i2c::ErrorCode err, const char *msg)
uint16_t x
Definition: tt21100.cpp:17
int get_native_height()
Get the native (original) height of the display in pixels.
Definition: display.h:223
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition: i2c.h:212
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
uint16_t y
Definition: tt21100.cpp:18
int get_native_width()
Get the native (original) width of the display in pixels.
Definition: display.h:221
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
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition: helpers.h:183
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
uint8_t status
Definition: bl0942.h:74
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
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11