ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ft63x6.cpp
Go to the documentation of this file.
1/**************************************************************************/
6/**************************************************************************/
7
8#include "ft63x6.h"
9#include "esphome/core/log.h"
11
12// Registers
13// Reference: https://focuslcds.com/content/FT6236.pdf
14
15namespace esphome::ft63x6 {
16static const uint8_t FT6X36_ADDR_DEVICE_MODE = 0x00;
17
18static const uint8_t FT63X6_ADDR_TD_STATUS = 0x02;
19static const uint8_t FT63X6_ADDR_TOUCH1_STATE = 0x03;
20static const uint8_t FT63X6_ADDR_TOUCH1_X = 0x03;
21static const uint8_t FT63X6_ADDR_TOUCH1_ID = 0x05;
22static const uint8_t FT63X6_ADDR_TOUCH1_Y = 0x05;
23static const uint8_t FT63X6_ADDR_TOUCH1_WEIGHT = 0x07;
24static const uint8_t FT63X6_ADDR_TOUCH1_MISC = 0x08;
25static const uint8_t FT6X36_ADDR_THRESHHOLD = 0x80;
26static const uint8_t FT6X36_ADDR_TOUCHRATE_ACTIVE = 0x88;
27static const uint8_t FT63X6_ADDR_CHIP_ID = 0xA3;
28
29ESPHOME_LOG_TAG(TAG, "FT63X6");
30
32 if (this->interrupt_pin_ != nullptr) {
34 this->interrupt_pin_->setup();
36 }
37
38 pulse_reset(this->reset_pin_, {.low_ms = 10});
39
40 // Get touch resolution
41 if (this->x_raw_max_ == this->x_raw_min_) {
42 this->x_raw_max_ = this->display_->get_native_width();
43 }
44 if (this->y_raw_max_ == this->y_raw_min_) {
45 this->y_raw_max_ = this->display_->get_native_height();
46 }
47 uint8_t chip_id = this->read_byte_(FT63X6_ADDR_CHIP_ID);
48 if (chip_id != 0) {
49 ESP_LOGI(TAG, "FT6336U touch driver started chipid: %d", chip_id);
50 } else {
51 ESP_LOGE(TAG, "FT6336U touch driver failed to start");
52 }
53 this->write_byte(FT6X36_ADDR_DEVICE_MODE, 0x00);
54 this->write_byte(FT6X36_ADDR_THRESHHOLD, this->threshold_);
55 this->write_byte(FT6X36_ADDR_TOUCHRATE_ACTIVE, 0x0E);
56}
57
59 ESP_LOGCONFIG(TAG, "FT63X6 Touchscreen:");
60 LOG_I2C_DEVICE(this);
61 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
62 LOG_PIN(" Reset Pin: ", this->reset_pin_);
63 ESP_LOGCONFIG(TAG,
64 " X Calibration: [%d, %d]\n"
65 " Y Calibration: [%d, %d]",
66 this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
67 LOG_UPDATE_INTERVAL(this);
68}
69
71 uint16_t touch_id, x, y;
72
73 uint8_t touches = this->read_touch_number_();
74 ESP_LOGV(TAG, "Touches found: %d", touches);
75 if ((touches == 0x00) || (touches == 0xff)) {
76 // ESP_LOGD(TAG, "No touches detected");
77 return;
78 }
79
80 for (auto point = 0; point < touches; point++) {
81 if (((this->read_touch_event_(point)) & 0x01) == 0) { // checking event flag bit 6 if it is null
82 touch_id = this->read_touch_id_(point); // id1 = 0 or 1
83 x = this->read_touch_x_(point);
84 y = this->read_touch_y_(point);
85 if ((x == 0) && (y == 0)) {
86 ESP_LOGW(TAG, "Reporting a (0,0) touch on %d", touch_id);
87 }
88 this->add_raw_touch_position_(touch_id, x, y, this->read_touch_weight_(point));
89 }
90 }
91}
92
93uint8_t FT63X6Touchscreen::read_touch_number_() { return this->read_byte_(FT63X6_ADDR_TD_STATUS) & 0x0F; }
94// Touch 1 functions
95uint16_t FT63X6Touchscreen::read_touch_x_(uint8_t touch) {
96 uint8_t read_buf[2];
97 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6));
98 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + 1 + (touch * 6));
99 return encode_uint16(read_buf[0] & 0x0f, read_buf[1]);
100}
101uint16_t FT63X6Touchscreen::read_touch_y_(uint8_t touch) {
102 uint8_t read_buf[2];
103 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + (touch * 6));
104 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + 1 + (touch * 6));
105 return encode_uint16(read_buf[0] & 0x0f, read_buf[1]);
106}
108 return this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6)) >> 6;
109}
110uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t touch) {
111 return this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)) >> 4;
112}
114 return this->read_byte_(FT63X6_ADDR_TOUCH1_WEIGHT + (touch * 6));
115}
117 return this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)) >> 4;
118}
119
120uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
121 uint8_t byte = 0;
122 this->read_byte(addr, &byte);
123 return byte;
124}
125
126} // namespace esphome::ft63x6
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:341
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:343
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:195
uint16_t read_touch_y_(uint8_t touch)
Definition ft63x6.cpp:101
uint8_t read_touch_misc_(uint8_t touch)
Definition ft63x6.cpp:116
uint8_t read_touch_id_(uint8_t touch)
Definition ft63x6.cpp:110
uint8_t read_touch_weight_(uint8_t touch)
Definition ft63x6.cpp:113
uint8_t read_byte_(uint8_t addr)
Definition ft63x6.cpp:120
uint16_t read_touch_x_(uint8_t touch)
Definition ft63x6.cpp:95
uint8_t read_touch_event_(uint8_t touch)
Definition ft63x6.cpp:107
InternalGPIOPin * interrupt_pin_
Definition ft63x6.h:32
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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)
ESPHOME_LOG_TAG(TAG, "FT63X6")
@ INTERRUPT_ANY_EDGE
Definition gpio_pin.h:54
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:1043
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:163
uint16_t x
Definition tt21100.cpp:5
uint8_t touch_id
Definition tt21100.cpp:4
uint16_t y
Definition tt21100.cpp:6