ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up FT63X6 Touchscreen...");
32 if (this->interrupt_pin_ != nullptr) {
34 this->interrupt_pin_->setup();
36 }
37
38 if (this->reset_pin_ != nullptr) {
39 this->reset_pin_->setup();
40 this->hard_reset_();
41 }
42
43 // Get touch resolution
44 if (this->x_raw_max_ == this->x_raw_min_) {
45 this->x_raw_max_ = this->display_->get_native_width();
46 }
47 if (this->y_raw_max_ == this->y_raw_min_) {
48 this->y_raw_max_ = this->display_->get_native_height();
49 }
50 uint8_t chip_id = this->read_byte_(FT63X6_ADDR_CHIP_ID);
51 if (chip_id != 0) {
52 ESP_LOGI(TAG, "FT6336U touch driver started chipid: %d", chip_id);
53 } else {
54 ESP_LOGE(TAG, "FT6336U touch driver failed to start");
55 }
56 this->write_byte(FT6X36_ADDR_DEVICE_MODE, 0x00);
57 this->write_byte(FT6X36_ADDR_THRESHHOLD, this->threshold_);
58 this->write_byte(FT6X36_ADDR_TOUCHRATE_ACTIVE, 0x0E);
59}
60
62 if (this->reset_pin_ != nullptr) {
63 this->reset_pin_->digital_write(false);
64 delay(10);
65 this->reset_pin_->digital_write(true);
66 }
67}
68
70 ESP_LOGCONFIG(TAG, "FT63X6 Touchscreen:");
71 LOG_I2C_DEVICE(this);
72 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
73 LOG_PIN(" Reset Pin: ", this->reset_pin_);
74 ESP_LOGCONFIG(TAG, " X Calibration: [%d, %d]", this->x_raw_min_, this->x_raw_max_);
75 ESP_LOGCONFIG(TAG, " Y Calibration: [%d, %d]", this->y_raw_min_, this->y_raw_max_);
76 LOG_UPDATE_INTERVAL(this);
77}
78
80 uint16_t touch_id, x, y;
81
82 uint8_t touches = this->read_touch_number_();
83 ESP_LOGV(TAG, "Touches found: %d", touches);
84 if ((touches == 0x00) || (touches == 0xff)) {
85 // ESP_LOGD(TAG, "No touches detected");
86 return;
87 }
88
89 for (auto point = 0; point < touches; point++) {
90 if (((this->read_touch_event_(point)) & 0x01) == 0) { // checking event flag bit 6 if it is null
91 touch_id = this->read_touch_id_(point); // id1 = 0 or 1
92 x = this->read_touch_x_(point);
93 y = this->read_touch_y_(point);
94 if ((x == 0) && (y == 0)) {
95 ESP_LOGW(TAG, "Reporting a (0,0) touch on %d", touch_id);
96 }
97 this->add_raw_touch_position_(touch_id, x, y, this->read_touch_weight_(point));
98 }
99 }
100}
101
102uint8_t FT63X6Touchscreen::read_touch_number_() { return this->read_byte_(FT63X6_ADDR_TD_STATUS) & 0x0F; }
103// Touch 1 functions
104uint16_t FT63X6Touchscreen::read_touch_x_(uint8_t touch) {
105 uint8_t read_buf[2];
106 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6));
107 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + 1 + (touch * 6));
108 return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
109}
110uint16_t FT63X6Touchscreen::read_touch_y_(uint8_t touch) {
111 uint8_t read_buf[2];
112 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + (touch * 6));
113 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + 1 + (touch * 6));
114 return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
115}
117 return this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6)) >> 6;
118}
119uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t touch) {
120 return this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)) >> 4;
121}
123 return this->read_byte_(FT63X6_ADDR_TOUCH1_WEIGHT + (touch * 6));
124}
126 return this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)) >> 4;
127}
128
129uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
130 uint8_t byte = 0;
131 this->read_byte(addr, &byte);
132 return byte;
133}
134
135} // namespace ft63x6
136} // 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:110
uint8_t read_touch_misc_(uint8_t touch)
Definition ft63x6.cpp:125
uint8_t read_touch_id_(uint8_t touch)
Definition ft63x6.cpp:119
uint8_t read_touch_weight_(uint8_t touch)
Definition ft63x6.cpp:122
uint8_t read_byte_(uint8_t addr)
Definition ft63x6.cpp:129
uint16_t read_touch_x_(uint8_t touch)
Definition ft63x6.cpp:104
uint8_t read_touch_event_(uint8_t touch)
Definition ft63x6.cpp:116
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:28
uint16_t x
Definition tt21100.cpp:5
uint8_t touch_id
Definition tt21100.cpp:4
uint16_t y
Definition tt21100.cpp:6