Generalized adaptation of the WiiChuk_compat library.
Fork of WiiChuk_compat by
WiiChuck.cpp
- Committer:
- d34d
- Date:
- 2014-12-20
- Revision:
- 2:9c4ac1a64c30
- Parent:
- 1:a5dbc8a15c83
- Child:
- 3:fcc2f24d0644
File content as of revision 2:9c4ac1a64c30:
#include "WiiChuck.h" WiiChuck::WiiChuck(PinName data, PinName clk):_i2c(data, clk), _callback(NULL) { _i2c.frequency(400000); // should work at 100000 too Error = true; // initialize Wii extension (nunchuk) based on "new way" initialization // http://wiibrew.org/wiki/Wiimote#The_New_Way // also disables need for decryption unsigned char cmd[] = {0xF0, 0x55}; if (_i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) { Error = false; } cmd[0] = 0xfb; cmd[1]= 0x00; _i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)); } bool WiiChuck::read(nunchuck_data_t* data) { //int i; char readBuf[NUNCHUCK_READLEN]; if (Error) { return false; } const unsigned char cmd[] = {0x00}; if (_i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) { wait(I2C_READ_DELAY); if (_i2c.read(NUNCHUCK_ADDR, readBuf, sizeof(readBuf)) == I2C_ACK) { data->joyX = readBuf[JOY_X_IDX]; data->joyY = readBuf[JOY_Y_IDX]; data->accX = (uint16_t) readBuf[ACC_X_IDX] << 2 + ((readBuf[BUTTON_IDX] >> 2) & 0x03); data->accY = (uint16_t) readBuf[ACC_Y_IDX] << 2 + ((readBuf[BUTTON_IDX] >> 4) & 0x03); data->accZ = (uint16_t) readBuf[ACC_Z_IDX] << 2 + ((readBuf[BUTTON_IDX] >> 6) & 0x03); data->buttonZ = !(readBuf[BUTTON_IDX] & 0x01); data->buttonC = !(readBuf[BUTTON_IDX] & 0x02); return true; } } return false; } void WiiChuck::attach(pt2Func function, float t) { _callback = function; _getValues.attach(this, &WiiChuck::getValues, t); } void WiiChuck::detach() { _getValues.detach(); _callback = NULL; } void WiiChuck::getValues() { bool hasData = read(&_data); if(hasData) { _callback(&_data); } }