-
class
Nunchuk {
5
public:
-
void
initialize();
-
bool
update();
-
-
int
joystick_x()
10
int
joystick_y()
-
-
int
x_acceleration()
-
return
((int)(_buffer[2]) << 2) | ((_buffer[5] >> 2) & 0x03);
-
}
15
-
int
y_acceleration()
-
return
((int)(_buffer[3]) << 2) | ((_buffer[5] >> 4) & 0x03);
-
}
-
20
int
z_acceleration()
-
return
((int)(_buffer[4]) << 2) | ((_buffer[5] >> 6) & 0x03);
-
}
-
bool
z_button()
const
-
bool
c_button()
const
25
-
private:
-
void
request_data();
-
char decode_byte(const
-
30
unsigned char
_buffer[NUNCHUK_BUFFER_SIZE];
-
};
-
-
#endif
-
This small C++ class is all you need to use a Nunchuk controller with your
Arduino. It starts with a double-include prevention mechanism: it checks
whether a preprocessor macro named
using
#ifndef
. If it hasn t been defined, we define it and continue with the
declaration of the
Nunchuk
ration, so you can safely include this header file more than once in your
application.
In line 3, we create a constant for the size of the array we need to store the
data the Nunchuk returns. We define this array in line 31, and in this case,
we define the constant using the preprocessor instead of the
because array constants must be known at compile time in C++.
Then the actual declaration of the
nication channel between the Arduino and the Nunchuk, you have to invoke
const
{
return
_buffer[0]; }
const
{
return
_buffer[1]; }
const
{
const
{
const
{
{
return
!(_buffer[5] & 0x01); }
{
return
!(_buffer[5] & 0x02); }
char);
__NUNCHUK_H__
class. Otherwise, the preprocessor skips the decla-
Nunchuk
www.it-ebooks.info
Building a Nunchuk Class
has been defined already
const
class begins. To initiate the commu-
149
keyword,
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?