-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpine_gestures.cpp
327 lines (258 loc) · 7.05 KB
/
pine_gestures.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Zachary Schroeder
* Created: July 28 2020
*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <cstring>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <errno.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <dirent.h>
#define SHAKE_THRESHOLD 32000
#define TWIST_THRESHOLD 15000
char twist_command[256];
char shake_command[256];
//reads file and converts to int
int ftoi(char* path)
{
FILE *f = fopen(path, "r");
char buffer[10];
if(f == NULL)
{
return NULL;
}
if(fgets(buffer, 10, f) == NULL)
{
return NULL;
}
fclose(f);
return std::stoi(buffer);
}
//find file containing string in dir
bool find(char* path, char* contains, char* newPath)
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path)) != NULL) {
bool found = false;
while ((ent = readdir (dir)) != NULL) {
if(strstr(ent->d_name, contains) != NULL)
{
sprintf(newPath, "%s/%s", path, ent->d_name);
closedir (dir);
return true;
}
}
closedir (dir);
return false;
}
}
int open_event_dev(const char* name_needle, int flags)
{
char path[256];
char name[256];
int fd, ret;
// find the right device and open it
for (int i = 0; i < 10; i++) {
snprintf(path, sizeof path, "/dev/input/event%d", i);
fd = open(path, flags);
if (fd < 0)
continue;
ret = ioctl(fd, EVIOCGNAME(256), name);
if (ret < 0)
continue;
if (strstr(name, name_needle))
return fd;
close(fd);
}
errno = ENOENT;
return -1;
}
void syscall_error(int is_err, const char* fmt, ...)
{
va_list ap;
if (!is_err)
return;
printf("ERROR: ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(": %s\n", strerror(errno));
}
extern "C" {
struct ff_effect e;
struct input_event play;
}
//vibrate code adapted from https://git.sr.ht/~mil/sxmo-utils/tree/master/programs/sxmo_vibratepine.c
void vibrate(int durationMs, int strength)
{
int fd, ret;
struct pollfd pfds[1];
int effects;
fd = open_event_dev("vibrator", O_RDWR | O_CLOEXEC);
syscall_error(fd < 0, "Can't open vibrator event device");
ret = ioctl(fd, EVIOCGEFFECTS, &effects);
syscall_error(ret < 0, "EVIOCGEFFECTS failed");
e.type = FF_RUMBLE;
e.id = -1;
e.u.rumble.strong_magnitude = strength;
ret = ioctl(fd, EVIOCSFF, &e);
syscall_error(ret < 0, "EVIOCSFF failed");
play.type = EV_FF;
play.code = e.id;
play.value = 3;
ret = write(fd, &play, sizeof play);
syscall_error(ret < 0, "write failed");
usleep(durationMs * 1000);
ret = ioctl(fd, EVIOCRMFF, e.id);
syscall_error(ret < 0, "EVIOCRMFF failed");
close(fd);
}
//get the value of a given argument
int get_arg(int argc, char **argv, char *arg_name, char *arg_value)
{
for(int i = 1; i < argc; i++)
{
if(strcmp(argv[i], arg_name) == 0 && i < argc-1)
{
sprintf(arg_value, argv[i+1]);
return 0;
}else if(strcmp(argv[i], arg_name) == 0 && i == argc-1)
{
return 2;
}
}
return 1;
}
void usage(char *name)
{
printf("Usage: %s [--shake_cmd <cmd>] [--twist_cmd <cmd>]\n");
printf("\t--shake_cmd\tpath to cmd or 'none' to disable. default: /usr/bin/toggleflash\n");
printf("\t--twist_cmd\tpath to cmd or 'none' to disable. default: /usr/bin/megapixels\n");
exit(1);
}
int main(int argc, char **argv)
{
char IMU_path[256];
char gyro_x_path[256];
char accel_y_path[256];
int ret = get_arg(argc, argv, "--shake_cmd", shake_command);
if(ret == 1){
sprintf(shake_command, "none");
}else if(ret == 2)
{
usage(argv[0]);
}
ret = get_arg(argc, argv, "--twist_cmd", twist_command);
if(ret == 1){
sprintf(twist_command, "none");
}else if(ret == 2)
{
usage(argv[0]);
}
if(strcmp(twist_command, "none") != 0)
{
sprintf(twist_command, "%s &", twist_command);
}
if(strcmp(shake_command, "none") != 0)
{
sprintf(shake_command, "%s &", shake_command);
}
//scan for IMU
if(find("/sys/bus/i2c/drivers/inv-mpu6050-i2c", "0068", IMU_path))
{
if(!find(IMU_path, "iio:device", IMU_path))
{
printf("could not find IMU path\n");
return 1;
}
}else{
printf("could not find IMU path\n");
return 1;
}
sprintf(gyro_x_path, "%s/in_anglvel_x_raw", IMU_path);
sprintf(accel_y_path, "%s/in_accel_y_raw", IMU_path);
//the last accel values, initialized to current accel value
int last_accel_y = ftoi(accel_y_path);
struct timeval start, now;
long mtime, seconds, useconds = 0;
gettimeofday(&start, NULL);
//number of shakes detected. The phone must be shaken at least twice to enable the flashlight.
int shakes = 0;
int twists = 0;
while(true)
{
//update accel value
int gyro_x = ftoi(gyro_x_path);
int accel_y = ftoi(accel_y_path);
gettimeofday(&now, NULL);
seconds = now.tv_sec - start.tv_sec;
useconds = now.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
//check for shake
if(abs(accel_y-last_accel_y) > SHAKE_THRESHOLD)
{
gettimeofday(&start, NULL);
shakes++;
usleep(100000);
}
if(abs(gyro_x) > TWIST_THRESHOLD)
{
gettimeofday(&start, NULL);
twists++;
usleep(100000);
}
if(shakes >= 2)
{
shakes = 0;
printf(shake_command);
if(strcmp(shake_command, "none") != 0)
system(shake_command);
vibrate(200, 4000);
//wait 1 second before continuing to avoid multiple detections
usleep(1000000);
}
if(twists >= 2)
{
twists = 0;
printf(twist_command);
if(strcmp(twist_command, "none") != 0)
system(twist_command);
vibrate(200, 4000);
//wait 1 second before continuing to avoid multiple detections
usleep(1000000);
}
//took too long
if(mtime > 1000)
{
shakes = 0;
twists = 0;
}
//printf("%d\n", abs(accel_y-last_accel_y) );
last_accel_y = accel_y;
usleep(25000);
}
}