Subversion Repositories Projects

Rev

Rev 127 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 127 Rev 130
1
/**************************************************
1
/**************************************************
2
 *
2
 *
3
 *
3
 *
4
 * Riddim
4
 * Riddim
5
 * Remote Interactive Digital Drone Interface Mashup
5
 * Remote Interactive Digital Drone Interface Mashup
6
 *
6
 *
7
 * 2007-2008 Marcus -LiGi- Bueschleb
7
 * 2007-2008 Marcus -LiGi- Bueschleb
8
 *
8
 *
9
 *
9
 *
10
**************************************************/
10
 **************************************************/
11
 
11
 
12
#include "riddim.h"
12
#include "riddim.h"
13
 
-
 
14
#define FALSE 0
-
 
15
#define TRUE 1
-
 
16
 
13
 
17
#define test_bit(bit, array)    (array[bit/8] & (1<<(bit%8)))
14
 
18
 
15
 
19
int state=STATEID_SCANNING;
16
int state=STATEID_SCANNING;
20
 
17
 
21
 
-
 
22
// from config 
-
 
23
char *input_evdev_name;
-
 
24
int  mk_socket_port=0;
-
 
25
int  loop_delay=0;
18
 
26
 
19
 
27
 
20
 
28
 
-
 
29
double nick_mul=0.3f;
-
 
30
double roll_mul=0.3f;
-
 
31
double gier_mul=0.3f;
-
 
32
double gas_mul=0.3f;
-
 
33
 
-
 
34
 
-
 
35
int rel_axis_nick=1;
-
 
36
int rel_axis_roll=0;
-
 
37
int rel_axis_gier=5;
-
 
38
int rel_axis_gas=2;
-
 
39
 
-
 
40
 
-
 
41
cfg_bool_t exit_after_init = cfg_false;
-
 
42
 
-
 
43
// time struct for measuring
-
 
44
struct timeval time_struct1;
-
 
45
struct timeval time_struct2;
-
 
46
 
-
 
47
 
-
 
48
char RxBuffer[150];
-
 
49
 
-
 
50
 
-
 
51
int status;
21
 
52
 
22
 
53
 
23
 
54
 
24
 
55
 
25
 
56
int x52_input_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
26
int x52_input_fd, num_of_axis=0, num_of_buttons=0;
57
char *button=NULL,*button_trigger=NULL, name_of_joystick[80];
27
char *button_trigger=NULL, name_of_joystick[80];
58
 
28
 
59
struct js_event x52_event_struct;
29
struct js_event x52_event_struct;
60
 
30
 
61
int engines_on=0;
31
int engines_on=0;
62
int old_engines_on=0;
32
int old_engines_on=0;
63
 
-
 
64
char in_char;
-
 
65
 
-
 
66
struct x52 *x52_output;
-
 
67
 
-
 
68
int selected_bt_device=0;
-
 
69
 
-
 
70
 int yalv;           /* loop counter */
-
 
71
    size_t read_bytes;  /* how many bytes were read */
-
 
72
    struct input_event ev[64]; /* the events (up to 64 at once) */
-
 
73
 
33
 
74
int evdev_fd;
-
 
75
int evdev_out_fd;
-
 
76
 
-
 
77
int evdev_rw=TRUE;
-
 
78
 
-
 
79
int connect_evdev()
-
 
80
{
-
 
81
 
-
 
82
  struct input_devinfo {
-
 
83
    uint16_t bustype;
-
 
84
    uint16_t vendor;
-
 
85
    uint16_t product;
-
 
86
    uint16_t version;
-
 
87
  };
-
 
88
  struct input_devinfo device_info;
-
 
89
 
-
 
90
  if ((evdev_out_fd = open(input_evdev_name, O_WRONLY)) < 0)
-
 
91
    {
-
 
92
      printf(" cant open evdev read/write - trying readonly\n");
-
 
93
      evdev_rw=FALSE;
-
 
94
    }
-
 
95
  if ((evdev_fd = open(input_evdev_name, O_RDONLY)) < 0)
-
 
96
    {
-
 
97
      printf(" cant open evdev !");
-
 
98
      return 0;
-
 
99
    }
-
 
100
 
-
 
101
  ioctl(evdev_fd,EVIOCGID,&device_info);
-
 
102
   
-
 
103
  printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n",
-
 
104
         device_info.vendor, device_info.product,
-
 
105
         device_info.version);
-
 
106
 
-
 
107
 
-
 
108
  char name[256]= "Unknown";
-
 
109
 
-
 
110
  if(ioctl(evdev_fd, EVIOCGNAME(sizeof(name)), name) < 0) {
-
 
111
    perror("evdev ioctl");
-
 
112
  }
-
 
113
 
-
 
114
  printf("EVDEV reports name: %s\n", name);
-
 
115
 
-
 
