ESPHome 2025.6.3
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, "Running setup");
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,
75 " X Calibration: [%d, %d]\n"
76 " Y Calibration: [%d, %d]",
77 this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
78 LOG_UPDATE_INTERVAL(this);
79}
80
82 uint16_t touch_id, x, y;
83
84 uint8_t touches = this->read_touch_number_();
85 ESP_LOGV(TAG, "Touches found: %d", touches);
86 if ((touches == 0x00) || (touches == 0xff)) {
87 // ESP_LOGD(TAG, "No touches detected");
88 return;
89 }
90
91 for (auto point = 0; point < touches; point++) {
92 if (((this->read_touch_event_(point)) & 0x01) == 0) { // checking event flag bit 6 if it is null
93 touch_id = this->read_touch_id_(point); // id1 = 0 or 1
94 x = this->read_touch_x_(point);
95 y = this->read_touch_y_(point);
96 if ((x == 0) && (y == 0)) {
97 ESP_LOGW(TAG, "Reporting a (0,0) touch on %d", touch_id);
98 }
99 this->add_raw_touch_position_(touch_id, x, y, this->read_touch_weight_(point));
100 }
101 }
102}
103
104uint8_t FT63X6Touchscreen::read_touch_number_() { return this->read_byte_(FT63X6_ADDR_TD_STATUS) & 0x0F; }
105// Touch 1 functions
106uint16_t FT63X6Touchscreen::read_touch_x_(uint8_t touch) {
107 uint8_t read_buf[2];
108 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6));
109 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + 1 + (touch * 6));
110 return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
111}
112uint16_t FT63X6Touchscreen::read_touch_y_(uint8_t touch) {
113 uint8_t read_buf[2];
114 read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + (touch * 6));
115 read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + 1 + (touch * 6));
116 return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
117}
119 return this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6)) >> 6;
120}
121uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t touch) {
122 return this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)) >> 4;
123}
125 return this->read_byte_(FT63X6_ADDR_TOUCH1_WEIGHT + (touch * 6));
126}
128 return this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)) >> 4;
129}
130
131uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
132 uint8_t byte = 0;
133 this->read_byte(addr, &byte);
134 return byte;
135}
136
137} // namespace ft63x6
138} // 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:223
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:225
uint16_t read_touch_y_(uint8_t touch)
Definition ft63x6.cpp:112
uint8_t read_touch_misc_(uint8_t touch)
Definition ft63x6.cpp:127
uint8_t read_touch_id_(uint8_t touch)
Definition ft63x6.cpp:121
uint8_t read_touch_weight_(uint8_t touch)
Definition ft63x6.cpp:124
uint8_t read_byte_(uint8_t addr)
Definition ft63x6.cpp:131
uint16_t read_touch_x_(uint8_t touch)
Definition ft63x6.cpp:106
uint8_t read_touch_event_(uint8_t touch)
Definition ft63x6.cpp:118
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