Dependents: HoverboardTest RobotArmControl
Revision 2:9652195745e8, committed 2010-12-18
- Comitter:
- knaka
- Date:
- Sat Dec 18 11:19:14 2010 +0000
- Parent:
- 1:2fde37da31b9
- Commit message:
Changed in this revision
diff -r 2fde37da31b9 -r 9652195745e8 TexLCDP.lib --- a/TexLCDP.lib Tue Nov 23 05:50:09 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/pom911/code/TexLCDP/#cb784b022ec8
diff -r 2fde37da31b9 -r 9652195745e8 WiiNunchuck.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiiNunchuck.cpp Sat Dec 18 11:19:14 2010 +0000 @@ -0,0 +1,115 @@ +#include "mbed.h" +#include "WiiNunchuck.h" + +WiiNunchuck::WiiNunchuck(PinName p_sda, PinName p_scl):i2c(p_sda, p_scl) +{ + WiiNunInitFlag = init(); + timer.start(); +} + + +bool WiiNunchuck::init() +{ + bool result = false; + unsigned char cmd[] = {NUNCHUCK_REGADDR, 0x00}; + if (i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) { + result = true; + } + return result; +} + + +void WiiNunchuck::read() +{ + int i; + + if(timer.read_ms() < 50) return; + + if(!WiiNunInitFlag) { + WiiNunInitFlag = init(); + } + + if(WiiNunInitFlag) { + 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) { + 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 += 1; + if(readBuf[Button] & 0x08) accX += 2; + if(readBuf[Button] & 0x10) accY += 1; + if(readBuf[Button] & 0x20) accY += 2; + if(readBuf[Button] & 0x40) accZ += 1; + if(readBuf[Button] & 0x80) accZ += 2; + } + } + } + timer.reset(); +} + + +unsigned char WiiNunchuck::joyx() +{ + read(); + return joyX; +} + + +unsigned char WiiNunchuck::joyy() +{ + read(); + return joyY; +} + + +int WiiNunchuck::accx() +{ + read(); + return accX; +} + + +int WiiNunchuck::accy() +{ + read(); + return accY; +} + + +int WiiNunchuck::accz() +{ + read(); + return accZ; +} + + +bool WiiNunchuck::buttonc() +{ + read(); + return buttonC; +} + + +bool WiiNunchuck::buttonz() +{ + read(); + return buttonZ; +}
diff -r 2fde37da31b9 -r 9652195745e8 WiiNunchuck.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiiNunchuck.h Sat Dec 18 11:19:14 2010 +0000 @@ -0,0 +1,44 @@ +#ifndef WII_NUNCHUCK_H +#define WII_NUNCHUCK_H + +#include "mbed.h" + +// I2C +#define NUNCHUCK_ADDR 0xA4 // 0x52 << 1 +#define NUNCHUCK_REGADDR 0x40 // +#define NUNCHUCK_READLEN 0x06 // +#define I2C_ACK 0 +#define I2C_READ_DELAY 0.01 + +#define Joy_X 0 +#define Joy_Y 1 +#define Acc_X 2 +#define Acc_Y 3 +#define Acc_Z 4 +#define Button 5 + +class WiiNunchuck { +public: + WiiNunchuck(PinName p_sda, PinName p_scl); + //read(); + unsigned char joyx(); + unsigned char joyy(); + int accx(); + int accy(); + int accz(); + bool buttonc(); + bool buttonz(); +private: + I2C i2c; + Timer timer; + bool WiiNunInitFlag; //= false; + char readBuf[NUNCHUCK_READLEN]; + unsigned char joyX, joyY; + int accX, accY, accZ; + bool buttonC, buttonZ; + bool init(); + void read(); +}; + + +#endif \ No newline at end of file
diff -r 2fde37da31b9 -r 9652195745e8 main.cpp --- a/main.cpp Tue Nov 23 05:50:09 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -// -// WiiNunchuck -// - -#include "mbed.h" -#include "TextLCD.h" - -// I2C -#define NUNCHUCK_ADDR 0xA4 // 0x52 << 1 -#define NUNCHUCK_REGADDR 0x40 // -#define NUNCHUCK_READLEN 0x06 // -#define I2C_ACK 0 -#define I2C_READ_DELAY 0.01 - -#define Joy_X 0 -#define Joy_Y 1 -#define Acc_X 2 -#define Acc_Y 3 -#define Acc_Z 4 -#define Button 5 - -bool WiiNunInitFlag = false; -int joyX = 0, joyY = 0, accX = 0, accY = 0, accZ = 0, buttonC = 0, buttonZ = 0; -char readBuf[NUNCHUCK_READLEN]; -I2C WiiNun(p9, p10); - -TextLCD lcd(p24, p26, p27, p28, p29, p30); - -bool WiiNunInit() -{ - bool result = false; - unsigned char cmd[] = {NUNCHUCK_REGADDR, 0x00}; - if (WiiNun.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) { - result = true; - } - return result; -} - -void WiiNunRead() -{ - int i; - - if(!WiiNunInitFlag) { - WiiNunInitFlag = WiiNunInit(); - } - - if(WiiNunInitFlag) { - const unsigned char cmd[] = {0x00}; - if (WiiNun.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) { - wait(I2C_READ_DELAY); - if (WiiNun.read(NUNCHUCK_ADDR, readBuf, sizeof(readBuf)) == I2C_ACK) { - 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 += 1; - if(readBuf[Button] & 0x08) accX += 2; - if(readBuf[Button] & 0x10) accY += 1; - if(readBuf[Button] & 0x20) accY += 2; - if(readBuf[Button] & 0x40) accZ += 1; - if(readBuf[Button] & 0x80) accZ += 2; - } - } - } -} - - -int main() { - while(1) { - WiiNunRead(); - lcd.cls(); - lcd.printf("x%3d y%3d c%1d z%1d", joyX, joyY, buttonC, buttonZ); - lcd.locate(0, 1); - lcd.printf("x%d y%d z%d", accX, accY, accZ); - wait(0.1); - } -}
diff -r 2fde37da31b9 -r 9652195745e8 mbed.bld --- a/mbed.bld Tue Nov 23 05:50:09 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e