Subversion Repositories Projects

Rev

Details | 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
 
7
 
8
 
9
#include <unistd.h>
10
#include <sys/socket.h>
11
#include <bluetooth/bluetooth.h>
12
#include <bluetooth/hci.h>
13
#include <bluetooth/hci_lib.h>
14
 
15
 
16
 
17
#include <linux/joystick.h>
18
#define JOY_DEV "/dev/input/js0"
19
 
20
#define MAX_BT_DEVICES 3
21
 
22
int bt_device_count=0;
23
 
24
char names[MAX_BT_DEVICES][248];
25
char addrs[MAX_BT_DEVICES][19];
26
 
27
void scan_bt()
28
{
29
  inquiry_info *ii = NULL;
30
 
31
  int dev_id, sock, len, flags;
32
  int i;
33
  char addr[19] = { 0 };
34
  char name[248] = { 0 };
35
 
36
  dev_id = hci_get_route(NULL);
37
  sock = hci_open_dev( dev_id );
38
  if (dev_id < 0 || sock < 0) {
39
    perror("opening socket");
40
    exit(1);
41
  }
42
 
43
  len  = 8;
44
 
45
  flags = IREQ_CACHE_FLUSH;
46
  ii = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info));
47
 
48
  bt_device_count = hci_inquiry(dev_id, len, MAX_BT_DEVICES, NULL, &ii, flags);
49
  if(  bt_device_count < 0 ) perror("hci_inquiry");
50
 
51
  for (i = 0; i <  bt_device_count; i++) {
52
    ba2str(&(ii+i)->bdaddr, addr);
53
    sprintf(addrs[i],"%s",addr);
54
 
55
    memset(name, 0, sizeof(name));
56
 
57
    if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),
58
                             name, 0) < 0)
59
      sprintf(names[i],"[unknown]");
60
    else
61
      sprintf(names[i],"%s",name);
62
 
63
  }
64
 
65
 
66
  free( ii );
67
  close( sock );
68
}
69
 
70
 
71
 
72
 
73
int main(int argc, char**argv)
74
{
75
  int i;
76
 
77
  int x52_input_fd;
78
  struct JS_DATA_TYPE x52_input_struct;
79
 
80
 
81
  if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
82
    {
83
      printf( "Couldn't open joystick device %s\n", JOY_DEV );
84
      return -1;
85
    }
86
 
87
 
88
 
89
  printf("Initializing X-52 output ..");
90
  struct x52 *x52_output = x52_init();
91
  if (x52_output) x52_settext(x52_output, 0 , "RIDDIM active", strlen("Scanning BT"));
92
  if (x52_output) x52_setbri(x52_output, 1,128);  
93
  if (x52_output)
94
    printf(" done \n");  
95
  else
96
    printf(" not found \n");      
97
 
98
 
99
  printf("Scanning for Bluetooth Devices ..");
100
  if (x52_output) x52_settext(x52_output, 1 , "Scanning BT", strlen("Scanning BT"));
101
  //  scan_bt();
102
  printf(" done \n");  
103
  printf(" %d Devices found \n",bt_device_count);  
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 )    
110
    {
111
 
112
      if( read( x52_input_fd, &x52_input_struct, JS_RETURN ) != JS_RETURN )
113
        {
114
          printf( "\nFailed to read from Joystick\n" );
115
        }
116
 
117
      if (x52_output) x52_setbri(x52_output, 0,x52_input_struct.x );  
118
      if (x52_output) x52_setbri(x52_output, 1,x52_input_struct.y );  
119
      if (x52_output) x52_setbri(x52_output, 2,x52_input_struct.x );  
120
 
121
      printf("X: % 4d  Y: % 4d Z: % 4d  B1: %1d  B2: %1d  \r"
122
             ,x52_input_struct.x   /* X axis */
123
             ,x52_input_struct.y   /* Y axis */
124
             ,x52_input_struct.y   /* Z axis */
125
             ,(x52_input_struct.buttons & 1) ? 1 : 0       /* button 1 */
126
             ,(x52_input_struct.buttons & 2) ? 1 : 0 );    /* button 2 */
127
    }
128
 
129
  close(x52_input_fd);  /* too bad we never get here */
130
 
131
  if (x52_output) x52_close(x52_output);
132
  return 0;
133
}