-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventHandler.cpp
325 lines (280 loc) · 9.58 KB
/
EventHandler.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
/*
* File: EventHandler.cpp
* Author: josh
*
* Created on August 6, 2019, 9:03 PM
*/
#include "EventHandler.h"
#define DEBUG
#define EVENT_DEBUG
EventHandler::EventHandler() {
kill = false;
}
EventHandler::EventHandler(const EventHandler& orig) {
}
EventHandler::~EventHandler() {
rc = 0;
libevdev_free(dev);
}
/*
* Copyright © 2013 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
void EventHandler::init(std::string device_name, std::shared_ptr<Robot>r_in) {
fd = open(device_name.c_str(), O_RDONLY);
if (fd < 0) {
perror("Failed to open device");
exit(0);
}
rc = libevdev_new_from_fd(fd, &dev);
if (rc < 0) {
fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
exit(0);
}
r = r_in;
#ifdef EVENT_DEBUG
printf("Input device ID: bus %#x vendor %#x product %#x\n",
libevdev_get_id_bustype(dev),
libevdev_get_id_vendor(dev),
libevdev_get_id_product(dev));
printf("Evdev version: %x\n", libevdev_get_driver_version(dev));
printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
printf("Phys location: %s\n", libevdev_get_phys(dev));
printf("Uniq identifier: %s\n", libevdev_get_uniq(dev));
print_bits(dev);
#endif
//print_props(dev);
}
#ifdef EVENT_DEBUG
void EventHandler::print_abs_bits(struct libevdev *dev, int axis) {
const struct input_absinfo *abs;
if (!libevdev_has_event_code(dev, EV_ABS, axis))
return;
abs = libevdev_get_abs_info(dev, axis);
printf(" Value %6d\n", abs->value);
printf(" Min %6d\n", abs->minimum);
printf(" Max %6d\n", abs->maximum);
if (abs->fuzz)
printf(" Fuzz %6d\n", abs->fuzz);
if (abs->flat)
printf(" Flat %6d\n", abs->flat);
if (abs->resolution)
printf(" Resolution %6d\n", abs->resolution);
}
void EventHandler::print_code_bits(struct libevdev *dev, unsigned int type, unsigned int max) {
unsigned int i;
for (i = 0; i <= max; i++) {
if (!libevdev_has_event_code(dev, type, i))
continue;
printf(" Event code %i (%s)\n", i, libevdev_event_code_get_name(type, i));
if (type == EV_ABS)
print_abs_bits(dev, i);
}
}
void EventHandler::print_bits(struct libevdev *dev) {
unsigned int i;
printf("Supported events:\n");
for (i = 0; i <= EV_MAX; i++) {
if (libevdev_has_event_type(dev, i))
printf(" Event type %d (%s)\n", i, libevdev_event_type_get_name(i));
switch (i) {
case EV_KEY:
print_code_bits(dev, EV_KEY, KEY_MAX);
break;
case EV_REL:
print_code_bits(dev, EV_REL, REL_MAX);
break;
case EV_ABS:
print_code_bits(dev, EV_ABS, ABS_MAX);
break;
case EV_LED:
print_code_bits(dev, EV_LED, LED_MAX);
break;
}
}
}
void EventHandler::print_props(struct libevdev *dev) {
unsigned int i;
printf("Properties:\n");
for (i = 0; i <= INPUT_PROP_MAX; i++) {
if (libevdev_has_property(dev, i))
printf(" Property type %d (%s)\n", i,
libevdev_property_get_name(i));
}
}
int EventHandler::print_event(struct input_event *ev) {
if (ev->type == EV_SYN)
printf("Event: time %ld.%06ld, ++++++++++++++++++++ %s +++++++++++++++\n",
#ifndef JETSON
ev->input_event_sec,
ev->input_event_usec,
#else
ev->time.tv_sec,
ev->time.tv_usec,
#endif
libevdev_event_type_get_name(ev->type));
else
printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
#ifndef JETSON
ev->input_event_sec,
ev->input_event_usec,
#else
ev->time.tv_sec,
ev->time.tv_usec,
#endif
ev->type,
libevdev_event_type_get_name(ev->type),
ev->code,
libevdev_event_code_get_name(ev->type, ev->code),
ev->value);
return 0;
}
int EventHandler::print_sync_event(struct input_event *ev) {
printf("SYNC: ");
print_event(ev);
return 0;
}
#endif
int EventHandler::event_loop() {
do {
struct input_event ev;
rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_BLOCKING, &ev);
if (rc == LIBEVDEV_READ_STATUS_SYNC) {
//printf("::::::::::::::::::::: dropped ::::::::::::::::::::::\n");
while (rc == LIBEVDEV_READ_STATUS_SYNC) {
#ifdef EVENT_DEBUG
print_sync_event(&ev);
#endif
rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_SYNC, &ev);
}
// printf("::::::::::::::::::::: re-synced ::::::::::::::::::::::\n");
}
#ifdef EVENT_DEBUG
else if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
print_event(&ev);
std::cout << "current robot mode: " << r->get_text_mode() << std::endl;
#endif
switch (ev.code) {
case BTN_START:
{ //start Button disconnects
if (ev.value == 1) {
r->toggle_disconnected();
}
};
break;
case BTN_SELECT:
{ //Back Button sets mode
if (ev.value == 1) {
r->toggle_mode();
}
r->drive_brake();
};
break;
case BTN_MODE:
{//big logitech button in the center
if (ev.value == 1)
//set_kill(true);
;
};
break;
case BTN_WEST:
{ //Y Button -- used as a reset, kills everything and starts over
if (ev.value == 1) {
r->init();
}
}
break;
case BTN_SOUTH:
{ //"A" Button -- used to shift from forward to reverse
if (ev.value == 1) {
r->toggle_driving_direction();
}
};
break;
default:
{
;
};
}
switch (r->get_mode()) {
case MANUAL:
{
switch (ev.code) {
case ABS_X:
{
if (ev.value < -MIN_TURN) {
r->turn_left(abs(ev.value));
} else if (ev.value > MIN_TURN) {
r->turn_right(abs(ev.value));
}
};
break;
case ABS_RY:
{
if (ev.value < MIN_THROTTLE_FORWARD) {
r->set_driving_direction(FORWARD);
if (r->get_driving()) {
r->change_speed(abs(ev.value));
r->drive();
} else {
r->change_speed(abs(ev.value));
r->drive();
}
} else if (ev.value > MIN_THROTTLE_BACKWARD) {
r->set_driving_direction(BACKWARD);
if (r->get_driving()) {
r->change_speed(abs(ev.value));
r->drive();
} else {
r->change_speed(abs(ev.value));
r->drive();
}
} else if (ev.value >= MIN_THROTTLE_FORWARD && ev.value <= MIN_THROTTLE_BACKWARD) {
r->drive_brake();
r->set_driving(false);
}
};
break;
}
//run here??
};
break; //case manual
case AUTOMATIC:
{
};
break;
case GPS:
{
};
break;
case LIDAR_AUTOMATIC:
{
};
break;
default:
{
}
break;
}
} while ((rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS || rc == -EAGAIN) && kill == false);
if (rc != LIBEVDEV_READ_STATUS_SUCCESS && rc != -EAGAIN)
fprintf(stderr, "Failed to handle events: %s\n", strerror(-rc));
return rc;
}