ESPHome 2025.6.3
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 ESP_LOGCONFIG(TAG, "Running setup");
13 if (this->interrupt_pin_ != nullptr) {
14 this->interrupt_pin_->setup();
16 this->interrupt_pin_->setup();
18 }
19
20 // wait 200ms after reset.
21 this->set_timeout(200, [this] { this->continue_setup_(); });
22}
23
25 uint8_t data[4];
26 if (!this->set_mode_(FT5X06_OP_MODE))
27 return;
28
29 if (!this->err_check_(this->read_register(FT5X06_VENDOR_ID_REG, data, 1), "Read Vendor ID"))
30 return;
31 switch (data[0]) {
32 case FT5X06_ID_1:
33 case FT5X06_ID_2:
34 case FT5X06_ID_3:
35 this->vendor_id_ = (VendorId) data[0];
36 ESP_LOGD(TAG, "Read vendor ID 0x%X", data[0]);
37 break;
38
39 default:
40 ESP_LOGE(TAG, "Unknown vendor ID 0x%X", data[0]);
41 this->mark_failed();
42 return;
43 }
44 // reading the chip registers to get max x/y does not seem to work.
45 if (this->display_ != nullptr) {
46 if (this->x_raw_max_ == this->x_raw_min_) {
47 this->x_raw_max_ = this->display_->get_native_width();
48 }
49 if (this->y_raw_max_ == this->y_raw_min_) {
50 this->y_raw_max_ = this->display_->get_native_height();
51 }
52 }
53 ESP_LOGCONFIG(TAG, "FT5x06 Touchscreen setup complete");
54}
55
57 uint8_t touch_cnt;
58 uint8_t data[MAX_TOUCHES][6];
59
60 if (!this->read_byte(FT5X06_TD_STATUS, &touch_cnt) || touch_cnt > MAX_TOUCHES) {
61 ESP_LOGW(TAG, "Failed to read status");
62 return;
63 }
64 if (touch_cnt == 0)
65 return;
66
67 if (!this->read_bytes(FT5X06_TOUCH_DATA, (uint8_t *) data, touch_cnt * 6)) {
68 ESP_LOGW(TAG, "Failed to read touch data");
69 return;
70 }
71 for (uint8_t i = 0; i != touch_cnt; i++) {
72 uint8_t status = data[i][0] >> 6;
73 uint8_t id = data[i][2] >> 3;
74 uint16_t x = encode_uint16(data[i][0] & 0x0F, data[i][1]);
75 uint16_t y = encode_uint16(data[i][2] & 0xF, data[i][3]);
76
77 ESP_LOGD(TAG, "Read %X status, id: %d, pos %d/%d", status, id, x, y);
78 if (status == 0 || status == 2) {
79 this->add_raw_touch_position_(id, x, y);
80 }
81 }
82}
83
85 ESP_LOGCONFIG(TAG,
86 "FT5x06 Touchscreen:\n"
87 " Address: 0x%02X\n"
88 " Vendor ID: 0x%X",
89 this->address_, (int) this->vendor_id_);
90}
91
93 if (err != i2c::ERROR_OK) {
94 this->mark_failed();
95 ESP_LOGE(TAG, "%s failed - err 0x%X", msg, err);
96 return false;
97 }
98 return true;
99}
101 return this->err_check_(this->write_register(FT5X06_MODE_REG, (uint8_t *) &mode, 1), "Set mode");
102}
103
104} // namespace ft5x06
105} // 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.
Definition component.cpp:75
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:223
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:225
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:192
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6