116
  /* this macro is used to tell if "bit" is set in "array"
-
 
117
   * it selects a byte from the array, and does a boolean AND
-
 
118
   * operation with a byte that only has the relevant bit set.
-
 
119
   * eg. to check for the 12th bit, we do (array[1] & 1<<4)
-
 
120
   */
-
 
121
 
-
 
122
 
-
 
123
    uint8_t evtype_bitmask[EV_MAX/8 + 1];
-
 
124
 
-
 
125
    if(ioctl(evdev_fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0)
-
 
126
      perror("evdev ioctl");
-
 
127
 
-
 
128
     printf("Supported event types:\n");
-
 
129
     for (yalv = 0; yalv < EV_MAX; yalv++) {
-
 
130
       if (test_bit(yalv, evtype_bitmask)) {
-
 
131
         /* this means that the bit is set in the event types list */
-
 
132
         printf("  Event type 0x%02x ", yalv);
-
 
133
         switch ( yalv)
-
 
134
           {
-
 
135
           case EV_KEY :
-
 
136
             printf(" (Keys or Buttons)\n");
-
 
137
             break;
-
 
138
           case EV_ABS :
-
 
139
             printf(" (Absolute Axes)\n");
-
 
140
             break;
-
 
141
           case EV_LED :
-
 
142
             printf(" (LEDs)\n");
-
 
143
             break;
-
 
144
           case EV_REP :
-
 
145
             printf(" (Repeat)\n");
-
 
146
             break;
-
 
147
           case EV_SYN :
-
 
148
             printf(" (Sync?)\n");
-
 
149
             break;
-
 
150
           case EV_REL :
-
 
151
             printf(" (Relative Axis)\n");
-
 
152
             break;
-
 
153
           case EV_MSC :
-
 
154
             printf(" (Misc)\n");
-
 
155
             break;
-
 
156
           default:
-
 
157
             printf(" (Unknown event type: 0x%04hx)\n", yalv);
-
 
158
           }}}
-
 
159
 
-
 
160
  return 1;
-
 
161
}
-
 
162
 
-
 
163
int connect_joy()
-
 
164
{
-
 
165
 
-
 
166
  axis = (int *) calloc( 100, sizeof( int ) );
-
 
167
  button = (char *)calloc( 100, sizeof( char ) );
-
 
168
  button_trigger = (char *) calloc( 100, sizeof( char ) );
-
 
169
 
-
 
170
 
-
 
171
  //  axis = (int *) calloc( num_of_axis, sizeof( int ) );
-
 
172
  // button = (char *)calloc( num_of_buttons, sizeof( char ) );
-
 
173
  // button_trigger = (char *) calloc( num_of_buttons, sizeof( char ) );
-
 
174
 
-
 
175
 
-
 
176
  if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
-
 
177
    {
-
 
178
      printf( "Couldn't open joystick device %s\n", JOY_DEV );
-
 
179
      printf( "try modprobe joydev\n"  );
34
int *axis;
180
      return 0;
-
 
181
    }
-
 
182
 
-
 
183
  ioctl( x52_input_fd, JSIOCGAXES, &num_of_axis );
-
 
184
  ioctl( x52_input_fd, JSIOCGBUTTONS, &num_of_buttons );
-
 
185
  ioctl( x52_input_fd, JSIOCGNAME(80), &name_of_joystick );
-
 
186
 
-
 
187
  printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
-
 
188
         , name_of_joystick
-
 
189
         , num_of_axis
-
 
190
         , num_of_buttons );
-
 
191
 
-
 
192
  fcntl( x52_input_fd, F_SETFL, O_NONBLOCK );   /* use non-blocking mode */
-
 
193
 
-
 
194
  return 1;
-
 
195
}
35
char *button;
196
 
36
 
197
 
37
 
-
 
38
 
-
 
39
struct x52 *x52_output;
-
 
40
 
-
 
41
int selected_bt_device=0;
198
 
42
 
199
 
43
//int evdev_fd;
200
 
44
//int evdev_out_fd;
201
 
45
 
202
 
46
 
203
void write_display(int line,char* text)
47
void write_display(int line,char* text)
204
{
48
{
205
  if (x52_output) x52_settext(x52_output, line , text, strlen(text));
49
  if (x52_output) x52_settext(x52_output, line , text, strlen(text));
206
}
50
}
207
 
51
 
208
void clear_display()
52
void clear_display()
209
{
53
{
210
  write_display(0,"");
54
  write_display(0,"");
211
  write_display(1,"");
55
  write_display(1,"");
212
  write_display(2,"");
56
  write_display(2,"");
213
}
57
}
214
 
58
 
215
 
59
 
