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.
Revision 4:9169c602b0d8, committed 2011-02-28
- Comitter:
- FrankWeissenborn
- Date:
- Mon Feb 28 06:49:27 2011 +0000
- Parent:
- 3:13e1d0a345a6
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiiChuck.c Mon Feb 28 06:49:27 2011 +0000 @@ -0,0 +1,140 @@ +#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; + } + _oldC = 0; + _oldZ = 0; +} + +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; + } +} + +void WiiChuck::start() +{ + _getValues.attach(this, &WiiChuck::getValues,0.2); +} +void WiiChuck::stop() +{ + _getValues.detach(); +} + +void WiiChuck::getValues() +{ + int joyX = 0;int joyY = 0; + int accX = 0;int accY = 0;int accZ = 0; + int buttonC = 0;int buttonZ = 0; + + bool read = Read(&joyX,&joyY,&accX,&accY,&accZ,&buttonC,&buttonZ); + + if(read) + { + //analyse + if(_oldC == 0 && buttonC == 1) + { + _oldC = 1; + _callback_input(BUTTON_CANCEL_VALUE); + return; + } + else + { + _oldC = buttonC; + } + + //analyse + if(_oldZ == 0 && buttonZ == 1) + { + _oldZ = 1; + _callback_input(BUTTON_OK_VALUE); + return; + } + else + { + _oldZ = buttonZ; + } + + if(joyY>160) + { + _callback_input(BUTTON_VOLUME_PLUS); + return; + } + if(joyY<80) + { + + _callback_input(BUTTON_VOLUME_MINUS); + return; + } + if(joyX>160) + { + _callback_input(BUTTON_NEXT_VALUE); + return; + } + if(joyX<80) + { + + _callback_input(BUTTON_PREV_VALUE); + return; + } + + } +} +void WiiChuck::attach(pt2Func function) +{ + _callback_input = function; +} + +
--- a/WiiChuck.cpp Wed Feb 02 19:44:59 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,140 +0,0 @@ -#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; - } - _oldC = 0; - _oldZ = 0; -} - -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; - } -} - -void WiiChuck::start() -{ - _getValues.attach(this, &WiiChuck::getValues,0.2); -} -void WiiChuck::stop() -{ - _getValues.detach(); -} - -void WiiChuck::getValues() -{ - int joyX = 0;int joyY = 0; - int accX = 0;int accY = 0;int accZ = 0; - int buttonC = 0;int buttonZ = 0; - - bool read = Read(&joyX,&joyY,&accX,&accY,&accZ,&buttonC,&buttonZ); - - if(read) - { - //analyse - if(_oldC == 0 && buttonC == 1) - { - _oldC = 1; - _callback_input(BUTTON_CANCEL_VALUE); - return; - } - else - { - _oldC = buttonC; - } - - //analyse - if(_oldZ == 0 && buttonZ == 1) - { - _oldZ = 1; - _callback_input(BUTTON_OK_VALUE); - return; - } - else - { - _oldZ = buttonZ; - } - - if(joyY>160) - { - _callback_input(BUTTON_VOLUME_PLUS); - return; - } - if(joyY<80) - { - - _callback_input(BUTTON_VOLUME_MINUS); - return; - } - if(joyX>160) - { - _callback_input(BUTTON_NEXT_VALUE); - return; - } - if(joyX<80) - { - - _callback_input(BUTTON_PREV_VALUE); - return; - } - - } -} -void WiiChuck::attach(pt2Func function) -{ - _callback_input = function; -} - -
--- a/WiiChuck.h Wed Feb 02 19:44:59 2011 +0000 +++ b/WiiChuck.h Mon Feb 28 06:49:27 2011 +0000 @@ -1,3 +1,30 @@ +/* mbed audio player + * Copyright (c) 2010, 2011 Frank Weissenborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * +/* This code based on: + * Wii Nunchuck + * http://mbed.org/users/knaka/libraries/WiiNunchuck/ljkynp + * 2010-12-18 + */ + #ifndef __WIICHUCK_H #define __WIICHUCK_H