mbedbidos
/
METRIX_presentacion
corrección spi.
metrix.cpp@6:973f374e8ea4, 2018-11-22 (annotated)
- Committer:
- jiuk
- Date:
- Thu Nov 22 11:30:46 2018 +0000
- Revision:
- 6:973f374e8ea4
- Parent:
- 5:616b5894b610
correcci?n spi.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jiuk | 0:2b55edb1057b | 1 | #include "mbed.h" |
jiuk | 0:2b55edb1057b | 2 | #include "moggo.h" //agrega la libreria que contiene las figuras. |
jiuk | 6:973f374e8ea4 | 3 | #include "MPU6050.h" |
jiuk | 0:2b55edb1057b | 4 | |
jiuk | 5:616b5894b610 | 5 | Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. |
jiuk | 6:973f374e8ea4 | 6 | MPU6050 Wire(PB_9 , PB_8 ); |
jiuk | 6:973f374e8ea4 | 7 | |
jiuk | 6:973f374e8ea4 | 8 | SPI deviceM(PB_15, PB_14, PB_13); |
jiuk | 6:973f374e8ea4 | 9 | DigitalOut ssel (PB_12); |
jiuk | 6:973f374e8ea4 | 10 | //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi). |
jiuk | 6:973f374e8ea4 | 11 | //DigitalOut ssel (PB_9); //Chip Select para el controlador. |
jiuk | 6:973f374e8ea4 | 12 | //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. |
jiuk | 6:973f374e8ea4 | 13 | Serial com_tar(PC_10,PC_11); //master f446R |
jiuk | 6:973f374e8ea4 | 14 | //Serial com_tar(PA_15,PB_7); //slave f411R //habilitar la comunicacion serial a traves del puerto usb. |
jiuk | 6:973f374e8ea4 | 15 | Ticker timer; |
jiuk | 6:973f374e8ea4 | 16 | DigitalIn pulsador(PH_1); |
jiuk | 6:973f374e8ea4 | 17 | AnalogIn in1(PC_2); |
jiuk | 6:973f374e8ea4 | 18 | AnalogIn in2(PC_3); |
jiuk | 0:2b55edb1057b | 19 | |
jiuk | 5:616b5894b610 | 20 | #define VEL 120 //Velocidad de actualizacion de dato en el controlador. |
jiuk | 0:2b55edb1057b | 21 | |
jiuk | 0:2b55edb1057b | 22 | uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0}; //Matriz para mostrar en pantalla. |
jiuk | 5:616b5894b610 | 23 | uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0}; //Matriz temporal para arrojar las fichas. |
jiuk | 0:2b55edb1057b | 24 | uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0}; |
jiuk | 6:973f374e8ea4 | 25 | uint8_t cont=0,ncolumna=1,grados=1,ficha=0,GIRO=2,G_DIAG=2,correr=0,linea_spi=0; |
jiuk | 3:7ae3dd76ba0a | 26 | int fila=0; |
jiuk | 6:973f374e8ea4 | 27 | bool _boton=0; |
jiuk | 6:973f374e8ea4 | 28 | |
jiuk | 6:973f374e8ea4 | 29 | void boton(); |
jiuk | 6:973f374e8ea4 | 30 | void sendSPI(uint8_t,uint8_t); |
jiuk | 6:973f374e8ea4 | 31 | void borrar(); |
jiuk | 6:973f374e8ea4 | 32 | |
jiuk | 6:973f374e8ea4 | 33 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 34 | //-------------------CARA_FELIZ------------------------ |
jiuk | 6:973f374e8ea4 | 35 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 36 | void cara_feliz() |
jiuk | 6:973f374e8ea4 | 37 | { |
jiuk | 6:973f374e8ea4 | 38 | borrar(); |
jiuk | 6:973f374e8ea4 | 39 | sendSPI(0x02,0b01100110); |
jiuk | 6:973f374e8ea4 | 40 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 41 | sendSPI(0x03,0b01100110); |
jiuk | 6:973f374e8ea4 | 42 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 43 | sendSPI(0x06,0b01000010); |
jiuk | 6:973f374e8ea4 | 44 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 45 | sendSPI(0x07,0b00111100); |
jiuk | 6:973f374e8ea4 | 46 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 47 | for(int i=0;i<4;i++) |
jiuk | 6:973f374e8ea4 | 48 | { |
jiuk | 6:973f374e8ea4 | 49 | sendSPI(0x06,0x00); |
jiuk | 6:973f374e8ea4 | 50 | sendSPI(0x07,0b00111100); |
jiuk | 6:973f374e8ea4 | 51 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 52 | sendSPI(0x07,0b00111100); |
jiuk | 6:973f374e8ea4 | 53 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 54 | sendSPI(0x06,0b01000010); |
jiuk | 6:973f374e8ea4 | 55 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 56 | } |
jiuk | 6:973f374e8ea4 | 57 | } |
jiuk | 6:973f374e8ea4 | 58 | |
jiuk | 6:973f374e8ea4 | 59 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 60 | //-------------------CARA_TRISTE------------------------ |
jiuk | 6:973f374e8ea4 | 61 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 62 | void cara_triste() |
jiuk | 6:973f374e8ea4 | 63 | { |
jiuk | 6:973f374e8ea4 | 64 | borrar(); |
jiuk | 6:973f374e8ea4 | 65 | sendSPI(0x02,0b01100110); |
jiuk | 6:973f374e8ea4 | 66 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 67 | sendSPI(0x03,0b01100110); |
jiuk | 6:973f374e8ea4 | 68 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 69 | sendSPI(0x06,0b00111100); |
jiuk | 6:973f374e8ea4 | 70 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 71 | sendSPI(0x07,0b01000010); |
jiuk | 6:973f374e8ea4 | 72 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 73 | for(int i=0;i<4;i++) |
jiuk | 6:973f374e8ea4 | 74 | { |
jiuk | 6:973f374e8ea4 | 75 | sendSPI(0x07,0x00); |
jiuk | 6:973f374e8ea4 | 76 | sendSPI(0x06,0b00111100); |
jiuk | 6:973f374e8ea4 | 77 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 78 | sendSPI(0x06,0b00111100); |
jiuk | 6:973f374e8ea4 | 79 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 80 | sendSPI(0x07,0b01000010); |
jiuk | 6:973f374e8ea4 | 81 | wait(0.2); |
jiuk | 6:973f374e8ea4 | 82 | } |
jiuk | 6:973f374e8ea4 | 83 | } |
jiuk | 6:973f374e8ea4 | 84 | |
jiuk | 5:616b5894b610 | 85 | |
jiuk | 0:2b55edb1057b | 86 | void sendSPI(uint8_t d1, uint8_t d2) |
jiuk | 0:2b55edb1057b | 87 | { |
jiuk | 0:2b55edb1057b | 88 | deviceM.unlock(); |
jiuk | 0:2b55edb1057b | 89 | ssel=0; |
jiuk | 0:2b55edb1057b | 90 | deviceM.write(d1); |
jiuk | 0:2b55edb1057b | 91 | deviceM.write(d2); |
jiuk | 0:2b55edb1057b | 92 | ssel=1; |
jiuk | 0:2b55edb1057b | 93 | deviceM.lock(); |
jiuk | 0:2b55edb1057b | 94 | } |
jiuk | 0:2b55edb1057b | 95 | |
jiuk | 5:616b5894b610 | 96 | void test() //test |
jiuk | 0:2b55edb1057b | 97 | { |
jiuk | 0:2b55edb1057b | 98 | sendSPI(0x09,0); //no decodificacion |
jiuk | 5:616b5894b610 | 99 | sendSPI(0x0A,0x00); //intensidad |
jiuk | 0:2b55edb1057b | 100 | sendSPI(0x0B,0x07); //usa 7 leds |
jiuk | 0:2b55edb1057b | 101 | sendSPI(0x0C,1); //no apaga |
jiuk | 0:2b55edb1057b | 102 | sendSPI(0x0F,0); //operacion normal |
jiuk | 0:2b55edb1057b | 103 | } |
jiuk | 0:2b55edb1057b | 104 | |
jiuk | 0:2b55edb1057b | 105 | void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) |
jiuk | 0:2b55edb1057b | 106 | { |
jiuk | 3:7ae3dd76ba0a | 107 | if(((mat_act[cont-2]& (*(fig+c)>>columna-1))==0)&&((mat_act[cont-1]& (*(fig+b)>>columna-1))==0)&&((mat_act[cont]& (*(fig+a)>>columna-1))==0)){ |
jiuk | 2:fc251f89bd1b | 108 | mat_tmp[0]= *(fig+5)>>columna-1; |
jiuk | 2:fc251f89bd1b | 109 | mat_tmp[1]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila. |
jiuk | 2:fc251f89bd1b | 110 | mat_tmp[2]= *(fig+b)>>columna-1; |
jiuk | 2:fc251f89bd1b | 111 | mat_tmp[3]= *(fig+c)>>columna-1; |
jiuk | 3:7ae3dd76ba0a | 112 | } |
jiuk | 0:2b55edb1057b | 113 | } |
jiuk | 0:2b55edb1057b | 114 | |
jiuk | 2:fc251f89bd1b | 115 | void borrar() //borrar toda la matriz; |
jiuk | 0:2b55edb1057b | 116 | { |
jiuk | 0:2b55edb1057b | 117 | int i; |
jiuk | 0:2b55edb1057b | 118 | for(i=0;i<=8;i++) |
jiuk | 0:2b55edb1057b | 119 | { |
jiuk | 0:2b55edb1057b | 120 | sendSPI(i,0); |
jiuk | 0:2b55edb1057b | 121 | mat_act[i]=0; //Matriz para mostrar en pantalla. |
jiuk | 0:2b55edb1057b | 122 | } |
jiuk | 0:2b55edb1057b | 123 | }; |
jiuk | 0:2b55edb1057b | 124 | |
jiuk | 0:2b55edb1057b | 125 | void buscar_fil(){ |
jiuk | 0:2b55edb1057b | 126 | for (uint8_t i=0;i<9;i++){ |
jiuk | 0:2b55edb1057b | 127 | if((mat_act[i]& mat_tmp[3])==0){ |
jiuk | 0:2b55edb1057b | 128 | fila=i; |
jiuk | 0:2b55edb1057b | 129 | //command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]); |
jiuk | 0:2b55edb1057b | 130 | } |
jiuk | 0:2b55edb1057b | 131 | if((mat_act[i]& mat_tmp[3])!=0){ |
jiuk | 0:2b55edb1057b | 132 | fila=i-1; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 133 | i=9; |
jiuk | 0:2b55edb1057b | 134 | //command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]); |
jiuk | 0:2b55edb1057b | 135 | } |
jiuk | 0:2b55edb1057b | 136 | if((mat_act[i]& mat_tmp[2])!=0){ //se condicionan las salidas de la busqueda de lanueva fila |
jiuk | 0:2b55edb1057b | 137 | fila=i; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 138 | i=9; |
jiuk | 0:2b55edb1057b | 139 | //command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]); |
jiuk | 0:2b55edb1057b | 140 | } |
jiuk | 0:2b55edb1057b | 141 | if((mat_act[i]& mat_tmp[1])!=0){ |
jiuk | 0:2b55edb1057b | 142 | fila=i+1; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 143 | i=9; |
jiuk | 0:2b55edb1057b | 144 | //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]); |
jiuk | 0:2b55edb1057b | 145 | } |
jiuk | 0:2b55edb1057b | 146 | } |
jiuk | 0:2b55edb1057b | 147 | if(fila>8) |
jiuk | 2:fc251f89bd1b | 148 | fila=8; |
jiuk | 3:7ae3dd76ba0a | 149 | if(fila<=0){ |
jiuk | 2:fc251f89bd1b | 150 | wait_ms(VEL); |
jiuk | 2:fc251f89bd1b | 151 | borrar(); |
jiuk | 2:fc251f89bd1b | 152 | fila=8; |
jiuk | 0:2b55edb1057b | 153 | } |
jiuk | 6:973f374e8ea4 | 154 | //command.printf("\n buscar fila \n %d",fila); |
jiuk | 6:973f374e8ea4 | 155 | //command.printf("\n ******************************* \n "); |
jiuk | 0:2b55edb1057b | 156 | }; |
jiuk | 0:2b55edb1057b | 157 | |
jiuk | 0:2b55edb1057b | 158 | void guardar_mat(){ |
jiuk | 0:2b55edb1057b | 159 | mat_act[fila-3]=mat_tmp1[0]; |
jiuk | 0:2b55edb1057b | 160 | mat_act[fila-2]=mat_tmp1[1]; |
jiuk | 2:fc251f89bd1b | 161 | mat_act[fila-1]=mat_tmp1[2]; |
jiuk | 2:fc251f89bd1b | 162 | mat_act[fila ]=mat_tmp1[3]; |
jiuk | 2:fc251f89bd1b | 163 | }; |
jiuk | 4:78194ed9ff6b | 164 | |
jiuk | 0:2b55edb1057b | 165 | void correr_fig(){ |
jiuk | 6:973f374e8ea4 | 166 | |
jiuk | 6:973f374e8ea4 | 167 | //correr=command.getc(); |
jiuk | 6:973f374e8ea4 | 168 | int y=0,x=0,acc[3]; |
jiuk | 6:973f374e8ea4 | 169 | //float gyro[3]; Wire.getGyro(gyro); |
jiuk | 6:973f374e8ea4 | 170 | float _acc[3]; |
jiuk | 6:973f374e8ea4 | 171 | //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]); |
jiuk | 6:973f374e8ea4 | 172 | //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]); |
jiuk | 6:973f374e8ea4 | 173 | if(_boton==0) |
jiuk | 6:973f374e8ea4 | 174 | { |
jiuk | 6:973f374e8ea4 | 175 | x=((int)(in1.read()*-650))+13; |
jiuk | 6:973f374e8ea4 | 176 | y=((int)(in2.read()*-650))+13; |
jiuk | 6:973f374e8ea4 | 177 | acc[0]=x; |
jiuk | 6:973f374e8ea4 | 178 | acc[1]=y; |
jiuk | 6:973f374e8ea4 | 179 | //command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y); |
jiuk | 3:7ae3dd76ba0a | 180 | } |
jiuk | 6:973f374e8ea4 | 181 | else |
jiuk | 6:973f374e8ea4 | 182 | { |
jiuk | 6:973f374e8ea4 | 183 | Wire.getAccelero(_acc); |
jiuk | 6:973f374e8ea4 | 184 | acc[0]=(int)_acc[0]; |
jiuk | 6:973f374e8ea4 | 185 | acc[1]=(int)_acc[1]; |
jiuk | 6:973f374e8ea4 | 186 | //command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]); |
jiuk | 5:616b5894b610 | 187 | } |
jiuk | 6:973f374e8ea4 | 188 | if(acc[0]>GIRO) //eje x derecha |
jiuk | 6:973f374e8ea4 | 189 | correr=1; |
jiuk | 6:973f374e8ea4 | 190 | else if(acc[0]<-GIRO) //eje x izquierda |
jiuk | 6:973f374e8ea4 | 191 | correr=0; |
jiuk | 6:973f374e8ea4 | 192 | else if(acc[1]>GIRO) //eje y arriba |
jiuk | 6:973f374e8ea4 | 193 | correr=2; |
jiuk | 6:973f374e8ea4 | 194 | else if(acc[1]<-GIRO) //eje y abajo |
jiuk | 6:973f374e8ea4 | 195 | correr=3; |
jiuk | 6:973f374e8ea4 | 196 | else |
jiuk | 5:616b5894b610 | 197 | correr=0; |
jiuk | 4:78194ed9ff6b | 198 | |
jiuk | 2:fc251f89bd1b | 199 | |
jiuk | 0:2b55edb1057b | 200 | switch (correr){ |
jiuk | 2:fc251f89bd1b | 201 | |
jiuk | 0:2b55edb1057b | 202 | case 0: |
jiuk | 2:fc251f89bd1b | 203 | cont++; |
jiuk | 2:fc251f89bd1b | 204 | if(cont>8)cont=8; |
jiuk | 2:fc251f89bd1b | 205 | break; //cae la figura; |
jiuk | 2:fc251f89bd1b | 206 | |
jiuk | 0:2b55edb1057b | 207 | case 1: |
jiuk | 3:7ae3dd76ba0a | 208 | if((mat_tmp[1]==2) || (mat_tmp[1]==1) || (mat_tmp[1]==3)) //faltan condiciones para evitar desplazamiento innecesario. |
jiuk | 2:fc251f89bd1b | 209 | ncolumna--; |
jiuk | 0:2b55edb1057b | 210 | grados++; |
jiuk | 0:2b55edb1057b | 211 | if(grados>4) |
jiuk | 0:2b55edb1057b | 212 | grados=1; |
jiuk | 2:fc251f89bd1b | 213 | break; //Girar la figura; |
jiuk | 2:fc251f89bd1b | 214 | |
jiuk | 0:2b55edb1057b | 215 | case 2: |
jiuk | 4:78194ed9ff6b | 216 | if((mat_tmp[1]!=1) && (mat_tmp[2]!=1) && (mat_tmp[3]!=1)){ |
jiuk | 4:78194ed9ff6b | 217 | if((mat_tmp[1]!=3) && (mat_tmp[2]!=3) && (mat_tmp[3]!=3)){ |
jiuk | 4:78194ed9ff6b | 218 | if((mat_tmp[1]!=7) && (mat_tmp[2]!=7) && (mat_tmp[3]!=7)) |
jiuk | 4:78194ed9ff6b | 219 | ncolumna++; |
jiuk | 4:78194ed9ff6b | 220 | } |
jiuk | 1:bd308ee8776e | 221 | } |
jiuk | 4:78194ed9ff6b | 222 | |
jiuk | 2:fc251f89bd1b | 223 | break; //correr a la Derecha; |
jiuk | 2:fc251f89bd1b | 224 | |
jiuk | 0:2b55edb1057b | 225 | case 3: |
jiuk | 0:2b55edb1057b | 226 | ncolumna--; |
jiuk | 0:2b55edb1057b | 227 | if(ncolumna<1) |
jiuk | 0:2b55edb1057b | 228 | ncolumna=1; |
jiuk | 2:fc251f89bd1b | 229 | break; //correr a la izquierda; |
jiuk | 4:78194ed9ff6b | 230 | /* |
jiuk | 3:7ae3dd76ba0a | 231 | case 4: |
jiuk | 3:7ae3dd76ba0a | 232 | cont++; |
jiuk | 3:7ae3dd76ba0a | 233 | if(cont>8)cont=8; |
jiuk | 3:7ae3dd76ba0a | 234 | break; |
jiuk | 4:78194ed9ff6b | 235 | */ |
jiuk | 0:2b55edb1057b | 236 | } |
jiuk | 0:2b55edb1057b | 237 | } |
jiuk | 0:2b55edb1057b | 238 | |
jiuk | 1:bd308ee8776e | 239 | void imp_mat(uint8_t *temp){ //se genera la variable global cont. |
jiuk | 0:2b55edb1057b | 240 | mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); |
jiuk | 0:2b55edb1057b | 241 | mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); |
jiuk | 0:2b55edb1057b | 242 | mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); |
jiuk | 0:2b55edb1057b | 243 | mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart. |
jiuk | 0:2b55edb1057b | 244 | sendSPI(cont-3,mat_tmp1[0]); |
jiuk | 0:2b55edb1057b | 245 | sendSPI(cont-2,mat_tmp1[1]); |
jiuk | 0:2b55edb1057b | 246 | sendSPI(cont-1,mat_tmp1[2]); |
jiuk | 1:bd308ee8776e | 247 | sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 |
jiuk | 0:2b55edb1057b | 248 | wait_ms(VEL); |
jiuk | 0:2b55edb1057b | 249 | } |
jiuk | 0:2b55edb1057b | 250 | |
jiuk | 0:2b55edb1057b | 251 | void dibujar(char type_fig,char grados,char columna) |
jiuk | 0:2b55edb1057b | 252 | { |
jiuk | 5:616b5894b610 | 253 | switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz. |
jiuk | 0:2b55edb1057b | 254 | case 1: if(columna>7) |
jiuk | 0:2b55edb1057b | 255 | columna=7; |
jiuk | 0:2b55edb1057b | 256 | cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro; |
jiuk | 0:2b55edb1057b | 257 | |
jiuk | 0:2b55edb1057b | 258 | case 2: if(grados==1 || grados==3) |
jiuk | 0:2b55edb1057b | 259 | cop_mat(0,0,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 260 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 261 | cop_mat(5,5,2,FIG_ALL,columna); //2: I; |
jiuk | 0:2b55edb1057b | 262 | break; |
jiuk | 0:2b55edb1057b | 263 | |
jiuk | 0:2b55edb1057b | 264 | case 3: if(grados==1) |
jiuk | 0:2b55edb1057b | 265 | cop_mat(0,0,1,FIG_ALL,columna); //3: L; |
jiuk | 0:2b55edb1057b | 266 | if(grados==2) |
jiuk | 0:2b55edb1057b | 267 | cop_mat(5,2,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 268 | if(grados==3) |
jiuk | 0:2b55edb1057b | 269 | cop_mat(1,3,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 270 | if(grados==4) |
jiuk | 0:2b55edb1057b | 271 | cop_mat(5,6,2,FIG_ALL,columna); |
jiuk | 1:bd308ee8776e | 272 | break; |
jiuk | 0:2b55edb1057b | 273 | |
jiuk | 0:2b55edb1057b | 274 | case 4: if(grados==1) |
jiuk | 0:2b55edb1057b | 275 | cop_mat(5,3,2,FIG_ALL,columna); //4: T; |
jiuk | 0:2b55edb1057b | 276 | if(grados==2) |
jiuk | 0:2b55edb1057b | 277 | cop_mat(0,1,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 278 | if(grados==3) |
jiuk | 0:2b55edb1057b | 279 | cop_mat(5,2,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 280 | if(grados==4) |
jiuk | 0:2b55edb1057b | 281 | cop_mat(3,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 282 | break; |
jiuk | 0:2b55edb1057b | 283 | |
jiuk | 0:2b55edb1057b | 284 | case 5: if(grados==1 || grados==3) |
jiuk | 5:616b5894b610 | 285 | cop_mat(5,4,1,FIG_ALL,columna); //5: S; |
jiuk | 0:2b55edb1057b | 286 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 287 | cop_mat(0,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 288 | break; |
jiuk | 0:2b55edb1057b | 289 | } |
jiuk | 0:2b55edb1057b | 290 | } |
jiuk | 0:2b55edb1057b | 291 | |
jiuk | 0:2b55edb1057b | 292 | void fichas(){ |
jiuk | 0:2b55edb1057b | 293 | ficha= rand() % 5+1; |
jiuk | 4:78194ed9ff6b | 294 | //grados= rand() % 4+1; |
jiuk | 2:fc251f89bd1b | 295 | ncolumna=4; |
jiuk | 0:2b55edb1057b | 296 | } |
jiuk | 2:fc251f89bd1b | 297 | |
jiuk | 6:973f374e8ea4 | 298 | void buscar_linea() |
jiuk | 6:973f374e8ea4 | 299 | { |
jiuk | 6:973f374e8ea4 | 300 | for(uint8_t i=0;i<9;i++) |
jiuk | 6:973f374e8ea4 | 301 | { |
jiuk | 6:973f374e8ea4 | 302 | if(mat_act[i]==255) |
jiuk | 6:973f374e8ea4 | 303 | { |
jiuk | 6:973f374e8ea4 | 304 | linea_spi++; |
jiuk | 6:973f374e8ea4 | 305 | for(uint8_t a=i;a>=1;a--) |
jiuk | 6:973f374e8ea4 | 306 | { |
jiuk | 2:fc251f89bd1b | 307 | sendSPI(a,mat_act[a-1]); |
jiuk | 3:7ae3dd76ba0a | 308 | mat_act[a]=mat_act[a-1]; |
jiuk | 2:fc251f89bd1b | 309 | } |
jiuk | 2:fc251f89bd1b | 310 | } |
jiuk | 2:fc251f89bd1b | 311 | } |
jiuk | 6:973f374e8ea4 | 312 | if(linea_spi==2) |
jiuk | 6:973f374e8ea4 | 313 | { |
jiuk | 6:973f374e8ea4 | 314 | com_tar.putc(1); |
jiuk | 6:973f374e8ea4 | 315 | cara_feliz(); |
jiuk | 6:973f374e8ea4 | 316 | } |
jiuk | 2:fc251f89bd1b | 317 | } |
jiuk | 0:2b55edb1057b | 318 | |
jiuk | 6:973f374e8ea4 | 319 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 320 | //-------------------TICKER------------------------ |
jiuk | 6:973f374e8ea4 | 321 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 322 | void attime() |
jiuk | 6:973f374e8ea4 | 323 | { |
jiuk | 6:973f374e8ea4 | 324 | void correr_fig(); |
jiuk | 6:973f374e8ea4 | 325 | //blink(); |
jiuk | 6:973f374e8ea4 | 326 | boton(); |
jiuk | 6:973f374e8ea4 | 327 | if (command.readable()) |
jiuk | 6:973f374e8ea4 | 328 | { |
jiuk | 6:973f374e8ea4 | 329 | int x=com_tar.getc(); |
jiuk | 6:973f374e8ea4 | 330 | if(x==1) |
jiuk | 6:973f374e8ea4 | 331 | cara_triste(); |
jiuk | 6:973f374e8ea4 | 332 | //command.printf("Button pressed %b\n", _boton); |
jiuk | 6:973f374e8ea4 | 333 | } |
jiuk | 6:973f374e8ea4 | 334 | }; |
jiuk | 6:973f374e8ea4 | 335 | |
jiuk | 6:973f374e8ea4 | 336 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 337 | //-------------------BOTON------------------------ |
jiuk | 6:973f374e8ea4 | 338 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 339 | void boton() |
jiuk | 6:973f374e8ea4 | 340 | { |
jiuk | 6:973f374e8ea4 | 341 | if (pulsador == 0) |
jiuk | 6:973f374e8ea4 | 342 | { |
jiuk | 6:973f374e8ea4 | 343 | //command.printf("Button pressed %b\n", _boton); |
jiuk | 6:973f374e8ea4 | 344 | _boton=!_boton; |
jiuk | 6:973f374e8ea4 | 345 | } |
jiuk | 6:973f374e8ea4 | 346 | } |
jiuk | 6:973f374e8ea4 | 347 | |
jiuk | 6:973f374e8ea4 | 348 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 349 | //-------------------VELOCIDAD----------------------- |
jiuk | 6:973f374e8ea4 | 350 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 351 | int velocidad(float _acc[3]) |
jiuk | 6:973f374e8ea4 | 352 | { |
jiuk | 6:973f374e8ea4 | 353 | int vel,x=1,y=2; // |
jiuk | 6:973f374e8ea4 | 354 | int acc[3]; |
jiuk | 6:973f374e8ea4 | 355 | acc[0]=(int)_acc[0]; |
jiuk | 6:973f374e8ea4 | 356 | acc[1]=(int)_acc[1]; |
jiuk | 6:973f374e8ea4 | 357 | if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO)) |
jiuk | 6:973f374e8ea4 | 358 | { |
jiuk | 6:973f374e8ea4 | 359 | if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x))) |
jiuk | 6:973f374e8ea4 | 360 | { |
jiuk | 6:973f374e8ea4 | 361 | vel=300; |
jiuk | 6:973f374e8ea4 | 362 | if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y))) |
jiuk | 6:973f374e8ea4 | 363 | vel=100; |
jiuk | 6:973f374e8ea4 | 364 | } |
jiuk | 6:973f374e8ea4 | 365 | else |
jiuk | 6:973f374e8ea4 | 366 | vel=500; |
jiuk | 6:973f374e8ea4 | 367 | } |
jiuk | 6:973f374e8ea4 | 368 | return vel; |
jiuk | 6:973f374e8ea4 | 369 | } |
jiuk | 6:973f374e8ea4 | 370 | |
jiuk | 6:973f374e8ea4 | 371 | //-------------------------------------------------- |
jiuk | 6:973f374e8ea4 | 372 | //-------------------MAIN------------------------ |
jiuk | 6:973f374e8ea4 | 373 | //-------------------------------------------------- |
jiuk | 0:2b55edb1057b | 374 | int main() { |
jiuk | 6:973f374e8ea4 | 375 | timer.attach(&attime, 0.3); |
jiuk | 0:2b55edb1057b | 376 | test(); |
jiuk | 0:2b55edb1057b | 377 | borrar(); |
jiuk | 0:2b55edb1057b | 378 | while(1){ |
jiuk | 1:bd308ee8776e | 379 | fichas(); |
jiuk | 0:2b55edb1057b | 380 | buscar_fil(); |
jiuk | 1:bd308ee8776e | 381 | cont=0; |
jiuk | 5:616b5894b610 | 382 | |
jiuk | 2:fc251f89bd1b | 383 | while (cont<fila){ |
jiuk | 1:bd308ee8776e | 384 | correr_fig(); |
jiuk | 1:bd308ee8776e | 385 | dibujar(ficha,grados,ncolumna); |
jiuk | 1:bd308ee8776e | 386 | buscar_fil(); |
jiuk | 1:bd308ee8776e | 387 | imp_mat(mat_tmp); |
jiuk | 3:7ae3dd76ba0a | 388 | wait_ms(VEL*2); |
jiuk | 3:7ae3dd76ba0a | 389 | } |
jiuk | 2:fc251f89bd1b | 390 | guardar_mat(); |
jiuk | 3:7ae3dd76ba0a | 391 | buscar_linea(); |
jiuk | 0:2b55edb1057b | 392 | } |
jiuk | 0:2b55edb1057b | 393 | } |