Library to use the wii nunchuk distributed by Nintendo
Nunchuk.cpp@2:0a8f43931041, 2014-02-25 (annotated)
- Committer:
- adelino
- Date:
- Tue Feb 25 10:50:05 2014 +0000
- Revision:
- 2:0a8f43931041
- Parent:
- 0:37e9a0644485
start a new acquisition manually without internal Ticker
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adelino | 0:37e9a0644485 | 1 | #include "Nunchuk.h" |
adelino | 0:37e9a0644485 | 2 | |
adelino | 0:37e9a0644485 | 3 | Nunchuk::Nunchuk(PinName sda,PinName scl,float mTe):myI2C(sda,scl) |
adelino | 0:37e9a0644485 | 4 | { |
adelino | 0:37e9a0644485 | 5 | periodeTe=mTe; |
adelino | 0:37e9a0644485 | 6 | myI2C.frequency(100000); |
adelino | 0:37e9a0644485 | 7 | |
adelino | 0:37e9a0644485 | 8 | this->joyX=0x00; |
adelino | 0:37e9a0644485 | 9 | this->joyY=0x00; |
adelino | 0:37e9a0644485 | 10 | this->accX=0; |
adelino | 0:37e9a0644485 | 11 | this->accY=0; |
adelino | 0:37e9a0644485 | 12 | this->accZ=0; |
adelino | 0:37e9a0644485 | 13 | this->btnC=false; |
adelino | 0:37e9a0644485 | 14 | this->btnZ=false; |
adelino | 0:37e9a0644485 | 15 | |
adelino | 0:37e9a0644485 | 16 | setup(); |
adelino | 2:0a8f43931041 | 17 | |
adelino | 2:0a8f43931041 | 18 | if(periodeTe>0) |
adelino | 2:0a8f43931041 | 19 | myTicker.attach(this,&Nunchuk::process,periodeTe); |
adelino | 0:37e9a0644485 | 20 | } |
adelino | 0:37e9a0644485 | 21 | //----------------------- |
adelino | 0:37e9a0644485 | 22 | Nunchuk::~Nunchuk(void) |
adelino | 0:37e9a0644485 | 23 | { |
adelino | 2:0a8f43931041 | 24 | if(periodeTe>0) |
adelino | 0:37e9a0644485 | 25 | myTicker.detach(); |
adelino | 0:37e9a0644485 | 26 | } |
adelino | 0:37e9a0644485 | 27 | //----------------------- |
adelino | 2:0a8f43931041 | 28 | void Nunchuk::process(void) |
adelino | 0:37e9a0644485 | 29 | { |
adelino | 0:37e9a0644485 | 30 | bool res=false; |
adelino | 0:37e9a0644485 | 31 | static char datas[6]; |
adelino | 0:37e9a0644485 | 32 | |
adelino | 0:37e9a0644485 | 33 | int i=0; |
adelino | 0:37e9a0644485 | 34 | |
adelino | 0:37e9a0644485 | 35 | res=request(); //demande une mesure |
adelino | 0:37e9a0644485 | 36 | if(res==false) { |
adelino | 0:37e9a0644485 | 37 | return ; //pb |
adelino | 0:37e9a0644485 | 38 | } |
adelino | 0:37e9a0644485 | 39 | |
adelino | 0:37e9a0644485 | 40 | if(myI2C.read(ADRESSE,datas,6)==0) { |
adelino | 0:37e9a0644485 | 41 | res=true; |
adelino | 0:37e9a0644485 | 42 | |
adelino | 0:37e9a0644485 | 43 | for(i=0; i<6; i++) { |
adelino | 0:37e9a0644485 | 44 | datas[i]=decode(datas[i]); |
adelino | 0:37e9a0644485 | 45 | } |
adelino | 0:37e9a0644485 | 46 | |
adelino | 0:37e9a0644485 | 47 | // |
adelino | 0:37e9a0644485 | 48 | if(datas[0] < 100) |
adelino | 0:37e9a0644485 | 49 | joyX=-1; |
adelino | 0:37e9a0644485 | 50 | else if(datas[0] >200) |
adelino | 0:37e9a0644485 | 51 | joyX=1; |
adelino | 0:37e9a0644485 | 52 | else |
adelino | 0:37e9a0644485 | 53 | joyX=0; |
adelino | 0:37e9a0644485 | 54 | // |
adelino | 0:37e9a0644485 | 55 | if(datas[1] <100) |
adelino | 0:37e9a0644485 | 56 | joyY=-1; |
adelino | 0:37e9a0644485 | 57 | else if(datas[1] >140) |
adelino | 0:37e9a0644485 | 58 | joyY=1; |
adelino | 0:37e9a0644485 | 59 | else |
adelino | 0:37e9a0644485 | 60 | joyY=0; |
adelino | 0:37e9a0644485 | 61 | // |
adelino | 0:37e9a0644485 | 62 | accX=datas[2]<<2; |
adelino | 0:37e9a0644485 | 63 | accY=datas[3]<<2; |
adelino | 0:37e9a0644485 | 64 | accZ=datas[4]<<2; |
adelino | 0:37e9a0644485 | 65 | |
adelino | 0:37e9a0644485 | 66 | //btnZ |
adelino | 0:37e9a0644485 | 67 | if(datas[5] & 0x01) |
adelino | 0:37e9a0644485 | 68 | btnZ=false; |
adelino | 0:37e9a0644485 | 69 | else |
adelino | 0:37e9a0644485 | 70 | btnZ=true; |
adelino | 0:37e9a0644485 | 71 | |
adelino | 0:37e9a0644485 | 72 | |
adelino | 0:37e9a0644485 | 73 | //btnC |
adelino | 0:37e9a0644485 | 74 | if(datas[5]& 0x02) |
adelino | 0:37e9a0644485 | 75 | btnC=false; |
adelino | 0:37e9a0644485 | 76 | else |
adelino | 0:37e9a0644485 | 77 | btnC=true; |
adelino | 0:37e9a0644485 | 78 | |
adelino | 0:37e9a0644485 | 79 | accX+=(datas[5]>>2) & 0x0003; |
adelino | 0:37e9a0644485 | 80 | accY+=(datas[5]>>4) & 0x0003; |
adelino | 0:37e9a0644485 | 81 | accZ+=(datas[5]>>6) & 0x0003; |
adelino | 0:37e9a0644485 | 82 | |
adelino | 0:37e9a0644485 | 83 | |
adelino | 0:37e9a0644485 | 84 | } else { |
adelino | 0:37e9a0644485 | 85 | res=false; |
adelino | 0:37e9a0644485 | 86 | joyX=0; |
adelino | 0:37e9a0644485 | 87 | joyY=0; |
adelino | 0:37e9a0644485 | 88 | |
adelino | 0:37e9a0644485 | 89 | accX=0; |
adelino | 0:37e9a0644485 | 90 | accY=0; |
adelino | 0:37e9a0644485 | 91 | accZ=0; |
adelino | 0:37e9a0644485 | 92 | btnZ=false; |
adelino | 0:37e9a0644485 | 93 | btnC=false; |
adelino | 0:37e9a0644485 | 94 | } |
adelino | 0:37e9a0644485 | 95 | |
adelino | 0:37e9a0644485 | 96 | } |
adelino | 0:37e9a0644485 | 97 | //----------------------- |
adelino | 0:37e9a0644485 | 98 | bool Nunchuk::setup(void) |
adelino | 0:37e9a0644485 | 99 | { |
adelino | 0:37e9a0644485 | 100 | char cmd[2]; |
adelino | 0:37e9a0644485 | 101 | bool res=false; |
adelino | 0:37e9a0644485 | 102 | |
adelino | 0:37e9a0644485 | 103 | cmd[0]=0x40; |
adelino | 0:37e9a0644485 | 104 | cmd[1]=0x00; |
adelino | 0:37e9a0644485 | 105 | |
adelino | 0:37e9a0644485 | 106 | if(myI2C.write(ADRESSE,cmd,2)==0) { |
adelino | 0:37e9a0644485 | 107 | res=true; |
adelino | 0:37e9a0644485 | 108 | } else { |
adelino | 0:37e9a0644485 | 109 | res=false; |
adelino | 0:37e9a0644485 | 110 | } |
adelino | 0:37e9a0644485 | 111 | return res; |
adelino | 0:37e9a0644485 | 112 | } |
adelino | 0:37e9a0644485 | 113 | //---------------------------- |
adelino | 0:37e9a0644485 | 114 | bool Nunchuk::request(void) |
adelino | 0:37e9a0644485 | 115 | { |
adelino | 0:37e9a0644485 | 116 | bool res=false; |
adelino | 0:37e9a0644485 | 117 | char cmd[1]= {0x00}; |
adelino | 0:37e9a0644485 | 118 | |
adelino | 0:37e9a0644485 | 119 | if(myI2C.write(ADRESSE,cmd,1)==0) { |
adelino | 0:37e9a0644485 | 120 | res=true; |
adelino | 0:37e9a0644485 | 121 | wait(0.001); |
adelino | 0:37e9a0644485 | 122 | |
adelino | 0:37e9a0644485 | 123 | } else { |
adelino | 0:37e9a0644485 | 124 | res=false; |
adelino | 0:37e9a0644485 | 125 | } |
adelino | 0:37e9a0644485 | 126 | |
adelino | 0:37e9a0644485 | 127 | return res; |
adelino | 0:37e9a0644485 | 128 | } |
adelino | 0:37e9a0644485 | 129 | //----------------------------- |
adelino | 0:37e9a0644485 | 130 | float Nunchuk::getPeriodeTe(void) |
adelino | 0:37e9a0644485 | 131 | { |
adelino | 0:37e9a0644485 | 132 | return periodeTe; |
adelino | 0:37e9a0644485 | 133 | } |
adelino | 0:37e9a0644485 | 134 | //----------------------------- |
adelino | 0:37e9a0644485 | 135 | char Nunchuk::decode(char data) |
adelino | 0:37e9a0644485 | 136 | { |
adelino | 0:37e9a0644485 | 137 | return (data^0x17)+0x17; |
adelino | 0:37e9a0644485 | 138 | } |
adelino | 0:37e9a0644485 | 139 | //------------------------------ |
adelino | 0:37e9a0644485 | 140 | signed char Nunchuk::getJoyX(void) |
adelino | 0:37e9a0644485 | 141 | { |
adelino | 0:37e9a0644485 | 142 | return joyX; |
adelino | 0:37e9a0644485 | 143 | } |
adelino | 0:37e9a0644485 | 144 | //------------------------------- |
adelino | 0:37e9a0644485 | 145 | signed char Nunchuk::getJoyY(void) |
adelino | 0:37e9a0644485 | 146 | { |
adelino | 0:37e9a0644485 | 147 | return joyY; |
adelino | 0:37e9a0644485 | 148 | } |
adelino | 0:37e9a0644485 | 149 | //-------------------------------- |
adelino | 0:37e9a0644485 | 150 | int Nunchuk::getAccX(void) |
adelino | 0:37e9a0644485 | 151 | { |
adelino | 0:37e9a0644485 | 152 | return accX; |
adelino | 0:37e9a0644485 | 153 | } |
adelino | 0:37e9a0644485 | 154 | //------------------------------ |
adelino | 0:37e9a0644485 | 155 | int Nunchuk::getAccY(void) |
adelino | 0:37e9a0644485 | 156 | { |
adelino | 0:37e9a0644485 | 157 | return accY; |
adelino | 0:37e9a0644485 | 158 | } |
adelino | 0:37e9a0644485 | 159 | //---------------------------- |
adelino | 0:37e9a0644485 | 160 | int Nunchuk::getAccZ(void) |
adelino | 0:37e9a0644485 | 161 | { |
adelino | 0:37e9a0644485 | 162 | return accZ; |
adelino | 0:37e9a0644485 | 163 | } |
adelino | 0:37e9a0644485 | 164 | //----------------------------- |
adelino | 0:37e9a0644485 | 165 | bool Nunchuk::getBtnC(void) |
adelino | 0:37e9a0644485 | 166 | { |
adelino | 0:37e9a0644485 | 167 | return btnC; |
adelino | 0:37e9a0644485 | 168 | } |
adelino | 0:37e9a0644485 | 169 | //------------------------------- |
adelino | 0:37e9a0644485 | 170 | bool Nunchuk::getBtnZ(void) |
adelino | 0:37e9a0644485 | 171 | { |
adelino | 0:37e9a0644485 | 172 | return btnZ; |
adelino | 0:37e9a0644485 | 173 | } |