Advertisement
Guest User

Wii Nunchuck Arduino

a guest
Oct 29th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #ifndef Nunchuck_h
  2. #define Nunchuck_h
  3.  
  4. #include "Arduino.h"
  5. #include <Wire.h>
  6.  
  7. #define DEFAULT_ZERO_JOY_X 122
  8. #define DEFAULT_ZERO_JOY_Y 134
  9.  
  10. #define MAXANGLE 90
  11. #define MINANGLE -90
  12.  
  13. class Nunchuck {
  14.   private:
  15.     uint8_t outbuf[6];      // array to store arduino output
  16.     int cnt;
  17.    
  18.     boolean z_button;
  19.     boolean c_button;
  20.    
  21.     int zero_joy_x;
  22.     int zero_joy_y;
  23.    
  24.     int joy_x_axis;
  25.     int joy_y_axis;
  26.    
  27.     int accel_x_axis;
  28.     int accel_y_axis;
  29.     int accel_z_axis;
  30.    
  31.     // Encode data to format that most wiimote drivers except
  32.     // only needed if you use one of the regular wiimote drivers
  33.     char decodeByte (char x) {
  34.       x = (x ^ 0x17) + 0x17;
  35.       return x;
  36.     }
  37.  
  38.   public:
  39.     void begin()
  40.     {
  41.       cnt = 0;
  42.  
  43.       zero_joy_x = DEFAULT_ZERO_JOY_X;
  44.       zero_joy_y = DEFAULT_ZERO_JOY_Y;
  45.        
  46.       Wire.begin();
  47.       Wire.beginTransmission (0x52);    // transmit to device 0x52
  48.       Wire.write (0x40);        // sends memory address
  49.       Wire.write (0x00);        // sends sent a zero.  
  50.       Wire.endTransmission ();  // stop transmitting
  51.     }
  52.    
  53.     void sendZero ()
  54.     {
  55.       Wire.beginTransmission (0x52);    // transmit to device 0x52
  56.       Wire.write (0x00);        // sends one byte
  57.       Wire.endTransmission ();  // stop transmitting
  58.     }
  59.    
  60.     void read()
  61.     {
  62.       Wire.requestFrom (0x52, 6);   // request data from nunchuck
  63.      
  64.       while (Wire.available()) {
  65.           outbuf[cnt] = decodeByte (Wire.read ());  // receive byte as an integer
  66.           cnt++;
  67.       }
  68.    
  69.       // If we recieved the 6 bytes, then go print them
  70.       if (cnt >= 5) {
  71.           setValues();
  72.           printValues();
  73.       }
  74.    
  75.       cnt = 0;
  76.       sendZero(); // send the request for next bytes
  77.     }
  78.    
  79.     void setValues() {
  80.       z_button = true;
  81.       c_button = true;
  82.      
  83.       joy_x_axis = outbuf[0] - zero_joy_x;
  84.       joy_y_axis = outbuf[1] - zero_joy_y;
  85.        
  86.       joy_x_axis = joy_x_axis > MAXANGLE ? MAXANGLE : joy_x_axis;
  87.       joy_x_axis = joy_x_axis < MINANGLE ? MINANGLE : joy_x_axis;
  88.      
  89.       joy_y_axis = joy_y_axis > MAXANGLE ? MAXANGLE : joy_y_axis;
  90.       joy_y_axis = joy_y_axis < MINANGLE ? MINANGLE : joy_y_axis;
  91.      
  92.       accel_x_axis = outbuf[2] * 2 * 2;
  93.       accel_y_axis = outbuf[3] * 2 * 2;
  94.       accel_z_axis = outbuf[4] * 2 * 2;
  95.    
  96.      // byte outbuf[5] contains bits for z and c buttons
  97.      // it also contains the least significant bits for the accelerometer data
  98.      // so we have to check each bit of byte outbuf[5]
  99.       if ((outbuf[5] >> 0) & 1)
  100.         {
  101.           z_button = false;
  102.         }
  103.       if ((outbuf[5] >> 1) & 1)
  104.         {
  105.           c_button = false;
  106.         }
  107.    
  108.       if ((outbuf[5] >> 2) & 1)
  109.         {
  110.           accel_x_axis += 2;
  111.         }
  112.       if ((outbuf[5] >> 3) & 1)
  113.         {
  114.           accel_x_axis += 1;
  115.         }
  116.    
  117.       if ((outbuf[5] >> 4) & 1)
  118.         {
  119.           accel_y_axis += 2;
  120.         }
  121.       if ((outbuf[5] >> 5) & 1)
  122.         {
  123.           accel_y_axis += 1;
  124.         }
  125.    
  126.       if ((outbuf[5] >> 6) & 1)
  127.         {
  128.           accel_z_axis += 2;
  129.         }
  130.       if ((outbuf[5] >> 7) & 1)
  131.         {
  132.           accel_z_axis += 1;
  133.         }
  134.     }
  135.    
  136.     void printValues() {
  137.         Serial.print (accel_x_axis, DEC); //values: 312tiltedleft  512mid  745tiltedright
  138.         Serial.print (", ");
  139.      
  140.         Serial.print (accel_y_axis, DEC); //values: tippedforward754  mid576   tippedback361
  141.         Serial.print (", ");
  142.      
  143.         Serial.print (accel_z_axis, DEC);  // values: hovers around 530 - 743
  144.         Serial.print (", ");
  145.      
  146.         Serial.print (joy_x_axis, DEC); //values seen: 37left 134mid  234right
  147.         Serial.print (", ");
  148.      
  149.         Serial.print (joy_y_axis, DEC); //values seen: 218forward  124mid  23back
  150.         Serial.print (", ");
  151.        
  152.         Serial.print (z_button, DEC); //NB: 0 when pressed, 1 when not (i.e. opposite of what you might expect)
  153.         Serial.print (", ");
  154.      
  155.         Serial.print (c_button, DEC); //NB: 0 when pressed, 1 when not (i.e. opposite of what you might expect)
  156.         Serial.println();  
  157.     }
  158.    
  159.     boolean zPressed() {
  160.       return z_button;  
  161.     }
  162.    
  163.     boolean cPressed() {
  164.       return c_button;
  165.     }
  166.    
  167.     int getJoyX() {
  168.       return joy_x_axis;
  169.     }
  170.    
  171.     int getJoyY() {
  172.        return joy_y_axis;
  173.     }
  174.    
  175.     int getAccelX() {
  176.       return accel_x_axis;  
  177.     }
  178.    
  179.     int getAccelY() {
  180.       return accel_y_axis;  
  181.     }
  182.    
  183.     int getAccelZ() {
  184.       return accel_z_axis;
  185.     }  
  186. };
  187.  
  188. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement