PSI3422_Aula3

Fork of NunchukLib by decarvalho adelino

Committer:
gabrielkim13
Date:
Wed Aug 16 12:37:16 2017 +0000
Revision:
3:0ea2b14ff7f4
Parent:
2:0a8f43931041
PSI3422_Aula3

Who changed what in which revision?

UserRevisionLine numberNew 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
gabrielkim13 3:0ea2b14ff7f4 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;
gabrielkim13 3:0ea2b14ff7f4 54 */
gabrielkim13 3:0ea2b14ff7f4 55 joyX = datas[0];
gabrielkim13 3:0ea2b14ff7f4 56
gabrielkim13 3:0ea2b14ff7f4 57 /*
adelino 0:37e9a0644485 58 if(datas[1] <100)
adelino 0:37e9a0644485 59 joyY=-1;
adelino 0:37e9a0644485 60 else if(datas[1] >140)
adelino 0:37e9a0644485 61 joyY=1;
adelino 0:37e9a0644485 62 else
adelino 0:37e9a0644485 63 joyY=0;
gabrielkim13 3:0ea2b14ff7f4 64 */
gabrielkim13 3:0ea2b14ff7f4 65 joyY = datas[1];
gabrielkim13 3:0ea2b14ff7f4 66
adelino 0:37e9a0644485 67 accX=datas[2]<<2;
adelino 0:37e9a0644485 68 accY=datas[3]<<2;
adelino 0:37e9a0644485 69 accZ=datas[4]<<2;
adelino 0:37e9a0644485 70
adelino 0:37e9a0644485 71 //btnZ
adelino 0:37e9a0644485 72 if(datas[5] & 0x01)
adelino 0:37e9a0644485 73 btnZ=false;
adelino 0:37e9a0644485 74 else
adelino 0:37e9a0644485 75 btnZ=true;
adelino 0:37e9a0644485 76
adelino 0:37e9a0644485 77
adelino 0:37e9a0644485 78 //btnC
adelino 0:37e9a0644485 79 if(datas[5]& 0x02)
adelino 0:37e9a0644485 80 btnC=false;
adelino 0:37e9a0644485 81 else
adelino 0:37e9a0644485 82 btnC=true;
adelino 0:37e9a0644485 83
adelino 0:37e9a0644485 84 accX+=(datas[5]>>2) & 0x0003;
adelino 0:37e9a0644485 85 accY+=(datas[5]>>4) & 0x0003;
adelino 0:37e9a0644485 86 accZ+=(datas[5]>>6) & 0x0003;
adelino 0:37e9a0644485 87
adelino 0:37e9a0644485 88
adelino 0:37e9a0644485 89 } else {
adelino 0:37e9a0644485 90 res=false;
adelino 0:37e9a0644485 91 joyX=0;
adelino 0:37e9a0644485 92 joyY=0;
adelino 0:37e9a0644485 93
adelino 0:37e9a0644485 94 accX=0;
adelino 0:37e9a0644485 95 accY=0;
adelino 0:37e9a0644485 96 accZ=0;
adelino 0:37e9a0644485 97 btnZ=false;
adelino 0:37e9a0644485 98 btnC=false;
adelino 0:37e9a0644485 99 }
adelino 0:37e9a0644485 100
adelino 0:37e9a0644485 101 }
adelino 0:37e9a0644485 102 //-----------------------
adelino 0:37e9a0644485 103 bool Nunchuk::setup(void)
adelino 0:37e9a0644485 104 {
adelino 0:37e9a0644485 105 char cmd[2];
adelino 0:37e9a0644485 106 bool res=false;
adelino 0:37e9a0644485 107
adelino 0:37e9a0644485 108 cmd[0]=0x40;
adelino 0:37e9a0644485 109 cmd[1]=0x00;
adelino 0:37e9a0644485 110
adelino 0:37e9a0644485 111 if(myI2C.write(ADRESSE,cmd,2)==0) {
adelino 0:37e9a0644485 112 res=true;
adelino 0:37e9a0644485 113 } else {
adelino 0:37e9a0644485 114 res=false;
adelino 0:37e9a0644485 115 }
adelino 0:37e9a0644485 116 return res;
adelino 0:37e9a0644485 117 }
adelino 0:37e9a0644485 118 //----------------------------
adelino 0:37e9a0644485 119 bool Nunchuk::request(void)
adelino 0:37e9a0644485 120 {
adelino 0:37e9a0644485 121 bool res=false;
adelino 0:37e9a0644485 122 char cmd[1]= {0x00};
adelino 0:37e9a0644485 123
adelino 0:37e9a0644485 124 if(myI2C.write(ADRESSE,cmd,1)==0) {
adelino 0:37e9a0644485 125 res=true;
adelino 0:37e9a0644485 126 wait(0.001);
adelino 0:37e9a0644485 127
adelino 0:37e9a0644485 128 } else {
adelino 0:37e9a0644485 129 res=false;
adelino 0:37e9a0644485 130 }
adelino 0:37e9a0644485 131
adelino 0:37e9a0644485 132 return res;
adelino 0:37e9a0644485 133 }
adelino 0:37e9a0644485 134 //-----------------------------
adelino 0:37e9a0644485 135 float Nunchuk::getPeriodeTe(void)
adelino 0:37e9a0644485 136 {
adelino 0:37e9a0644485 137 return periodeTe;
adelino 0:37e9a0644485 138 }
adelino 0:37e9a0644485 139 //-----------------------------
adelino 0:37e9a0644485 140 char Nunchuk::decode(char data)
adelino 0:37e9a0644485 141 {
adelino 0:37e9a0644485 142 return (data^0x17)+0x17;
adelino 0:37e9a0644485 143 }
adelino 0:37e9a0644485 144 //------------------------------
adelino 0:37e9a0644485 145 signed char Nunchuk::getJoyX(void)
adelino 0:37e9a0644485 146 {
adelino 0:37e9a0644485 147 return joyX;
adelino 0:37e9a0644485 148 }
adelino 0:37e9a0644485 149 //-------------------------------
adelino 0:37e9a0644485 150 signed char Nunchuk::getJoyY(void)
adelino 0:37e9a0644485 151 {
adelino 0:37e9a0644485 152 return joyY;
adelino 0:37e9a0644485 153 }
adelino 0:37e9a0644485 154 //--------------------------------
adelino 0:37e9a0644485 155 int Nunchuk::getAccX(void)
adelino 0:37e9a0644485 156 {
adelino 0:37e9a0644485 157 return accX;
adelino 0:37e9a0644485 158 }
adelino 0:37e9a0644485 159 //------------------------------
adelino 0:37e9a0644485 160 int Nunchuk::getAccY(void)
adelino 0:37e9a0644485 161 {
adelino 0:37e9a0644485 162 return accY;
adelino 0:37e9a0644485 163 }
adelino 0:37e9a0644485 164 //----------------------------
adelino 0:37e9a0644485 165 int Nunchuk::getAccZ(void)
adelino 0:37e9a0644485 166 {
adelino 0:37e9a0644485 167 return accZ;
adelino 0:37e9a0644485 168 }
adelino 0:37e9a0644485 169 //-----------------------------
adelino 0:37e9a0644485 170 bool Nunchuk::getBtnC(void)
adelino 0:37e9a0644485 171 {
adelino 0:37e9a0644485 172 return btnC;
adelino 0:37e9a0644485 173 }
adelino 0:37e9a0644485 174 //-------------------------------
adelino 0:37e9a0644485 175 bool Nunchuk::getBtnZ(void)
adelino 0:37e9a0644485 176 {
adelino 0:37e9a0644485 177 return btnZ;
adelino 0:37e9a0644485 178 }