Version mas reciente 2.0. Contiene manejo de joystick, interrupciones y mejoras de algunos bugs. Game over
Diff: Tetrix.cpp
- Revision:
- 3:c9c3a56609f9
- Child:
- 4:6f2a8091f2a0
diff -r fc251f89bd1b -r c9c3a56609f9 Tetrix.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tetrix.cpp Thu Oct 18 23:55:58 2018 +0000 @@ -0,0 +1,253 @@ +#include "mbed.h" +#include "figs.h" + +SPI deviceM(PB_15, PB_14, PB_13); +DigitalOut ssel (PB_12); +Serial command(USBTX,USBRX); + +#define VEL 100 + +uint8_t mat_act [11]={0,0,0,0,0,0,0,0,0,0,0}; +uint8_t mat_tmp [8]={0,0,0,0,0,0,0,0}; +uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0}; +uint8_t fila=1,cont=0,ncolumna=1,grados=1,ficha=0; + +void sendSPI(uint8_t d1, uint8_t d2) +{ + deviceM.unlock(); + ssel=0; + deviceM.write(d1); + deviceM.write(d2); + ssel=1; + deviceM.lock(); +} + + + +void test() +{ + sendSPI(0x0c,1); + sendSPI(0x0b,7); + sendSPI(0x09,0); + sendSPI(0x0A,0x0E); + int j; + for (j=0;j<4;j++){ + sendSPI(0x0F,1); + wait (0.2); + sendSPI(0x0F,0); + wait (0.2); + } +} + +void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) +{ + 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)){ + mat_tmp[0]= *(fig+5)>>columna-1; + mat_tmp[1]= *(fig+a)>>columna-1; + mat_tmp[2]= *(fig+b)>>columna-1; + mat_tmp[3]= *(fig+c)>>columna-1; + } +} + +void borrar() +{ + int i; + for(i=0;i<=8;i++) + { + sendSPI(i,0); + mat_act[i]=0; + } +}; + +void buscar_fil(){ + for (uint8_t i=0;i<9;i++){ + if((mat_act[i]& mat_tmp[3])==0){ + fila=i; + } + if((mat_act[i]& mat_tmp[3])!=0){ + fila=i-1; + i=9; + } + if((mat_act[i]& mat_tmp[2])!=0){ + fila=i; + i=9; + } + if((mat_act[i]& mat_tmp[1])!=0){ + fila=i+1; + i=9; + //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]); + } + } + if(fila>8) + fila=8; + if(fila<=1){ + wait_ms(VEL); + borrar(); + fila=8; + } +}; + +void guardar_mat(){ + mat_act[fila-3]=mat_tmp1[0]; + mat_act[fila-2]=mat_tmp1[1]; + mat_act[fila-1]=mat_tmp1[2]; + mat_act[fila ]=mat_tmp1[3]; + }; + +void correr_fig(){ + + uint8_t correr=command.getc(); + + if(correr==30) + correr=1; + + if(correr==29) + correr=2; + + if(correr==28) + correr=3; + + if(correr==31) + correr=0; + + switch (correr){ + + case 0: + cont++; + if(cont>8)cont=8; + break; + + case 1: + if( (mat_tmp[1]==2) || (mat_tmp[1]==1)|| (mat_tmp[1]==3)) + ncolumna--; + grados++; + if(grados>4) + grados=1; + break; + + case 2: + if((mat_tmp[2]==7 || mat_tmp[3]==7)||(mat_tmp[2]==3 && mat_tmp[3]==6)) { + command.printf("a"); + ncolumna=5; + } + if((mat_tmp[1]==2 && mat_tmp[2]==2 && mat_tmp[3]==2)){ + command.printf("b"); + ncolumna=7; + } + if (ncolumna<7){ + command.printf("c"); + ncolumna++; + } + break; + /*if( ((mat_tmp[2]==3) && (mat_tmp[3]==6)) || (mat_tmp[2]==7) || (mat_tmp[3]==7)) { + command.printf("c"); + ncolumna=6; + } + + if( (mat_tmp[1]==2) &&(mat_tmp[2]==2) && (mat_tmp[3]==2) ) { + command.printf("d"); + ncolumna=7; + } + */ + + /*if(ncolumna>7) + {command.printf("d"); + ncolumna=7;}*/ + //correr a la Derecha; + + case 3: + ncolumna--; + if(ncolumna<1) + ncolumna=1; + break; + } + + } + + +void imp_mat(uint8_t *temp){ + mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); + mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); + mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); + mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); + sendSPI(cont-3,mat_tmp1[0]); + sendSPI(cont-2,mat_tmp1[1]); + sendSPI(cont-1,mat_tmp1[2]); + sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 + wait_ms(VEL); +} + +void dibujar(char type_fig,char grados,char columna) +{ + switch (type_fig){ + case 1: if(columna>7) + columna=7; + cop_mat(5,1,1,PLANT,columna); break; + + case 2: if(grados==1 || grados==3) + cop_mat(0,0,0,PLANT,columna); + if(grados==2 || grados==4) + cop_mat(5,5,2,PLANT,columna); + break; + + case 3: if(grados==1) + cop_mat(0,0,1,PLANT,columna); + if(grados==2) + cop_mat(5,2,0,PLANT,columna); + if(grados==3) + cop_mat(1,3,3,PLANT,columna); + if(grados==4) + cop_mat(5,6,2,PLANT,columna); + break; + + case 4: if(grados==1) + cop_mat(5,3,2,PLANT,columna); + if(grados==2) + cop_mat(0,1,0,PLANT,columna); + if(grados==3) + cop_mat(5,2,3,PLANT,columna); + if(grados==4) + cop_mat(3,1,3,PLANT,columna); + break; + + case 5: if(grados==1 || grados==3) + cop_mat(5,4,1,PLANT,columna); + if(grados==2 || grados==4) + cop_mat(0,1,3,PLANT,columna); + break; + } +} + +void fichas(){ + ficha= rand() % 5+1; + grados= rand() % 4+1; + ncolumna= 4; + } + +void buscar_linea(){ + for(uint8_t i=0;i<9;i++){ + if(mat_act[i]==255){ + for(uint8_t a=i;a>=1;a--){ + sendSPI(a,mat_act[a-1]); + } + } + } +} + +int main() { + test(); + borrar(); + while(1){ + fichas(); + buscar_fil(); + cont=0; + while (cont<fila){ + correr_fig(); + dibujar(ficha,grados,ncolumna); + buscar_fil(); + imp_mat(mat_tmp); + } + buscar_linea(); + guardar_mat(); + } + } \ No newline at end of file