ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
touchscreen.h
Go to the documentation of this file.
1#pragma once
2
5
7#include "esphome/core/hal.h"
8
9#include <vector>
10#include <map>
11
12namespace esphome::touchscreen {
13
14static const uint8_t STATE_RELEASED = 0x00;
15static const uint8_t STATE_PRESSED = 0x01;
16static const uint8_t STATE_UPDATED = 0x02;
17static const uint8_t STATE_RELEASING = 0x04;
18static const uint8_t STATE_CALIBRATE = 0x07;
19
20struct TouchPoint {
21 uint8_t id;
22 int16_t x_raw{0}, y_raw{0}, z_raw{0};
23 uint16_t x_prev{0}, y_prev{0};
24 uint16_t x_org{0}, y_org{0};
25 uint16_t x{0}, y{0};
26 int8_t state{0};
27};
28
29using TouchPoints_t = std::vector<TouchPoint>;
30
32 volatile bool touched{true};
33 bool init{false};
34 static void gpio_intr(TouchscreenInterrupt *store);
35};
36
38 public:
39 virtual void touch(TouchPoint tp) {}
40 virtual void update(const TouchPoints_t &tpoints) {}
41 virtual void release() {}
42};
43
45 public:
46 void set_display(display::Display *display) { this->display_ = display; }
47 display::Display *get_display() const { return this->display_; }
48
49 void set_touch_timeout(uint16_t val) { this->touch_timeout_ = val; }
50 void set_mirror_x(bool invert_x) { this->invert_x_ = invert_x; }
51 void set_mirror_y(bool invert_y) { this->invert_y_ = invert_y; }
52 void set_swap_xy(bool swap) { this->swap_x_y_ = swap; }
53
54 void set_calibration(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max) {
55 this->x_raw_min_ = x_min;
56 this->x_raw_max_ = x_max;
57 this->y_raw_min_ = y_min;
58 this->y_raw_max_ = y_max;
59 }
60
64
65 void register_listener(TouchListener *listener) { this->touch_listeners_.push_back(listener); }
66
67 optional<TouchPoint> get_touch() {
68 if (this->touches_.empty()) {
69 return {};
70 }
71 return this->touches_.begin()->second;
72 }
73
75 TouchPoints_t touches;
76 for (auto i : this->touches_) {
77 touches.push_back(i.second);
78 }
79 return touches;
80 }
81
82 void update() override;
83 void loop() override;
84 void call_setup() override;
85
86 protected:
88
90
91 void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw = 0);
92
93 virtual void update_touches() = 0;
94
95 void send_touches_();
96
97 int16_t normalize_(int16_t val, int16_t min_val, int16_t max_val, bool inverted = false);
98
100
103
104 uint16_t touch_timeout_{0};
105 bool invert_x_{false}, invert_y_{false}, swap_x_y_{false};
106
110 std::vector<TouchListener *> touch_listeners_;
111
112 std::map<uint8_t, TouchPoint> touches_;
114
115 bool first_touch_{true};
116 bool need_update_{false};
117 bool is_touched_{false};
118 bool was_touched_{false};
119 bool skip_update_{false};
120};
121
122} // namespace esphome::touchscreen
This class simplifies creating components that periodically check a state.
Definition component.h:585
virtual void touch(TouchPoint tp)
Definition touchscreen.h:39
virtual void update(const TouchPoints_t &tpoints)
Definition touchscreen.h:40
std::map< uint8_t, TouchPoint > touches_
display::Display * get_display() const
Definition touchscreen.h:47
int16_t normalize_(int16_t val, int16_t min_val, int16_t max_val, bool inverted=false)
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.
void set_mirror_y(bool invert_y)
Definition touchscreen.h:51
std::vector< TouchListener * > touch_listeners_
void set_mirror_x(bool invert_x)
Definition touchscreen.h:50
void set_touch_timeout(uint16_t val)
Definition touchscreen.h:49
Trigger< const TouchPoints_t & > * get_update_trigger()
Definition touchscreen.h:62
void set_display(display::Display *display)
Definition touchscreen.h:46
void register_listener(TouchListener *listener)
Definition touchscreen.h:65
Trigger< const TouchPoints_t & > update_trigger_
void set_calibration(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max)
Definition touchscreen.h:54
Trigger< TouchPoint, const TouchPoints_t & > * get_touch_trigger()
Definition touchscreen.h:61
optional< TouchPoint > get_touch()
Definition touchscreen.h:67
Trigger< TouchPoint, const TouchPoints_t & > touch_trigger_
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
uint16_t type
mopeka_std_values val[3]
std::vector< TouchPoint > TouchPoints_t
Definition touchscreen.h:29
static void gpio_intr(TouchscreenInterrupt *store)