ESPHome  2022.12.8
xpt2046.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include "esphome/core/helpers.h"
8 #include "esphome/core/log.h"
9 
10 namespace esphome {
11 namespace xpt2046 {
12 
13 using namespace touchscreen;
14 
16  volatile bool touch;
17  static void gpio_intr(XPT2046TouchscreenStore *store);
18 };
19 
21  public PollingComponent,
22  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
23  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_2MHZ> {
24  public:
26  void set_dimensions(int16_t x, int16_t y) {
27  this->display_width_ = x;
28  this->display_height_ = y;
29  }
31  void set_calibration(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max);
33  void set_swap_x_y(bool val) { this->swap_x_y_ = val; }
34 
36  void set_report_interval(uint32_t interval) { this->report_millis_ = interval; }
37  uint32_t get_report_interval() { return this->report_millis_; }
38 
40  void set_threshold(int16_t threshold) { this->threshold_ = threshold; }
42  void set_irq_pin(InternalGPIOPin *pin) { this->irq_pin_ = pin; }
43 
44  void setup() override;
45  void dump_config() override;
46  float get_setup_priority() const override;
47 
54  void loop() override;
55 
63  void update() override;
64 
72  int16_t x{0}, y{0};
75  bool touched{false};
77 
83  int16_t x_raw{0}, y_raw{0}, z_raw{0};
86  protected:
87  static int16_t best_two_avg(int16_t x, int16_t y, int16_t z);
88  static int16_t normalize(int16_t val, int16_t min_val, int16_t max_val);
89 
90  int16_t read_adc_(uint8_t ctrl);
91  void check_touch_();
92 
93  int16_t threshold_;
94  int16_t x_raw_min_, x_raw_max_, y_raw_min_, y_raw_max_;
95 
96  bool invert_x_, invert_y_;
97  bool swap_x_y_;
98 
99  uint32_t report_millis_;
100  uint32_t last_pos_ms_{0};
101 
102  InternalGPIOPin *irq_pin_{nullptr};
104 };
105 
106 } // namespace xpt2046
107 } // namespace esphome
void setup()
void loop()
void set_irq_pin(InternalGPIOPin *pin)
Set the pin used to detect the touch.
Definition: xpt2046.h:42
void set_dimensions(int16_t x, int16_t y)
Set the logical touch screen dimensions.
Definition: xpt2046.h:26
void set_swap_x_y(bool val)
If true the x and y axes will be swapped.
Definition: xpt2046.h:33
This class simplifies creating components that periodically check a state.
Definition: component.h:267
void set_threshold(int16_t threshold)
Set the threshold for the touch detection.
Definition: xpt2046.h:40
Definition: a4988.cpp:4
uint32_t val
Definition: datatypes.h:85
void set_report_interval(uint32_t interval)
Set the interval to report the touch point perodically.
Definition: xpt2046.h:36
XPT2046TouchscreenStore store_
Definition: xpt2046.h:103