-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
112 lines (84 loc) · 2.56 KB
/
main.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
/*
Copyright (C) 2017 Natchanan Thongtem
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, either version 3 of the License, or
(at your option) any later version.
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/>.
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "hidapi.h"
#include "gamepad.h"
#include "udp.h"
#define BUFFER_SIZE 65
#define SAMPLE_TIME 0.01
#define MEGA 1000000
#define KILO 1000
int main(int argc, char *argv[])
{
if(argc < 4)
{
return 1;
}
const char *rpi_ip = argv[1];
const int x_axis_port = atoi(argv[2]);
const int y_axis_port = atoi(argv[3]);
hid_device *handle;
unsigned char buffer[BUFFER_SIZE];
int response;
response = hid_init();
#ifdef WIN32
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
#endif
handle = hid_open(LOGITECT_VID, GAMEPAD_PID, NULL);
hid_set_nonblocking(handle, 1);
struct sockaddr_in server_x, server_y;
#ifdef WIN32
SOCKET client_socket_x, client_socket_y;
#else
int client_socket_x, client_socket_y;
#endif
client_socket_x = socket(PF_INET, SOCK_DGRAM, 0);
client_socket_y = socket(PF_INET, SOCK_DGRAM, 0);
server_x = init_socket_address(rpi_ip, x_axis_port);
server_y = init_socket_address(rpi_ip, y_axis_port);
while(1)
{
response = 0;
response = hid_read(handle, buffer, BUFFER_SIZE);
signed char x, y;
x = buffer[RIGHT_ANALOG_X_AXIS] - MAGIC_NUMBER_FOR_GAMEPAD;
y = buffer[LEFT_ANALOG_Y_AXIS] - MAGIC_NUMBER_FOR_GAMEPAD;
y += 1;
y *= -1;
printf("x = %d y = %d\n", x, y);
udp_send(client_socket_x, x, server_x);
udp_send(client_socket_y, y, server_y);
#ifdef WIN32
Sleep(SAMPLE_TIME * KILO);
#else
usleep(SAMPLE_TIME * MEGA);
#endif
}
hid_close(handle);
response = hid_exit();
return 0;
}