Subversion Repositories Projects

Rev

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