Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef Nunchuck_h
- #define Nunchuck_h
- #include "Arduino.h"
- #include <Wire.h>
- #define DEFAULT_ZERO_JOY_X 122
- #define DEFAULT_ZERO_JOY_Y 134
- #define MAXANGLE 90
- #define MINANGLE -90
- class Nunchuck {
- private:
- uint8_t outbuf[6]; // array to store arduino output
- int cnt;
- boolean z_button;
- boolean c_button;
- int zero_joy_x;
- int zero_joy_y;
- int joy_x_axis;
- int joy_y_axis;
- int accel_x_axis;
- int accel_y_axis;
- int accel_z_axis;
- // Encode data to format that most wiimote drivers except
- // only needed if you use one of the regular wiimote drivers
- char decodeByte (char x) {
- x = (x ^ 0x17) + 0x17;
- return x;
- }
- public:
- void begin()
- {
- cnt = 0;
- zero_joy_x = DEFAULT_ZERO_JOY_X;
- zero_joy_y = DEFAULT_ZERO_JOY_Y;
- Wire.begin();
- Wire.beginTransmission (0x52); // transmit to device 0x52
- Wire.write (0x40); // sends memory address
- Wire.write (0x00); // sends sent a zero.
- Wire.endTransmission (); // stop transmitting
- }
- void sendZero ()
- {
- Wire.beginTransmission (0x52); // transmit to device 0x52
- Wire.write (0x00); // sends one byte
- Wire.endTransmission (); // stop transmitting
- }
- void read()
- {
- Wire.requestFrom (0x52, 6); // request data from nunchuck
- while (Wire.available()) {
- outbuf[cnt] = decodeByte (Wire.read ()); // receive byte as an integer
- cnt++;
- }
- // If we recieved the 6 bytes, then go print them
- if (cnt >= 5) {
- setValues();
- printValues();
- }
- cnt = 0;
- sendZero(); // send the request for next bytes
- }
- void setValues() {
- z_button = true;
- c_button = true;
- joy_x_axis = outbuf[0] - zero_joy_x;
- joy_y_axis = outbuf[1] - zero_joy_y;
- joy_x_axis = joy_x_axis > MAXANGLE ? MAXANGLE : joy_x_axis;
- joy_x_axis = joy_x_axis < MINANGLE ? MINANGLE : joy_x_axis;
- joy_y_axis = joy_y_axis > MAXANGLE ? MAXANGLE : joy_y_axis;
- joy_y_axis = joy_y_axis < MINANGLE ? MINANGLE : joy_y_axis;
- accel_x_axis = outbuf[2] * 2 * 2;
- accel_y_axis = outbuf[3] * 2 * 2;
- accel_z_axis = outbuf[4] * 2 * 2;
- // byte outbuf[5] contains bits for z and c buttons
- // it also contains the least significant bits for the accelerometer data
- // so we have to check each bit of byte outbuf[5]
- if ((outbuf[5] >> 0) & 1)
- {
- z_button = false;
- }
- if ((outbuf[5] >> 1) & 1)
- {
- c_button = false;
- }
- if ((outbuf[5] >> 2) & 1)
- {
- accel_x_axis += 2;
- }
- if ((outbuf[5] >> 3) & 1)
- {
- accel_x_axis += 1;
- }
- if ((outbuf[5] >> 4) & 1)
- {
- accel_y_axis += 2;
- }
- if ((outbuf[5] >> 5) & 1)
- {
- accel_y_axis += 1;
- }
- if ((outbuf[5] >> 6) & 1)
- {
- accel_z_axis += 2;
- }
- if ((outbuf[5] >> 7) & 1)
- {
- accel_z_axis += 1;
- }
- }
- void printValues() {
- Serial.print (accel_x_axis, DEC); //values: 312tiltedleft 512mid 745tiltedright
- Serial.print (", ");
- Serial.print (accel_y_axis, DEC); //values: tippedforward754 mid576 tippedback361
- Serial.print (", ");
- Serial.print (accel_z_axis, DEC); // values: hovers around 530 - 743
- Serial.print (", ");
- Serial.print (joy_x_axis, DEC); //values seen: 37left 134mid 234right
- Serial.print (", ");
- Serial.print (joy_y_axis, DEC); //values seen: 218forward 124mid 23back
- Serial.print (", ");
- Serial.print (z_button, DEC); //NB: 0 when pressed, 1 when not (i.e. opposite of what you might expect)
- Serial.print (", ");
- Serial.print (c_button, DEC); //NB: 0 when pressed, 1 when not (i.e. opposite of what you might expect)
- Serial.println();
- }
- boolean zPressed() {
- return z_button;
- }
- boolean cPressed() {
- return c_button;
- }
- int getJoyX() {
- return joy_x_axis;
- }
- int getJoyY() {
- return joy_y_axis;
- }
- int getAccelX() {
- return accel_x_axis;
- }
- int getAccelY() {
- return accel_y_axis;
- }
- int getAccelZ() {
- return accel_z_axis;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement