Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
127 | ligi | 1 | /************************************************** |
2 | * |
||
3 | * |
||
4 | * Riddim |
||
5 | * Remote Interactive Digital Drone Interface Mashup |
||
6 | * |
||
7 | * 2007-2008 Marcus -LiGi- Bueschleb |
||
8 | * |
||
9 | * |
||
130 | ligi | 10 | **************************************************/ |
125 | ligi | 11 | |
127 | ligi | 12 | #include "riddim.h" |
41 | ligi | 13 | |
14 | |||
50 | ligi | 15 | |
127 | ligi | 16 | int state=STATEID_SCANNING; |
41 | ligi | 17 | |
43 | ligi | 18 | |
41 | ligi | 19 | |
20 | |||
47 | ligi | 21 | |
22 | |||
23 | |||
50 | ligi | 24 | |
117 | ligi | 25 | |
130 | ligi | 26 | int x52_input_fd, num_of_axis=0, num_of_buttons=0; |
27 | char *button_trigger=NULL, name_of_joystick[80]; |
||
117 | ligi | 28 | |
47 | ligi | 29 | struct js_event x52_event_struct; |
30 | |||
48 | ligi | 31 | int engines_on=0; |
32 | int old_engines_on=0; |
||
33 | |||
130 | ligi | 34 | int *axis; |
35 | char *button; |
||
48 | ligi | 36 | |
130 | ligi | 37 | |
38 | |||
127 | ligi | 39 | struct x52 *x52_output; |
47 | ligi | 40 | |
127 | ligi | 41 | int selected_bt_device=0; |
47 | ligi | 42 | |
130 | ligi | 43 | //int evdev_fd; |
44 | //int evdev_out_fd; |
||
47 | ligi | 45 | |
43 | ligi | 46 | |
47 | void write_display(int line,char* text) |
||
48 | { |
||
49 | if (x52_output) x52_settext(x52_output, line , text, strlen(text)); |
||
50 | } |
||
51 | |||
52 | void clear_display() |
||
53 | { |
||
54 | write_display(0,""); |
||
55 | write_display(1,""); |
||
56 | write_display(2,""); |
||
57 | } |
||
58 | |||
59 | |||
60 | void output_device_list() |
||
61 | { |
||
62 | int i; |
||
63 | char disp_txt[20]; |
||
64 | for(i=0;i<bt_device_count;i++) |
||
65 | { |
||
66 | if (i<3) |
||
67 | { |
||
68 | |||
69 | if (selected_bt_device==i) |
||
70 | sprintf(disp_txt,"#%s",names[i]); |
||
71 | else |
||
72 | sprintf(disp_txt," %s",names[i]); |
||
73 | write_display(i,disp_txt); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
127 | ligi | 78 | |
79 | void print_device_list() |
||
43 | ligi | 80 | { |
127 | ligi | 81 | int i; |
82 | for(i=0;i<bt_device_count;i++) |
||
83 | printf("device%i->%s\n",i,names[i]); |
||
43 | ligi | 84 | } |
85 | |||
86 | |||
130 | ligi | 87 | |
50 | ligi | 88 | int count=0; |
89 | int connected=0; |
||
43 | ligi | 90 | |
117 | ligi | 91 | |
92 | int input=INPUT_NONE; |
||
93 | |||
125 | ligi | 94 | |
43 | ligi | 95 | int main(int argc, char**argv) |
96 | { |
||
125 | ligi | 97 | |
43 | ligi | 98 | printf("Starting Riddim \n"); |
99 | printf("\tRemote Interactive Digital Drone Interface Mashup\n"); |
||
127 | ligi | 100 | printf("\nusage:\n"); |
101 | printf("\t riddim [config_file]\n\n"); |
||
125 | ligi | 102 | |
130 | ligi | 103 | // bt_host_init(); |
127 | ligi | 104 | |
105 | |||
130 | ligi | 106 | if (argv[1]) |
107 | parse_config(argv[1]); |
||
108 | else |
||
109 | parse_config("/etc/riddim.conf"); |
||
125 | ligi | 110 | |
111 | |||
130 | ligi | 112 | |
113 | printf("input %s:\n",input_evdev_name); |
||
114 | /* |
||
115 | if (bluetooth_mac) |
||
50 | ligi | 116 | { |
130 | ligi | 117 | |
118 | printf("Connecting via Bluetooth to %s\n",bluetooth_mac); |
||
119 | if (connect_mk_bluetooth(bluetooth_mac));; |
||
120 | connected=TRUE; |
||
127 | ligi | 121 | } |
130 | ligi | 122 | */ |
127 | ligi | 123 | if (mk_socket_port) |
124 | { |
||
130 | ligi | 125 | printf("connecting to local port: %i\n",mk_socket_port); |
126 | |||
127 | ligi | 127 | if (connect_mk_localhost_socket(mk_socket_port)==-1) |
128 | printf("cant connect !!"); |
||
129 | else |
||
50 | ligi | 130 | { |
130 | ligi | 131 | printf("connected !-)"); |
127 | ligi | 132 | connected=TRUE; |
50 | ligi | 133 | } |
134 | } |
||
127 | ligi | 135 | |
130 | ligi | 136 | // todo reenable bluetooth connection |
117 | ligi | 137 | |
130 | ligi | 138 | |
139 | /* |
||
43 | ligi | 140 | printf("\nInitializing X-52 input ..\n"); |
117 | ligi | 141 | if (connect_joy()) |
142 | { |
||
130 | ligi | 143 | printf(".. done");// |
144 | input=INPUT_JOYDEV; |
||
117 | ligi | 145 | } |
146 | else |
||
147 | printf(".. ERROR ");// |
||
130 | ligi | 148 | */ |
43 | ligi | 149 | |
130 | ligi | 150 | if (input_evdev_name) |
117 | ligi | 151 | { |
130 | ligi | 152 | printf("\nInitializing evdev input (%s) ..\n",input_evdev_name); |
153 | |||
154 | if (connect_evdev(input_evdev_name)) |
||
155 | { |
||
156 | printf(".. done");// |
||
157 | input=INPUT_EVDEV; |
||
158 | } |
||
159 | else |
||
160 | printf(".. ERROR ");// |
||
117 | ligi | 161 | } |
130 | ligi | 162 | |
43 | ligi | 163 | printf("\nInitializing X-52 output .."); |
164 | |||
47 | ligi | 165 | x52_output = x52_init(); |
166 | |||
167 | clear_display(); |
||
43 | ligi | 168 | |
47 | ligi | 169 | write_display(0, "RIDDIM active"); |
43 | ligi | 170 | |
47 | ligi | 171 | if (x52_output) x52_setbri(x52_output, 1,128); |
172 | if (x52_output) |
||
173 | printf(" done \n"); |
||
174 | else |
||
175 | printf(" not found \n"); |
||
43 | ligi | 176 | |
127 | ligi | 177 | /* |
130 | ligi | 178 | if (!connected) |
43 | ligi | 179 | { |
130 | ligi | 180 | printf("Scanning for Bluetooth Devices ..\n"); |
181 | write_display(1,"Bluetooth Scan"); |
||
182 | scan_bt(); |
||
183 | printf(" done \n"); |
||
184 | printf(" %d Devices found \n",bt_device_count); |
||
185 | print_device_list() ; |
||
43 | ligi | 186 | } |
127 | ligi | 187 | */ |
41 | ligi | 188 | |
130 | ligi | 189 | // int v_old; |
117 | ligi | 190 | int polls=0; |
127 | ligi | 191 | |
130 | ligi | 192 | |
127 | ligi | 193 | if (exit_after_init) |
194 | exit(0); |
||
117 | ligi | 195 | printf("starting loop ..\n"); |
196 | |||
197 | |||
130 | ligi | 198 | |
127 | ligi | 199 | int complete_misses=0; |
130 | ligi | 200 | int complete_matches=0; |
117 | ligi | 201 | |
130 | ligi | 202 | |
127 | ligi | 203 | int confirm_misses; |
130 | ligi | 204 | |
205 | |||
206 | |||
207 | init_evdevstatus_led(); |
||
208 | |||
209 | while( TRUE ) |
||
41 | ligi | 210 | { |
117 | ligi | 211 | |
130 | ligi | 212 | blink_evdev_led(); |
213 | // bt_host_tick(mk_socket); |
||
127 | ligi | 214 | usleep(loop_delay); |
117 | ligi | 215 | switch (input) |
43 | ligi | 216 | { |
127 | ligi | 217 | |
117 | ligi | 218 | |
219 | case INPUT_NONE: |
||
127 | ligi | 220 | printf("starting input none\n"); |
117 | ligi | 221 | break; |
43 | ligi | 222 | |
117 | ligi | 223 | case INPUT_EVDEV: |
130 | ligi | 224 | printf("input evdev\n"); |
117 | ligi | 225 | |
130 | ligi | 226 | |
227 | poll_evdev(); |
||
228 | |||
229 | break; |
||
117 | ligi | 230 | |
231 | case INPUT_JOYDEV: |
||
232 | printf("input joydev\n"); |
||
233 | // poll values from input device |
||
234 | |||
235 | for (polls=0;polls<100;polls++) // FIXME - better Polling |
||
236 | { |
||
237 | read(x52_input_fd, &x52_event_struct, sizeof(struct js_event)); |
||
238 | |||
239 | |||
240 | /* see what to do with the event */ |
||
241 | switch (x52_event_struct.type & ~JS_EVENT_INIT) |
||
242 | { |
||
243 | case JS_EVENT_AXIS: |
||
244 | axis [ x52_event_struct.number ] = x52_event_struct.value; |
||
245 | break; |
||
246 | case JS_EVENT_BUTTON: |
||
247 | button [ x52_event_struct.number ] = x52_event_struct.value; |
||
248 | break; |
||
249 | } |
||
250 | } |
||
130 | ligi | 251 | int x; |
117 | ligi | 252 | for( x=0 ; x<num_of_buttons ; ++x ) |
253 | if( button[x]==0) |
||
254 | button_trigger[x]=0; |
||
255 | else |
||
256 | { |
||
257 | if (button_trigger[x]<100)button_trigger[x]++; |
||
258 | } |
||
259 | break; |
||
43 | ligi | 260 | } |
261 | |||
117 | ligi | 262 | printf("input done\n"); |
43 | ligi | 263 | |
47 | ligi | 264 | switch(state) |
265 | { |
||
48 | ligi | 266 | |
267 | |||
47 | ligi | 268 | case STATEID_SCANNING: |
130 | ligi | 269 | |
50 | ligi | 270 | state=STATEID_CONNECTING; |
130 | ligi | 271 | /* |
48 | ligi | 272 | ExternControl.Digital[0]=0; |
273 | ExternControl.Digital[1]=0; |
||
274 | ExternControl.RemoteTasten=0; |
||
275 | ExternControl.Nick=(axis[1]>>8)*(-1)+127;; |
||
276 | |||
277 | printf("nick%d\n",ExternControl.Nick); |
||
278 | ExternControl.Roll=(axis[0]>>8)*(-1)+127;; |
||
279 | ExternControl.Gier=(axis[5]>>8)*(-1)+127;; |
||
280 | ExternControl.Gas=(axis[2]>>8)*(-1)+127; |
||
281 | ExternControl.Higt=0; |
||
282 | ExternControl.free=0; |
||
283 | ExternControl.Frame='t'; |
||
284 | ExternControl.Config=1; |
||
285 | |||
286 | printf("sending data\n"); |
||
287 | |||
50 | ligi | 288 | |
127 | ligi | 289 | if (connected)SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl)); |
50 | ligi | 290 | gettimeofday(&time_struct1,NULL); |
48 | ligi | 291 | |
47 | ligi | 292 | if (button_trigger[BUTTON_SELECT]==1) |
293 | { |
||
294 | state=STATEID_CONNECTING; |
||
295 | clear_display(); |
||
296 | write_display(0,"connecting to"); |
||
297 | write_display(1,names[selected_bt_device]); |
||
127 | ligi | 298 | //connect_mk(addrs[selected_bt_device]); |
47 | ligi | 299 | write_display(0,"connected to"); |
300 | } |
||
43 | ligi | 301 | |
47 | ligi | 302 | if ((button_trigger[BUTTON_UP]+button_trigger[BUTTON_DOWN])==1) |
303 | { |
||
304 | printf("-> sel_dev %d - %d\n",selected_bt_device,button_trigger[19]); |
||
305 | if (button_trigger[BUTTON_DOWN]==1) |
||
306 | if (selected_bt_device>0) selected_bt_device--; |
||
307 | if (button_trigger[BUTTON_UP]==1) |
||
308 | if (selected_bt_device<bt_device_count-1) selected_bt_device++; |
||
309 | |||
127 | ligi | 310 | |
47 | ligi | 311 | } |
130 | ligi | 312 | */ |
47 | ligi | 313 | break; |
48 | ligi | 314 | |
47 | ligi | 315 | case STATEID_CONNECTING: |
127 | ligi | 316 | |
50 | ligi | 317 | |
127 | ligi | 318 | confirm_misses=0; |
319 | printf("connected:%d",connected); |
||
130 | ligi | 320 | RxBuffer[1]=0; |
321 | if (connected) |
||
322 | |||
323 | |||
324 | while (RxBuffer[1]!='t') |
||
127 | ligi | 325 | { |
50 | ligi | 326 | |
127 | ligi | 327 | RxBuffer[1]=0; |
130 | ligi | 328 | read_from_mk(); |
329 | // bt_host_send(RxBuffer,rx_last_length); |
||
330 | printf("sending to host: %s",PrintableRxBuffer); |
||
50 | ligi | 331 | |
117 | ligi | 332 | |
127 | ligi | 333 | // ftime(&time_struct); |
130 | ligi | 334 | |
335 | printf("waiting for confirm frame ( confirmed:%d misses:%d )\n",complete_matches,complete_misses); |
||
127 | ligi | 336 | RxBuffer[2]=0; |
43 | ligi | 337 | |
127 | ligi | 338 | |
339 | |||
130 | ligi | 340 | // r=0; |
43 | ligi | 341 | |
117 | ligi | 342 | // new |
130 | ligi | 343 | /* |
117 | ligi | 344 | if (button_trigger[12]>1) |
345 | { |
||
346 | SendOutData('s', 0, (unsigned char *)&ExternControl, sizeof(ExternControl)); |
||
347 | button_trigger[12]=0; |
||
348 | } |
||
130 | ligi | 349 | */ |
350 | ExternControl.Frame='t'; |
||
117 | ligi | 351 | if (++confirm_misses>4) |
127 | ligi | 352 | { |
353 | complete_misses++; |
||
354 | printf("sending again\n"); |
||
355 | SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl)); |
||
356 | } |
||
48 | ligi | 357 | } |
127 | ligi | 358 | else |
359 | printf("not connected to mk\n"); |
||
130 | ligi | 360 | |
50 | ligi | 361 | gettimeofday(&time_struct2,NULL); |
43 | ligi | 362 | |
50 | ligi | 363 | printf("last trip: %d",(int)(time_struct1.tv_usec-time_struct2.tv_usec)); |
130 | ligi | 364 | // act_mode=button[24] | (button[25]<<1); |
43 | ligi | 365 | |
127 | ligi | 366 | |
367 | |||
368 | // Step converting axis data to nick/roll/gier/gas/.. |
||
369 | |||
130 | ligi | 370 | act_nick=evdev_rel_axis[rel_axis_nick]*nick_mul; |
371 | act_roll=evdev_rel_axis[rel_axis_roll]*roll_mul; |
||
372 | act_gier=evdev_rel_axis[rel_axis_gier]*gier_mul; |
||
373 | act_gas=evdev_rel_axis[rel_axis_gas]*gas_mul; |
||
127 | ligi | 374 | act_gas=255; |
375 | |||
376 | |||
377 | /* |
||
130 | ligi | 378 | switch (act_mode) |
50 | ligi | 379 | { |
380 | case 0: |
||
117 | ligi | 381 | |
382 | |||
130 | ligi | 383 | act_nick=(axis[AXIS_NICK])*(INVERT_NICK); |
384 | act_roll=(axis[AXIS_ROLL])*(INVERT_ROLL); |
||
385 | act_gier=(axis[AXIS_GIER])*(INVERT_GIER); |
||
386 | act_gas=((axis[AXIS_GAS])-128)*(-1); |
||
117 | ligi | 387 | |
130 | ligi | 388 | // clip gas |
389 | if (act_gas<0) act_gas=0; |
||
50 | ligi | 390 | |
130 | ligi | 391 | if (act_gas>250) act_gas=250; |
117 | ligi | 392 | |
130 | ligi | 393 | //////// act_gas=0; |
394 | break; |
||
50 | ligi | 395 | |
396 | case 1: |
||
130 | ligi | 397 | act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/2; |
398 | act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/2; |
||
399 | act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/2; |
||
400 | act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS); |
||
50 | ligi | 401 | |
130 | ligi | 402 | break; |
50 | ligi | 403 | |
404 | case 2: |
||
130 | ligi | 405 | act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/3; |
406 | act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/3; |
||
407 | act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/3; |
||
408 | act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS); |
||
50 | ligi | 409 | |
410 | |||
130 | ligi | 411 | break; |
50 | ligi | 412 | |
413 | } |
||
127 | ligi | 414 | */ |
48 | ligi | 415 | ExternControl.Digital[0]=0; |
416 | ExternControl.Digital[1]=0; |
||
417 | ExternControl.RemoteTasten=0; |
||
130 | ligi | 418 | ExternControl.Higt=0; |
419 | ExternControl.free=0; |
||
420 | ExternControl.Frame='t'; |
||
421 | ExternControl.Config=1; |
||
422 | |||
423 | |||
50 | ligi | 424 | ExternControl.Nick=act_nick; //(axis[1]>>8)*(-1)/2; |
117 | ligi | 425 | ExternControl.Roll=act_roll*(-1); //(axis[0]>>8)*(-1)/2; |
426 | ExternControl.Gier=act_gier; // ************ |
||
427 | ExternControl.Gas=act_gas; // ************ |
||
50 | ligi | 428 | |
130 | ligi | 429 | complete_matches++; |
430 | |||
117 | ligi | 431 | printf("---+++act_mode %d , act_nick %d , act_roll %d , act_gier %d , act_gas %d",act_mode , act_nick , act_roll , act_gier , act_gas); |
48 | ligi | 432 | |
127 | ligi | 433 | if (connected) |
434 | { |
||
435 | printf("sending data\n"); |
||
436 | |||
437 | SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl)); |
||
438 | gettimeofday(&time_struct1,NULL); |
||
439 | printf("sent data\n"); |
||
440 | } |
||
48 | ligi | 441 | |
130 | ligi | 442 | // printf("sleeping\n"); |
443 | // for (polls=0;polls<100;polls++) // FIXME - better Polling |
||
444 | // printf("end_sleep\n"); |
||
127 | ligi | 445 | |
130 | ligi | 446 | // int v=axis[6]/655+50; |
447 | // if (v!=v_old)if (x52_output) x52_setbri(x52_output, 0,v ); |
||
448 | // v_old=v; |
||
47 | ligi | 449 | |
130 | ligi | 450 | // printf("v: %d \n",v); |
127 | ligi | 451 | |
452 | |||
130 | ligi | 453 | /* |
454 | for (i=0;i<num_of_axis;i++) |
||
47 | ligi | 455 | printf("A%d: %d ", i,axis[i]>>8 ); |
456 | |||
130 | ligi | 457 | for( x=0 ; x<num_of_buttons ; ++x ) |
47 | ligi | 458 | |
459 | printf("B%d: %d ", x, button[x] ); |
||
130 | ligi | 460 | */ |
461 | |||
47 | ligi | 462 | break; |
463 | } |
||
127 | ligi | 464 | |
48 | ligi | 465 | printf("\n"); |
47 | ligi | 466 | fflush(stdout); |
130 | ligi | 467 | printf("loop fin ( confirmed:%d misses:%d | debug_sets:%d )\n",complete_matches,complete_misses,debug_sets); |
43 | ligi | 468 | |
50 | ligi | 469 | } |
41 | ligi | 470 | |
43 | ligi | 471 | |
47 | ligi | 472 | /******************** Cleanup **********************/ |
473 | close(x52_input_fd); |
||
127 | ligi | 474 | close(mk_socket); |
41 | ligi | 475 | |
476 | if (x52_output) x52_close(x52_output); |
||
477 | return 0; |
||
478 | } |