Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
41 | ligi | 1 | #include <stdio.h> |
2 | #include <stdlib.h> |
||
3 | #include <string.h> |
||
4 | #include <fcntl.h> |
||
5 | #include "lib/x52/x52.h" |
||
6 | #include <unistd.h> |
||
7 | #include <sys/socket.h> |
||
8 | #include <bluetooth/bluetooth.h> |
||
9 | #include <bluetooth/hci.h> |
||
10 | #include <bluetooth/hci_lib.h> |
||
11 | |||
12 | |||
43 | ligi | 13 | #include <bluetooth/rfcomm.h> |
41 | ligi | 14 | |
43 | ligi | 15 | |
41 | ligi | 16 | #include <linux/joystick.h> |
17 | #define JOY_DEV "/dev/input/js0" |
||
18 | |||
19 | #define MAX_BT_DEVICES 3 |
||
20 | |||
21 | int bt_device_count=0; |
||
22 | |||
23 | char names[MAX_BT_DEVICES][248]; |
||
24 | char addrs[MAX_BT_DEVICES][19]; |
||
25 | |||
43 | ligi | 26 | int s, status; |
27 | unsigned char TxBuffer[150]; |
||
28 | unsigned char _TxBuffer[150]; |
||
29 | |||
30 | #define STATEID_SCANNING 0 |
||
31 | #define STATEID_CONNECTING 1 |
||
32 | int state=STATEID_SCANNING; |
||
33 | |||
34 | #define BUTTON_SELECT 26 |
||
35 | #define BUTTON_DOWN 27 |
||
36 | #define BUTTON_UP 28 |
||
37 | |||
38 | struct x52 *x52_output; |
||
39 | |||
40 | int selected_bt_device=0; |
||
41 | ligi | 41 | void scan_bt() |
42 | { |
||
43 | inquiry_info *ii = NULL; |
||
44 | |||
45 | int dev_id, sock, len, flags; |
||
46 | int i; |
||
47 | char addr[19] = { 0 }; |
||
48 | char name[248] = { 0 }; |
||
49 | |||
50 | dev_id = hci_get_route(NULL); |
||
51 | sock = hci_open_dev( dev_id ); |
||
52 | if (dev_id < 0 || sock < 0) { |
||
53 | perror("opening socket"); |
||
54 | exit(1); |
||
55 | } |
||
56 | |||
57 | len = 8; |
||
58 | |||
59 | flags = IREQ_CACHE_FLUSH; |
||
60 | ii = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info)); |
||
61 | |||
62 | bt_device_count = hci_inquiry(dev_id, len, MAX_BT_DEVICES, NULL, &ii, flags); |
||
63 | if( bt_device_count < 0 ) perror("hci_inquiry"); |
||
64 | |||
65 | for (i = 0; i < bt_device_count; i++) { |
||
66 | ba2str(&(ii+i)->bdaddr, addr); |
||
67 | sprintf(addrs[i],"%s",addr); |
||
68 | |||
69 | memset(name, 0, sizeof(name)); |
||
70 | |||
71 | if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), |
||
72 | name, 0) < 0) |
||
73 | sprintf(names[i],"[unknown]"); |
||
74 | else |
||
75 | sprintf(names[i],"%s",name); |
||
76 | |||
77 | } |
||
78 | |||
79 | |||
80 | free( ii ); |
||
81 | close( sock ); |
||
82 | } |
||
83 | |||
43 | ligi | 84 | int x52_input_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x; |
85 | char *button=NULL,*button_trigger=NULL, name_of_joystick[80]; |
||
41 | ligi | 86 | |
43 | ligi | 87 | struct js_event x52_event_struct; |
41 | ligi | 88 | |
43 | ligi | 89 | void connect_joy() |
90 | { |
||
41 | ligi | 91 | |
43 | ligi | 92 | |
93 | if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 ) |
||
94 | { |
||
95 | printf( "Couldn't open joystick device %s\n", JOY_DEV ); |
||
96 | return ; |
||
97 | } |
||
98 | |||
99 | ioctl( x52_input_fd, JSIOCGAXES, &num_of_axis ); |
||
100 | ioctl( x52_input_fd, JSIOCGBUTTONS, &num_of_buttons ); |
||
101 | ioctl( x52_input_fd, JSIOCGNAME(80), &name_of_joystick ); |
||
102 | |||
103 | axis = (int *) calloc( num_of_axis, sizeof( int ) ); |
||
104 | button = (char *)calloc( num_of_buttons, sizeof( char ) ); |
||
105 | button_trigger = (char *) calloc( num_of_buttons, sizeof( char ) ); |
||
106 | |||
107 | printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n" |
||
108 | , name_of_joystick |
||
109 | , num_of_axis |
||
110 | , num_of_buttons ); |
||
111 | |||
112 | fcntl( x52_input_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */ |
||
113 | |||
114 | } |
||
115 | |||
116 | |||
117 | struct |
||
41 | ligi | 118 | { |
43 | ligi | 119 | char val[4]; |
120 | } MotortestParam; |
||
41 | ligi | 121 | |
122 | |||
43 | ligi | 123 | void AddCRC(unsigned int wieviele) |
124 | { |
||
125 | unsigned int tmpCRC = 0,i; |
||
126 | for(i = 0; i < wieviele;i++) |
||
127 | { |
||
128 | tmpCRC += TxBuffer[i]; |
||
129 | } |
||
130 | tmpCRC %= 4096; |
||
131 | TxBuffer[i++] = '=' + tmpCRC / 64; |
||
132 | TxBuffer[i++] = '=' + tmpCRC % 64; |
||
133 | TxBuffer[i++] = '\r'; |
||
134 | } |
||
41 | ligi | 135 | |
43 | ligi | 136 | void SendOutData(unsigned char cmd,unsigned char modul, int len) |
137 | { |
||
138 | unsigned int pt = 0,ptr=0,i; |
||
139 | int a,b,c; |
||
140 | TxBuffer[pt++] = '#'; // Startzeichen |
||
141 | TxBuffer[pt++] = modul; // Adresse (a=0; b=1,...) |
||
142 | TxBuffer[pt++] = cmd; // Commando |
||
143 | |||
144 | while(len) |
||
41 | ligi | 145 | { |
43 | ligi | 146 | if(len) { a = _TxBuffer[ptr++]; len--;} else a = 0; |
147 | if(len) { b = _TxBuffer[ptr++]; len--;} else b = 0; |
||
148 | if(len) { c = _TxBuffer[ptr++]; len--;} else c = 0; |
||
149 | TxBuffer[pt++] = '=' + (a >> 2); |
||
150 | TxBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
151 | TxBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
152 | TxBuffer[pt++] = '=' + ( c & 0x3f); |
||
41 | ligi | 153 | } |
43 | ligi | 154 | |
155 | status = send(s,"\r" , 1, 0); |
||
156 | i=0; |
||
157 | while(TxBuffer[i] !='\r' && i<150) |
||
158 | { |
||
159 | // TxBuffer[i]='#'; |
||
160 | status = send(s,&TxBuffer[i] , 1, 0); |
||
161 | printf(" +%d%c ",i,TxBuffer[i]); |
||
162 | i++; |
||
163 | } |
||
164 | |||
165 | status = send(s,"\r" , 1, 0); |
||
166 | |||
167 | printf("\n"); |
||
168 | AddCRC(pt); |
||
169 | printf("Sending to MK\n"); |
||
170 | |||
171 | } |
||
41 | ligi | 172 | |
173 | |||
43 | ligi | 174 | int engines_on=0; |
175 | int old_engines_on=0; |
||
41 | ligi | 176 | |
43 | ligi | 177 | |
178 | void write_display(int line,char* text) |
||
179 | { |
||
180 | if (x52_output) x52_settext(x52_output, line , text, strlen(text)); |
||
181 | } |
||
182 | |||
183 | void clear_display() |
||
184 | { |
||
185 | write_display(0,""); |
||
186 | write_display(1,""); |
||
187 | write_display(2,""); |
||
188 | } |
||
189 | |||
190 | |||
191 | void output_device_list() |
||
192 | { |
||
193 | int i; |
||
194 | char disp_txt[20]; |
||
195 | for(i=0;i<bt_device_count;i++) |
||
196 | { |
||
197 | if (i<3) |
||
198 | { |
||
199 | |||
200 | if (selected_bt_device==i) |
||
201 | sprintf(disp_txt,"#%s",names[i]); |
||
202 | else |
||
203 | sprintf(disp_txt," %s",names[i]); |
||
204 | write_display(i,disp_txt); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | |||
209 | void connect_mk(char dest[18]) |
||
210 | { |
||
211 | struct sockaddr_rc addr ; |
||
212 | |||
213 | // allocate a socket |
||
214 | s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
||
215 | |||
216 | // set the connection parameters (who to connect to) |
||
217 | addr.rc_family = AF_BLUETOOTH; |
||
218 | addr.rc_channel = 1; |
||
219 | str2ba( dest, &addr.rc_bdaddr ); |
||
220 | |||
221 | // connect to server |
||
222 | status = connect(s, (struct sockaddr *)&addr, sizeof(addr)); |
||
223 | |||
224 | } |
||
225 | |||
226 | |||
227 | |||
228 | |||
229 | |||
230 | int main(int argc, char**argv) |
||
231 | { |
||
232 | printf("Starting Riddim \n"); |
||
233 | printf("\tRemote Interactive Digital Drone Interface Mashup\n"); |
||
234 | |||
235 | //int tmp=2; |
||
236 | // connect_mk("00:0B:CE:01:6B:4F"); |
||
237 | |||
238 | /* |
||
239 | |||
240 | { |
||
241 | // send a message |
||
242 | |||
243 | MotortestParam.val[0]=tmp; |
||
244 | MotortestParam.val[1]=tmp; |
||
245 | MotortestParam.val[2]=tmp; |
||
246 | MotortestParam.val[3]=tmp; |
||
247 | |||
248 | engines_on=1; |
||
249 | |||
250 | |||
41 | ligi | 251 | else |
43 | ligi | 252 | engines_on=0; |
253 | |||
254 | if (engines_on!=old_engines_on) |
||
255 | { |
||
256 | int c; |
||
257 | |||
258 | if( 0 == status )for (c=0;c<10;c++) |
||
259 | int c; |
||
260 | for (c=0;c<2;c++) |
||
261 | SendOutData('t', 0, &MotortestParam, sizeof(MotortestParam)); |
||
262 | |||
41 | ligi | 263 | |
43 | ligi | 264 | |
265 | sleep(1); |
||
266 | } |
||
267 | */ |
||
41 | ligi | 268 | |
43 | ligi | 269 | |
270 | if( status < 0 ) perror("uh oh"); |
||
271 | printf ("send status %d",status); |
||
272 | // close(s); |
||
273 | |||
274 | int i; |
||
275 | /* |
||
276 | |||
277 | MotortestParam.val[0]=2; |
||
278 | MotortestParam.val[1]=2; |
||
279 | MotortestParam.val[2]=2; |
||
280 | MotortestParam.val[3]=2; |
||
281 | |||
282 | |||
283 | |||
284 | |||
285 | while(1) |
||
286 | { |
||
287 | initSerial("/dev/rfcomm0"); |
||
288 | sleep(2); |
||
289 | SendOutData('t', 0, &MotortestParam, sizeof(MotortestParam)); |
||
290 | sleep(1); |
||
291 | } |
||
292 | */ |
||
293 | |||
294 | |||
295 | printf("\nInitializing X-52 input ..\n"); |
||
296 | connect_joy(); |
||
297 | printf(".. done"); |
||
298 | |||
299 | printf("\nInitializing X-52 output .."); |
||
300 | |||
301 | x52_output = x52_init(); |
||
302 | |||
303 | clear_display(); |
||
304 | |||
305 | write_display(0, "RIDDIM active"); |
||
306 | |||
307 | if (x52_output) x52_setbri(x52_output, 1,128); |
||
308 | if (x52_output) |
||
309 | printf(" done \n"); |
||
310 | else |
||
311 | printf(" not found \n"); |
||
312 | |||
313 | |||
314 | |||
315 | printf("Scanning for Bluetooth Devices ..\n"); |
||
316 | write_display(1,"Bluetooth Scan"); |
||
317 | |||
318 | |||
319 | scan_bt(); |
||
41 | ligi | 320 | printf(" done \n"); |
321 | printf(" %d Devices found \n",bt_device_count); |
||
43 | ligi | 322 | |
323 | |||
41 | ligi | 324 | for(i=0;i<bt_device_count;i++) |
43 | ligi | 325 | { |
326 | printf(" %d -> %s (%s) \n",i,names[i],addrs[i]); |
||
327 | if (i<3) write_display(i,names[i]); |
||
328 | } |
||
329 | |||
330 | output_device_list() ; |
||
41 | ligi | 331 | |
332 | |||
43 | ligi | 333 | int v_old; |
41 | ligi | 334 | while( 1 ) |
335 | { |
||
43 | ligi | 336 | int polls=0; |
337 | for (polls=0;polls<1000;polls++) |
||
338 | { |
||
339 | read(x52_input_fd, &x52_event_struct, sizeof(struct js_event)); |
||
340 | |||
341 | |||
342 | /* see what to do with the event */ |
||
343 | switch (x52_event_struct.type & ~JS_EVENT_INIT) |
||
344 | { |
||
345 | case JS_EVENT_AXIS: |
||
346 | axis [ x52_event_struct.number ] = x52_event_struct.value; |
||
347 | break; |
||
348 | case JS_EVENT_BUTTON: |
||
349 | button [ x52_event_struct.number ] = x52_event_struct.value; |
||
350 | break; |
||
351 | } |
||
352 | } |
||
353 | if (1) |
||
354 | { |
||
355 | |||
356 | } |
||
357 | |||
358 | for( x=0 ; x<num_of_buttons ; ++x ) |
||
359 | if( button[x]==0) |
||
360 | button_trigger[x]=0; |
||
361 | else |
||
362 | { |
||
363 | if (button_trigger[x]<100)button_trigger[x]++; |
||
364 | } |
||
365 | |||
366 | |||
367 | |||
368 | switch(state) |
||
369 | { |
||
370 | case STATEID_SCANNING: |
||
371 | if (button_trigger[BUTTON_SELECT]==1) |
||
372 | { |
||
373 | state=STATEID_CONNECTING; |
||
374 | clear_display(); |
||
375 | write_display(0,"connecting to"); |
||
376 | write_display(1,names[selected_bt_device]); |
||
377 | connect_mk(addrs[selected_bt_device]); |
||
378 | write_display(0,"connected to"); |
||
379 | } |
||
380 | |||
381 | if ((button_trigger[BUTTON_UP]+button_trigger[BUTTON_DOWN])==1) |
||
382 | { |
||
383 | printf("-> sel_dev %d - %d\n",selected_bt_device,button_trigger[19]); |
||
384 | if (button_trigger[BUTTON_DOWN]==1) |
||
385 | if (selected_bt_device>0) selected_bt_device--; |
||
386 | if (button_trigger[BUTTON_UP]==1) |
||
387 | if (selected_bt_device<bt_device_count-1) selected_bt_device++; |
||
388 | |||
389 | output_device_list() ; |
||
390 | } |
||
391 | break; |
||
392 | case STATEID_CONNECTING: |
||
393 | |||
394 | |||
395 | _TxBuffer[0]=(axis[2]>>8)*(-1)+127; |
||
396 | if (button[7]!=1)_TxBuffer[0] =0; |
||
397 | |||
398 | _TxBuffer[1]=_TxBuffer[0]; |
||
399 | _TxBuffer[2]=_TxBuffer[0]; |
||
400 | _TxBuffer[3]=_TxBuffer[0]; |
||
401 | |||
402 | SendOutData('t', 0, 4); |
||
403 | |||
404 | |||
405 | sleep(0.05); |
||
406 | |||
407 | |||
408 | |||
409 | int v=axis[6]/655+50; |
||
410 | if (v!=v_old)if (x52_output) x52_setbri(x52_output, 0,v ); |
||
411 | v_old=v; |
||
412 | |||
413 | printf("v: %d \r",v); |
||
414 | |||
415 | |||
416 | for (i=0;i<num_of_axis;i++) |
||
417 | printf("A%d: %d ", i,axis[i]>>8 ); |
||
418 | |||
419 | for( x=0 ; x<num_of_buttons ; ++x ) |
||
420 | |||
421 | printf("B%d: %d ", x, button[x] ); |
||
422 | |||
423 | break; |
||
424 | } |
||
425 | /* |
||
426 | |||
427 | */ |
||
428 | printf("\r"); |
||
429 | fflush(stdout); |
||
430 | |||
431 | /* |
||
41 | ligi | 432 | if( read( x52_input_fd, &x52_input_struct, JS_RETURN ) != JS_RETURN ) |
433 | { |
||
434 | printf( "\nFailed to read from Joystick\n" ); |
||
435 | } |
||
436 | |||
43 | ligi | 437 | |
41 | ligi | 438 | if (x52_output) x52_setbri(x52_output, 1,x52_input_struct.y ); |
43 | ligi | 439 | // if (x52_output) x52_setbri(x52_output, 2,x52_input_struct.x ); |
41 | ligi | 440 | |
441 | |||
43 | ligi | 442 | */ |
443 | } |
||
444 | |||
41 | ligi | 445 | close(x52_input_fd); /* too bad we never get here */ |
446 | |||
447 | if (x52_output) x52_close(x52_output); |
||
448 | return 0; |
||
449 | } |