216
void output_device_list()
60
void output_device_list()
217
{
61
{
218
  int i;
62
  int i;
219
  char disp_txt[20];
63
  char disp_txt[20];
220
  for(i=0;i<bt_device_count;i++)
64
  for(i=0;i<bt_device_count;i++)
221
    {
65
    {
222
      if (i<3)
66
      if (i<3)
223
        {
67
        {
224
         
68
         
225
          if (selected_bt_device==i)
69
          if (selected_bt_device==i)
226
            sprintf(disp_txt,"#%s",names[i]);
70
            sprintf(disp_txt,"#%s",names[i]);
227
          else
71
          else
228
            sprintf(disp_txt," %s",names[i]);
72
            sprintf(disp_txt," %s",names[i]);
229
          write_display(i,disp_txt);
73
          write_display(i,disp_txt);
230
        }
74
        }
231
    }
75
    }
232
}
76
}
233
 
77
 
234
 
78
 
235
void print_device_list()
79
void print_device_list()
236
{
80
{
237
  int i;
81
  int i;
238
  for(i=0;i<bt_device_count;i++)
82
  for(i=0;i<bt_device_count;i++)
239
    printf("device%i->%s\n",i,names[i]);
83
    printf("device%i->%s\n",i,names[i]);
240
}
84
}
241
 
85
 
242
 
86
 
243
int r=0;
87
 
244
int count=0;
88
int count=0;
245
int connected=0;
89
int connected=0;
246
 
90
 
247
 
91
 
248
int input=INPUT_NONE;
92
int input=INPUT_NONE;
249
 
93
 
250
 
94
 
