11 static const char *
const TAG =
"xpt2046";
16 if (this->irq_pin_ !=
nullptr) {
21 this->irq_pin_->setup();
23 this->irq_pin_->setup();
31 if ((this->irq_pin_ !=
nullptr) && (this->store_.touch || this->touched)) {
32 this->store_.touch =
false;
38 if (this->irq_pin_ ==
nullptr)
49 int16_t touch_pressure_1 = read_adc_(0xB1 );
50 int16_t touch_pressure_2 = read_adc_(0xC1 );
52 this->z_raw = touch_pressure_1 + 0Xfff - touch_pressure_2;
54 touch = (this->z_raw >= this->threshold_);
57 data[0] = read_adc_(0x91 );
58 data[1] = read_adc_(0xD1 );
59 data[2] = read_adc_(0x91 );
60 data[3] = read_adc_(0xD1 );
61 data[4] = read_adc_(0x91 );
64 data[5] = read_adc_(0xD0 );
69 this->x_raw = best_two_avg(data[1], data[3], data[5]);
70 this->y_raw = best_two_avg(data[0], data[2], data[4]);
72 ESP_LOGVV(TAG,
"Update [x, y] = [%d, %d], z = %d", this->x_raw, this->y_raw, this->z_raw);
76 touchpoint.
x = normalize(this->x_raw, this->x_raw_min_, this->x_raw_max_);
77 touchpoint.
y = normalize(this->y_raw, this->y_raw_min_, this->y_raw_max_);
79 if (this->swap_x_y_) {
83 if (this->invert_x_) {
84 touchpoint.
x = 0xfff - touchpoint.
x;
87 if (this->invert_y_) {
88 touchpoint.
y = 0xfff - touchpoint.
y;
91 switch (static_cast<TouchRotation>(this->display_->get_rotation())) {
96 touchpoint.
y = 0xfff - touchpoint.
y;
99 touchpoint.
x = 0xfff - touchpoint.
x;
100 touchpoint.
y = 0xfff - touchpoint.
y;
104 touchpoint.
x = 0xfff - touchpoint.
x;
108 touchpoint.
x = (int16_t) ((
int) touchpoint.
x * this->display_->get_width() / 0xfff);
109 touchpoint.
y = (int16_t) ((
int) touchpoint.
y * this->display_->get_height() / 0xfff);
111 if (!this->touched || (now - this->last_pos_ms_) >= this->report_millis_) {
112 ESP_LOGV(TAG,
"Touching at [%03X, %03X] => [%3d, %3d]", this->x_raw, this->y_raw, touchpoint.
x, touchpoint.
y);
114 this->defer([
this, touchpoint]() { this->send_touch_(touchpoint); });
116 this->
x = touchpoint.
x;
117 this->
y = touchpoint.
y;
118 this->touched =
true;
119 this->last_pos_ms_ = now;
123 if (!touch && this->touched) {
124 this->x_raw = this->y_raw = this->z_raw = 0;
125 ESP_LOGV(TAG,
"Released [%d, %d]", this->
x, this->
y);
126 this->touched =
false;
127 for (
auto *listener : this->touch_listeners_)
133 this->x_raw_min_ = std::min(x_min, x_max);
134 this->x_raw_max_ = std::max(x_min, x_max);
135 this->y_raw_min_ = std::min(y_min, y_max);
136 this->y_raw_max_ = std::max(y_min, y_max);
137 this->invert_x_ = (x_min > x_max);
138 this->invert_y_ = (y_min > y_max);
142 ESP_LOGCONFIG(TAG,
"XPT2046:");
144 LOG_PIN(
" IRQ Pin: ", this->irq_pin_);
145 ESP_LOGCONFIG(TAG,
" X min: %d", this->x_raw_min_);
146 ESP_LOGCONFIG(TAG,
" X max: %d", this->x_raw_max_);
147 ESP_LOGCONFIG(TAG,
" Y min: %d", this->y_raw_min_);
148 ESP_LOGCONFIG(TAG,
" Y max: %d", this->y_raw_max_);
150 ESP_LOGCONFIG(TAG,
" Swap X/Y: %s", YESNO(this->swap_x_y_));
151 ESP_LOGCONFIG(TAG,
" Invert X: %s", YESNO(this->invert_x_));
152 ESP_LOGCONFIG(TAG,
" Invert Y: %s", YESNO(this->invert_y_));
154 ESP_LOGCONFIG(TAG,
" threshold: %d", this->threshold_);
155 ESP_LOGCONFIG(TAG,
" Report interval: %" PRIu32, this->report_millis_);
157 LOG_UPDATE_INTERVAL(
this);
166 da = (x >
y) ? x - y : y - x;
167 db = (x > z) ? x - z : z - x;
168 dc = (z >
y) ? z - y : y - z;
170 if (da <= db && da <= dc) {
172 }
else if (db <= da && db <= dc) {
184 if (val <= min_val) {
186 }
else if (val >= max_val) {
189 ret = (int16_t) ((
int) 0xfff * (val - min_val) / (max_val - min_val));
200 data[0] = read_byte();
201 data[1] = read_byte();
203 return ((data[0] << 8) | data[1]) >> 3;
const float DATA
For components that import data from directly connected sensors like DHT.
int16_t read_adc_(uint8_t ctrl)
void dump_config() override
static int16_t normalize(int16_t val, int16_t min_val, int16_t max_val)
void set_calibration(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max)
Set the coordinates for the touch screen edges.
uint32_t IRAM_ATTR HOT millis()
float get_setup_priority() const override
void swap(optional< T > &x, optional< T > &y)
static int16_t best_two_avg(int16_t x, int16_t y, int16_t z)
void update() override
Read and process the values from the hardware.
Implementation of SPI Controller mode.
static void gpio_intr(XPT2046TouchscreenStore *store)
void loop() override
Detect the touch if the irq pin is specified.
void IRAM_ATTR HOT delay(uint32_t ms)