ESPHome 2025.5.0
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 {
13namespace touchscreen {
14
15static const uint8_t STATE_RELEASED = 0x00;
16static const uint8_t STATE_PRESSED = 0x01;
17static const uint8_t STATE_UPDATED = 0x02;
18static const uint8_t STATE_RELEASING = 0x04;
19static const uint8_t STATE_CALIBRATE = 0x07;
20
21struct TouchPoint {
22 uint8_t id;
23 int16_t x_raw{0}, y_raw{0}, z_raw{0};
24 uint16_t x_prev{0}, y_prev{0};
25 uint16_t x_org{0}, y_org{0};
26 uint16_t x{0}, y{0};
27 int8_t state{0};
28};
29
30using TouchPoints_t = std::vector<TouchPoint>;
31
33 volatile bool touched{true};
34 bool init{false};
35 static void gpio_intr(TouchscreenInterrupt *store);
36};
37
39 public:
40 virtual void touch(TouchPoint tp) {}
41 virtual void update(const TouchPoints_t &tpoints) {}
42 virtual void release() {}
43};
44
46 public:
47 void set_display(display::Display *display) { this->display_ = display; }
48 display::Display *get_display() const { return this->display_; }
49
50 void set_touch_timeout(uint16_t val) { this->touch_timeout_ = val; }
51 void set_mirror_x(bool invert_x) { this->invert_x_ = invert_x; }
52 void set_mirror_y(bool invert_y) { this->invert_y_ = invert_y; }
53 void set_swap_xy(bool swap) { this->swap_x_y_ = swap; }
54
55 void set_calibration(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max) {
56 this->x_raw_min_ = x_min;
57 this->x_raw_max_ = x_max;
58 this->y_raw_min_ = y_min;
59 this->y_raw_max_ = y_max;
60 }
61
65
66 void register_listener(TouchListener *listener) { this->touch_listeners_.push_back(listener); }
67
68 optional<TouchPoint> get_touch() { return this->touches_.begin()->second; }
69
71 TouchPoints_t touches;
72 for (auto i : this->touches_) {
73 touches.push_back(i.second);
74 }
75 return touches;
76 }
77
78 void update() override;
79 void loop() override;
80 void call_setup() override;
81
82 protected:
84
86
87 void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw = 0);
88
89 virtual void update_touches() = 0;
90
91 void send_touches_();
92
93 int16_t normalize_(int16_t val, int16_t min_val, int16_t max_val, bool inverted = false);
94
96
99
100 uint16_t touch_timeout_{0};
101 bool invert_x_{false}, invert_y_{false}, swap_x_y_{false};
102
106 std::vector<TouchListener *> touch_listeners_;
107
108 std::map<uint8_t, TouchPoint> touches_;
110
111 bool first_touch_{true};
112 bool need_update_{false};
113 bool is_touched_{false};
114 bool was_touched_{false};
115 bool skip_update_{false};
116};
117
118} // namespace touchscreen
119} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:301
virtual void touch(TouchPoint tp)
Definition touchscreen.h:40
virtual void update(const TouchPoints_t &tpoints)
Definition touchscreen.h:41
std::map< uint8_t, TouchPoint > touches_
display::Display * get_display() const
Definition touchscreen.h:48
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:52
std::vector< TouchListener * > touch_listeners_
void set_mirror_x(bool invert_x)
Definition touchscreen.h:51
void set_touch_timeout(uint16_t val)
Definition touchscreen.h:50
Trigger< const TouchPoints_t & > * get_update_trigger()
Definition touchscreen.h:63
void set_display(display::Display *display)
Definition touchscreen.h:47
void register_listener(TouchListener *listener)
Definition touchscreen.h:66
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:55
Trigger< TouchPoint, const TouchPoints_t & > * get_touch_trigger()
Definition touchscreen.h:62
optional< TouchPoint > get_touch()
Definition touchscreen.h:68
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)
uint8_t type
mopeka_std_values val[4]
std::vector< TouchPoint > TouchPoints_t
Definition touchscreen.h:30
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void swap(optional< T > &x, optional< T > &y) noexcept
Definition optional.h:209
static void gpio_intr(TouchscreenInterrupt *store)