Advertisement
xerpi

C linux NXT

Jun 26th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <sys/socket.h>
  6. #include <bluetooth/bluetooth.h>
  7. #include <bluetooth/hci.h>
  8. #include <bluetooth/hci_lib.h>
  9. #include <bluetooth/rfcomm.h>
  10.  
  11. #define INQUIRY_LEN       8
  12. #define MAX_RSP           5
  13. #define READ_NAME_TIMEOUT 0
  14. #define NAME_BUFFER_LEN   255
  15.  
  16. char bt_address[19]             = {0};
  17. char bt_name[NAME_BUFFER_LEN]   = {0};
  18. struct sockaddr_rc sockaddr     = {0};
  19. int dev_id, dev_sock, bt_socket, num_rsp, i, ret;
  20. int bytes_sent, bytes_received;
  21. inquiry_info *ii = NULL;
  22. long inquiry_flags;
  23.  
  24. void signal_handler(int s);
  25.  
  26. //NXT
  27. enum NXT_PORT
  28. {
  29.     PORT_A = 0x0,
  30.     PORT_B = 0x1,
  31.     PORT_C = 0x2
  32. };
  33.  
  34. enum MOTOR_MODE
  35. {
  36.     MOTORON   = 0x01,
  37.     BRAKE     = 0x02,
  38.     REGULATED = 0x04
  39. };
  40.  
  41. enum REGULATION_MODE
  42. {
  43.     REGULATION_MODE_IDLE        = 0x00,
  44.     REGULATION_MODE_MOTOR_SPEED = 0x01,
  45.     REGULATION_MODE_MOTOR_SYNC  = 0x02
  46. };
  47.  
  48. enum RUNSTATE
  49. {
  50.     MOTOR_RUN_STATE_IDLE     = 0x00,
  51.     MOTOR_RUN_STATE_RAMPUMP  = 0x10,
  52.     MOTOR_RUN_STATE_RUNNING  = 0x20,
  53.     MOTOR_RUN_STATE_RAMPDOWN = 0x40
  54. };
  55.  
  56. int play_tone(short frequency, short duration);
  57. int set_output_state(enum NXT_PORT port, int8_t power, enum MOTOR_MODE mode, enum REGULATION_MODE regulation_mode,
  58.                      int8_t turn_ratio, enum RUNSTATE run_state, uint32_t tacho_limit);
  59.  
  60.  
  61. int main(int argc, char **argv)
  62. {
  63.     (void) signal(SIGINT, signal_handler);
  64.  
  65.     /*dev_id = hci_get_route(NULL);
  66.     if(dev_id < 0)
  67.     {
  68.         perror("no bt adapters connected");
  69.     }
  70.  
  71.     dev_sock = hci_open_dev(dev_id);
  72.     if(dev_sock < 0)
  73.     {
  74.         perror("could not open adapter");
  75.     }
  76.  
  77.     ii = (inquiry_info *)malloc(sizeof(inquiry_info) * MAX_RSP);
  78.     inquiry_flags = IREQ_CACHE_FLUSH;
  79.  
  80.     num_rsp = hci_inquiry(dev_id, INQUIRY_LEN, MAX_RSP, NULL, &ii, inquiry_flags);
  81.     if(num_rsp < 0)
  82.     {
  83.         perror("could not inquiry devices");
  84.     }
  85.     else if(num_rsp == 0)
  86.     {
  87.         perror("no devices have been found");
  88.     }
  89.     else
  90.     {
  91.         printf("Found %i bluetooth device(s)!\n", num_rsp);
  92.     }
  93.  
  94.     for(i = 0; i < num_rsp; i++)
  95.     {
  96.         printf("->Device number %i:\n", i);
  97.         ba2str(&(ii[i].bdaddr), bt_address);
  98.         printf("----->Bt addr: %s\n", bt_address);
  99.         ret =  hci_read_remote_name(dev_sock, &(ii[i].bdaddr), NAME_BUFFER_LEN, bt_name, READ_NAME_TIMEOUT);
  100.         if(ret < 0)
  101.         {
  102.             printf("----->Device name: could not read device name\n");
  103.         }
  104.         else
  105.         {
  106.             printf("----->Device name: %s\n", bt_name);
  107.         }
  108.     }*/
  109.  
  110.     char dest_addr[18] = "00:16:53:0A:3B:98";
  111.  
  112.     bt_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  113.     sockaddr.rc_family = AF_BLUETOOTH;
  114.     str2ba(dest_addr, &sockaddr.rc_bdaddr);
  115.     sockaddr.rc_channel = 1;
  116.  
  117.     ret = connect(bt_socket, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  118.     if(ret < 0)
  119.     {
  120.         perror("error connecting to the device");
  121.     }
  122.  
  123.  
  124.     while(1)
  125.     {
  126.         bytes_sent = set_output_state(PORT_A, 100, MOTORON, REGULATION_MODE_IDLE, 100, MOTOR_RUN_STATE_RUNNING, 360);
  127.         printf("sent %i bytes(s)\n", bytes_sent);
  128.         usleep(2 * 1000 * 1000);
  129.     }
  130.  
  131.     close(bt_socket);
  132.     printf("\nEND\n");
  133.     return 0;
  134. }
  135.  
  136. //NXT
  137.  
  138. int play_tone(short frequency, short duration)
  139. {
  140.     uint8_t command[8] = {6, 0, 0x80, 0x03, frequency & 0xFF, (frequency>>8) & 0xFF, duration & 0xFF, (duration>>8) & 0xFF};
  141.     return send(bt_socket, command, 8, 0);
  142. }
  143.  
  144.  
  145. int set_output_state(enum NXT_PORT port, int8_t power, enum MOTOR_MODE mode, enum REGULATION_MODE regulation_mode,
  146.                      int8_t turn_ratio, enum RUNSTATE run_state, uint32_t tacho_limit)
  147. {
  148.     uint8_t command[14] = {12, 0, 0x80, 0x04,
  149.                            port, power, mode, regulation_mode,
  150.                            turn_ratio, run_state, tacho_limit & 0xFF,
  151.                            (tacho_limit>>8) & 0xFF, (tacho_limit>>16) & 0xFF,
  152.                            (tacho_limit>>24) & 0xFF
  153.                            };
  154.     return send(bt_socket, command, 14, 0);
  155. }
  156.  
  157. //
  158. void signal_handler(int s)
  159. {
  160.     close(bt_socket);
  161.     printf("\nsignal: %i\n", s);
  162.     exit(0);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement