ESPHome  2024.10.3
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
13 namespace esphome {
14 namespace ft63x6 {
15 static const uint8_t FT6X36_ADDR_DEVICE_MODE = 0x00;
16 
17 static const uint8_t FT63X6_ADDR_TD_STATUS = 0x02;
18 static const uint8_t FT63X6_ADDR_TOUCH1_STATE = 0x03;
19 static const uint8_t FT63X6_ADDR_TOUCH1_X = 0x03;
20 static const uint8_t FT63X6_ADDR_TOUCH1_ID = 0x05;
21 static const uint8_t FT63X6_ADDR_TOUCH1_Y = 0x05;
22 static const uint8_t FT63X6_ADDR_TOUCH1_WEIGHT = 0x07;
23 static const uint8_t FT63X6_ADDR_TOUCH1_MISC = 0x08;
24 static const uint8_t FT6X36_ADDR_THRESHHOLD = 0x80;
25 static const uint8_t FT6X36_ADDR_TOUCHRATE_ACTIVE = 0x88;
26 static const uint8_t FT63X6_ADDR_CHIP_ID = 0xA3;
27 
28 static 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_ = 320;
46  }
47  if (this->y_raw_max_ == this->y_raw_min_) {
48  this->y_raw_max_ = 480;
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  LOG_UPDATE_INTERVAL(this);
75 }
76 
78  uint16_t touch_id, x, y;
79 
80  uint8_t touches = this->read_touch_number_();
81  ESP_LOGV(TAG, "Touches found: %d", touches);
82  if ((touches == 0x00) || (touches == 0xff)) {
83  // ESP_LOGD(TAG, "No touches detected");
84  return;
85  }
86 
87  for (auto point = 0; point < touches; point++) {
88  if (((this->read_touch_event_(point)) & 0x01) == 0) { // checking event flag bit 6 if it is null
89  touch_id = this->read_touch_id_(point); // id1 = 0 or 1
90  x = this->read_touch_x_(point);
91  y = this->read_touch_y_(point);
92  if ((x == 0) && (y == 0)) {
93  ESP_LOGW(TAG, "Reporting a (0,0) touch on %d", touch_id);
94  }
95  this->add_raw_touch_position_(touch_id, x, y, this->read_touch_weight_(point));
96  }
97  }
98 }
99 
100 uint8_t FT63X6Touchscreen::read_touch_number_() { return this->read_byte_(FT63X6_ADDR_TD_STATUS) & 0x0F; }
101 // Touch 1 functions
102 uint16_t FT63X6Touchscreen::read_touch_x_(uint8_t touch) {
103  uint8_t read_buf[2];
104  read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6));
105  read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + 1 + (touch * 6));
106  return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
107 }
108 uint16_t FT63X6Touchscreen::read_touch_y_(uint8_t touch) {
109  uint8_t read_buf[2];
110  read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + (touch * 6));
111  read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + 1 + (touch * 6));
112  return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
113 }
114 uint8_t FT63X6Touchscreen::read_touch_event_(uint8_t touch) {
115  return this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6)) >> 6;
116 }
117 uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t touch) {
118  return this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)) >> 4;
119 }
121  return this->read_byte_(FT63X6_ADDR_TOUCH1_WEIGHT + (touch * 6));
122 }
123 uint8_t FT63X6Touchscreen::read_touch_misc_(uint8_t touch) {
124  return this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)) >> 4;
125 }
126 
127 uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
128  uint8_t byte = 0;
129  this->read_byte(addr, &byte);
130  return byte;
131 }
132 
133 } // namespace ft63x6
134 } // namespace esphome
virtual void digital_write(bool value)=0
uint8_t touch_id
Definition: tt21100.cpp:16
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
InternalGPIOPin * interrupt_pin_
Definition: ft63x6.h:34
uint16_t x
Definition: tt21100.cpp:17
uint8_t read_touch_weight_(uint8_t touch)
Definition: ft63x6.cpp:120
virtual void pin_mode(gpio::Flags flags)=0
void update_touches() override
Definition: ft63x6.cpp:77
virtual void setup()=0
uint16_t y
Definition: tt21100.cpp:18
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.
Definition: touchscreen.cpp:12
uint16_t read_touch_x_(uint8_t touch)
Definition: ft63x6.cpp:102
uint8_t read_byte_(uint8_t addr)
Definition: ft63x6.cpp:127
uint8_t read_touch_misc_(uint8_t touch)
Definition: ft63x6.cpp:123
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
Definition: touchscreen.cpp:74
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t read_touch_id_(uint8_t touch)
Definition: ft63x6.cpp:117
uint16_t read_touch_y_(uint8_t touch)
Definition: ft63x6.cpp:108
uint8_t read_touch_event_(uint8_t touch)
Definition: ft63x6.cpp:114
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26