Generalized adaptation of the WiiChuk_compat library.
Fork of WiiChuk_compat by
WiiChuck.cpp
- Committer:
- d34d
- Date:
- 2014-12-19
- Revision:
- 1:a5dbc8a15c83
- Child:
- 2:9c4ac1a64c30
File content as of revision 1:a5dbc8a15c83:
#include "WiiChuck.h" WiiChuck::WiiChuck(PinName data, PinName clk):_i2c(data, clk) { _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)); _oldC = 0; _oldZ = 0; } 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]; data->joyY = readBuf[Joy_Y]; data->accX = (uint16_t) readBuf[Acc_X] << 2 + ((readBuf[Button] >> 2) & 0x03); data->accY = (uint16_t) readBuf[Acc_Y] << 2 + ((readBuf[Button] >> 4) & 0x03); data->accZ = (uint16_t) readBuf[Acc_Z] << 2 + ((readBuf[Button] >> 6) & 0x03); data->buttonZ = !(readBuf[Button] & 0x01); data->buttonC = !(readBuf[Button] & 0x02); return true; } } return false; } void WiiChuck::start() { _getValues.attach(this, &WiiChuck::getValues,0.2); } void WiiChuck::stop() { _getValues.detach(); } void WiiChuck::getValues() { nunchuck_data_t data; bool hasData = read(&data); if(hasData) { //analyse if(!_oldC && data.buttonC) { _oldC = true; _callback_input(BUTTON_CANCEL_VALUE); return; } else { _oldC = data.buttonC; } //analyse if(!_oldZ&& data.buttonZ) { _oldZ = true; _callback_input(BUTTON_OK_VALUE); return; } else { _oldZ = data.buttonZ; } if(data.joyY>160) { _callback_input(BUTTON_VOLUME_PLUS); return; } if(data.joyY<80) { _callback_input(BUTTON_VOLUME_MINUS); return; } if(data.joyX>160) { _callback_input(BUTTON_NEXT_VALUE); return; } if(data.joyX<80) { _callback_input(BUTTON_PREV_VALUE); return; } } } void WiiChuck::attach(pt2Func function) { _callback_input = function; }