Frank Weissenborn / WiiChuck

Dependents:   WiiNunchuckTest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WiiChuck.c Source File

WiiChuck.c

00001 #include "WiiChuck.h"
00002 
00003 
00004 
00005 WiiChuck::WiiChuck(PinName data, PinName clk):_i2c(data, clk) {
00006     Error = true;
00007     unsigned char cmd[] = {NUNCHUCK_REGADDR, 0x00};
00008     if (_i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) {
00009         Error = false;
00010     }
00011     _oldC = 0;
00012     _oldZ = 0;
00013 }
00014 
00015 bool WiiChuck::Read(int* joyX,int* joyY,int* accX,int* accY,int* accZ,int* buttonC,int* buttonZ) {
00016 
00017     int i;
00018     char readBuf[NUNCHUCK_READLEN];
00019     
00020     if (Error) {
00021         return false;
00022     }
00023     
00024     const unsigned char cmd[] = {0x00};
00025     if (_i2c.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd)) == I2C_ACK) {
00026         wait(I2C_READ_DELAY);
00027         if (_i2c.read(NUNCHUCK_ADDR, readBuf, sizeof(readBuf)) == I2C_ACK) {
00028             //init values
00029             *joyX = 0; *joyY = 0; *accX = 0; *accY = 0; *accZ = 0; *buttonC = 0; *buttonZ = 0;
00030    
00031             for (i = 0; i < NUNCHUCK_READLEN; ++i) {
00032                 readBuf[i] = (readBuf[i] ^ 0x17) + 0x17;
00033             }
00034             *joyX = readBuf[Joy_X];
00035             *joyY = readBuf[Joy_Y];
00036             *accX = readBuf[Acc_X] << 2;
00037             *accY = readBuf[Acc_Y] << 2;
00038             *accZ = readBuf[Acc_Z] << 2;
00039 
00040             if (readBuf[Button] & 0x01) {
00041                 *buttonZ = 0;
00042             } else {
00043                 *buttonZ = 1;
00044             }
00045             if (readBuf[Button] & 0x02) {
00046                 *buttonC = 0;
00047             } else {
00048                 *buttonC = 1;
00049             }
00050             if (readBuf[Button] & 0x04) accX += 2;
00051             if (readBuf[Button] & 0x08) accX += 1;
00052             if (readBuf[Button] & 0x10) accY += 2;
00053             if (readBuf[Button] & 0x20) accY += 1;
00054             if (readBuf[Button] & 0x40) accZ += 2;
00055             if (readBuf[Button] & 0x80) accZ += 1;
00056             return true;
00057         }
00058         else
00059         {
00060             return false;
00061         }
00062     } else {
00063         return false;
00064     }
00065 }
00066 
00067 void WiiChuck::start()
00068 {
00069     _getValues.attach(this, &WiiChuck::getValues,0.2);
00070 }
00071 void WiiChuck::stop()
00072 {
00073      _getValues.detach();
00074 }
00075 
00076 void WiiChuck::getValues()
00077 {
00078     int joyX = 0;int joyY = 0;
00079     int accX = 0;int accY = 0;int accZ = 0;
00080     int buttonC = 0;int buttonZ = 0;
00081      
00082     bool read = Read(&joyX,&joyY,&accX,&accY,&accZ,&buttonC,&buttonZ);
00083   
00084     if(read)
00085     {
00086         //analyse
00087         if(_oldC == 0 && buttonC == 1)
00088         {
00089             _oldC = 1;
00090             _callback_input(BUTTON_CANCEL_VALUE);
00091             return;
00092         }
00093         else
00094         {
00095             _oldC = buttonC;
00096         }
00097         
00098         //analyse
00099         if(_oldZ == 0 && buttonZ == 1)
00100         {
00101             _oldZ = 1;
00102             _callback_input(BUTTON_OK_VALUE);
00103             return;
00104         }
00105         else
00106         {
00107             _oldZ = buttonZ;
00108         }
00109         
00110         if(joyY>160)
00111         {
00112             _callback_input(BUTTON_VOLUME_PLUS);
00113             return;
00114         }
00115         if(joyY<80)
00116         {
00117         
00118             _callback_input(BUTTON_VOLUME_MINUS);
00119             return;
00120         }
00121         if(joyX>160)
00122         {
00123             _callback_input(BUTTON_NEXT_VALUE);
00124             return;
00125         }
00126         if(joyX<80)
00127         {
00128         
00129             _callback_input(BUTTON_PREV_VALUE);
00130             return;
00131         }
00132         
00133     }
00134 }
00135 void WiiChuck::attach(pt2Func function)
00136 {
00137     _callback_input = function;
00138 }
00139 
00140