121 |
pfowler |
1 |
/*
|
|
|
2 |
* Nunchuck functions -- Talk to a Wii Nunchuck
|
|
|
3 |
*
|
|
|
4 |
* This library is from the Bionic Arduino course :
|
|
|
5 |
* http://todbot.com/blog/bionicarduino/
|
|
|
6 |
*
|
|
|
7 |
* 2007-11 Tod E. Kurt, http://todbot.com/blog/
|
|
|
8 |
*
|
|
|
9 |
* The Wii Nunchuck reading code originally from Windmeadow Labs
|
|
|
10 |
* http://www.windmeadow.com/node/42
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
#if (ARDUINO >= 100)
|
|
|
14 |
#include <Arduino.h>
|
|
|
15 |
#else
|
|
|
16 |
#include <WProgram.h>
|
|
|
17 |
//#define Wire.write(x) Wire.send(x)
|
|
|
18 |
//#define Wire.read() Wire.receive()
|
|
|
19 |
#endif
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
static uint8_t nunchuck_buf[6]; // array to store nunchuck data,
|
|
|
24 |
|
|
|
25 |
// Uses port C (analog in) pins as power & ground for Nunchuck
|
|
|
26 |
static void nunchuck_setpowerpins()
|
|
|
27 |
{
|
|
|
28 |
#define pwrpin PORTC3
|
|
|
29 |
#define gndpin PORTC2
|
|
|
30 |
DDRC |= _BV(pwrpin) | _BV(gndpin);
|
|
|
31 |
PORTC &=~ _BV(gndpin);
|
|
|
32 |
PORTC |= _BV(pwrpin);
|
|
|
33 |
delay(100); // wait for things to stabilize
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// initialize the I2C system, join the I2C bus,
|
|
|
37 |
// and tell the nunchuck we're talking to it
|
|
|
38 |
static void nunchuck_init()
|
|
|
39 |
{
|
|
|
40 |
Wire.begin(); // join i2c bus as master
|
|
|
41 |
Wire.beginTransmission(0x52);// transmit to device 0x52
|
|
|
42 |
#if (ARDUINO >= 100)
|
|
|
43 |
Wire.write((uint8_t)0x40);// sends memory address
|
|
|
44 |
Wire.write((uint8_t)0x00);// sends sent a zero.
|
|
|
45 |
#else
|
|
|
46 |
Wire.send((uint8_t)0x40);// sends memory address
|
|
|
47 |
Wire.send((uint8_t)0x00);// sends sent a zero.
|
|
|
48 |
#endif
|
|
|
49 |
Wire.endTransmission();// stop transmitting
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// Send a request for data to the nunchuck
|
|
|
53 |
// was "send_zero()"
|
|
|
54 |
static void nunchuck_send_request()
|
|
|
55 |
{
|
|
|
56 |
Wire.beginTransmission(0x52);// transmit to device 0x52
|
|
|
57 |
#if (ARDUINO >= 100)
|
|
|
58 |
Wire.write((uint8_t)0x00);// sends one byte
|
|
|
59 |
#else
|
|
|
60 |
Wire.send((uint8_t)0x00);// sends one byte
|
|
|
61 |
#endif
|
|
|
62 |
Wire.endTransmission();// stop transmitting
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Encode data to format that most wiimote drivers except
|
|
|
66 |
// only needed if you use one of the regular wiimote drivers
|
|
|
67 |
static char nunchuk_decode_byte (char x)
|
|
|
68 |
{
|
|
|
69 |
x = (x ^ 0x17) + 0x17;
|
|
|
70 |
return x;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Receive data back from the nunchuck,
|
|
|
74 |
// returns 1 on successful read. returns 0 on failure
|
|
|
75 |
static int nunchuck_get_data()
|
|
|
76 |
{
|
|
|
77 |
int cnt=0;
|
|
|
78 |
Wire.requestFrom (0x52, 6);// request data from nunchuck
|
|
|
79 |
while (Wire.available ()) {
|
|
|
80 |
// receive byte as an integer
|
|
|
81 |
#if (ARDUINO >= 100)
|
|
|
82 |
nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.read() );
|
|
|
83 |
#else
|
|
|
84 |
nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.receive() );
|
|
|
85 |
#endif
|
|
|
86 |
cnt++;
|
|
|
87 |
}
|
|
|
88 |
nunchuck_send_request(); // send request for next data payload
|
|
|
89 |
// If we recieved the 6 bytes, then go print them
|
|
|
90 |
if (cnt >= 5) {
|
|
|
91 |
return 1; // success
|
|
|
92 |
}
|
|
|
93 |
return 0; //failure
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// Print the input data we have recieved
|
|
|
97 |
// accel data is 10 bits long
|
|
|
98 |
// so we read 8 bits, then we have to add
|
|
|
99 |
// on the last 2 bits. That is why I
|
|
|
100 |
// multiply them by 2 * 2
|
|
|
101 |
static void nunchuck_print_data()
|
|
|
102 |
{
|
|
|
103 |
static int i=0;
|
|
|
104 |
int joy_x_axis = nunchuck_buf[0];
|
|
|
105 |
int joy_y_axis = nunchuck_buf[1];
|
|
|
106 |
int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
|
|
|
107 |
int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
|
|
|
108 |
int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;
|
|
|
109 |
|
|
|
110 |
int z_button = 0;
|
|
|
111 |
int c_button = 0;
|
|
|
112 |
|
|
|
113 |
// byte nunchuck_buf[5] contains bits for z and c buttons
|
|
|
114 |
// it also contains the least significant bits for the accelerometer data
|
|
|
115 |
// so we have to check each bit of byte outbuf[5]
|
|
|
116 |
if ((nunchuck_buf[5] >> 0) & 1)
|
|
|
117 |
z_button = 1;
|
|
|
118 |
if ((nunchuck_buf[5] >> 1) & 1)
|
|
|
119 |
c_button = 1;
|
|
|
120 |
|
|
|
121 |
if ((nunchuck_buf[5] >> 2) & 1)
|
|
|
122 |
accel_x_axis += 2;
|
|
|
123 |
if ((nunchuck_buf[5] >> 3) & 1)
|
|
|
124 |
accel_x_axis += 1;
|
|
|
125 |
|
|
|
126 |
if ((nunchuck_buf[5] >> 4) & 1)
|
|
|
127 |
accel_y_axis += 2;
|
|
|
128 |
if ((nunchuck_buf[5] >> 5) & 1)
|
|
|
129 |
accel_y_axis += 1;
|
|
|
130 |
|
|
|
131 |
if ((nunchuck_buf[5] >> 6) & 1)
|
|
|
132 |
accel_z_axis += 2;
|
|
|
133 |
if ((nunchuck_buf[5] >> 7) & 1)
|
|
|
134 |
accel_z_axis += 1;
|
|
|
135 |
|
|
|
136 |
Serial.print(i,DEC);
|
|
|
137 |
Serial.print("\t");
|
|
|
138 |
|
|
|
139 |
Serial.print("joy:");
|
|
|
140 |
Serial.print(joy_x_axis,DEC);
|
|
|
141 |
Serial.print(",");
|
|
|
142 |
Serial.print(joy_y_axis, DEC);
|
|
|
143 |
Serial.print(" \t");
|
|
|
144 |
|
|
|
145 |
Serial.print("acc:");
|
|
|
146 |
Serial.print(accel_x_axis, DEC);
|
|
|
147 |
Serial.print(",");
|
|
|
148 |
Serial.print(accel_y_axis, DEC);
|
|
|
149 |
Serial.print(",");
|
|
|
150 |
Serial.print(accel_z_axis, DEC);
|
|
|
151 |
Serial.print("\t");
|
|
|
152 |
|
|
|
153 |
Serial.print("but:");
|
|
|
154 |
Serial.print(z_button, DEC);
|
|
|
155 |
Serial.print(",");
|
|
|
156 |
Serial.print(c_button, DEC);
|
|
|
157 |
|
|
|
158 |
Serial.print("\r\n"); // newline
|
|
|
159 |
i++;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
// returns zbutton state: 1=pressed, 0=notpressed
|
|
|
163 |
static int nunchuck_zbutton()
|
|
|
164 |
{
|
|
|
165 |
return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1; // voodoo
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
// returns zbutton state: 1=pressed, 0=notpressed
|
|
|
169 |
static int nunchuck_cbutton()
|
|
|
170 |
{
|
|
|
171 |
return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; // voodoo
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// returns value of x-axis joystick
|
|
|
175 |
static int nunchuck_joyx()
|
|
|
176 |
{
|
|
|
177 |
return nunchuck_buf[0];
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
// returns value of y-axis joystick
|
|
|
181 |
static int nunchuck_joyy()
|
|
|
182 |
{
|
|
|
183 |
return nunchuck_buf[1];
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// returns value of x-axis accelerometer
|
|
|
187 |
static int nunchuck_accelx()
|
|
|
188 |
{
|
|
|
189 |
return nunchuck_buf[2]; // FIXME: this leaves out 2-bits of the data
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// returns value of y-axis accelerometer
|
|
|
193 |
static int nunchuck_accely()
|
|
|
194 |
{
|
|
|
195 |
return nunchuck_buf[3]; // FIXME: this leaves out 2-bits of the data
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
// returns value of z-axis accelerometer
|
|
|
199 |
static int nunchuck_accelz()
|
|
|
200 |
{
|
|
|
201 |
return nunchuck_buf[4]; // FIXME: this leaves out 2-bits of the data
|
|
|
202 |
}
|