Files
esphome-dev/esphome/components/xpt2046/touchscreen/xpt2046.cpp
T
NP v/d Spek 5393a09872 Touchscreen component and driver fixes (#5997)
* Introduce calibration settings for all touchscreen drivers.
this will override the common values.
The x,y coordinates only calculated when the right calibrations are set.

* resolve issues reported by CI

* remove unneeded spaces and newlines

* Forgot to remove some obsolete code

* remove get_setup_priority from xpt2046

* remove media_player changes.

* media_player: removed to much,

* Update suggestions

* referd back the `get_setup_priority` removal so it can be moved into a othe PR.

* tt21100: restore init read

* fix spacing

* load native display dimensions instead of using internal dimensons.
and load it only onse on setup

* moved `update_touches()` to protexted section

* adding Clydes PR#6049

* add multitouch test script

* Update all Touchscreen replacing `get_*_internal` to `get_native_*`

* fixed some CI recomendations

* couple of fixes

* make sure the display is running before  touchscreen is setup

* fix clang

* revert back last changes

* xpt2046: change log level for testing

* logging information

* add test file

* fix polling issue with the for example the xpt2046

* fixed some CI issues

* fixed some CI issues

* restore mirror parameter discriptions

* same for the swap_xy

* same for the transform

* remove the above const from const.py

* and put  the above const bacl const.py

* Merge branch 'nvds-touchscreen-fix1' of https://github.com/nielsnl68/esphome into nvds-touchscreen-fix1

* and put  the above const bacl const.py

* [tt21100] making interupt pin optional

* [tt21100] making interupt pin optional (now complete)

* update the display part based on @clyde'
s changes.

* fix issue with ft6x36 touvhscreen

* reverd back touch check. add comment

* add some extra checks to the ft6x36

* add an other log and a typo fixed

* okay an other fix.

* add an extra check like others do
and fix data type

* [ft6336] fix update race when ts is touched.

* [touchscreen] update some log's with a verbose level.

* fix clang issues

* fix the clang issues

* fix the clang issues

* fix virtual issue.

* fix the clang issues

* an other clang issues

* remove anti-aliased fonts support. It does not belong here.

* remove anti-aliased fonts support. It does not belong here.

* rename test script

* Moving the test files to there right location.

* rename the test files

* clean up the code

* add a new line

* clang fixings

* clang fixings

* remove comment

* remove comment

* Update esphome/components/touchscreen/__init__.py

Co-authored-by: guillempages <guillempages@users.noreply.github.com>

* Update esphome/components/touchscreen/__init__.py

Co-authored-by: guillempages <guillempages@users.noreply.github.com>

* Update esphome/components/touchscreen/__init__.py

Co-authored-by: guillempages <guillempages@users.noreply.github.com>

* Update esphome/components/touchscreen/touchscreen.cpp

* Update esphome/components/touchscreen/touchscreen.cpp

* [ft63x6] add threshold

---------

Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com>
Co-authored-by: guillempages <guillempages@users.noreply.github.com>
2024-02-28 02:42:11 +00:00

113 lines
3.4 KiB
C++

#include "xpt2046.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include <algorithm>
namespace esphome {
namespace xpt2046 {
static const char *const TAG = "xpt2046";
void XPT2046Component::setup() {
if (this->irq_pin_ != nullptr) {
// The pin reports a touch with a falling edge. Unfortunately the pin goes also changes state
// while the channels are read and wiring it as an interrupt is not straightforward and would
// need careful masking. A GPIO poll is cheap so we'll just use that.
this->irq_pin_->setup(); // INPUT
this->irq_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->irq_pin_->setup();
this->attach_interrupt_(this->irq_pin_, gpio::INTERRUPT_FALLING_EDGE);
}
this->spi_setup();
this->read_adc_(0xD0); // ADC powerdown, enable PENIRQ pin
}
void XPT2046Component::update_touches() {
int16_t data[6], x_raw, y_raw, z_raw;
bool touch = false;
enable();
int16_t touch_pressure_1 = this->read_adc_(0xB1 /* touch_pressure_1 */);
int16_t touch_pressure_2 = this->read_adc_(0xC1 /* touch_pressure_2 */);
z_raw = touch_pressure_1 + 0Xfff - touch_pressure_2;
ESP_LOGVV(TAG, "Touchscreen Update z = %d", z_raw);
touch = (z_raw >= this->threshold_);
if (touch) {
read_adc_(0xD1 /* X */); // dummy Y measure, 1st is always noisy
data[0] = this->read_adc_(0x91 /* Y */);
data[1] = this->read_adc_(0xD1 /* X */); // make 3 x-y measurements
data[2] = this->read_adc_(0x91 /* Y */);
data[3] = this->read_adc_(0xD1 /* X */);
data[4] = this->read_adc_(0x91 /* Y */);
}
data[5] = this->read_adc_(0xD0 /* X */); // Last X touch power down
disable();
if (touch) {
x_raw = best_two_avg(data[1], data[3], data[5]);
y_raw = best_two_avg(data[0], data[2], data[4]);
ESP_LOGD(TAG, "Touchscreen Update [%d, %d], z = %d", x_raw, y_raw, z_raw);
this->add_raw_touch_position_(0, x_raw, y_raw, z_raw);
}
}
void XPT2046Component::dump_config() {
ESP_LOGCONFIG(TAG, "XPT2046:");
LOG_PIN(" IRQ Pin: ", this->irq_pin_);
ESP_LOGCONFIG(TAG, " X min: %d", this->x_raw_min_);
ESP_LOGCONFIG(TAG, " X max: %d", this->x_raw_max_);
ESP_LOGCONFIG(TAG, " Y min: %d", this->y_raw_min_);
ESP_LOGCONFIG(TAG, " Y max: %d", this->y_raw_max_);
ESP_LOGCONFIG(TAG, " Swap X/Y: %s", YESNO(this->swap_x_y_));
ESP_LOGCONFIG(TAG, " Invert X: %s", YESNO(this->invert_x_));
ESP_LOGCONFIG(TAG, " Invert Y: %s", YESNO(this->invert_y_));
ESP_LOGCONFIG(TAG, " threshold: %d", this->threshold_);
LOG_UPDATE_INTERVAL(this);
}
// float XPT2046Component::get_setup_priority() const { return setup_priority::DATA; }
int16_t XPT2046Component::best_two_avg(int16_t value1, int16_t value2, int16_t value3) {
int16_t delta_a, delta_b, delta_c;
int16_t reta = 0;
delta_a = (value1 > value2) ? value1 - value2 : value2 - value1;
delta_b = (value1 > value3) ? value1 - value3 : value3 - value1;
delta_c = (value3 > value2) ? value3 - value2 : value2 - value3;
if (delta_a <= delta_b && delta_a <= delta_c) {
reta = (value1 + value2) >> 1;
} else if (delta_b <= delta_a && delta_b <= delta_c) {
reta = (value1 + value3) >> 1;
} else {
reta = (value2 + value3) >> 1;
}
return reta;
}
int16_t XPT2046Component::read_adc_(uint8_t ctrl) { // NOLINT
uint8_t data[2];
this->write_byte(ctrl);
delay(1);
data[0] = this->read_byte();
data[1] = this->read_byte();
return ((data[0] << 8) | data[1]) >> 3;
}
} // namespace xpt2046
} // namespace esphome