ESPHome  2022.6.2
xpt2046.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 namespace esphome {
9 namespace xpt2046 {
10 
11 class XPT2046OnStateTrigger : public Trigger<int, int, bool> {
12  public:
13  void process(int x, int y, bool touched);
14 };
15 
17  public:
19  void set_area(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max) {
20  this->x_min_ = x_min;
21  this->x_max_ = x_max;
22  this->y_min_ = y_min;
23  this->y_max_ = y_max;
24  }
25 
26  void touch(int16_t x, int16_t y);
27  void release();
28 
29  protected:
30  int16_t x_min_, x_max_, y_min_, y_max_;
31  bool state_{false};
32 };
33 
35  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
36  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_2MHZ> {
37  public:
39  void set_dimensions(int16_t x, int16_t y) {
40  this->x_dim_ = x;
41  this->y_dim_ = y;
42  }
44  void set_calibration(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max);
46  void set_swap_x_y(bool val) { this->swap_x_y_ = val; }
47 
49  void set_report_interval(uint32_t interval) { this->report_millis_ = interval; }
51  void set_threshold(int16_t threshold) { this->threshold_ = threshold; }
53  void set_irq_pin(GPIOPin *pin) { this->irq_pin_ = pin; }
55  XPT2046OnStateTrigger *get_on_state_trigger() const { return this->on_state_trigger_; }
57  void register_button(XPT2046Button *button) { this->buttons_.push_back(button); }
58 
59  void setup() override;
60  void dump_config() override;
61  float get_setup_priority() const override;
62 
69  void loop() override;
70 
78  void update() override;
79 
87  int16_t x{0}, y{0};
90  bool touched{false};
92 
98  int16_t x_raw{0}, y_raw{0}, z_raw{0};
101  protected:
102  static int16_t best_two_avg(int16_t x, int16_t y, int16_t z);
103  static int16_t normalize(int16_t val, int16_t min_val, int16_t max_val);
104 
105  int16_t read_adc_(uint8_t ctrl);
106 
107  int16_t threshold_;
108  int16_t x_raw_min_, x_raw_max_, y_raw_min_, y_raw_max_;
109  int16_t x_dim_, y_dim_;
110  bool invert_x_, invert_y_;
111  bool swap_x_y_;
112 
113  uint32_t report_millis_;
114  uint32_t last_pos_ms_{0};
115 
116  GPIOPin *irq_pin_{nullptr};
117  bool last_irq_{true};
118 
119  XPT2046OnStateTrigger *on_state_trigger_{new XPT2046OnStateTrigger()};
120  std::vector<XPT2046Button *> buttons_{};
121 };
122 
123 } // namespace xpt2046
124 } // namespace esphome
void setup()
void loop()
void set_area(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max)
Set the touch screen area where the button will detect the touch.
Definition: xpt2046.h:19
void set_dimensions(int16_t x, int16_t y)
Set the logical touch screen dimensions.
Definition: xpt2046.h:39
void set_swap_x_y(bool val)
If true the x and y axes will be swapped.
Definition: xpt2046.h:46
This class simplifies creating components that periodically check a state.
Definition: component.h:266
void set_threshold(int16_t threshold)
Set the threshold for the touch detection.
Definition: xpt2046.h:51
void set_irq_pin(GPIOPin *pin)
Set the pin used to detect the touch.
Definition: xpt2046.h:53
void register_button(XPT2046Button *button)
Register a virtual button to the component.
Definition: xpt2046.h:57
XPT2046OnStateTrigger * get_on_state_trigger() const
Get an access to the on_state automation trigger.
Definition: xpt2046.h:55
void process(int x, int y, bool touched)
Definition: xpt2046.cpp:196
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
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:49