upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Member Functions | Data Fields
NUNCHUCK Class Reference

API for the Wii* Nunchuk controller. More...

Detailed Description

UPM module for the Wii Nunchuk controller. This module was tested with Wii Nunchuk connected to I2C via a Grove Wii Nunchuk adapter.

See http://wiibrew.org/wiki/Wiimote/Extension_Controllers and http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck for more details on the controller and its protocol.

A warning for the Grove Wii Nunchuk adapter: it has 2 traces on one side, and 3 traces on the other. Do not match these up with the Nunchuk connector's traces. The connector's 'Grove' should be on the same side as the Grove interface socket on the adapter.

nunchuck.jpg
// Instantiate a nunchuck controller bus 0
upm::NUNCHUCK *nunchuck = new upm::NUNCHUCK(0);
// always do this first
cout << "Initializing... " << endl;
if (!nunchuck->init())
{
cerr << "nunchuck->init() failed." << endl;
return 0;
}
while (shouldRun)
{
nunchuck->update();
cout << "stickX: " << nunchuck->stickX
<< ", stickY: " << nunchuck->stickY << endl;
cout << "accelX: " << nunchuck->accelX
<< ", accelY: " << nunchuck->accelY
<< ", accelZ: " << nunchuck->accelZ << endl;
cout << "button C: "
<< ((nunchuck->buttonC) ? "pressed" : "not pressed") << endl;
cout << "button Z: "
<< ((nunchuck->buttonZ) ? "pressed" : "not pressed") << endl;
cout << endl;
usleep(100000);
}

Public Member Functions

 NUNCHUCK (int bus, uint8_t addr=NUNCHUCK_I2C_ADDR)
 
 ~NUNCHUCK ()
 
bool writeByte (uint8_t reg, uint8_t byte)
 
int readBytes (uint8_t reg, uint8_t *buffer, int len)
 
bool init ()
 
void update ()
 

Data Fields

int stickX
 
int stickY
 
int accelX
 
int accelY
 
int accelZ
 
bool buttonC
 
bool buttonZ
 

Constructor & Destructor Documentation

NUNCHUCK ( int  bus,
uint8_t  addr = NUNCHUCK_I2C_ADDR 
)

NUNCHUCK constructor

Parameters
busI2C bus to use
addrI2C address to use
~NUNCHUCK ( )

NUNCHUCK destructor

Member Function Documentation

bool writeByte ( uint8_t  reg,
uint8_t  byte 
)

Writes value(s) into registers

Parameters
regRegister location to start writing into
byteByte to write
Returns
True if successful
int readBytes ( uint8_t  reg,
uint8_t *  buffer,
int  len 
)

Reads value(s) from registers

Parameters
regRegister location to start reading from
bufferBuffer for data storage
lenNumber of bytes to read
Returns
Number of bytes read
bool init ( )

Initializes the controller. Here, we disable encryption after delaying for a time to ensure the controller is ready.

Returns
True if initialization is successful
void update ( void  )

Reads and updates the current state of the controller.

Field Documentation

int stickX

Current analog stick X position

int stickY

Current analog stick Y position

int accelX

Current accelerometer X value

int accelY

Current accelerometer Y value

int accelZ

Current accelerometer Z value

bool buttonC

Button C pressed?

bool buttonZ

Button Z pressed?


The documentation for this class was generated from the following files: