M5Unified
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Touch_Class.cpp
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 #include "Touch_Class.hpp"
5 
6 namespace m5
7 {
8  void Touch_Class::update(std::uint32_t msec)
9  {
10  if (msec - _last_msec <= TOUCH_MIN_UPDATE_MSEC)
11  {
12  if (_touch_count == 0) { return; }
13  std::size_t count = 0;
14  for (std::size_t i = 0; i < TOUCH_MAX_POINTS; ++i)
15  {
16  count += update_detail(&_touch_detail[i], msec);
17  }
18  _touch_count = count;
19  return;
20  }
21 
22  _last_msec = msec;
23  std::size_t count = _gfx->getTouchRaw(_touch_raw, TOUCH_MAX_POINTS);
24  if (!(count || _touch_count)) { return; }
25 
26  uint32_t updated_id = 0;
27  if (count)
28  {
30  memcpy(tp, _touch_raw, sizeof(m5gfx::touch_point_t) * count);
31  _gfx->convertRawXY(tp, count);
32  for (std::size_t i = 0; i < count; ++i)
33  {
34  if (tp[i].id < TOUCH_MAX_POINTS)
35  {
36  updated_id |= 1 << tp[i].id;
37  update_detail(&_touch_detail[tp[i].id], msec, true, &tp[i]);
38  }
39  }
40  }
41  {
42  for (std::size_t i = 0; i < TOUCH_MAX_POINTS; ++i)
43  {
44  if ((!(updated_id & (1 << i)))
45  && update_detail(&_touch_detail[i], msec, false, nullptr)
46  && (count < TOUCH_MAX_POINTS))
47  {
48  ++count;
49  }
50  }
51  }
52  _touch_count = count;
53  }
54 
55  bool Touch_Class::update_detail(touch_detail_t* det, std::uint32_t msec, bool pressed, m5gfx::touch_point_t* tp)
56  {
57  touch_state_t tm = det->state;
58  if (tm == touch_state_t::none && !pressed)
59  {
60  return false;
61  }
62  tm = static_cast<touch_state_t>(tm & ~touch_state_t::mask_change);
63  if (pressed)
64  {
65  det->prev_x = det->x;
66  det->prev_y = det->y;
67  det->size = tp->size;
68  det->id = tp->id;
69  if (!(tm & touch_state_t::mask_moving))
70  { // Processing when not flicked.
72  { // Not immediately after the touch.
73  if (abs(det->base_x - tp->x) > _flickThresh
74  || abs(det->base_y - tp->y) > _flickThresh)
75  {
76  det->prev = det->base;
77  tm = static_cast<touch_state_t>(tm | touch_state_t::flick_begin);
78  }
79  else
80  if ((tm == touch) && (msec - det->base_msec > _msecHold))
81  {
83  }
84  }
85  else
86  {
87  *(static_cast<m5gfx::touch_point_t*>(det)) = *tp;
89  det->base_msec = msec;
90  det->base_x = tp->x;
91  det->base_y = tp->y;
92  det->prev = det->base;
93  }
94  }
95  if (tm & mask_moving)
96  {
97  det->x = tp->x;
98  det->y = tp->y;
99  }
100  }
101  else
102  {
103  tm = (tm & touch_state_t::mask_touch)
106  }
107  det->state = tm;
108  return true;
109  }
110 
111  bool Touch_Class::update_detail(touch_detail_t* det, std::uint32_t msec)
112  {
113  touch_state_t tm = det->state;
114  if (tm == touch_state_t::none)
115  {
116  return false;
117  }
118  tm = static_cast<touch_state_t>(tm & ~touch_state_t::mask_change);
119  if (tm & touch)
120  {
121  det->prev_x = det->x;
122  det->prev_y = det->y;
123  if ((tm == touch) && (msec - det->base_msec > _msecHold))
124  {
126  }
127  }
128  else
129  {
130  tm = touch_state_t::none;
131  }
132  det->state = tm;
133  return true;
134  }
135 }
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]
static constexpr std::size_t TOUCH_MAX_POINTS
Definition: Touch_Class.hpp:40
m5gfx::touch_point_t _touch_raw[TOUCH_MAX_POINTS]
void update(std::uint32_t msec)
Definition: Touch_Class.cpp:8
bool update_detail(touch_detail_t *dt, std::uint32_t msec, bool pressed, m5gfx::touch_point_t *tp)
Definition: Touch_Class.cpp:55
m5gfx::LGFX_Device * _gfx
std::uint8_t _touch_count
std::uint32_t _last_msec
Definition: M5Unified.cpp:48
touch_state_t
Definition: Touch_Class.hpp:14
@ mask_touch
Definition: Touch_Class.hpp:31
@ touch_begin
Definition: Touch_Class.hpp:17
@ touch
Definition: Touch_Class.hpp:15
@ mask_change
Definition: Touch_Class.hpp:32
@ hold_begin
Definition: Touch_Class.hpp:21
@ flick_begin
Definition: Touch_Class.hpp:25
@ none
Definition: Touch_Class.hpp:14
@ mask_moving
Definition: Touch_Class.hpp:34
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