Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WiiChuck.c
- Committer:
- FrankWeissenborn
- Date:
- 2010-12-12
- Revision:
- 0:f442b7eb68b0
- Child:
- 1:f04b559e06b3
File content as of revision 0:f442b7eb68b0:
#include "WiiChuck.h" WiiChuck::WiiChuck(PinName data, PinName clk):_i2c(data, clk) { Error = true; unsigned char cmd[] = {NUNCHUCK_REGADDR, 0x00}; if (_i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) { Error = false; } } bool WiiChuck::Read(int* joyX,int* joyY,int* accX,int* accY,int* accZ,int* buttonC,int* buttonZ) { 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) { //init values *joyX = 0; *joyY = 0; *accX = 0; *accY = 0; *accZ = 0; *buttonC = 0; *buttonZ = 0; for (i = 0; i < NUNCHUCK_READLEN; ++i) { readBuf[i] = (readBuf[i] ^ 0x17) + 0x17; } *joyX = readBuf[Joy_X]; *joyY = readBuf[Joy_Y]; *accX = readBuf[Acc_X] << 2; *accY = readBuf[Acc_Y] << 2; *accZ = readBuf[Acc_Z] << 2; if (readBuf[Button] & 0x01) { *buttonZ = 0; } else { *buttonZ = 1; } if (readBuf[Button] & 0x02) { *buttonC = 0; } else { *buttonC = 1; } if (readBuf[Button] & 0x04) accX += 2; if (readBuf[Button] & 0x08) accX += 1; if (readBuf[Button] & 0x10) accY += 2; if (readBuf[Button] & 0x20) accY += 1; if (readBuf[Button] & 0x40) accZ += 2; if (readBuf[Button] & 0x80) accZ += 1; return true; } else { return false; } } else { return false; } }