ESPHome 2025.9.0-dev
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"
10
11// Registers
12// Reference: https://focuslcds.com/content/FT6236.pdf
13namespace esphome {
14namespace ft63x6 {
15static const uint8_t FT6X36_ADDR_DEVICE_MODE = 0x00;
16
17static const uint8_t FT63X6_ADDR_TD_STATUS = 0x02;
18static const uint8_t FT63X6_ADDR_TOUCH1_STATE = 0x03;
19static const uint8_t FT63X6_ADDR_TOUCH1_X = 0x03;
20static const uint8_t FT63X6_ADDR_TOUCH1_ID = 0x05;
21static const uint8_t FT63X6_ADDR_TOUCH1_Y = 0x05;
22static const uint8_t FT63X6_ADDR_TOUCH1_WEIGHT = 0x07;
23static const uint8_t FT63X6_ADDR_TOUCH1_MISC = 0x08;
24static const uint8_t FT6X36_ADDR_THRESHHOLD = 0x80;
25static const uint8_t FT6X36_ADDR_TOUCHRATE_ACTIVE = 0x88;
26static const uint8_t FT63X6_ADDR_CHIP_ID = 0xA3;
27
28static const char *const TAG = "FT63X6";
29
31 if (this->interrupt_pin_ != nullptr) {
33 this->interrupt_pin_->setup();
35 }
36
37 if (this->reset_pin_ != nullptr) {
38 this->reset_pin_->setup();
39 this->hard_reset_();
40 }
41
42 // Get touch resolution
43 if (this->x_raw_max_ == this->x_raw_min_) {
44 this->x_raw_max_ = this->display_->get_native_width();
45 }
46 if (this->y_raw_max_ == this->y_raw_min_) {
47 this->y_raw_max_ = this->display_->get_native_height();
48 }
49 uint8_t chip_id = this->read_byte_(FT63X6_ADDR_CHIP_ID);
50 if (chip_id != 0) {
51 ESP_LOGI(TAG, "FT6336U touch driver started chipid: %d", chip_id);
52 } else {
53 ESP_LOGE(TAG, "FT6336U touch driver failed to start");
54 }
55 this->write_byte(FT6X36_ADDR_DEVICE_MODE, 0x00);
56 this->write_byte(FT6X36_ADDR_THRESHHOLD, this->threshold_);
57 this->write_byte(FT6X36_ADDR_TOUCHRATE_ACTIVE, 0x0E);
58}
59
61 if (this->reset_pin_ != nullptr) {
62 this->reset_pin_->digital_write(false);
63 delay(10);
64 this->reset_pin_->digital_write(true);
65 }
66}
67
69 ESP_LOGCONFIG(TAG, "FT63X6 Touchscreen:");
70 LOG_I2C_DEVICE(this);
71 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
72 LOG_PIN(" Reset Pin: ", this->reset_pin_);
73 ESP_LOGCONFIG(TAG,
74 " X Calibration: [%d, %d]\n"
75 " Y Calibration: [%d, %d]",
76 this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
77 LOG_UPDATE_INTERVAL(this);
78}
79
81 uint16_t touch_id, x, y;
82
83 uint8_t touches = this->read_touch_number_();
84 ESP_LOGV(TAG, "Touches found: %d", touches);
85 if ((touches == 0x00) || (touches == 0xff)) {
86 // ESP_LOGD(TAG, "No touches detected");
87 return;
88 }
89
90 for (auto point = 0; point < touches; point++) {
91 if (((this->read_touch_event_(point)) & 0x01) == 0) { // checking event flag bit 6 if it is null
92 touch_id = this->read_touch_id_(point); // id1 = 0 or 1
93 x = this->read_touch_x_(point);
94 y = this->read_touch_y_(point);
95 if ((x == 0) && (y == 0)) {
96 ESP_LOGW(TAG, "Reporting a (0,0) touch on %d", touch_id);
97 }
98 this->add_raw_touch_position_(touch_id, x, y, this->read_touch_weight_(point));
99 }
100 }
101}
102
103uint8_t FT63X6Touchscreen::read_touch_number_() { return this->read_byte_(FT63X6_ADDR_TD_STATUS) & 0x0F; }
104// Touch 1 functions
105uint16_t FT63X6Touchscreen::read_touch_x_(uint8_t touch) {
106 uint8_t read_buf[2];
107 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6));
108 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + 1 + (touch * 6));
109 return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
110}
111uint16_t FT63X6Touchscreen::read_touch_y_(uint8_t touch) {
112 uint8_t read_buf[2];
113 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + (touch * 6));
114 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + 1 + (touch * 6));
115 return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
116}
118 return this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6)) >> 6;
119}
120uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t touch) {
121 return this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)) >> 4;
122}
124 return this->read_byte_(FT63X6_ADDR_TOUCH1_WEIGHT + (touch * 6));
125}
127 return this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)) >> 4;
128}
129
130uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
131 uint8_t byte = 0;
132 this->read_byte(addr, &byte);
133 return byte;
134}
135
136} // namespace ft63x6
137} // namespace esphome
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=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
uint16_t read_touch_y_(uint8_t touch)
Definition ft63x6.cpp:111
uint8_t read_touch_misc_(uint8_t touch)
Definition ft63x6.cpp:126
uint8_t read_touch_id_(uint8_t touch)
Definition ft63x6.cpp:120
uint8_t read_touch_weight_(uint8_t touch)
Definition ft63x6.cpp:123
uint8_t read_byte_(uint8_t addr)
Definition ft63x6.cpp:130
uint16_t read_touch_x_(uint8_t touch)
Definition ft63x6.cpp:105
uint8_t read_touch_event_(uint8_t touch)
Definition ft63x6.cpp:117
InternalGPIOPin * interrupt_pin_
Definition ft63x6.h:34
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
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_ANY_EDGE
Definition gpio.h:43
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t x
Definition tt21100.cpp:5
uint8_t touch_id
Definition tt21100.cpp:4
uint16_t y
Definition tt21100.cpp:6