command_joystick.c 3.35 KB
Newer Older
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
/*
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2015

	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 <fcntl.h>
#include <linux/joystick.h>
#include <command/command.h>
#include <dt_port_numbers.h>

#include <stdbool.h>

static bool found_name = false;
static bool found_float = false;
static bool found_setpoint = false;
static char *arg_name;

static void handler_list_entry(struct command *command, 
    char *name, enum command_value_type type, char *unit,
    int typec, enum command_ptype typev[])
{
	int i;

	if (strcmp(name, arg_name))
		return;
	
	found_name = true;
	found_float = (type == COMMAND_VALUE_TYPE_FLOAT);
	
	for (i = 0; i < typec; i++) {
		if (typev[i] == COMMAND_PTYPE_SETPOINT)
			found_setpoint = true;
	}
}


int main(int argc, char **argv)
{
	struct command *command;
	char *id = "command_joystick";
	char *host;
	short port = 21000;
	char *js_dev;
	int fd_joy = -1;
	
	if (argc < 5) {
		printf("Usage: %s <host> <port> <name> <joystick dev>\n", argv[0]);
		printf("Example: %s localhost %d spgname /dev/input/js0", argv[0], port);
		return 1;
	}
	
	host = argv[1];
	port = atoi(argv[2]);
	arg_name = argv[3];
	js_dev = argv[4];

	fd_joy = open(js_dev, O_RDONLY);
	if (fd_joy < 0) {
		perror("Could not open joystick device");
		return 1;
	}
	ioctl(fd_joy, FIONBIO, &(int){ 1 });

	command = command_open(host, port);
	command->handler_list_entry = handler_list_entry;

	while (command_state_get(command) != COMMAND_STATE_READY &&
	    command_state_get(command) != COMMAND_STATE_DISCONNECTED) {
		fd_set fdrx;
		int high = 0;
		
		FD_ZERO(&fdrx);

		command_fd_set(command, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		command_handle(command, &fdrx);
	}
	
	if (command_state_get(command) == COMMAND_STATE_DISCONNECTED) {
		printf("Not connected.\n");
		return 1;
	}

	if (!found_name) {
		printf("Could not find spg '%s'\n", arg_name);
		return 1;
	}
	if (!found_float) {
		printf("Only float type is supported\n");
		return 1;
	}
	if (!found_setpoint) {
		printf("Setpoint command is not supported\n");
		return 1;
	}
	
	command->type = COMMAND_VALUE_TYPE_FLOAT;
	command_id_set(command, id);
	command_name_set(command, arg_name);
	
	do {
		fd_set fdrx;
		int high;
		
		FD_ZERO(&fdrx);
		FD_SET(fd_joy, &fdrx);
		high = fd_joy;

		command_fd_set(command, &fdrx, &high);
		
		select(high + 1, &fdrx, NULL, NULL, NULL);
		
		command_handle(command, &fdrx);
		if (FD_ISSET(fd_joy, &fdrx)) {
			struct js_event event;
			ssize_t ret = read(fd_joy, &event, sizeof(event));
			if (ret == sizeof(event)) {
				if (event.type & JS_EVENT_AXIS && event.number == 0) {
					struct command_entry entry;
					entry.type = COMMAND_PTYPE_SETPOINT;
					entry.value.f = (float)event.value / 32768.0;
	
					command_send(command, &entry);
				}
			}
		}
		
	} while (1);
	
	return 0;
}