This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
GestureRecognizerApp.cpp
107 lines (80 loc) · 2.4 KB
/
GestureRecognizerApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include "Config.h"
#include "GestureRecognizer.h"
#include "ScheduledAction.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
GestureRecognizer gestureRecognizer;
ScheduledAction sampleAccelerationDataAction;
ScheduledAction recognizeGestureAction;
void setup() {
// initialize device
printf("Initializing I2C devices...\n");
accelgyro.initialize();
// verify connection
printf("Testing device connections...\n");
printf(accelgyro.testConnection() ? "MPU6050 connection successful\n" : "MPU6050 connection failed\n");
printf("Loading recorded gestures\n");
gestureRecognizer.loadGestures();
sampleAccelerationDataAction.setDelayMicros((1000L * 1000L) / SAMPLE_FREQUENCY_HZ);
sampleAccelerationDataAction.reset();
}
void sampleAccelerationData(void) {
AccelerationData accelerationData;
accelgyro.getAcceleration(&accelerationData.x, &accelerationData.y, &accelerationData.z);
gestureRecognizer.addAccelerationData(accelerationData);
}
void recognizeGesture(void) {
Gesture* recognizedGesture = gestureRecognizer.recognize();
if (!recognizedGesture) {
return;
}
printf("\nRecognized gesture: %d\n\n", recognizedGesture->getId());
}
void replayAndRecognize() {
gestureRecognizer.load("replay.ges");
printf("Recognizing...\n");
recognizeGesture();
}
void recordAndRecognize(bool continuousMode) {
unsigned long recognizeDelayMicros = (1000L * 1000L) * 0.25;
if (!continuousMode) {
recognizeDelayMicros = (1000L * 1000L) * 1;
}
recognizeGestureAction.setDelayMicros(recognizeDelayMicros);
recognizeGestureAction.reset();
printf("Recording...\n");
while (true) {
if (sampleAccelerationDataAction.isActionDue()) {
sampleAccelerationData();
}
if (recognizeGestureAction.isActionDue()) {
printf("Recognizing...\n");
recognizeGesture();
if (!continuousMode) {
break;
}
}
}
}
int main(int argc, char *argv[]) {
setup();
printf("args: %d\n", argc);
for (int index = 0; index < argc; index++) {
printf("%s\n", argv[index]);
}
if (argc == 2 && strcasecmp(argv[1], "REPLAY") == 0) {
replayAndRecognize();
return 0;
}
bool continuousMode = false;
if (argc == 2 && strcasecmp(argv[1], "CONTINUOUS") == 0) {
continuousMode = true;
}
recordAndRecognize(continuousMode);
return 0;
}