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