M5Unified
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Touch_Class.hpp
Go to the documentation of this file.
1 // Copyright (c) M5Stack. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #ifndef __M5_Touch_Class_H__
5 #define __M5_Touch_Class_H__
6 
7 #include <M5GFX.h>
8 
9 #include <cstdint>
10 
11 namespace m5
12 {
14  { none = 0b0000
15  , touch = 0b0001
16  , touch_end = 0b0010
17  , touch_begin = 0b0011
18 
19  , hold = 0b0101
20  , hold_end = 0b0110
21  , hold_begin = 0b0111
22 
23  , flick = 0b1001
24  , flick_end = 0b1010
25  , flick_begin = 0b1011
26 
27  , drag = 0b1101
28  , drag_end = 0b1110
29  , drag_begin = 0b1111
30 
31  , mask_touch = 0b0001
32  , mask_change = 0b0010
33  , mask_holding = 0b0100
34  , mask_moving = 0b1000
35  };
36 
38  {
39  public:
40  static constexpr std::size_t TOUCH_MAX_POINTS = 3;
41  static constexpr std::size_t TOUCH_MIN_UPDATE_MSEC = 4;
42 
43  struct point_t
44  {
45  std::int16_t x;
46  std::int16_t y;
47  };
48 
50  {
51  union
52  {
54  struct
55  {
56  std::int16_t prev_x;
57  std::int16_t prev_y;
58  };
59  };
60  union
61  {
63  struct
64  {
65  std::int16_t base_x;
66  std::int16_t base_y;
67  };
68  };
69 
70  std::uint32_t base_msec;
72 
73  inline int deltaX(void) const { return x - prev_x; }
74  inline int deltaY(void) const { return y - prev_y; }
75  inline int distanceX(void) const { return x - base_x; }
76  inline int distanceY(void) const { return y - base_y; }
77  inline bool isPressed(void) const { return state & touch_state_t::mask_touch; };
78  inline bool wasPressed(void) const { return state == touch_state_t::touch_begin; };
79  inline bool wasClicked(void) const { return state == touch_state_t::touch_end; };
80  inline bool isReleased(void) const { return !(state & touch_state_t::mask_touch); };
83  inline bool wasHold(void) const { return state == touch_state_t::hold_begin; }
84  };
85 
88  inline std::uint8_t getCount(void) const { return _touch_count; }
89 
91  inline const touch_detail_t& getDetail(std::size_t index = 0) const { return _touch_detail[_touch_raw[index].id < TOUCH_MAX_POINTS ? _touch_raw[index].id : 0]; }
92 
93 
94  inline const m5gfx::touch_point_t& getTouchPointRaw(std::size_t index = 0) const { return _touch_raw[index < _touch_count ? index : 0]; }
95 
96  void setHoldThresh(std::uint16_t msec) { _msecHold = msec; }
97 
98  void setFlickThresh(std::uint16_t distance) { _flickThresh = distance; }
99 
100  bool isEnabled(void) const { return _gfx; }
101 
102  void begin(m5gfx::LGFX_Device* gfx) { _gfx = gfx; }
103  void update(std::uint32_t msec);
104  void end(void) { _gfx = nullptr; }
105 
106  protected:
107  std::uint32_t _last_msec = 0;
108  std::int32_t _flickThresh = 8;
109  std::int32_t _msecHold = 500;
110  m5gfx::LGFX_Device* _gfx = nullptr;
113  std::uint8_t _touch_count;
114 
115  bool update_detail(touch_detail_t* dt, std::uint32_t msec, bool pressed, m5gfx::touch_point_t* tp);
116  bool update_detail(touch_detail_t* dt, std::uint32_t msec);
117  };
118 }
119 #endif
int16_t x
Definition: IMU_Base.hpp:3
int16_t y
Definition: IMU_Base.hpp:4
std::int32_t _msecHold
std::int32_t _flickThresh
static constexpr std::size_t TOUCH_MIN_UPDATE_MSEC
Definition: Touch_Class.hpp:41
touch_detail_t _touch_detail[TOUCH_MAX_POINTS]
void end(void)
const touch_detail_t & getDetail(std::size_t index=0) const
Definition: Touch_Class.hpp:91
static constexpr std::size_t TOUCH_MAX_POINTS
Definition: Touch_Class.hpp:40
m5gfx::touch_point_t _touch_raw[TOUCH_MAX_POINTS]
void setHoldThresh(std::uint16_t msec)
Definition: Touch_Class.hpp:96
void begin(m5gfx::LGFX_Device *gfx)
void update(std::uint32_t msec)
Definition: Touch_Class.cpp:8
std::uint8_t getCount(void) const
Definition: Touch_Class.hpp:88
bool update_detail(touch_detail_t *dt, std::uint32_t msec, bool pressed, m5gfx::touch_point_t *tp)
Definition: Touch_Class.cpp:55
const m5gfx::touch_point_t & getTouchPointRaw(std::size_t index=0) const
Definition: Touch_Class.hpp:94
bool isEnabled(void) const
m5gfx::LGFX_Device * _gfx
std::uint8_t _touch_count
void setFlickThresh(std::uint16_t distance)
Definition: Touch_Class.hpp:98
std::uint32_t _last_msec
Definition: M5Unified.cpp:48
touch_state_t
Definition: Touch_Class.hpp:14
@ hold
Definition: Touch_Class.hpp:19
@ mask_holding
Definition: Touch_Class.hpp:33
@ flick
Definition: Touch_Class.hpp:23
@ mask_touch
Definition: Touch_Class.hpp:31
@ touch_begin
Definition: Touch_Class.hpp:17
@ drag_end
Definition: Touch_Class.hpp:28
@ touch
Definition: Touch_Class.hpp:15
@ touch_end
Definition: Touch_Class.hpp:16
@ mask_change
Definition: Touch_Class.hpp:32
@ hold_begin
Definition: Touch_Class.hpp:21
@ drag_begin
Definition: Touch_Class.hpp:29
@ hold_end
Definition: Touch_Class.hpp:20
@ flick_begin
Definition: Touch_Class.hpp:25
@ none
Definition: Touch_Class.hpp:14
@ mask_moving
Definition: Touch_Class.hpp:34
@ drag
Definition: Touch_Class.hpp:27
@ flick_end
Definition: Touch_Class.hpp:24
m5gfx::touch_point_t touch_point_t
Definition: M5Unified.hpp:24
point_t base
Flick start point.
Definition: Touch_Class.hpp:62
point_t prev
Previous point.
Definition: Touch_Class.hpp:53