Gesture Library
hold.c
Go to the documentation of this file.
1 #include "hold.h"
2 
3 #include "math.h"
4 #include "stroke.h"
5 #include "utils.h"
6 
7 // data[group1, group2, group3, group4, group5]
8 
10 void (*on_hold)(const hold_t*) = 0;
11 
12 void init_hold() {
13  for (int i = 0; i < MAX_TOUCHES; i++) {
15  hold_d[i].t0 = 0;
16  hold_d[i].x0 = 0;
17  hold_d[i].y0 = 0;
18  hold_d[i].t = 0;
19  hold_d[i].x = 0;
20  hold_d[i].y = 0;
21  }
22  on_hold = 0;
23 }
24 
25 static void update_hold(hold_t* hold, const stroke_t* stroke, char down);
26 
27 void recognize_hold(const touch_event_t* event) {
28  const stroke_t* strokes = get_stroke();
29  for (int index = 0; index < MAX_TOUCHES; index++) {
30  update_hold(hold_d + index, strokes + index, event->type == TOUCH_EVENT_DOWN);
31  }
32 }
33 
34 int set_on_hold(void (*listener)(const hold_t*)) {
35  if (on_hold) {
36  on_hold = listener;
37  return 0;
38  } else {
39  on_hold = listener;
40  return 1;
41  }
42 }
43 
44 const hold_t* get_hold() {
45  return hold_d;
46 }
47 
48 static void update_hold(hold_t* hold, const stroke_t* stroke, char down) {
49  // hold->x0 = stroke->x0;
50  // hold->y0 = stroke->y0;
51  // hold->t0 = stroke->t0;
52  hold->x = stroke->x;
53  hold->y = stroke->y;
54  hold->t = stroke->t;
55 
56  switch (hold->state) {
60  hold->x0 = stroke->x0;
61  hold->y0 = stroke->y0;
62  hold->t0 = stroke->t0;
63  if (on_hold) {
64  on_hold(hold);
65  }
66  }
67  break;
70  float dx = stroke->x - stroke->x0;
71  float dy = stroke->y - stroke->y0;
72  if (SQUARE_SUM(dx, dy) > SQUARE(HOLD_DIST_MAX)) {
74  }
75  } else if (stroke->state == RECOGNIZER_STATE_COMPLETED) {
76  float dt = stroke->t - stroke->t0;
77  float dx = stroke->x - stroke->x0;
78  float dy = stroke->y - stroke->y0;
79  if (dt <= HOLD_TIME_MIN || SQUARE_SUM(dx, dy) > SQUARE(HOLD_DIST_MAX)) {
81  } else {
83  }
84  }
85  if (on_hold) {
86  on_hold(hold);
87  }
88  break;
91  if (down && stroke->state == RECOGNIZER_STATE_IN_PROGRESS) {
93  hold->x0 = stroke->x0;
94  hold->y0 = stroke->y0;
95  hold->t0 = stroke->t0;
96  if (on_hold) {
97  on_hold(hold);
98  }
99  }
100  break;
101  default:
102  return;
103  }
104 }
@ TOUCH_EVENT_DOWN
Definition: gesturelib.h:9
#define HOLD_TIME_MIN
Definition: gestureparams.h:22
#define MAX_TOUCHES
Definition: gestureparams.h:5
#define HOLD_DIST_MAX
Definition: gestureparams.h:34
const hold_t * get_hold()
Access hold data array of size MAX_TOUCHES.
Definition: hold.c:44
void recognize_hold(const touch_event_t *event)
Recognize hold gesture.
Definition: hold.c:27
void(* on_hold)(const hold_t *)=0
Definition: hold.c:10
int set_on_hold(void(*listener)(const hold_t *))
Listen to hold events.
Definition: hold.c:34
hold_t hold_d[MAX_TOUCHES]
Definition: hold.c:9
void init_hold()
Initialize hold data structures.
Definition: hold.c:12
@ RECOGNIZER_STATE_IN_PROGRESS
Definition: recognizer.h:10
@ RECOGNIZER_STATE_FAILED
Definition: recognizer.h:12
@ RECOGNIZER_STATE_NULL
Definition: recognizer.h:6
@ RECOGNIZER_STATE_COMPLETED
Definition: recognizer.h:14
const stroke_t * get_stroke()
Access array of stroke_t of size MAX_TOUCHES.
Definition: stroke.c:47
Data structure for hold data.
Definition: hold.h:8
float x0
Definition: hold.h:12
state_t state
Definition: hold.h:9
float t0
Definition: hold.h:14
float y
Definition: hold.h:18
float x
Definition: hold.h:17
float y0
Definition: hold.h:13
float t
Definition: hold.h:19
Data structure for stroke gesture data.
Definition: stroke.h:12
float x0
Definition: stroke.h:22
state_t state
Definition: stroke.h:14
float t0
Definition: stroke.h:24
float y
Definition: stroke.h:28
float x
Definition: stroke.h:27
float y0
Definition: stroke.h:23
float t
Definition: stroke.h:29
To use the gesture library, users create touch events and fill in the appropriate fields.
Definition: gesturelib.h:17
event_type_t type
Definition: gesturelib.h:19
#define SQUARE_SUM(x, y)
Definition: utils.h:4
#define SQUARE(x)
Definition: utils.h:3