Subversion Repositories Projects

Rev

Rev 41 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 41 Rev 43
Line 1... Line 1...
1
#include <stdio.h>
1
#include <stdio.h>
2
#include <stdlib.h>
2
#include <stdlib.h>
3
#include <string.h>
3
#include <string.h>
4
#include <fcntl.h>
4
#include <fcntl.h>
5
#include "lib/x52/x52.h"
5
#include "lib/x52/x52.h"
6
 
-
 
7
 
-
 
8
 
-
 
9
#include <unistd.h>
6
#include <unistd.h>
10
#include <sys/socket.h>
7
#include <sys/socket.h>
11
#include <bluetooth/bluetooth.h>
8
#include <bluetooth/bluetooth.h>
12
#include <bluetooth/hci.h>
9
#include <bluetooth/hci.h>
13
#include <bluetooth/hci_lib.h>
10
#include <bluetooth/hci_lib.h>
Line -... Line 11...
-
 
11
 
-
 
12
 
Line 14... Line 13...
14
 
13
#include <bluetooth/rfcomm.h>
15
 
14
 
Line 16... Line 15...
16
 
15
 
Line 22... Line 21...
22
int bt_device_count=0;
21
int bt_device_count=0;
Line 23... Line 22...
23
 
22
 
24
char names[MAX_BT_DEVICES][248];
23
char names[MAX_BT_DEVICES][248];
Line -... Line 24...
-
 
24
char addrs[MAX_BT_DEVICES][19];
-
 
25
 
-
 
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;
25
char addrs[MAX_BT_DEVICES][19];
39
 
26
 
40
int selected_bt_device=0;
27
void scan_bt()
41
void scan_bt()
Line 28... Line 42...
28
{
42
{
Line 65... Line 79...
65
 
79
 
66
  free( ii );
80
  free( ii );
67
  close( sock );
81
  close( sock );
Line -... Line 82...
-
 
82
}
-
 
83
 
Line -... Line 84...
-
 
84
int x52_input_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
Line -... Line 85...
-
 
85
char *button=NULL,*button_trigger=NULL, name_of_joystick[80];
-
 
86
 
Line -... Line 87...
-
 
87
struct js_event x52_event_struct;
-
 
88
 
-
 
89
void connect_joy()
-
 
90
{
-
 
91
 
-
 
92
 
-
 
93
  if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
68
}
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 */
69
 
113
 
70
 
114
}
-
 
115
 
Line 71... Line -...
71
 
-
 
72
 
-
 
Line -... Line 116...
-
 
116
 
-
 
117
struct
-
 
118
{
-
 
119
  char val[4];
-
 
120
} MotortestParam;
-
 
121
 
-
 
122
 
-
 
123
void AddCRC(unsigned int wieviele)
-
 
124
{
-
 
125
  unsigned int tmpCRC = 0,i;
-
 
126
  for(i = 0; i < wieviele;i++)
-
 
127
    {
Line -... Line 128...
-
 
128
      tmpCRC += TxBuffer[i];
-
 
129
    }
-
 
130
  tmpCRC %= 4096;
-
 
131
  TxBuffer[i++] = '=' + tmpCRC / 64;
-
 
132
  TxBuffer[i++] = '=' + tmpCRC % 64;
73
int main(int argc, char**argv)
133
  TxBuffer[i++] = '\r';
-
 
134
}
-
 
135
 
-
 
136
void SendOutData(unsigned char cmd,unsigned char modul, int len)
74
{
137
{
-
 
138
  unsigned int pt = 0,ptr=0,i;
-
 
139
  int a,b,c;
75
  int i;
140
  TxBuffer[pt++] = '#';           // Startzeichen
76
 
141
  TxBuffer[pt++] = modul;        // Adresse (a=0; b=1,...)
-
 
142
  TxBuffer[pt++] = cmd;          // Commando
-
 
143
 
-
 
144
  while(len)
77
  int x52_input_fd;
145
    {
-
 
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);
-
 
153
    }
-
 
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
}
-
 
172
 
-
 
173
 
Line -... Line 174...
-
 
174
int engines_on=0;
-
 
