-
Notifications
You must be signed in to change notification settings - Fork 36
/
joy_RPi.c
321 lines (276 loc) · 8.05 KB
/
joy_RPi.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
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
/**** joy_RPi.c ****************************/
/* M. Moller 2013-01-16 */
/* Universal RPi GPIO keyboard daemon */
/*******************************************/
/*
Copyright (C) 2013 Michael Moller.
This file is part of the Universal Raspberry Pi GPIO keyboard daemon.
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The software 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
*/
/*******************************************/
/* based on the xmame driver by
/* Jason Birch 2012-11-21 V1.00 */
/* Joystick control for Raspberry Pi GPIO. */
/*******************************************/
//#include "xmame.h"
//#include "devices.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "joy_RPi.h"
#include "config.h"
#if defined RPI_JOYSTICK
#define GPIO_PERI_BASE 0x20000000
#define GPIO_BASE (GPIO_PERI_BASE + 0x200000)
#define BLOCK_SIZE (4 * 1024)
#define PAGE_SIZE (4 * 1024)
#define GPIO_ADDR_OFFSET 13
#define BUFF_SIZE 128
#define JOY_BUTTONS 32
#define JOY_AXES 2
#define JOY_DIRS 2
#define BOUNCE_TIME 2
// Raspberry Pi V1 GPIO
//static int GPIO_Pin[] = { 0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25 };
// Raspberry Pi V2 GPIO
//static int GPIO_Pin[] = { 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27 };
//MameBox pins
//static int GPIO_Pin[] = { 4, 17, 18, 22, 23, 24, 10, 25, 11, 8, 7 };
static char GPIO_Filename[JOY_BUTTONS][BUFF_SIZE];
static int GpioFile;
static char* GpioMemory;
static char* GpioMemoryMap;
volatile unsigned* GPIO;
static int AllMask;
static int lastGpio=0;
static int xGpio=0;
static int bounceCount=0;
static int doRepeat=0;
struct joydata_struct
{
int fd;
int num_axes;
int num_buttons;
int button_mask;
int change_mask;
int xio_mask;
int buttons[JOY_BUTTONS];
int change[JOY_BUTTONS];
int is_xio[JOY_BUTTONS];
} joy_data[1];
int joy_RPi_init(void)
{
FILE* File;
int Index;
char Buffer[BUFF_SIZE];
int n = gpios_used();
int xios = 0;
for (Index = 0; Index < n; ++Index){
sprintf(Buffer, "/sys/class/gpio/export");
if (!(File = fopen(Buffer, "w"))){
perror(Buffer);
//printf("Failed to open file: %s\n", Buffer);
return -1;
}
else{
fprintf(File, "%u", gpio_pin(Index));
fclose(File);
sprintf(Buffer, "/sys/class/gpio/gpio%u/direction", gpio_pin(Index));
if (!(File = fopen(Buffer, "w"))){
perror(Buffer);
//printf("Failed to open file: %s\n", Buffer);
}
else{
fprintf(File, "in");
fclose(File);
sprintf(GPIO_Filename[Index], "/sys/class/gpio/gpio%u/value", gpio_pin(Index));
}
AllMask |= (1 << gpio_pin(Index));
xios |= ( is_xio(gpio_pin(Index)) << gpio_pin(Index) );
joy_data[0].is_xio[Index] = is_xio(gpio_pin(Index));
}
}
GPIO = NULL;
GpioMemory = NULL;
if((GpioFile = open("/dev/mem", O_RDWR | O_SYNC)) < 0){
perror("/dev/mem");
printf("Failed to open memory\n");
return -1;
}
else if(!(GpioMemory = malloc(BLOCK_SIZE + PAGE_SIZE - 1))){
perror("malloc");
printf("Failed to allocate memory map\n");
return -1;
}
else{
if ((unsigned long)GpioMemory % PAGE_SIZE){
GpioMemory += PAGE_SIZE - ((unsigned long)GpioMemory % PAGE_SIZE);
}
if ((long)(GpioMemoryMap =
(unsigned char*)mmap(
(caddr_t)GpioMemory,
BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED,
GpioFile,
GPIO_BASE
)
) < 0){
perror("mmap");
printf("Failed to map memory\n");
return -1;
}
else{
close(GpioFile);
GPIO = (volatile unsigned*)GpioMemoryMap;
lastGpio = ((int*)GPIO)[GPIO_ADDR_OFFSET];
}
}
/* Set the file descriptor to a dummy value. */
joy_data[0].fd = 1;
joy_data[0].num_buttons = n;
joy_data[0].num_axes = 0;
joy_data[0].button_mask=0;
joy_data[0].xio_mask=xios;
bounceCount=0;
printf("Joystick init OK.\n");
return 0;
}
void joy_RPi_exit(void)
{
if (GpioFile >= 0)
close(GpioFile);
}
void joy_RPi_poll(void)
{
FILE* File;
int Joystick;
int Index;
int Char;
int newGpio;
int iomask;
Joystick = 0;
if(joy_data[Joystick].fd){
if (!GPIO){ /* fallback I/O? don't use - very slow. */
for (Index = 0; Index < joy_data[Joystick].num_buttons; ++Index){
if( (File = fopen(GPIO_Filename[Index], "r")) ){
Char = fgetc(File);
fclose(File);
iomask = (1 << gpio_pin(Index));
if (Char == '0'){
newGpio |= iomask;
//joy_data[Joystick].buttons[Index] = 1;
}
else{
newGpio &= ~iomask;
//joy_data[Joystick].buttons[Index] = 0;
}
}
}
}
else{
newGpio = ((int*)GPIO)[GPIO_ADDR_OFFSET];
}
newGpio &= AllMask;
//printf("%d: %08x\n", bounceCount, newGpio);
if(newGpio != lastGpio){
bounceCount=0;
xGpio |= newGpio ^ lastGpio;
//printf("%08x\n", xGpio);
}
lastGpio = newGpio;
xGpio &= ~joy_data[Joystick].xio_mask; /* remove expanders from change monitor */
if(bounceCount>=BOUNCE_TIME){
joy_data[Joystick].button_mask = newGpio;
joy_data[Joystick].change_mask = xGpio;
for (Index = 0; Index < joy_data[Joystick].num_buttons; ++Index){
iomask = (1 << gpio_pin(Index));
joy_data[Joystick].buttons[Index] = !(newGpio & iomask);
joy_data[Joystick].change[Index] = !!(xGpio & iomask);
}
xGpio = 0;
}
if(bounceCount<BOUNCE_TIME)bounceCount++;
joy_handle_event();
}
}
void joy_enable_repeat(void)
{
doRepeat = 1;
}
static void joy_handle_repeat(void)
{
const struct {
int time[4];
int value[4];
int next[4];
}mxkey = {
{80, 200, 40, 40},
{0, 1, 0, 1},
{1, 2, 3, 2}
};
/* key repeat metrics: release after 80ms, press after 200ms, release after 40ms, press after 40ms */
static int idx = -1;
static int prev_key = -1;
static unsigned t_now = 0;
static unsigned t_next = 0;
keyinfo_s ks;
get_last_key(&ks);
if(doRepeat){
if(!ks.val || (ks.key != prev_key)){ /* restart on release or key change */
prev_key = ks.key;
idx=-1;
t_next = t_now;
}
else if(idx<0){ /* start new cycle */
idx = 0;
t_next = t_now + mxkey.time[idx];
}
else if(t_now == t_next){
sendKey(ks.key, mxkey.value[idx]);
idx = mxkey.next[idx];
t_next = t_now + mxkey.time[idx];
}
t_now+=4; /* runs every 4 ms */
}
}
void joy_handle_event(void)
{
int Joystick = 0;
int Index;
/* handle all active irqs */
if(~joy_data[Joystick].button_mask & joy_data[Joystick].xio_mask){ /* if active ints exist */
//printf("XIO = %08x\n", ~joy_data[Joystick].button_mask & joy_data[Joystick].xio_mask);
for (Index = 0; Index < joy_data[Joystick].num_buttons; ++Index){
if( joy_data[Joystick].is_xio[Index] & joy_data[Joystick].buttons[Index]){
send_gpio_keys(gpio_pin(Index), joy_data[Joystick].buttons[Index]);
}
}
}
/* handle normal gpios */
if(joy_data[Joystick].change_mask){
//printf("GPIOs = %08x\n", joy_data[Joystick].button_mask);
joy_data[Joystick].change_mask = 0;
for (Index = 0; Index < joy_data[Joystick].num_buttons; ++Index){
if( joy_data[Joystick].change[Index] ){
joy_data[Joystick].change[Index] = 0;
//printf("Button %d = %d\n", Index, joy_data[Joystick].buttons[Index]);
send_gpio_keys(gpio_pin(Index), joy_data[Joystick].buttons[Index]);
}
}
}
joy_handle_repeat();
}
#endif