pikeyd165 – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**** joy_RPi.c ****************************/
2 /* M. Moller 2013-01-16 */
3 /* Universal RPi GPIO keyboard daemon */
4 /*******************************************/
5  
6 /*
7 Copyright (C) 2013 Michael Moller.
8 This file is part of the Universal Raspberry Pi GPIO keyboard daemon.
9  
10 This is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
14  
15 The software is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19  
20 You should have received a copy of the GNU Lesser General Public
21 License along with the GNU C Library; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA.
24 */
25  
26 /*******************************************/
27 /* based on the xmame driver by
28 /* Jason Birch 2012-11-21 V1.00 */
29 /* Joystick control for Raspberry Pi GPIO. */
30 /*******************************************/
31  
32 //#include "xmame.h"
33 //#include "devices.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <sys/mman.h>
38 #include "joy_RPi.h"
39 #include "config.h"
40 #include "uinput.h"
41  
42 #include <bcm2835.h>
43 #include "gpio_joystick.h"
44  
45  
46  
47 #if defined RPI_JOYSTICK
48  
49 #define GPIO_PERI_BASE 0x20000000
50 #define GPIO_BASE (GPIO_PERI_BASE + 0x200000)
51 #define BLOCK_SIZE (4 * 1024)
52 #define PAGE_SIZE (4 * 1024)
53 #define GPIO_ADDR_OFFSET 13
54 #define BUFF_SIZE 128
55 #define JOY_BUTTONS 32
56 #define JOY_AXES 2
57 #define JOY_DIRS 2
58  
59 #define BOUNCE_TIME 4
60  
61 extern bool no_key_debounce ;
62  
63  
64  
65 long unsigned int Read_Port_HC165(long unsigned int *) ;
66  
67  
68  
69 // Raspberry Pi V1 GPIO
70 //static int GPIO_Pin[] = { 0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25 };
71 // Raspberry Pi V2 GPIO
72 //static int GPIO_Pin[] = { 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27 };
73 //MameBox pins
74 //static int GPIO_Pin[] = { 4, 17, 18, 22, 23, 24, 10, 25, 11, 8, 7 };
75 static char GPIO_Filename[JOY_BUTTONS][BUFF_SIZE];
76  
77 static int GpioFile;
78 static char* GpioMemory;
79 static char* GpioMemoryMap;
80 volatile unsigned* GPIO;
81 static int AllMask;
82 static int lastGpio=0;
83 static int xGpio=0;
84 static int bounceCount=0;
85  
86 struct joydata_struct
87 {
88 int fd;
89 int num_axes;
90 int num_buttons;
91 int button_mask;
92 int change_mask;
93 int buttons[JOY_BUTTONS];
94 int change[JOY_BUTTONS];
95 } joy_data[1];
96  
97  
98 extern key_special_s KEY_SPECIAL[20];
99 int idx;
100  
101 int joy_RPi_init(void)
102 {
103 FILE* File;
104 int Index;
105 char Buffer[BUFF_SIZE];
106 int n = gpios_used();
107  
108  
109 // CM
110 // alten joy init code geloescht
111 //
112  
113  
114  
115 printf("bcm open\n");
116 if (!bcm2835_init())
117 {
118 printf("bcm open error\n");
119 exit(-1);
120 }
121 // Set the pins to be an output
122 bcm2835_gpio_fsel(CLK, BCM2835_GPIO_FSEL_OUTP);
123 bcm2835_gpio_fsel(PL, BCM2835_GPIO_FSEL_OUTP);
124 // Set the pin to be an input
125 bcm2835_gpio_fsel(DIN, BCM2835_GPIO_FSEL_INPT);
126  
127  
128 /* Set the file descriptor to a dummy value. */
129 joy_data[0].fd = 1;
130 joy_data[0].num_buttons = n;
131 joy_data[0].num_axes = 0;
132 joy_data[0].button_mask=0;
133  
134 bounceCount=0;
135  
136 printf("Joystick init OK.\n");
137  
138 return 0;
139 }
140  
141 void joy_RPi_exit(void)
142 {
143 printf("bcm close\n");
144 bcm2835_close();
145 }
146  
147  
148  
149  
150  
151  
152  
153 #define NRHC165BITS 96
154  
155 #define WAIT1 delayMicroseconds(1);
156  
157 long unsigned int pi2jamma_input[4]={5,5,5,5};
158  
159  
160  
161  
162  
163  
164 long unsigned int __attribute__((optimize("-O2"))) Read_Port_HC165(long unsigned int *inarray)
165 {
166  
167 long unsigned int val=0xffffffff;
168 int i;
169 int j=0;
170  
171 bcm2835_gpio_write(CLK, HIGH);
172 WAIT1
173 WAIT1
174 bcm2835_gpio_write(PL, LOW);
175 WAIT1
176 WAIT1
177 bcm2835_gpio_write(PL, HIGH);
178  
179  
180 // diese waits sind neu fuer irq setup Trackball irq
181 // gemessen mit Oszi: 10usec Luecke
182 // delayMicroseconds(9);
183  
184 delayMicroseconds(5);
185  
186  
187  
188 for(i=1;i<=NRHC165BITS ;i++)
189 {
190 val= val<<1 ;
191 WAIT1
192 WAIT1
193 uint8_t value = bcm2835_gpio_lev(DIN);
194 val=val|value;
195 bcm2835_gpio_write(CLK, LOW);
196 WAIT1
197 WAIT1
198 bcm2835_gpio_write(CLK, HIGH);
199  
200 if ( i%32 == 0 )
201 {
202 inarray[j++]=val;
203 //val=0xffffffff;
204 }
205 }
206  
207 return inarray[0]; // kompatibel zur alten version
208  
209 }
210  
211  
212  
213  
214 #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
215  
216  
217 void joy_RPi_poll(void)
218 {
219 FILE* File;
220 int Joystick;
221 int Index;
222 int Char;
223 int newGpio;
224 int iomask;
225  
226  
227 Joystick = 0;
228  
229 //hier muss was getan werden
230 //newGpio= ( (int) Read_Port_HC165(pi2jamma_input)) ^ 0xffffffff;
231 Read_Port_HC165(pi2jamma_input);
232  
233  
234  
235  
236 newGpio=(int) (pi2jamma_input[0] ^ 0xffffffff ) ;
237 newGpio = newGpio>>8;
238  
239  
240 //iomask=0xffffffff;
241  
242 // printf("val: %08X ",newGpio );
243 // newGpio &= AllMask;
244 // printf("%d: %08x\n", bounceCount, newGpio);
245  
246 if(newGpio != lastGpio){
247 bounceCount=0;
248 xGpio |= newGpio ^ lastGpio;
249 // printf("%08x %08x\n", xGpio, newGpio);
250  
251  
252 // scan special key table bit combination
253 for(idx=0; KEY_SPECIAL[idx].key!=0 && KEY_SPECIAL[idx].bits!=0 ; idx++)
254 {
255 // printf("%i %08x\n", KEY_SPECIAL[idx].key,KEY_SPECIAL[idx].bits);
256 if( ((newGpio & KEY_SPECIAL[idx].bits) ^ KEY_SPECIAL[idx].bits) == 0 ) // pressed
257 {
258 KEY_SPECIAL[idx].pressed=1;
259 sendKey( KEY_SPECIAL[idx].key, 1);
260 // printf("pressed!\n");
261 }
262 else
263 {
264 if( KEY_SPECIAL[idx].pressed ) // pressed key is released, if at least one bit goes to 0
265 if( ((newGpio&xGpio)^KEY_SPECIAL[idx].bits) )
266 {
267 KEY_SPECIAL[idx].pressed=0;
268 sendKey( KEY_SPECIAL[idx].key, 0);
269 // printf("released!\n");
270 }
271 }
272  
273 }
274  
275 }
276 lastGpio = newGpio;
277  
278 if(bounceCount>=BOUNCE_TIME || no_key_debounce ) {
279 joy_data[Joystick].button_mask = newGpio;
280 joy_data[Joystick].change_mask = xGpio;
281  
282 for (Index = 0; Index < joy_data[Joystick].num_buttons; ++Index){
283 iomask = (1 << gpio_pin(Index));
284 joy_data[Joystick].buttons[Index] = (newGpio & iomask);
285 joy_data[Joystick].change[Index] = (xGpio & iomask);
286 }
287 xGpio = 0;
288 }
289  
290  
291 if(bounceCount<BOUNCE_TIME)bounceCount++;
292  
293 joy_handle_event();
294  
295  
296 extern bool skip_mouse_init;
297  
298 //mouse initialisiert?
299 if( skip_mouse_init == false)
300 {
301 mouse_handle_event();
302 }
303  
304  
305 }
306  
307  
308  
309  
310 void mouse_handle_event(void)
311 {
312 unsigned int val;
313 int mouse_xrel, mouse_yrel;
314  
315 extern int uidev_fd_mouse1;
316 extern int uidev_fd_mouse2;
317  
318  
319 //mouse1
320 val=pi2jamma_input[0];
321 val=val & 0x00000ff;
322 mouse_xrel=(int)(signed char) val;
323  
324 val=pi2jamma_input[1];
325 val=val >> 24;
326 val=val & 0x00000ff;
327 mouse_yrel=(int)(signed char) val;
328  
329 // ist etwas passiert?
330 if( mouse_xrel!=0 || mouse_yrel != 0 )
331 sendRel(uidev_fd_mouse1, mouse_xrel,mouse_yrel ); //faktor test - versuch
332  
333  
334 //mouse2
335 val=pi2jamma_input[1];
336 val=val >> 16;
337 val=val & 0x000000ff;
338 mouse_xrel=(int)(signed char) val;
339  
340 val=pi2jamma_input[1];
341 val=val >> 8;
342 val=val & 0x00000ff;
343 mouse_yrel=(int)(signed char) val;
344  
345 // ist etwas passiert?
346 if( mouse_xrel!=0 || mouse_yrel != 0 )
347 sendRel(uidev_fd_mouse2, mouse_xrel,mouse_yrel ); //faktor test - versuch
348  
349 }
350  
351  
352  
353  
354  
355  
356  
357  
358  
359  
360 void joy_handle_event(void)
361 {
362 int Joystick = 0;
363 int Index;
364  
365  
366  
367  
368  
369  
370 if(joy_data[Joystick].change_mask){
371 joy_data[Joystick].change_mask = 0;
372 for (Index = 0; Index < joy_data[Joystick].num_buttons; ++Index) {
373 if( joy_data[Joystick].change[Index] ){
374 joy_data[Joystick].change[Index] = 0;
375 //printf("Button %d = %d\n", Index, joy_data[Joystick].buttons[Index]);
376 send_gpio_keys(gpio_pin(Index), joy_data[Joystick].buttons[Index]);
377 // printf("%i %i\n",gpio_pin(Index), joy_data[Joystick].buttons[Index] );
378  
379 }
380 }
381 }
382 }
383  
384  
385 #endif
386