Subversion Repositories Projects

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "lib/x52/x52.h"



#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>



#include <linux/joystick.h>
#define JOY_DEV "/dev/input/js0"

#define MAX_BT_DEVICES 3

int bt_device_count=0;

char names[MAX_BT_DEVICES][248];
char addrs[MAX_BT_DEVICES][19];

void scan_bt()
{
  inquiry_info *ii = NULL;

  int dev_id, sock, len, flags;
  int i;
  char addr[19] = { 0 };
  char name[248] = { 0 };
 
  dev_id = hci_get_route(NULL);
  sock = hci_open_dev( dev_id );
  if (dev_id < 0 || sock < 0) {
    perror("opening socket");
    exit(1);
  }

  len  = 8;
 
  flags = IREQ_CACHE_FLUSH;
  ii = (inquiry_info*)malloc(MAX_BT_DEVICES * sizeof(inquiry_info));
 
  bt_device_count = hci_inquiry(dev_id, len, MAX_BT_DEVICES, NULL, &ii, flags);
  if(  bt_device_count < 0 ) perror("hci_inquiry");
 
  for (i = 0; i <  bt_device_count; i++) {
    ba2str(&(ii+i)->bdaddr, addr);
    sprintf(addrs[i],"%s",addr);

    memset(name, 0, sizeof(name));
   
    if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),
                             name, 0) < 0)
      sprintf(names[i],"[unknown]");
    else
      sprintf(names[i],"%s",name);

  }

 
  free( ii );
  close( sock );
}




int main(int argc, char**argv)
{
  int i;

  int x52_input_fd;
  struct JS_DATA_TYPE x52_input_struct;


  if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
    {
      printf( "Couldn't open joystick device %s\n", JOY_DEV );
      return -1;
    }



  printf("Initializing X-52 output ..");
  struct x52 *x52_output = x52_init();
  if (x52_output) x52_settext(x52_output, 0 , "RIDDIM active", strlen("Scanning BT"));
  if (x52_output) x52_setbri(x52_output, 1,128);  
  if (x52_output)
    printf(" done \n");  
  else
    printf(" not found \n");      


  printf("Scanning for Bluetooth Devices ..");
  if (x52_output) x52_settext(x52_output, 1 , "Scanning BT", strlen("Scanning BT"));
  //  scan_bt();
  printf(" done \n");  
  printf(" %d Devices found \n",bt_device_count);  
  for(i=0;i<bt_device_count;i++)
    printf(" %d -> %s (%s) \n",i,names[i],addrs[i]);  
  printf("Starting UFO Control Seet ( ^c to quit ) \n");


  while( 1 )    
    {
     
      if( read( x52_input_fd, &x52_input_struct, JS_RETURN ) != JS_RETURN )
        {
          printf( "\nFailed to read from Joystick\n" );
        }

      if (x52_output) x52_setbri(x52_output, 0,x52_input_struct.x );  
      if (x52_output) x52_setbri(x52_output, 1,x52_input_struct.y );  
      if (x52_output) x52_setbri(x52_output, 2,x52_input_struct.x );  

      printf("X: % 4d  Y: % 4d Z: % 4d  B1: %1d  B2: %1d  \r"
             ,x52_input_struct.x   /* X axis */
             ,x52_input_struct.y   /* Y axis */
             ,x52_input_struct.y   /* Z axis */
             ,(x52_input_struct.buttons & 1) ? 1 : 0       /* button 1 */
             ,(x52_input_struct.buttons & 2) ? 1 : 0 );    /* button 2 */
    }

  close(x52_input_fd);  /* too bad we never get here */
 
  if (x52_output) x52_close(x52_output);
  return 0;
}