Advertisement
xerpi

Untitled

Jul 6th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <signal.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <linux/i2c-dev.h>
  7. #include <linux/uinput.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11.  
  12. #define NUNCHUK_ADDRESS     0x52
  13. #define NUNCHUK_DATA_LENGTH 0x06
  14. #define DEFAULT_ADAPTER     2
  15. #define DEFAULT_UPDATE_FREQ 100
  16.  
  17. static const char* adapter = "/dev/i2c-1";
  18. static int fd = -1, ret = -1;
  19. static int update_freq  = DEFAULT_UPDATE_FREQ;
  20. static int update_period = 0;
  21. static int nunchuk_inited = 0;
  22.  
  23. struct i2c_smbus_ioctl_data ioctl_data;
  24. uint8_t nunchuk_buffer[6];
  25.  
  26. void nunchuk_init();
  27. void nunchuk_request_data();
  28. void nunchuk_read_data();
  29. void nunchuk_process_data();
  30. static void catch_signal(int signal);
  31.  
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.     signal(SIGTERM, catch_signal);
  36.     signal(SIGKILL, catch_signal);
  37.     signal(SIGINT, catch_signal);
  38.    
  39.     fd = open(adapter, O_RDWR);
  40.    
  41.     if(fd < 0) {
  42.         printf("Error opening adapter %s\n", adapter);
  43.         perror("open");
  44.         exit(EXIT_FAILURE);
  45.     }
  46.     usleep(1000);
  47.    
  48.     ret = ioctl(fd, I2C_SLAVE, NUNCHUK_ADDRESS);
  49.     if(ret < 0) {
  50.         perror("ioctl I2C_SLAVE");
  51.         close(fd);
  52.         exit(EXIT_FAILURE);
  53.     }
  54.    
  55.     update_period = (1/(float)update_freq) * 1000 * 1000;
  56.    
  57.     usleep(1000);
  58.     nunchuk_init();
  59.     usleep(1000);
  60.    
  61.     while(1) {
  62.         if(nunchuk_inited) {
  63.             nunchuk_request_data();
  64.             usleep(100);
  65.             nunchuk_read_data();
  66.             nunchuk_process_data();
  67.            
  68.             usleep(update_period);
  69.         } else {
  70.             nunchuk_init();
  71.             usleep(100 * 1000);
  72.         }
  73.     }
  74.    
  75.  
  76.     close(fd);
  77.     printf("\nn2pi exit\n");
  78.     return 0;
  79. }
  80.  
  81.  
  82. void nunchuk_process_data()
  83. {
  84.     uint8_t joyX, joyY, Z, C;
  85.     int aX, aY, aZ;
  86.    
  87.     joyX = nunchuk_buffer[0];
  88.     joyY = nunchuk_buffer[1];
  89.     Z = ~(nunchuk_buffer[5] & 0b1);
  90.     C = ~(nunchuk_buffer[5] & 0b10);
  91.    
  92.     aX = (((nunchuk_buffer[5]>>2) & 0b11) | ((int)nunchuk_buffer[2])<<2);
  93.     aY = (((nunchuk_buffer[5]>>4) & 0b11) | ((int)nunchuk_buffer[3])<<2);
  94.     aZ = (((nunchuk_buffer[5]>>6) & 0b11) | ((int)nunchuk_buffer[4])<<2);
  95.    
  96.     printf("joyX: %d  joyY: %d  Z: %d  C: %d\n", joyX, joyY, Z, C);
  97.     printf("aX: %d  aY: %d  aZ: %d\n", aX, aY, aZ);
  98. }
  99.  
  100.  
  101. void nunchuk_read_data()
  102. {
  103.     ret = i2c_smbus_read_block_data(fd, 0, nunchuk_buffer);
  104.     if(ret < 0) {
  105.         printf("error reading data\n");
  106.     } else {
  107.         printf("read %d byte(s)!!\n", ret);
  108.     }
  109. }
  110.  
  111. void nunchuk_request_data()
  112. {
  113.     if(i2c_smbus_write_byte_data(fd, 0x00, 0x00) < 0) {
  114.         printf("error requesting data: smbus_write_byte error\n"); 
  115.     }  
  116. }
  117.  
  118. void nunchuk_init()
  119. {
  120.     if(i2c_smbus_write_byte_data(fd, 0xF0, 0x55) < 0) {
  121.         perror("error initializing nunchuk (step 1): smbus_write_byte error \n");
  122.         nunchuk_inited = 0;
  123.         return;
  124.     }
  125.     if(i2c_smbus_write_byte_data(fd, 0xFB, 0x00) < 0) {
  126.         perror("error initializing nunchuk (step 2): smbus_write_byte error \n");
  127.         nunchuk_inited = 0;
  128.         return;    
  129.     }
  130.     nunchuk_inited = 1;
  131. }
  132.  
  133. static void catch_signal(int signal)
  134. {
  135.     printf("signal %i caught!\n", signal);
  136.     if(fd > 0) close(fd);
  137.     exit(-1);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement