Advertisement
xerpi

Nunchuk + Arduino

Dec 1st, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <Streaming.h>
  2. #include <Wire.h>
  3.  
  4. #define NUNCHUK_ADDRESS 0x52
  5. #define NUNCHUK_DATA_LENGTH 6
  6. #define JOY_X_CENTER 123
  7. #define JOY_Y_CENTER 129
  8.  
  9. uint8_t nunchuk_buffer[NUNCHUK_DATA_LENGTH];
  10. uint8_t nunchuk_buffer_count;
  11.  
  12. void nunchuk_init()
  13. {
  14.     Wire.beginTransmission(NUNCHUK_ADDRESS);
  15.     Wire.write(0xF0);
  16.     Wire.write(0x55);
  17.     Wire.endTransmission();
  18.    
  19.     Wire.beginTransmission(NUNCHUK_ADDRESS);
  20.     Wire.write(0xFB);
  21.     Wire.write(0x00);
  22.     Wire.endTransmission();
  23. }
  24.  
  25. void nunchuk_request_data()
  26. {
  27.     Wire.beginTransmission(NUNCHUK_ADDRESS);
  28.     Wire.write(0x00);
  29.     Wire.endTransmission();
  30. }
  31.  
  32. uint8_t nunchuk_read()
  33. {
  34.     Wire.requestFrom(NUNCHUK_ADDRESS, NUNCHUK_DATA_LENGTH);
  35.     nunchuk_buffer_count = 0;
  36.     while(Wire.available())
  37.     {
  38.         nunchuk_buffer[nunchuk_buffer_count] = Wire.read();
  39.         nunchuk_buffer_count++;
  40.     }
  41.     if (nunchuk_buffer_count > 5)
  42.     {
  43.         nunchuk_request_data();
  44.     }
  45.     return nunchuk_buffer_count;
  46. }
  47.  
  48. inline uint8_t nunchuk_joyX()
  49. {
  50.     return nunchuk_buffer[0];
  51. }
  52.  
  53. inline uint8_t nunchuk_joyY()
  54. {
  55.     return nunchuk_buffer[1];
  56. }
  57.  
  58. inline uint8_t nunchuk_buttonZ()
  59. {
  60.     return !(nunchuk_buffer[5] & 0b1);
  61. }
  62.  
  63. inline uint8_t nunchuk_buttonC()
  64. {
  65.     return !(nunchuk_buffer[5] & 0b10);
  66. }
  67.  
  68. inline int nunchuk_aX()
  69. {
  70.     return (((nunchuk_buffer[5]>>2) & 0b11) | ((int)nunchuk_buffer[2])<<2);
  71. }
  72.  
  73. inline int nunchuk_aY()
  74. {
  75.     return (((nunchuk_buffer[5]>>4) & 0b11) | ((int)nunchuk_buffer[3])<<2);
  76. }
  77.  
  78. inline int nunchuk_aZ()
  79. {
  80.     return (((nunchuk_buffer[5]>>6) & 0b11) | ((int)nunchuk_buffer[4])<<2);
  81. }
  82.  
  83. inline double nunchuk_joyAng()
  84. {
  85.     return atan2(nunchuk_joyY(), nunchuk_joyX());
  86. }
  87.  
  88. inline double nunchuk_joyMag()
  89. {
  90.     double x = (double)(nunchuk_joyX() - JOY_X_CENTER)/125.0;
  91.     double y = (double)(nunchuk_joyY() - JOY_Y_CENTER)/125.0;
  92.     return sqrt(x*x + y*y);
  93. }
  94.  
  95. void print_nunchuk_data()
  96. {
  97.     Serial<<"Acc X: "<< nunchuk_aX() << "  Acc X: " << nunchuk_aY() << "  Acc Z: " << nunchuk_aZ() << endl;
  98.     Serial<<"Joy X: "<< nunchuk_joyX() << "  Joy y: " << nunchuk_joyY() << endl;
  99.     Serial<<"Button C: "<< nunchuk_buttonC() << "  Button Z: " << nunchuk_buttonZ() << endl<<endl;
  100. }
  101.  
  102.  
  103. void setup()
  104. {
  105.     Serial.begin(9600);
  106.     Wire.begin();
  107.     nunchuk_init();
  108.     Serial.println("Inited");
  109. }
  110.  
  111. void loop()
  112. {
  113.     nunchuk_read();
  114.     print_nunchuk_data();
  115.     delay(500);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement