2 * Nunchuck functions -- Talk to a Wii Nunchuck
4 * This library is from the Bionic Arduino course :
5 * http://todbot.com/blog/bionicarduino/
7 * 2007 Tod E. Kurt, http://todbot.com/blog/
9 * The Wii Nunchuck reading code originally from Windmeadow Labs
10 * http://www.windmeadow.com/node/42
15 static uint8_t nunchuck_buf
[6]; // array to store nunchuck data,
17 // Uses port C (analog in) pins as power & ground for Nunchuck
18 static void nunchuck_setpowerpins()
22 DDRC
|= _BV(pwrpin
) | _BV(gndpin
);
23 PORTC
&=~ _BV(gndpin
);
25 delay(100); // wait for things to stabilize
28 // initialize the I2C system, join the I2C bus,
29 // and tell the nunchuck we're talking to it
30 static void nunchuck_init()
32 Wire
.begin(); // join i2c bus as master
33 Wire
.beginTransmission(0x52);// transmit to device 0x52
34 Wire
.send(0x40);// sends memory address
35 Wire
.send(0x00);// sends sent a zero.
36 Wire
.endTransmission();// stop transmitting
39 // Send a request for data to the nunchuck
41 static void nunchuck_send_request()
43 Wire
.beginTransmission(0x52);// transmit to device 0x52
44 Wire
.send(0x00);// sends one byte
45 Wire
.endTransmission();// stop transmitting
48 // Encode data to format that most wiimote drivers except
49 // only needed if you use one of the regular wiimote drivers
50 static char nunchuk_decode_byte (char x
)
52 x
= (x
^ 0x17) + 0x17;
56 // Receive data back from the nunchuck,
57 // returns 1 on successful read. returns 0 on failure
58 static int nunchuck_get_data()
61 Wire
.requestFrom (0x52, 6);// request data from nunchuck
62 while (Wire
.available ()) {
63 // receive byte as an integer
64 nunchuck_buf
[cnt
] = nunchuk_decode_byte(Wire
.receive());
67 nunchuck_send_request(); // send request for next data payload
68 // If we recieved the 6 bytes, then go print them
75 // Print the input data we have recieved
76 // accel data is 10 bits long
77 // so we read 8 bits, then we have to add
78 // on the last 2 bits. That is why I
79 // multiply them by 2 * 2
80 static void nunchuck_print_data()
83 int joy_x_axis
= nunchuck_buf
[0];
84 int joy_y_axis
= nunchuck_buf
[1];
85 int accel_x_axis
= nunchuck_buf
[2]; // * 2 * 2;
86 int accel_y_axis
= nunchuck_buf
[3]; // * 2 * 2;
87 int accel_z_axis
= nunchuck_buf
[4]; // * 2 * 2;
92 // byte nunchuck_buf[5] contains bits for z and c buttons
93 // it also contains the least significant bits for the accelerometer data
94 // so we have to check each bit of byte outbuf[5]
95 if ((nunchuck_buf
[5] >> 0) & 1)
97 if ((nunchuck_buf
[5] >> 1) & 1)
100 if ((nunchuck_buf
[5] >> 2) & 1)
102 if ((nunchuck_buf
[5] >> 3) & 1)
105 if ((nunchuck_buf
[5] >> 4) & 1)
107 if ((nunchuck_buf
[5] >> 5) & 1)
110 if ((nunchuck_buf
[5] >> 6) & 1)
112 if ((nunchuck_buf
[5] >> 7) & 1)
118 Serial
.print("joy:");
119 Serial
.print(joy_x_axis
,DEC
);
121 Serial
.print(joy_y_axis
, DEC
);
124 Serial
.print("acc:");
125 Serial
.print(accel_x_axis
, DEC
);
127 Serial
.print(accel_y_axis
, DEC
);
129 Serial
.print(accel_z_axis
, DEC
);
132 Serial
.print("but:");
133 Serial
.print(z_button
, DEC
);
135 Serial
.print(c_button
, DEC
);
137 Serial
.print("\r\n"); // newline
141 // returns zbutton state: 1=pressed, 0=notpressed
142 static int nunchuck_zbutton()
144 return ((nunchuck_buf
[5] >> 0) & 1) ? 0 : 1; // voodoo
147 // returns zbutton state: 1=pressed, 0=notpressed
148 static int nunchuck_cbutton()
150 return ((nunchuck_buf
[5] >> 1) & 1) ? 0 : 1; // voodoo
153 // returns value of x-axis joystick
154 static int nunchuck_joyx()
156 return nunchuck_buf
[0];
159 // returns value of y-axis joystick
160 static int nunchuck_joyy()
162 return nunchuck_buf
[1];
165 // returns value of x-axis accelerometer
166 static int nunchuck_accelx()
168 return nunchuck_buf
[2]; // FIXME: this leaves out 2-bits of the data
171 // returns value of y-axis accelerometer
172 static int nunchuck_accely()
174 return nunchuck_buf
[3]; // FIXME: this leaves out 2-bits of the data
177 // returns value of z-axis accelerometer
178 static int nunchuck_accelz()
180 return nunchuck_buf
[4]; // FIXME: this leaves out 2-bits of the data