Gesture Library
tap.c
Go to the documentation of this file.
1 #include "tap.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_tap)(const tap_t*) = 0;
11 
12 void init_tap() {
13  for (int i = 0; i < MAX_TOUCHES; i++) {
15  tap_d[i].t0 = 0;
16  tap_d[i].x0 = 0;
17  tap_d[i].y0 = 0;
18  tap_d[i].t = 0;
19  tap_d[i].x = 0;
20  tap_d[i].y = 0;
21  }
22  on_tap = 0;
23 }
24 
25 static void update_tap(tap_t* tap, const stroke_t* stroke, char down);
26 
27 void recognize_tap(const touch_event_t* event) {
28  const stroke_t* strokes = get_stroke();
29  for (int index = 0; index < MAX_TOUCHES; index++) {
30  update_tap(tap_d + index, strokes + index, event->type == TOUCH_EVENT_DOWN);
31  }
32 }
33 
34 const tap_t* get_tap() {
35  return tap_d;
36 }
37 
38 int set_on_tap(void (*listener)(const tap_t*)) {
39  if (on_tap) {
40  on_tap = listener;
41  return 0;
42  } else {
43  on_tap = listener;
44  return 1;
45  }
46 }
47 
48 static void update_tap(tap_t* tap, const stroke_t* stroke, char down) {
49  switch (tap->state) {
53  if (on_tap) {
54  on_tap(tap);
55  }
56  }
57  break;
60  float dt = stroke->t - stroke->t0;
61  float dx = stroke->x - stroke->x0;
62  float dy = stroke->y - stroke->y0;
63  if (dt > TAP_TIME_MAX || SQUARE_SUM(dx, dy) > SQUARE(TAP_DIST_MAX)) {
65  }
66  } else if (stroke->state == RECOGNIZER_STATE_COMPLETED) {
67  float dt = stroke->t - stroke->t0;
68  float dx = stroke->x - stroke->x0;
69  float dy = stroke->y - stroke->y0;
70  if (dt > TAP_TIME_MAX || SQUARE_SUM(dx, dy) > SQUARE(TAP_DIST_MAX)) {
72  } else {
74  }
75  }
76  if (on_tap) {
77  on_tap(tap);
78  }
79  break;
82  if (down && stroke->state == RECOGNIZER_STATE_IN_PROGRESS) {
84  if (on_tap) {
85  on_tap(tap);
86  }
87  }
88  break;
89  default:
90  return;
91  }
92 
93  tap->x0 = stroke->x0;
94  tap->y0 = stroke->y0;
95  tap->t0 = stroke->t0;
96  tap->x = stroke->x;
97  tap->y = stroke->y;
98  tap->t = stroke->t;
99 }
@ TOUCH_EVENT_DOWN
Definition: gesturelib.h:9
float TAP_TIME_MAX
Definition: gestureparams.c:7
float TAP_DIST_MAX
Definition: gestureparams.c:15
#define MAX_TOUCHES
Definition: gestureparams.h:5
@ 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
@ RECOGNIZER_STATE_POSSIBLE
Definition: recognizer.h:8
const stroke_t * get_stroke()
Access array of stroke_t of size MAX_TOUCHES.
Definition: stroke.c:47
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
Data structure for tap data.
Definition: tap.h:8
float x0
Definition: tap.h:12
state_t state
Definition: tap.h:9
float t0
Definition: tap.h:14
float y
Definition: tap.h:18
float x
Definition: tap.h:17
float y0
Definition: tap.h:13
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
event_type_t type
Definition: gesturelib.h:19
tap_t tap_d[MAX_TOUCHES]
Definition: tap.c:9
int set_on_tap(void(*listener)(const tap_t *))
Subscribe listener to tap gesture updates.
Definition: tap.c:38
void init_tap()
Initialize tap data structures.
Definition: tap.c:12
void(* on_tap)(const tap_t *)=0
Definition: tap.c:10
const tap_t * get_tap()
Access tap data array of size MAX_TOUCHES.
Definition: tap.c:34
void recognize_tap(const touch_event_t *event)
Recognize tap gesture.
Definition: tap.c:27
#define SQUARE_SUM(x, y)
Definition: utils.h:4
#define SQUARE(x)
Definition: utils.h:3