251
int main(int argc, char**argv)
95
int main(int argc, char**argv)
252
{
96
{
253
 
97
 
254
  printf("Starting Riddim \n");
98
  printf("Starting Riddim \n");
255
  printf("\tRemote Interactive Digital Drone Interface Mashup\n");
99
  printf("\tRemote Interactive Digital Drone Interface Mashup\n");
256
  printf("\nusage:\n");
100
  printf("\nusage:\n");
257
  printf("\t riddim [config_file]\n\n");
101
  printf("\t riddim [config_file]\n\n");
-
 
102
 
258
 
-
 
259
 
-
 
260
  cfg_opt_t opts[] = {
-
 
261
 
-
 
262
    CFG_SIMPLE_BOOL("exit_after_init", &exit_after_init),
-
 
263
    CFG_SIMPLE_STR("input_evdev", &input_evdev_name),
-
 
264
    CFG_SIMPLE_INT("loop_delay", &loop_delay),
-
 
265
    CFG_SIMPLE_INT("mk_socket_port", &mk_socket_port),
-
 
266
 
-
 
267
    CFG_SIMPLE_FLOAT("nick_mul", &nick_mul),
-
 
268
    CFG_SIMPLE_FLOAT("roll_mul", &roll_mul),
-
 
269
    CFG_SIMPLE_FLOAT("gier_mul", &gier_mul),
-
 
270
    CFG_SIMPLE_FLOAT("gas_mul", &gas_mul),
-
 
271
   
-
 
272
    CFG_SIMPLE_INT("rel_axis_nick", &rel_axis_nick),
-
 
273
    CFG_SIMPLE_INT("rel_axis_roll", &rel_axis_roll),
-
 
274
    CFG_SIMPLE_INT("rel_axis_gier", &rel_axis_gier),
103
  //  bt_host_init();
-
 
104
 
275
    CFG_SIMPLE_INT("rel_axis_gas", &rel_axis_gas),
105
 
276
 
-
 
277
    CFG_END()
106
  if (argv[1])
278
  };
-
 
279
 
-
 
280
  cfg_t *cfg;
-
 
281
 
-
 
282
  //  input_evdev_name=strdup("/dev/event0");
-
 
283
  printf("Parsing config file ");
-
 
284
 
-
 
285
  cfg = cfg_init(opts, 0);
-
 
286
 
-
 
287
  if (argv[1])
-
 
288
    {
-
 
289
      printf("%s\n ",argv[1]);
-
 
290
      cfg_parse(cfg, argv[1]);
-
 
291
    }
107
    parse_config(argv[1]);
-
 
108
  else
-
 
109
    parse_config("/etc/riddim.conf");
-
 
110
 
292
  else
111
 
-
 
112
 
-
 
113
  printf("input %s:\n",input_evdev_name);  
-
 
114
  /*
293
    {
115
  if (bluetooth_mac)
294
      printf("/etc/riddim.conf\n");
116
    {
295
      cfg_parse(cfg, "/etc/riddim.conf");
117
 
296
    }
118
      printf("Connecting via Bluetooth to %s\n",bluetooth_mac);
297
  printf("input %s:\n",input_evdev_name);  
119
      if (connect_mk_bluetooth(bluetooth_mac));;
298
 
120
      connected=TRUE;
299
  // 1st argument -> Bluetooth adrees to bypass scanning ( takes to long for short testing roundtrips )
121
    }
300
 
122
*/
301
  if (mk_socket_port)
123
  if (mk_socket_port)
302
    {
124
    {
303
        printf("connecting to local port: %i\n",mk_socket_port);
125
      printf("connecting to local port: %i\n",mk_socket_port);
304
       
126
     
305
      if (connect_mk_localhost_socket(mk_socket_port)==-1)
127
      if (connect_mk_localhost_socket(mk_socket_port)==-1)
306
        printf("cant connect !!");
128
        printf("cant connect !!");
307
      else
129
      else
308
        {
130
        {
309
          printf("connected to QC at adress: %s\n",argv[1]);
131
          printf("connected !-)");
310
          connected=TRUE;
132
          connected=TRUE;
311
        }
133
        }
312
    }
134
    }
313
 
135
 
-
 
136
  // todo reenable  bluetooth connection
-
 
137
 
314
  int i;
138
 
315
 
139
  /*
316
  printf("\nInitializing X-52 input ..\n");
140
  printf("\nInitializing X-52 input ..\n");
317
  if (connect_joy())
141
  if (connect_joy())
318
    {
142
    {
319
    printf(".. done");//
143
      printf(".. done");//
320
    input=INPUT_JOYDEV;
144
      input=INPUT_JOYDEV;
321
    }
145
    }
322
  else
146
  else
323
    printf(".. ERROR ");//
147
    printf(".. ERROR ");//
-
 
148
  */
-
 
149
 
-
 
150
  if (input_evdev_name)
324
 
151
    {
-
 
152
      printf("\nInitializing evdev input (%s) ..\n",input_evdev_name);
325
    printf("\nInitializing evdev input (%s) ..\n",input_evdev_name);
153
     
326
  if (connect_evdev())
154
      if (connect_evdev(input_evdev_name))
327
    {
155
        {
328
    printf(".. done");//
156
          printf(".. done");//
329
    input=INPUT_EVDEV;
157
          input=INPUT_EVDEV;
330
    }
158
        }
331
  else
159
      else
332
    printf(".. ERROR ");//
160
        printf(".. ERROR ");//
-
 
161
    }
333
 
162
 
334
  printf("\nInitializing X-52 output ..");
163
  printf("\nInitializing X-52 output ..");
335
 
164
 
336
  x52_output = x52_init();
165
  x52_output = x52_init();
337
   
166
   
338
  clear_display();
167
  clear_display();
339
 
168
 
340
  write_display(0, "RIDDIM active");
169
  write_display(0, "RIDDIM active");
341
 
170
 
342
  if (x52_output) x52_setbri(x52_output, 1,128);  
171
  if (x52_output) x52_setbri(x52_output, 1,128);  
343
  if (x52_output)
172
  if (x52_output)
344
    printf(" done \n");  
173
    printf(" done \n");  
345
  else
174
  else
346
    printf(" not found \n");      
175
    printf(" not found \n");      
347
 
176
 
348
  /*
177
  /*
349
  if (!connected)
178
    if (!connected)
350
    {
179
    {
351
      printf("Scanning for Bluetooth Devices ..\n");
180
    printf("Scanning for Bluetooth Devices ..\n");
352
      write_display(1,"Bluetooth Scan");
181
    write_display(1,"Bluetooth Scan");
353
      scan_bt();
182
    scan_bt();
354
      printf(" done \n");  
183
    printf(" done \n");  
355
      printf(" %d Devices found \n",bt_device_count);  
184
    printf(" %d Devices found \n",bt_device_count);  
356
      print_device_list() ;
185
    print_device_list() ;
357
    }
186
    }
358
  */
187
  */
359
 
188
 
360
  int v_old;
189
  //  int v_old;
361
  int polls=0;
190
  int polls=0;
362
 
191
 
363
  int retval;
192
 
364
  if (exit_after_init)
193
  if (exit_after_init)
365
    exit(0);
194
    exit(0);
366
  printf("starting loop ..\n");
195
  printf("starting loop ..\n");
367
 
-
 
368
  struct input_event led_event;
-
 
-
 
196
 
369
  led_event.type=EV_LED;
197
 
370
 
-
 
371
  int complete_misses=0;
-
 
372
  if (evdev_out_fd)
-
 
373
    {
198
 
374
      led_event.code = LED_MISC;
-
 
375
      led_event.value = 1;
199
  int complete_misses=0;
376
      retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
200
  int complete_matches=0;
377
    }
-
 
378
 
-
 
-
 
201
 
-
 
202
 
379
  int confirm_misses;
203
  int confirm_misses;
-
 
204
 
-
 
205
 
380
  while( 1 )    
206
 
381
    {
-
 
382
 
-
 
383
      if (evdev_out_fd)
-
 
384
        {
-
 
385
          if (led_event.value)
-
 
386
            led_event.value = 0;
-
 
-
 
207
  init_evdevstatus_led();
-
 
208
 
387
          else
209
  while( TRUE )    
388
            led_event.value = 1 ;
210
    {
389
          retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
211
 
390
        }
212
      blink_evdev_led();
391
 
213
      //      bt_host_tick(mk_socket);
392
      usleep(loop_delay);
214
      usleep(loop_delay);
393
      switch (input)
215
      switch (input)
394
        {
216
        {
395
 
217
 
396
         
218
         
397
        case INPUT_NONE:
219
        case INPUT_NONE:
398
          printf("starting input none\n");
220
          printf("starting input none\n");
399
          break;
221
          break;
400
 
222
 
401
        case INPUT_EVDEV:
223
        case INPUT_EVDEV:
402
           printf("input evdev\n");
224
          printf("input evdev\n");
403
 
-
 
404
           struct timeval tv;
-
 
405
           int retval;
-
 
406
           fd_set rfds;
-
 
407
           FD_ZERO(&rfds);
-
 
408
           FD_SET(evdev_fd,&rfds);
-
 
409
 
-
 
410
           tv.tv_sec = 0;
-
 
411
           tv.tv_usec = 5;
-
 
412
 
-
 
413
           retval = select(evdev_fd+1, &rfds, NULL, NULL, &tv);
-
 
414
 
-
 
415
           if (retval==-1)
-
 
416
             printf("error in select!!!!!!!!\n");
-
 
417
           else if (retval)
-
 
418
             {
-
 
419
             read_bytes = read(evdev_fd, ev, sizeof(struct input_event) * 64);
-
 
420
 
-
 
421
             if (read_bytes < (int) sizeof(struct input_event)) {
225
 
422
               perror("evtest: short read");
-
 
423
               exit (1);
-
 
424
             }
-
 
425
             
-
 
426
             for (yalv = 0; yalv < (int) (read_bytes / sizeof(struct input_event)); yalv++)
-
 
427
               {
-
 
428
 
-
 
429
                 printf("%d type:%d code:%d val:%d \n",yalv,ev[yalv].type,ev[yalv].code,ev[yalv].value);
-
 
430
                 if (ev[yalv].type==EV_REL) axis[ ev[yalv].code]= ev[yalv].value;
-
 
431
                 if (ev[yalv].type==EV_KEY) button[ ev[yalv].code-256]= ev[yalv].value;
-
 
432
               }
-
 
433
 
-
 
434
 
-
 
435
             for (yalv=0;yalv<10;yalv++)
-
 
436
               printf("A%d %d -" , yalv, axis[yalv] );
-
 
437
             printf("\n");
-
 
438
 
-
 
439
             for (yalv=0;yalv<10;yalv++)
-
 
440
               printf("B%d %d -" , yalv, button[yalv] );
-
 
441
             //         printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]); 
-
 
442
             }
-
 
443
           else
226
         
444
             printf("no data from evdev data from evdev: \n");
227
          poll_evdev();
445
 
228
         
446
        break;
229
          break;
447
 
230
 
448
        case INPUT_JOYDEV:
231
        case INPUT_JOYDEV:
449
          printf("input joydev\n");      
232
          printf("input joydev\n");      
450
          // poll values from input device
233
          // poll values from input device
451
         
234
         
452
          for (polls=0;polls<100;polls++) // FIXME - better Polling
235
          for (polls=0;polls<100;polls++) // FIXME - better Polling
453
            {
236
            {
454
              read(x52_input_fd, &x52_event_struct, sizeof(struct js_event));
237
              read(x52_input_fd, &x52_event_struct, sizeof(struct js_event));
455
             
238
             
456
             
239
             
457
              /* see what to do with the event */
240
              /* see what to do with the event */
458
              switch (x52_event_struct.type & ~JS_EVENT_INIT)
241
              switch (x52_event_struct.type & ~JS_EVENT_INIT)
459
                {
242
                {
460
                case JS_EVENT_AXIS:
243
                case JS_EVENT_AXIS:
461
                  axis   [ x52_event_struct.number ] = x52_event_struct.value;
244
                  axis   [ x52_event_struct.number ] = x52_event_struct.value;
462
                  break;
245
                  break;
463
                case JS_EVENT_BUTTON:
246
                case JS_EVENT_BUTTON:
464
                  button [ x52_event_struct.number ] = x52_event_struct.value;
247
                  button [ x52_event_struct.number ] = x52_event_struct.value;
465
                  break;
248
                  break;
466
                }
249
                }
467
            }
250
            }
468
         
251
          int x;
469
          for( x=0 ; x<num_of_buttons ; ++x )
252
          for( x=0 ; x<num_of_buttons ; ++x )
470
            if( button[x]==0)
253
            if( button[x]==0)
471
              button_trigger[x]=0;
254
              button_trigger[x]=0;
472
            else
255
            else
473
              {
256
              {
474
                if (button_trigger[x]<100)button_trigger[x]++;
257
                if (button_trigger[x]<100)button_trigger[x]++;
475
              }
258
              }
476
          break;
259
          break;
477
        }
260
        }
478
 
261
 
479
      printf("input done\n");    
262
      printf("input done\n");    
480
               
263
               
481
      switch(state)
264
      switch(state)
482
        {
265
        {
483
 
266
 
484
 
267
 
485
        case STATEID_SCANNING:
268
        case STATEID_SCANNING:
486
 
269
         
487
          state=STATEID_CONNECTING;
270
          state=STATEID_CONNECTING;
488
 
271
          /*
489
          ExternControl.Digital[0]=0;
272
          ExternControl.Digital[0]=0;
490
          ExternControl.Digital[1]=0;
273
          ExternControl.Digital[1]=0;
491
          ExternControl.RemoteTasten=0;
274
          ExternControl.RemoteTasten=0;
492
          ExternControl.Nick=(axis[1]>>8)*(-1)+127;;
275
          ExternControl.Nick=(axis[1]>>8)*(-1)+127;;
493
 
276
 
494
          printf("nick%d\n",ExternControl.Nick);         
277
          printf("nick%d\n",ExternControl.Nick);         
495
          ExternControl.Roll=(axis[0]>>8)*(-1)+127;;
278
          ExternControl.Roll=(axis[0]>>8)*(-1)+127;;
496
          ExternControl.Gier=(axis[5]>>8)*(-1)+127;;
279
          ExternControl.Gier=(axis[5]>>8)*(-1)+127;;
497
          ExternControl.Gas=(axis[2]>>8)*(-1)+127;
280
          ExternControl.Gas=(axis[2]>>8)*(-1)+127;
498
          ExternControl.Higt=0;
281
          ExternControl.Higt=0;
499
          ExternControl.free=0;
282
          ExternControl.free=0;
500
          ExternControl.Frame='t';
283
          ExternControl.Frame='t';
501
          ExternControl.Config=1;
284
          ExternControl.Config=1;
502
 
285
 
503
          printf("sending data\n");
286
          printf("sending data\n");
504
 
287
 
505
         
288
         
506
          if (connected)SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
289
          if (connected)SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
507
          gettimeofday(&time_struct1,NULL);
290
          gettimeofday(&time_struct1,NULL);
508
 
291
 
509
          if (button_trigger[BUTTON_SELECT]==1)
292
          if (button_trigger[BUTTON_SELECT]==1)
510
            {
293
            {
511
              state=STATEID_CONNECTING;
294
              state=STATEID_CONNECTING;
512
              clear_display();
295
              clear_display();
513
              write_display(0,"connecting to");
296
              write_display(0,"connecting to");
514
              write_display(1,names[selected_bt_device]);
297
              write_display(1,names[selected_bt_device]);
515
              //connect_mk(addrs[selected_bt_device]);
298
              //connect_mk(addrs[selected_bt_device]);
516
              write_display(0,"connected to");
299
              write_display(0,"connected to");
517
            }
300
            }
518
                   
301
                   
519
          if ((button_trigger[BUTTON_UP]+button_trigger[BUTTON_DOWN])==1)
302
          if ((button_trigger[BUTTON_UP]+button_trigger[BUTTON_DOWN])==1)
520
            {
303
            {
521
              printf("-> sel_dev %d - %d\n",selected_bt_device,button_trigger[19]);
304
              printf("-> sel_dev %d - %d\n",selected_bt_device,button_trigger[19]);
522
              if (button_trigger[BUTTON_DOWN]==1)
305
              if (button_trigger[BUTTON_DOWN]==1)
523
                if (selected_bt_device>0) selected_bt_device--;
306
                if (selected_bt_device>0) selected_bt_device--;
524
              if (button_trigger[BUTTON_UP]==1)
307
              if (button_trigger[BUTTON_UP]==1)
525
                if (selected_bt_device<bt_device_count-1) selected_bt_device++;
308
                if (selected_bt_device<bt_device_count-1) selected_bt_device++;
526
             
309
             
527
 
310
 
528
            }
311
            }
-
 
312
          */
529
          break;
313
          break;
530
       
314
       
531
        case STATEID_CONNECTING:
315
        case STATEID_CONNECTING:
532
         
316
         
533
 
317
 
534
          confirm_misses=0;
318
          confirm_misses=0;
535
          printf("connected:%d",connected);
319
          printf("connected:%d",connected);
-
 
320
            RxBuffer[1]=0;
-
 
321
          if (connected)
-
 
322
 
-
 
323
 
536
          if (connected) while (RxBuffer[1]!='t')
324
            while (RxBuffer[1]!='t')
537
            {
325
            {
538
 
326
 
539
              RxBuffer[1]=0;
327
              RxBuffer[1]=0;
-
 
328
              read_from_mk();
-
 
329
              //                      bt_host_send(RxBuffer,rx_last_length);
-
 
330
                      printf("sending to host: %s",PrintableRxBuffer);
540
 
331
 
541
 
332
 
542
              //          ftime(&time_struct);
333
              //          ftime(&time_struct);
-
 
334
 
543
              printf("waiting for confirm frame ( misses:%d )\n",confirm_misses);
335
              printf("waiting for confirm frame ( confirmed:%d misses:%d )\n",complete_matches,complete_misses);
544
              RxBuffer[2]=0;
336
              RxBuffer[2]=0;
545
 
337
 
546
 
338
 
547
 
339
 
548
              r=0;
-
 
549
              in_char='#';
-
 
550
 
-
 
551
              while(in_char!='\n')
-
 
552
                {
-
 
553
                  count=read(mk_socket,&in_char,1);
-
 
554
                  if (in_char!=0)
-
 
555
                    {
-
 
556
                      RxBuffer[r++]=in_char;
-
 
557
                    }
-
 
558
                  else
-
 
559
                    {
-
 
560
                      RxBuffer[r++]='0';
-
 
561
                    }
-
 
562
                  //               printf("\ncount:%d r:%d %d %c \n",count , r, in_char, in_char);
-
 
563
                }
-
 
564
              RxBuffer[r++]='\0';
340
              //              r=0;
-
 
341
 
565
              printf("%d--->%s\n",complete_misses,RxBuffer);
342
              // new
566
              // new
343
              /*
567
              if (button_trigger[12]>1)
344
              if (button_trigger[12]>1)
568
                {
345
                {
569
                  SendOutData('s', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
346
                  SendOutData('s', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
570
                  button_trigger[12]=0;
347
                  button_trigger[12]=0;
571
                }
348
                }
-
 
349
              */
-
 
350
              ExternControl.Frame='t';
572
              if (++confirm_misses>4)
351
              if (++confirm_misses>4)
573
                {
352
                {
574
                  complete_misses++;
353
                  complete_misses++;
575
                  printf("sending again\n");
354
                  printf("sending again\n");
576
                  SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
355
                  SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
577
                }
356
                }
578
            }
357
            }
579
          else
358
          else
580
            printf("not connected to mk\n");
359
            printf("not connected to mk\n");
-
 
360
 
581
          gettimeofday(&time_struct2,NULL);
361
          gettimeofday(&time_struct2,NULL);
582
 
362
 
583
          printf("last trip: %d",(int)(time_struct1.tv_usec-time_struct2.tv_usec));
363
          printf("last trip: %d",(int)(time_struct1.tv_usec-time_struct2.tv_usec));
584
          act_mode=button[24] | (button[25]<<1);
364
          //      act_mode=button[24] | (button[25]<<1);
585
 
365
 
586
 
366
 
587
 
367
 
588
          // Step converting axis data to nick/roll/gier/gas/..
368
          // Step converting axis data to nick/roll/gier/gas/..
589
 
369
 
590
          act_nick=axis[rel_axis_nick]*nick_mul;
370
          act_nick=evdev_rel_axis[rel_axis_nick]*nick_mul;
591
          act_roll=axis[rel_axis_roll]*roll_mul;
371
          act_roll=evdev_rel_axis[rel_axis_roll]*roll_mul;
592
          act_gier=axis[rel_axis_gier]*gier_mul;
372
          act_gier=evdev_rel_axis[rel_axis_gier]*gier_mul;
593
          act_gas=axis[rel_axis_gas]*gas_mul;
373
          act_gas=evdev_rel_axis[rel_axis_gas]*gas_mul;
594
          act_gas=255;
374
          act_gas=255;
595
 
375
 
596
 
376
 
597
          /*
377
          /*
598
          switch (act_mode)
378
            switch (act_mode)
599
            {
379
            {
600
            case 0:
380
            case 0:
601
 
381
 
602
 
382
 
603
              act_nick=(axis[AXIS_NICK])*(INVERT_NICK);
383
            act_nick=(axis[AXIS_NICK])*(INVERT_NICK);
604
              act_roll=(axis[AXIS_ROLL])*(INVERT_ROLL);
384
            act_roll=(axis[AXIS_ROLL])*(INVERT_ROLL);
605
              act_gier=(axis[AXIS_GIER])*(INVERT_GIER);
385
            act_gier=(axis[AXIS_GIER])*(INVERT_GIER);
606
              act_gas=((axis[AXIS_GAS])-128)*(-1);
386
            act_gas=((axis[AXIS_GAS])-128)*(-1);
607
             
387
             
608
              // clip gas      
388
            // clip gas      
609
              if (act_gas<0) act_gas=0;
389
            if (act_gas<0) act_gas=0;
610
 
390
 
611
              if (act_gas>250) act_gas=250;        
391
            if (act_gas>250) act_gas=250;          
612
 
392
 
613
////////              act_gas=0;
393
            ////////          act_gas=0;
614
              break;
394
            break;
615
 
395
 
616
            case 1:
396
            case 1:
617
              act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/2;
397
            act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/2;
618
              act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/2;
398
            act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/2;
619
              act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/2;
399
            act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/2;
620
              act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
400
            act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
621
 
401
 
622
              break;
402
            break;
623
             
403
             
624
            case 2:
404
            case 2:
625
              act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/3;
405
            act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/3;
626
              act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/3;
406
            act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/3;
627
              act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/3;
407
            act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/3;
628
              act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
408
            act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
629
 
409
 
630
 
410
 
631
              break;
411
            break;
632
 
412
 
633
            }
413
            }
634
          */  
414
          */  
635
          ExternControl.Digital[0]=0;
415
          ExternControl.Digital[0]=0;
636
          ExternControl.Digital[1]=0;
416
          ExternControl.Digital[1]=0;
637
          ExternControl.RemoteTasten=0;
417
          ExternControl.RemoteTasten=0;
638
          ExternControl.Nick=act_nick;  //(axis[1]>>8)*(-1)/2;
-
 
639
          //      printf("nick%d\n",ExternControl.Nick);         
-
 
640
          ExternControl.Roll=act_roll*(-1); //(axis[0]>>8)*(-1)/2;
-
 
641
          ExternControl.Gier=(axis[5]>>8);
-
 
642
          ExternControl.Gier=act_gier; // ************
-
 
643
          ExternControl.Gas=(axis[2]>>8)*(-1)+127;
-
 
644
          ExternControl.Gas=act_gas; // ************
-
 
645
          //      ExternControl.Gas=0; // ************
-
 
646
          ExternControl.Higt=0;
418
          ExternControl.Higt=0;
647
          ExternControl.free=0;
419
          ExternControl.free=0;
648
          ExternControl.Frame='t';
420
          ExternControl.Frame='t';
649
         
-
 
650
          ExternControl.Config=1;
421
          ExternControl.Config=1;
651
          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);
-
 
-
 
422
 
-
 
423
 
-
 
424
          ExternControl.Nick=act_nick;  //(axis[1]>>8)*(-1)/2;
-
 
425
          ExternControl.Roll=act_roll*(-1); //(axis[0]>>8)*(-1)/2;
-
 
426
          ExternControl.Gier=act_gier; // ************
-
 
427
          ExternControl.Gas=act_gas; // ************
-
 
428
         
-
 
429
          complete_matches++;
652
 
430
 
653
 
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);
654
 
432
 
655
          if (connected)
433
          if (connected)
656
            {
434
            {
657
              printf("sending data\n");
435
              printf("sending data\n");
658
             
436
             
659
              SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
437
              SendOutData('b', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
660
              gettimeofday(&time_struct1,NULL);
438
              gettimeofday(&time_struct1,NULL);
661
              printf("sent data\n");
439
              printf("sent data\n");
662
            }
440
            }
663
         
441
         
664
              // printf("sleeping\n");
442
          // printf("sleeping\n");
665
              //          for (polls=0;polls<100;polls++) // FIXME - better Polling
443
          //      for (polls=0;polls<100;polls++) // FIXME - better Polling
666
              // printf("end_sleep\n");
444
          // printf("end_sleep\n");
667
             
445
             
668
          int v=axis[6]/655+50;
446
          //      int v=axis[6]/655+50;
669
          if (v!=v_old)if (x52_output) x52_setbri(x52_output, 0,v );  
447
          //      if (v!=v_old)if (x52_output) x52_setbri(x52_output, 0,v );  
670
          v_old=v;
448
          //      v_old=v;
-
 
449
         
671
         
450
          //      printf("v: %d \n",v);
672
          printf("v: %d \n",v);
451
             
673
             
452
         
674
         
453
          /*
675
          for (i=0;i<num_of_axis;i++)
454
            for (i=0;i<num_of_axis;i++)
676
            printf("A%d: %d  ", i,axis[i]>>8 );
455
            printf("A%d: %d  ", i,axis[i]>>8 );
677
         
456
         
678
          for( x=0 ; x<num_of_buttons ; ++x )
457
            for( x=0 ; x<num_of_buttons ; ++x )
679
           
458
           
680
            printf("B%d: %d  ", x, button[x] );            
459
            printf("B%d: %d  ", x, button[x] );            
-
 
460
          */
681
         
461
 
682
          break;
462
          break;
683
        }
463
        }
684
     
464
     
685
      printf("\n");
465
      printf("\n");
686
      fflush(stdout);
466
      fflush(stdout);
687
      printf("loop fin ( misses:%d)\n",complete_misses);
467
      printf("loop fin ( confirmed:%d misses:%d | debug_sets:%d )\n",complete_matches,complete_misses,debug_sets);
688
               
468
               
689
    }
469
    }
690
 
470
 
691
 
471
 
692
  /******************** Cleanup **********************/
472
  /******************** Cleanup **********************/
693
  close(x52_input_fd);
473
  close(x52_input_fd);
694
  close(mk_socket);
474
  close(mk_socket);
695
 
475
 
696
  if (x52_output) x52_close(x52_output);
476
  if (x52_output) x52_close(x52_output);
697
  return 0;
477
  return 0;
698
}
478
}
699
 
479