Bit of a tidyup, added arduino_make.sh for commandline builds on Linux
[KuttMonster.git] / nunchuck_funcs.h
blob792d472f3e4b2e43fc918319bd3306d5db295a01
1 /*
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
13 #include <WProgram.h>
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()
20 #define pwrpin PORTC3
21 #define gndpin PORTC2
22 DDRC |= _BV(pwrpin) | _BV(gndpin);
23 PORTC &=~ _BV(gndpin);
24 PORTC |= _BV(pwrpin);
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
40 // was "send_zero()"
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;
53 return x;
56 // Receive data back from the nunchuck,
57 // returns 1 on successful read. returns 0 on failure
58 static int nunchuck_get_data()
60 int cnt=0;
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());
65 cnt++;
67 nunchuck_send_request(); // send request for next data payload
68 // If we recieved the 6 bytes, then go print them
69 if (cnt >= 5) {
70 return 1; // success
72 return 0; //failure
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()
82 static int i=0;
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;
89 int z_button = 0;
90 int c_button = 0;
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)
96 z_button = 1;
97 if ((nunchuck_buf[5] >> 1) & 1)
98 c_button = 1;
100 if ((nunchuck_buf[5] >> 2) & 1)
101 accel_x_axis += 2;
102 if ((nunchuck_buf[5] >> 3) & 1)
103 accel_x_axis += 1;
105 if ((nunchuck_buf[5] >> 4) & 1)
106 accel_y_axis += 2;
107 if ((nunchuck_buf[5] >> 5) & 1)
108 accel_y_axis += 1;
110 if ((nunchuck_buf[5] >> 6) & 1)
111 accel_z_axis += 2;
112 if ((nunchuck_buf[5] >> 7) & 1)
113 accel_z_axis += 1;
115 Serial.print(i,DEC);
116 Serial.print("\t");
118 Serial.print("joy:");
119 Serial.print(joy_x_axis,DEC);
120 Serial.print(",");
121 Serial.print(joy_y_axis, DEC);
122 Serial.print(" \t");
124 Serial.print("acc:");
125 Serial.print(accel_x_axis, DEC);
126 Serial.print(",");
127 Serial.print(accel_y_axis, DEC);
128 Serial.print(",");
129 Serial.print(accel_z_axis, DEC);
130 Serial.print("\t");
132 Serial.print("but:");
133 Serial.print(z_button, DEC);
134 Serial.print(",");
135 Serial.print(c_button, DEC);
137 Serial.print("\r\n"); // newline
138 i++;
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