-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiiNunchuk.h
280 lines (245 loc) · 6.76 KB
/
WiiNunchuk.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Wii Nunchuk Arduino library
*
* created 04 Feb 2021
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/DS3231
* Original source: https://github.com/infusion/Fritzing/tree/master/Nunchuk
*
* Licensed under the MIT License
* https://opensource.org/licenses/MIT
*
*/
#ifndef NUNCHUK_H
#define NUNCHUK_H
#include <Wire.h>
// Calibration accelerometer values, depends on your Nunchuk
#define NUNCHUK_ACCEL_X_ZERO 512
#define NUNCHUK_ACCEL_Y_ZERO 512
#define NUNCHUK_ACCEL_Z_ZERO 512
// Calibration joystick values
#define NUNCHUK_JOYSTICK_X_ZERO 127
#define NUNCHUK_JOYSTICK_Y_ZERO 128
// Whether to disable encryption. Enabling encryption means that every packet
// must be decrypted, which wastes cpu cycles. Cheap Nunchuk clones have
// problems with the encrypted init sequence, so be sure you know what you're
// doing
#define NUNCHUK_DISABLE_ENCRYPTION
// Print debug information instead of a CSV stream to the serial port
// #define NUNCHUK_DEBUG
// The Nunchuk I2C address
#define NUNCHUK_ADDRESS 0x52
#if ARDUINO >= 100
#define I2C_READ() Wire.read()
#define I2C_WRITE(x) Wire.write(x)
#else
#define I2C_READ() Wire.receive()
#define I2C_WRITE(x) Wire.send(x)
#endif
#define I2C_START(x) Wire.beginTransmission(x)
#define I2C_STOP() Wire.endTransmission(true)
uint8_t nunchuk_data[6];
uint8_t nunchuk_cali[16];
#if defined(__AVR_ATmega168__) || \
defined(__AVR_ATmega328P__) // Only Arduino UNO
/**
* Use normal analog ports as power supply, which is useful if you want to have
* all pins in a row Like for the famous WiiChuck adapter
* @see
* https://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
*/
static void nunchuk_init_power() {
// Add power supply for port C2 (GND) and C3 (PWR)
PORTC &= ~_BV(PORTC2);
PORTC |= _BV(PORTC3);
DDRC |= _BV(PORTC2) | _BV(PORTC3);
delay(100);
}
#endif
/**
* Initializes the Nunchuk communication by sending a sequence of bytes
*/
static void nunchuk_init() {
#ifdef NUNCHUK_DISABLE_ENCRYPTION
I2C_START(NUNCHUK_ADDRESS);
I2C_WRITE(0xF0);
I2C_WRITE(0x55);
I2C_STOP();
// delay(1);
I2C_START(NUNCHUK_ADDRESS);
I2C_WRITE(0xFB);
I2C_WRITE(0x00);
I2C_STOP();
// delay(1);
#else
I2C_START(NUNCHUK_ADDRESS);
I2C_WRITE(0x40);
I2C_WRITE(0x00);
I2C_STOP();
// delay(1);
#endif
#ifdef NUNCHUK_DEBUG
Serial.print("Ident: "); // 0xA4200000 for Nunchuck, 0xA4200101 for Classic,
// 0xA4200402 for Balance
I2C_START(NUNCHUK_ADDRESS);
I2C_WRITE(0xFA);
I2C_STOP();
Wire.requestFrom(NUNCHUK_ADDRESS, 6);
for (uint8_t i = 0; i < 6; i++) {
if (Wire.available()) {
Serial.print(I2C_READ(), HEX);
Serial.print(" ");
}
}
I2C_STOP();
Serial.println("");
delay(100); // Wait for serial transfer, before loop()ing
#endif
}
/**
* Decodes a byte if encryption is used
*
* @param x The byte to be decoded
*/
static inline uint8_t nunchuk_decode_byte(uint8_t x) {
#ifdef NUNCHUK_DISABLE_ENCRYPTION
return x;
#else
return (x ^ 0x17) + 0x17;
#endif
}
/**
* Central function to read a full chunk of data from Nunchuk
*
* @return A boolean if the data transfer was successful
*/
static uint8_t nunchuk_read() {
uint8_t i;
Wire.requestFrom(NUNCHUK_ADDRESS, 6);
#ifdef SAM
delayMicroseconds(10);
#endif
for (i = 0; i < 6 && Wire.available(); i++) {
nunchuk_data[i] = nunchuk_decode_byte(I2C_READ());
}
I2C_START(NUNCHUK_ADDRESS);
I2C_WRITE(0x00);
#ifdef SAM
delayMicroseconds(100);
#endif
I2C_STOP();
return i == 6;
}
/**
* Checks the current state of button Z
*/
static uint8_t nunchuk_buttonZ() { return (~nunchuk_data[5] >> 0) & 1; }
/**
* Checks the current state of button C
*/
static uint8_t nunchuk_buttonC() { return (~nunchuk_data[5] >> 1) & 1; }
/**
* Retrieves the raw X-value of the joystick
*/
static uint8_t nunchuk_joystickX_raw() { return nunchuk_data[0]; }
/**
* Retrieves the raw Y-value of the joystick
*/
static uint8_t nunchuk_joystickY_raw() { return nunchuk_data[1]; }
/**
* Retrieves the calibrated X-value of the joystick
*/
static int16_t nunchuk_joystickX() {
return (int16_t)nunchuk_joystickX_raw() - (int16_t)NUNCHUK_JOYSTICK_X_ZERO;
}
/**
* Retrieves the calibrated Y-value of the joystick
*/
static int16_t nunchuk_joystickY() {
return (int16_t)nunchuk_joystickY_raw() - (int16_t)NUNCHUK_JOYSTICK_Y_ZERO;
}
/**
* Calculates the angle of the joystick
*/
static float nunchuk_joystick_angle() {
return atan2((float)nunchuk_joystickY(), (float)nunchuk_joystickX());
}
/**
* Retrieves the raw X-value of the accelerometer
*/
static uint16_t nunchuk_accelX_raw() {
return ((uint16_t)nunchuk_data[2] << 2) | ((nunchuk_data[5] >> 2) & 3);
}
/**
* Retrieves the raw Y-value of the accelerometer
*/
static uint16_t nunchuk_accelY_raw() {
return ((uint16_t)nunchuk_data[3] << 2) | ((nunchuk_data[5] >> 4) & 3);
}
/**
* Retrieves the raw Z-value of the accelerometer
*/
static uint16_t nunchuk_accelZ_raw() {
return ((uint16_t)nunchuk_data[4] << 2) | ((nunchuk_data[5] >> 6) & 3);
}
/**
* Retrieves the calibrated X-value of the accelerometer
*/
static int16_t nunchuk_accelX() {
return (int16_t)nunchuk_accelX_raw() - (int16_t)NUNCHUK_ACCEL_X_ZERO;
}
/**
* Retrieves the calibrated Y-value of the accelerometer
*/
static int16_t nunchuk_accelY() {
return (int16_t)nunchuk_accelY_raw() - (int16_t)NUNCHUK_ACCEL_Y_ZERO;
}
/**
* Retrieves the calibrated Z-value of the accelerometer
*/
static int16_t nunchuk_accelZ() {
return (int16_t)nunchuk_accelZ_raw() - (int16_t)NUNCHUK_ACCEL_Z_ZERO;
}
/**
* Calculates the pitch angle THETA around y-axis of the Nunchuk in radians
*/
static float nunchuk_pitch() { // tilt-y
return atan2((float)nunchuk_accelY(), (float)nunchuk_accelZ());
}
/**
* Calculates the roll angle PHI around x-axis of the Nunchuk in radians
*/
static float nunchuk_roll() { // tilt-x
return atan2((float)nunchuk_accelX(), (float)nunchuk_accelZ());
}
/**
* A handy function to print either verbose information of the Nunchuk or a CSV
* stream for Processing
*/
static void nunchuk_print() {
#ifdef NUNCHUK_DEBUG
char buf[100];
sprintf(buf, "Joy X=%4d Y=%4d Acc X=%4d Y=%4d Z=%4d Btn Z=%1d C=%1d\n",
nunchuk_joystickX(), nunchuk_joystickY(), nunchuk_accelX(),
nunchuk_accelY(), nunchuk_accelZ(), nunchuk_buttonZ(),
nunchuk_buttonC());
Serial.print(buf);
#else
Serial.print(nunchuk_joystickX(), DEC);
Serial.print(",");
Serial.print(nunchuk_joystickY(), DEC);
Serial.print(",");
Serial.print(nunchuk_accelX(), DEC);
Serial.print(",");
Serial.print(nunchuk_accelY(), DEC);
Serial.print(",");
Serial.print(nunchuk_accelZ(), DEC);
Serial.print(",");
Serial.print(nunchuk_buttonZ(), DEC);
Serial.print(",");
Serial.print(nunchuk_buttonC(), DEC);
Serial.print("\n");
#endif
}
#endif