Gesture Library
double.c
Go to the documentation of this file.
1 #include "double.h"
2 
3 #include "gestureparams.h"
4 #include "utils.h"
5 
6 // #include <stdio.h>
7 
9 void (*on_double_tap)(const double_tap_t*) = 0;
10 
12  for (int i = 0; i < MAX_TOUCHES; i++) {
14  double_tap_d[i].group = i + 1;
15  // double_tap_d[i].num_touches = 0;
16  double_tap_d[i].t = 0;
17  double_tap_d[i].x = 0;
18  double_tap_d[i].y = 0;
19  }
20  on_double_tap = 0;
21 }
22 
23 // gesture_event_t double_tap = {.type = GESTURE_TYPE_DOUBLE_TAP, .get_data = (void* (*)(void))get_double_tap};
24 
25 // static touch_event_t* prev_event;
26 
27 static void update_double_taps(const tap_t* tap, const touch_event_t* event);
28 static void update_data0_fields(int index, int group_index, const touch_event_t* event);
29 
30 void recognize_double_tap(const touch_event_t* event) {
31  // pass in the event and the taps incrementally
32  // if a tap is completed, compare it to all of the double taps in the double data array
33  // and check if for any in_progress ones (if it's null or complete, save that index cuz
34  // we'll need it later) and check if it's close enough; if so, change that double_tap to
35  // complete and update the data fields for it; along the way also compare the timestamps
36  // save the index of any within GROUP_TIME_DIFF (we'll group touches this way); if
37  // nothing is found, update the group and replace the existing complete/null with this
38  // new in_progress double tap
39  // change all in_progress to possible
40 
41  const tap_t* taps = get_tap();
42 
43  // for (int i = 0; i < MAX_TOUCHES; i++) {
44  // if ((taps + i)->t == event->t && (taps + i)->x == event->x &&
45  // (taps + i)->y == event->y) { // make sure we're not looking at an old completed tap
46  // update_double_taps(taps + i, event); // we don't need event, but it was for the sake of security
47  // }
48  // }
49  update_double_taps(taps + event->group, event);
50 }
51 
52 int set_on_double_tap(void (*listener)(const double_tap_t*)) {
53  if (on_double_tap) {
54  on_double_tap = listener;
55  return 0;
56  } else {
57  on_double_tap = listener;
58  return 1;
59  }
60 }
61 
62 static void update_double_taps(const tap_t* tap, const touch_event_t* event) {
63  int null_index = -1; // last seen null or failed double_d spot
64  int complete_index = -1; // last seen complete spot
65  int update = 1;
66  int group_index = -1; // double tap with the closest time the completed tap, will group with this tap
67  // int group_number;
68 
69  // this is used to prevent double triggers
70  int found = 0;
71 
72  // float x_diff;
73  // float y_diff;
74  float pos_diff;
75  float time_diff;
76 
77  // printf("printing tap_state: %d\n", tap->state);
78 
80  // printf("entered with tap x = %f\n", tap->x);
81  // if completed, compare it to all double_taps in double_d
82  // look for any possible ones and compare time and position
83 
84  for (int i = 0; i < MAX_TOUCHES; i++) {
85  // printf("entered for loop with tap x = %f\n", tap->x);
86  double_tap_t d_tap = double_tap_d[i];
87 
88  switch (d_tap.state) {
90  // check position and time
91 
92  // checking tap position to double tap position
93 
94  pos_diff = SQUARED_DIST_0(event, d_tap);
95  // x_diff = (event->x - d_tap.x0) < 0 ? (event->x - d_tap.x0) * -1 : (event->x - d_tap.x0);
96  // y_diff = (event->y - d_tap.y0) < 0 ? (event->y - d_tap.y0) * -1 : (event->y - d_tap.y0);
97  time_diff = event->t - d_tap.t0;
98 
99  // printf("Printing possible case: x = %f\n", d_tap.x0);
100  // printf("Printing tap data: x = %f\n", tap->x);
101  // printf("Printing distances: x_diff = %f, y_diff = %f, time_diff = %f\n", x_diff, y_diff, time_diff);
102 
103  if (pos_diff < DOUBLE_DIST) {
104  if (time_diff < DOUBLE_TIME_DIFF) {
105  // found the corresponding d_tap
107  found = 1;
108  null_index = -1;
109  update = 0;
110 
111  // update data fields
112  double_tap_d[i].x = event->x;
113  double_tap_d[i].y = event->y;
114  double_tap_d[i].t = event->t;
115  } else {
117  }
118 
119  if (on_double_tap) { // listener
121  }
122  } else { // grouping method needs to change
123  float group_time_diff = tap->t - d_tap.t0; // time shouldn't be negative
124  float group_dist_diff = SQU_DIST(tap->x, tap->y, d_tap.x0, d_tap.y0);
125 
126  if (group_time_diff < DOUBLE_GROUP_TIME_DIFF &&
127  group_dist_diff <
128  DOUBLE_GROUP_DIST_DIFF) { // if not it, we need to check if this is part of the same group
129  // distance makes sure its close to a finger in the group
130  group_index = i;
131  // group_number = d_tap.group; //set this one's group to this if group_index dne -1
132  }
133  }
134 
135  break;
138  // printf("entered null with tap x = %f\n", tap->x);
139  // save the index (need to replace)
140  null_index = i; // we want to prioritize null spots and failed spots over completed ones
141  break;
143  complete_index = i;
144  default:
145  break;
146  }
147 
148  if (found)
149  break;
150  }
151 
152  if (update) {
153  if (null_index != -1) {
154  // printf("entered null_index if with tap x = %f and index = %d\n", tap->x, null_index);
155  update_data0_fields(null_index, group_index, event);
156  } else if (complete_index != -1) {
157  update_data0_fields(complete_index, group_index, event);
158  }
159  }
160 
161  // for (int j = 0; j < MAX_TOUCHES; j++) {
162  // printf("Printing state: %d, x: %f, x0: %f, group: %d\n",
163  // double_tap_d[j].state,
164  // double_tap_d[j].x,
165  // double_tap_d[j].x0,
166  // double_tap_d[j].group);
167  // }
168  }
169 }
170 
171 static void update_data0_fields(int index, int group_index, const touch_event_t* event) {
173  double_tap_d[index].x0 = event->x;
174  double_tap_d[index].y0 = event->y;
175  double_tap_d[index].t0 = event->t;
176 
177  if (group_index != -1) {
178  double_tap_d[index].group = double_tap_d[group_index].group;
179  }
180 
181  if (on_double_tap) { // listener
182  on_double_tap(&double_tap_d[index]);
183  }
184 }
185 
187  return double_tap_d;
188 }
const double_tap_t * get_double_tap()
returns an array of the ongoing double_touch data for each finger
Definition: double.c:186
void recognize_double_tap(const touch_event_t *event)
reads the last tap and stroke data and determines whethers this new stroke is part of a double_tap ev...
Definition: double.c:30
void init_double_tap()
initializes the data array by setting all field to 0/NULL
Definition: double.c:11
void(* on_double_tap)(const double_tap_t *)=0
Definition: double.c:9
double_tap_t double_tap_d[MAX_TOUCHES]
Definition: double.c:8
int set_on_double_tap(void(*listener)(const double_tap_t *))
Listen to double_tap events.
Definition: double.c:52
float DOUBLE_DIST
Definition: gestureparams.c:12
float DOUBLE_GROUP_DIST_DIFF
Definition: gestureparams.c:10
float DOUBLE_TIME_DIFF
Definition: gestureparams.c:13
float DOUBLE_GROUP_TIME_DIFF
Definition: gestureparams.c:9
#define MAX_TOUCHES
Definition: gestureparams.h:5
@ RECOGNIZER_STATE_FAILED
Definition: recognizer.h:12
@ RECOGNIZER_STATE_NULL
Definition: recognizer.h:6
@ RECOGNIZER_STATE_COMPLETED
Definition: recognizer.h:14
@ RECOGNIZER_STATE_POSSIBLE
Definition: recognizer.h:8
holds the state, last location, and timestamp of a double tap instance
Definition: double.h:16
int group
Definition: double.h:19
float x0
Definition: double.h:23
state_t state
Definition: double.h:17
float t0
Definition: double.h:25
float y
Definition: double.h:27
float x
Definition: double.h:26
float y0
Definition: double.h:24
float t
Definition: double.h:28
Data structure for tap data.
Definition: tap.h:8
state_t state
Definition: tap.h:9
float y
Definition: tap.h:18
float x
Definition: tap.h:17
float t
Definition: tap.h:19
To use the gesture library, users create touch events and fill in the appropriate fields.
Definition: gesturelib.h:17
unsigned int group
Definition: gesturelib.h:31
const tap_t * get_tap()
Access tap data array of size MAX_TOUCHES.
Definition: tap.c:34
#define SQU_DIST(x, y, x0, y0)
Definition: utils.h:7
#define SQUARED_DIST_0(a, b)
Definition: utils.h:6