-
Notifications
You must be signed in to change notification settings - Fork 8
/
joystick_remote.c
196 lines (167 loc) · 5.27 KB
/
joystick_remote.c
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
/*
This file is part of joystick_remote.
joystick_remote 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, either version 3 of the
License, or any later version.
joystick_remote 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 joystick_remote.
If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <poll.h>
#include <getopt.h>
#include <stdint.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include "joystick.h"
#include "remote.h"
#include "joystick_remote.h"
static struct timespec start_time;
static struct option long_options[] = {
{"list", no_argument, 0, 'l' },
{"device", required_argument, 0, 'd' },
{"mode", required_argument, 0, 'm' },
{"verbose", no_argument, 0, 'v' },
{"remote", required_argument, 0, 'r' },
{"type", required_argument, 0, 't' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
static struct joystick joystick;
static struct remote remote;
static const char usage[] = "usage:\n\tjoystick_remote -d your_device "
"-t joystick_type -r remote_address:remote_port\n\n"
"\tjoystick types: xbox360, skycontroller and ps3\n\n";
static uint8_t verbose = 0;
void debug_printf(const char *fmt, ...)
{
va_list arglist;
if (verbose == 1) {
va_start(arglist, fmt);
vprintf(fmt, arglist);
va_end(arglist);
}
}
static void microsleep(uint32_t usec) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = usec * 1000UL;
while (nanosleep(&ts, &ts) == -1 && errno == EINTR);
}
static uint64_t get_micro64()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return 1.0e6*((ts.tv_sec + (ts.tv_nsec*1.0e-9)) -
(start_time.tv_sec +
(start_time.tv_nsec*1.0e-9)));
}
int main(int argc, char **argv)
{
int c;
char *device_path = NULL;
char *joystick_type = NULL;
uint64_t next_run_usec;
char *remote_host = NULL;
uint16_t pwms[RCINPUT_UDP_NUM_CHANNELS];
if (argc < 2)
printf(usage);
while (1) {
c = getopt_long(argc, argv, "vld:m:r:cht:", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'l':
debug_printf("get the list of joysticks\n");
break;
case 'd':
debug_printf("device : %s\n", optarg);
device_path = optarg;
break;
case 'm':
debug_printf("mode : %s\n", optarg);
break;
case 'v':
verbose = 1;
break;
case 'r':
debug_printf("set remote to %s\n", optarg);
remote_host = optarg;
break;
case 't':
debug_printf("set joystick_type to %s\n", optarg);
joystick_type = optarg;
break;
case 'h':
printf(usage);
goto end;
case '?':
printf(usage);
goto end;
default:
fprintf(stderr, "bad option parsed by getopt : %d\n", c);
goto end;
}
}
if (optind < argc) {
fprintf(stderr, "wrong option in arguments: ");
while (optind < argc)
printf("%s", argv[optind++]);
printf("\n");
}
if (device_path == NULL) {
fprintf(stderr, "you must specify a device with -d option\n");
goto end;
}
if (joystick_start(device_path, &joystick) == -1) {
fprintf(stderr, "joystick start failed\n");
goto end;
}
if (remote_host == NULL) {
fprintf(stderr, "no ip address specified\n");
goto end;
}
if (remote_start(remote_host, &remote) == -1) {
fprintf(stderr, "remote start failed\n");
goto end;
}
/* Calibration procedure to be added */
if (joystick_type == NULL) {
fprintf(stderr, "no joystick type specified\n");
goto end;
}
joystick_set_type(&joystick, joystick_type);
/* get start time, necessary for get_micro64) */
clock_gettime(CLOCK_MONOTONIC, &start_time);
next_run_usec = get_micro64() + 10000;
while (1) {
uint64_t dt = next_run_usec - get_micro64();
uint64_t micro64;
uint8_t len;
if (dt > 20000) {
// we've lost sync - restart
next_run_usec = get_micro64();
} else {
microsleep(dt);
}
next_run_usec += 10000;
joystick_get_pwms(&joystick, pwms, &len);
remote_send_pwms(&remote, pwms, len, (micro64 = get_micro64()));
debug_printf("Micros : %" PRIu64", Roll : %d, Pitch : %d, Throttle : %d, Yaw : %d, Mode : %d\n",
micro64, pwms[0], pwms[1], pwms[2], pwms[3], pwms[4]);
}
end:
exit(EXIT_SUCCESS);
}