/
gamepad
executable file
·226 lines (196 loc) · 4.78 KB
/
gamepad
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
#!/bin/bash
# contains and uses this version of Jason White's joystick.c at bottom
# https://gist.github.com/jasonwhite/c5b2048c15993d285130/eb3bcd6e1cd88002d21764b9965d075eceaf4b83
#
# requires SNES joystick connected to Raspberry USB, pigpio library installed
#
js=/tmp/joystick
if [ ! -f $js ]; then sed -n "/^\/\*\*$/,\$p" $0 | gcc -x c - -o $js; fi
g=1500
l=1500
u=600
p=1500
d=100
calib=0
if [ ! -f /var/run/pigpio.pid ]; then sudo pigpiod; sleep 0.5; fi
pigs s 8 $g; pigs s 9 $u; pigs s 10 $l; pigs s 11 $p
while IFS= read -r line; do
case $line in
"Axis 0 at (-32767, 0)")
let p=p+d
;;
"Axis 0 at ( 32767, 0)")
let p=p-d
;;
"Axis 0 at ( 0, -32767)")
let l=l+d
;;
"Axis 0 at ( 0, 32767)")
let l=l-d
;;
"Button 0 pressed")
let u=u+d
;;
"Button 2 pressed")
let u=u-d
;;
"Button 1 pressed")
let g=g+d
;;
"Button 3 pressed")
let g=g-d
;;
"Button 4 pressed")
let d=d/10
if [ $d -lt 10 ]; then
d=10
fi
;;
"Button 5 pressed")
let d=d*10
if [ $d -gt 1000 ]; then
d=1000
fi
;;
"Button 8 pressed")
g=2500
d=100
calib=1
;;
"Button 9 pressed")
g=1500
l=1500
u=600
p=1500
d=100
calib=0
;;
esac
if [ $g -lt 550 ]; then g=550; fi
if [ $g -gt 2000 -a $calib -lt 1 ]; then g=2000; fi
if [ $u -lt 600 ]; then u=600; fi
if [ $u -gt 2250 ]; then u=2250; fi
if [ $l -lt 500 ]; then l=500; fi
if [ $l -gt 2500 ]; then l=2500; fi
if [ $p -lt 500 ]; then p=500; fi
if [ $p -gt 2500 ]; then p=2500; fi
echo $g $u $l $p $d $calib
pigs s 8 $g; pigs s 9 $u; pigs s 10 $l; pigs s 11 $p
done < <($js)
exit
/**
* Author: Jason White
*
* Description:
* Reads joystick/gamepad events and displays them.
*
* Compile:
* gcc joystick.c -o joystick
*
* Run:
* ./joystick [/dev/input/jsX]
*
* See also:
* https://www.kernel.org/doc/Documentation/input/joystick-api.txt
*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/joystick.h>
/**
* Reads a joystick event from the joystick device.
*
* Returns 0 on success. Otherwise -1 is returned.
*/
int read_event(int fd, struct js_event *event)
{
ssize_t bytes;
bytes = read(fd, event, sizeof(*event));
if (bytes == sizeof(*event))
return 0;
/* Error, could not read full event. */
return -1;
}
/**
* Returns the number of axes on the controller or 0 if an error occurs.
*/
size_t get_axis_count(int fd)
{
__u8 axes;
if (ioctl(fd, JSIOCGAXES, &axes) == -1)
return 0;
return axes;
}
/**
* Returns the number of buttons on the controller or 0 if an error occurs.
*/
size_t get_button_count(int fd)
{
__u8 buttons;
if (ioctl(fd, JSIOCGBUTTONS, &buttons) == -1)
return 0;
return buttons;
}
/**
* Current state of an axis.
*/
struct axis_state {
short x, y;
};
/**
* Keeps track of the current axis state.
*
* NOTE: This function assumes that axes are numbered starting from 0, and that
* the X axis is an even number, and the Y axis is an odd number. However, this
* is usually a safe assumption.
*
* Returns the axis that the event indicated.
*/
size_t get_axis_state(struct js_event *event, struct axis_state axes[3])
{
size_t axis = event->number / 2;
if (axis < 3)
{
if (event->number % 2 == 0)
axes[axis].x = event->value;
else
axes[axis].y = event->value;
}
return axis;
}
int main(int argc, char *argv[])
{
const char *device;
int js;
struct js_event event;
struct axis_state axes[3] = {0};
size_t axis;
if (argc > 1)
device = argv[1];
else
device = "/dev/input/js0";
js = open(device, O_RDONLY);
if (js == -1)
perror("Could not open joystick");
/* This loop will exit if the controller is unplugged. */
while (read_event(js, &event) == 0)
{
switch (event.type)
{
case JS_EVENT_BUTTON:
printf("Button %u %s\n", event.number, event.value ? "pressed" : "released");
break;
case JS_EVENT_AXIS:
axis = get_axis_state(&event, axes);
if (axis < 3)
printf("Axis %zu at (%6d, %6d)\n", axis, axes[axis].x, axes[axis].y);
break;
default:
/* Ignore init events. */
break;
}
fflush(stdout);
}
close(js);
return 0;
}