175
int old_engines_on=0;
-
 
176
 
-
 
177
 
-
 
178
void write_display(int line,char* text)
-
 
179
{
Line -... Line 180...
-
 
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()
78
  struct JS_DATA_TYPE x52_input_struct;
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
         
79
 
200
          if (selected_bt_device==i)
-
 
201
            sprintf(disp_txt,"#%s",names[i]);
-
 
202
          else
80
 
203
            sprintf(disp_txt," %s",names[i]);
-
 
204
          write_display(i,disp_txt);
81
  if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
205
        }
-
 
206
    }
-
 
207
}
-
 
208
 
-
 
209
void connect_mk(char dest[18])
82
    {
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
 
83
      printf( "Couldn't open joystick device %s\n", JOY_DEV );
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
 
84
      return -1;
240
  {
-
 
241
  // send a message
-
 
242
 
85
    }
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
 
-
 
251
  else
-
 
252
  engines_on=0;
-
 
253
 
-
 
254
  if (engines_on!=old_engines_on)
-
 
255
  {
-
 
256
  int c;
Line -... Line 257...
-
 
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
 
-
 
263
 
-
 
264
   
-
 
265
  sleep(1);
-
 
266
  }
-
 
267
        */
-
 
268
 
-
 
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();
86
 
302
 
87
 
303
    clear_display();
-
 
304
 
-
 
305
    write_display(0, "RIDDIM active");
88
 
306
 
89
  printf("Initializing X-52 output ..");
307
    if (x52_output) x52_setbri(x52_output, 1,128);  
90
  struct x52 *x52_output = x52_init();
308
    if (x52_output)
-
 
309
      printf(" done \n");  
-
 
310
    else
91
  if (x52_output) x52_settext(x52_output, 0 , "RIDDIM active", strlen("Scanning BT"));
311
      printf(" not found \n");      
-
 
312
 
92
  if (x52_output) x52_setbri(x52_output, 1,128);  
313
 
93
  if (x52_output)
314
 
-
 
315
  printf("Scanning for Bluetooth Devices ..\n");
-
 
316
  write_display(1,"Bluetooth Scan");
-
 
317
 
Line -... Line 318...
-
 
318
 
94
    printf(" done \n");  
319
  scan_bt();
95
  else
320
  printf(" done \n");  
-
 
321
  printf(" %d Devices found \n",bt_device_count);  
-
 
322
 
-
 
323
 
-
 
324
  for(i=0;i<bt_device_count;i++)
-
 
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() ;
-
 
331
 
-
 
332
 
-
 
333
  int v_old;
-
 
334
  while( 1 )    
-
 
335
    {
-
 
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
 
96
    printf(" not found \n");      
415
                   
-
 
416
                    for (i=0;i<num_of_axis;i++)
97
 
417
                      printf("A%d: %d  ", i,axis[i]>>8 );
98
 
418
                   
99
  printf("Scanning for Bluetooth Devices ..");
419
                    for( x=0 ; x<num_of_buttons ; ++x )
100
  if (x52_output) x52_settext(x52_output, 1 , "Scanning BT", strlen("Scanning BT"));
420
 
Line 101... Line -...
101
  //  scan_bt();
-
 
-
 
421
                      printf("B%d: %d  ", x, button[x] );                  
102
  printf(" done \n");  
422
                   
103
  printf(" %d Devices found \n",bt_device_count);  
423
                    break;
Line 104... Line -...
104
  for(i=0;i<bt_device_count;i++)
-
 
105
    printf(" %d -> %s (%s) \n",i,names[i],addrs[i]);  
-
 
106
  printf("Starting UFO Control Seet ( ^c to quit ) \n");
-
 
107
 
-
 
108
 
-
 
109
  while( 1 )    
-
 
-
 
424
                  }
-
 
425
                    /*
110
    {
426
 
Line 111... Line 427...
111
     
427
*/                
Line 112... Line 428...
112
      if( read( x52_input_fd, &x52_input_struct, JS_RETURN ) != JS_RETURN )
428
              printf("\r");
113
        {
429
fflush(stdout);