ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ft5x06_touchscreen.cpp
Go to the documentation of this file.
2
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace ft5x06 {
8
9static const char *const TAG = "ft5x06.touchscreen";
10
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}
53
55 uint8_t touch_cnt;
56 uint8_t data[MAX_TOUCHES][6];
57
58 if (!this->read_byte(FT5X06_TD_STATUS, &touch_cnt) || touch_cnt > MAX_TOUCHES) {
59 ESP_LOGW(TAG, "Failed to read status");
60 return;
61 }
62 if (touch_cnt == 0)
63 return;
64
65 if (!this->read_bytes(FT5X06_TOUCH_DATA, (uint8_t *) data, touch_cnt * 6)) {
66 ESP_LOGW(TAG, "Failed to read touch data");
67 return;
68 }
69 for (uint8_t i = 0; i != touch_cnt; i++) {
70 uint8_t status = data[i][0] >> 6;
71 uint8_t id = data[i][2] >> 3;
72 uint16_t x = encode_uint16(data[i][0] & 0x0F, data[i][1]);
73 uint16_t y = encode_uint16(data[i][2] & 0xF, data[i][3]);
74
75 ESP_LOGD(TAG, "Read %X status, id: %d, pos %d/%d", status, id, x, y);
76 if (status == 0 || status == 2) {
77 this->add_raw_touch_position_(id, x, y);
78 }
79 }
80}
81
83 ESP_LOGCONFIG(TAG,
84 "FT5x06 Touchscreen:\n"
85 " Address: 0x%02X\n"
86 " Vendor ID: 0x%X",
87 this->address_, (int) this->vendor_id_);
88}
89
91 if (err != i2c::ERROR_OK) {
92 this->mark_failed();
93 ESP_LOGE(TAG, "%s failed - err 0x%X", msg, err);
94 return false;
95 }
96 return true;
97}
99 return this->err_check_(this->write_register(FT5X06_MODE_REG, (uint8_t *) &mode, 1), "Set mode");
100}
101
102} // namespace ft5x06
103} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=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
bool err_check_(i2c::ErrorCode err, const char *msg)
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
uint8_t address_
store the address of the device on the bus
Definition i2c.h:273
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 read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
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:216
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)
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:173
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6