alis

Dependencies:   mbed

Committer:
acastiblancoc
Date:
Thu Nov 22 04:47:47 2018 +0000
Revision:
5:63e225628634
Parent:
4:6f2a8091f2a0
Child:
6:0e75e91820f3
Ultimo tetris

Who changed what in which revision?

UserRevisionLine numberNew contents of line
acastiblancoc 3:c9c3a56609f9 1 #include "mbed.h"
acastiblancoc 3:c9c3a56609f9 2 #include "figs.h"
acastiblancoc 3:c9c3a56609f9 3
acastiblancoc 4:6f2a8091f2a0 4 Ticker bajar;
acastiblancoc 3:c9c3a56609f9 5 SPI deviceM(PB_15, PB_14, PB_13);
acastiblancoc 3:c9c3a56609f9 6 DigitalOut ssel (PB_12);
acastiblancoc 4:6f2a8091f2a0 7 Serial command(USBTX,USBRX);
acastiblancoc 4:6f2a8091f2a0 8 AnalogIn joy(A0);
acastiblancoc 4:6f2a8091f2a0 9 AnalogIn joy1(A1);
acastiblancoc 4:6f2a8091f2a0 10 float vx=0;
acastiblancoc 4:6f2a8091f2a0 11 float vy=0;
acastiblancoc 4:6f2a8091f2a0 12 DigitalIn cambio(PB_8);
acastiblancoc 5:63e225628634 13 int cc=0, punto=1;
acastiblancoc 4:6f2a8091f2a0 14 uint8_t movimiento=0;
acastiblancoc 3:c9c3a56609f9 15
acastiblancoc 5:63e225628634 16 float VEL= 500;
acastiblancoc 3:c9c3a56609f9 17
acastiblancoc 4:6f2a8091f2a0 18 uint8_t matrix_act [11]={0,0,0,0,0,0,0,0,0,0,0};
acastiblancoc 4:6f2a8091f2a0 19 uint8_t matrix_temp [8]={0,0,0,0,0,0,0,0};
acastiblancoc 4:6f2a8091f2a0 20 uint8_t matrix_temp1[8]={0,0,0,0,0,0,0,0};
acastiblancoc 4:6f2a8091f2a0 21 uint8_t fila=1,cont=0,columna=1,rotacion=1,figura=0;
acastiblancoc 3:c9c3a56609f9 22
acastiblancoc 3:c9c3a56609f9 23 void sendSPI(uint8_t d1, uint8_t d2)
acastiblancoc 3:c9c3a56609f9 24 {
acastiblancoc 3:c9c3a56609f9 25 deviceM.unlock();
acastiblancoc 3:c9c3a56609f9 26 ssel=0;
acastiblancoc 3:c9c3a56609f9 27 deviceM.write(d1);
acastiblancoc 3:c9c3a56609f9 28 deviceM.write(d2);
acastiblancoc 3:c9c3a56609f9 29 ssel=1;
acastiblancoc 3:c9c3a56609f9 30 deviceM.lock();
acastiblancoc 3:c9c3a56609f9 31 }
acastiblancoc 3:c9c3a56609f9 32
acastiblancoc 3:c9c3a56609f9 33
acastiblancoc 3:c9c3a56609f9 34
acastiblancoc 4:6f2a8091f2a0 35 void init_display()
acastiblancoc 3:c9c3a56609f9 36 {
acastiblancoc 3:c9c3a56609f9 37 sendSPI(0x0c,1);
acastiblancoc 3:c9c3a56609f9 38 sendSPI(0x0b,7);
acastiblancoc 3:c9c3a56609f9 39 sendSPI(0x09,0);
acastiblancoc 3:c9c3a56609f9 40 sendSPI(0x0A,0x0E);
acastiblancoc 3:c9c3a56609f9 41 int j;
acastiblancoc 3:c9c3a56609f9 42 for (j=0;j<4;j++){
acastiblancoc 3:c9c3a56609f9 43 sendSPI(0x0F,1);
acastiblancoc 3:c9c3a56609f9 44 wait (0.2);
acastiblancoc 4:6f2a8091f2a0 45 sendSPI(0x0f,0);
acastiblancoc 3:c9c3a56609f9 46 wait (0.2);
acastiblancoc 4:6f2a8091f2a0 47 }
acastiblancoc 3:c9c3a56609f9 48 }
acastiblancoc 3:c9c3a56609f9 49
acastiblancoc 4:6f2a8091f2a0 50 void copy_matrix(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
acastiblancoc 3:c9c3a56609f9 51 {
acastiblancoc 4:6f2a8091f2a0 52 if(((matrix_act[cont-2]& *(fig+c)>>columna-1)==0)&&((matrix_act[cont-1]& *(fig+b)>>columna-1)==0)&&((matrix_act[cont]& *(fig+a)>>columna-1)==0)){
acastiblancoc 4:6f2a8091f2a0 53 matrix_temp[0]= *(fig+5)>>columna-1;
acastiblancoc 4:6f2a8091f2a0 54 matrix_temp[1]= *(fig+a)>>columna-1;
acastiblancoc 4:6f2a8091f2a0 55 matrix_temp[2]= *(fig+b)>>columna-1;
acastiblancoc 4:6f2a8091f2a0 56 matrix_temp[3]= *(fig+c)>>columna-1;
acastiblancoc 3:c9c3a56609f9 57 }
acastiblancoc 3:c9c3a56609f9 58 }
acastiblancoc 3:c9c3a56609f9 59
acastiblancoc 3:c9c3a56609f9 60 void borrar()
acastiblancoc 3:c9c3a56609f9 61 {
acastiblancoc 3:c9c3a56609f9 62 int i;
acastiblancoc 3:c9c3a56609f9 63 for(i=0;i<=8;i++)
acastiblancoc 3:c9c3a56609f9 64 {
acastiblancoc 3:c9c3a56609f9 65 sendSPI(i,0);
acastiblancoc 4:6f2a8091f2a0 66 matrix_act[i]=0;
acastiblancoc 3:c9c3a56609f9 67 }
acastiblancoc 3:c9c3a56609f9 68 };
acastiblancoc 3:c9c3a56609f9 69
acastiblancoc 5:63e225628634 70 void game_over()
acastiblancoc 5:63e225628634 71 {
acastiblancoc 5:63e225628634 72 uint8_t go [9]={0,0,0b01000010,0b00100100,0b00011000,0b00100100,0b01000010,0b00000000,0};
acastiblancoc 5:63e225628634 73 int k,n;
acastiblancoc 5:63e225628634 74 for(n=0;n<6;n++)
acastiblancoc 5:63e225628634 75 {
acastiblancoc 5:63e225628634 76 borrar();
acastiblancoc 5:63e225628634 77 for(k=1;k<=8;k++)
acastiblancoc 5:63e225628634 78 {
acastiblancoc 5:63e225628634 79 sendSPI(k,go[k]);
acastiblancoc 5:63e225628634 80 }
acastiblancoc 5:63e225628634 81 wait(0.2);
acastiblancoc 5:63e225628634 82 borrar();
acastiblancoc 5:63e225628634 83 wait(0.2);
acastiblancoc 5:63e225628634 84 }
acastiblancoc 5:63e225628634 85 }
acastiblancoc 5:63e225628634 86
acastiblancoc 5:63e225628634 87
acastiblancoc 4:6f2a8091f2a0 88 void buscar_fila(){
acastiblancoc 3:c9c3a56609f9 89 for (uint8_t i=0;i<9;i++){
acastiblancoc 4:6f2a8091f2a0 90 if((matrix_act[i]& matrix_temp[3])==0){
acastiblancoc 3:c9c3a56609f9 91 fila=i;
acastiblancoc 3:c9c3a56609f9 92 }
acastiblancoc 4:6f2a8091f2a0 93 if(((matrix_act[i]& matrix_temp[3])!=0)&& (i!=9)){
acastiblancoc 4:6f2a8091f2a0 94 fila=i-1;
acastiblancoc 4:6f2a8091f2a0 95 command.printf("Fila pos 1: %d \n", fila);
acastiblancoc 3:c9c3a56609f9 96 i=9;
acastiblancoc 3:c9c3a56609f9 97 }
acastiblancoc 4:6f2a8091f2a0 98 if(((matrix_act[i]& matrix_temp[2])!=0)&& (i!=9)){
acastiblancoc 3:c9c3a56609f9 99 fila=i;
acastiblancoc 3:c9c3a56609f9 100 i=9;
acastiblancoc 4:6f2a8091f2a0 101 command.printf("Fila pos 2: %d \n", fila);
acastiblancoc 3:c9c3a56609f9 102 }
acastiblancoc 4:6f2a8091f2a0 103 if(((matrix_act[i]& matrix_temp[1])!=0)&& (i!=9)){
acastiblancoc 3:c9c3a56609f9 104 fila=i+1;
acastiblancoc 3:c9c3a56609f9 105 i=9;
acastiblancoc 4:6f2a8091f2a0 106 command.printf("Fila pos 3: %d \n", fila);
acastiblancoc 3:c9c3a56609f9 107 }
acastiblancoc 3:c9c3a56609f9 108 }
acastiblancoc 3:c9c3a56609f9 109 if(fila>8)
acastiblancoc 3:c9c3a56609f9 110 fila=8;
acastiblancoc 3:c9c3a56609f9 111 if(fila<=1){
acastiblancoc 5:63e225628634 112 punto=1;
acastiblancoc 5:63e225628634 113 VEL=500;
acastiblancoc 3:c9c3a56609f9 114 wait_ms(VEL);
acastiblancoc 5:63e225628634 115 game_over();
acastiblancoc 3:c9c3a56609f9 116 borrar();
acastiblancoc 3:c9c3a56609f9 117 fila=8;
acastiblancoc 3:c9c3a56609f9 118 }
acastiblancoc 3:c9c3a56609f9 119 };
acastiblancoc 3:c9c3a56609f9 120
acastiblancoc 4:6f2a8091f2a0 121 void guardar(){
acastiblancoc 4:6f2a8091f2a0 122 matrix_act[fila-3]=matrix_temp1[0];
acastiblancoc 4:6f2a8091f2a0 123 matrix_act[fila-2]=matrix_temp1[1];
acastiblancoc 4:6f2a8091f2a0 124 matrix_act[fila-1]=matrix_temp1[2];
acastiblancoc 4:6f2a8091f2a0 125 matrix_act[fila ]=matrix_temp1[3];
acastiblancoc 3:c9c3a56609f9 126 };
acastiblancoc 3:c9c3a56609f9 127
acastiblancoc 4:6f2a8091f2a0 128 void reada()
acastiblancoc 4:6f2a8091f2a0 129 { vx=0;
acastiblancoc 4:6f2a8091f2a0 130 vy=0;
acastiblancoc 4:6f2a8091f2a0 131 vx=joy.read();
acastiblancoc 4:6f2a8091f2a0 132 vy=joy1.read();
acastiblancoc 4:6f2a8091f2a0 133 cc=cambio.read();
acastiblancoc 4:6f2a8091f2a0 134 }
acastiblancoc 3:c9c3a56609f9 135
acastiblancoc 4:6f2a8091f2a0 136 void bajaf()
acastiblancoc 4:6f2a8091f2a0 137 {
acastiblancoc 4:6f2a8091f2a0 138 movimiento=4;}
acastiblancoc 4:6f2a8091f2a0 139 void movimiento_fig(){
acastiblancoc 5:63e225628634 140 reada();
acastiblancoc 4:6f2a8091f2a0 141 command.printf("vy = %f \n", vy);
acastiblancoc 4:6f2a8091f2a0 142 command.printf("vx = %f \n", vx);
acastiblancoc 4:6f2a8091f2a0 143 command.printf("cc = %f \n", cambio.read());
acastiblancoc 4:6f2a8091f2a0 144 if(vy<0.2)
acastiblancoc 4:6f2a8091f2a0 145 movimiento=1;
acastiblancoc 5:63e225628634 146 if(vy>0.8)
acastiblancoc 4:6f2a8091f2a0 147 movimiento=4;
acastiblancoc 4:6f2a8091f2a0 148 if(vx<0.2)
acastiblancoc 4:6f2a8091f2a0 149 movimiento=3;
acastiblancoc 4:6f2a8091f2a0 150 if(vx>0.80)
acastiblancoc 4:6f2a8091f2a0 151 movimiento=2;
acastiblancoc 4:6f2a8091f2a0 152 command.printf("movimiento = %d \n", movimiento);
acastiblancoc 4:6f2a8091f2a0 153 switch (movimiento){
acastiblancoc 3:c9c3a56609f9 154
acastiblancoc 4:6f2a8091f2a0 155 case 4:
acastiblancoc 3:c9c3a56609f9 156 cont++;
acastiblancoc 4:6f2a8091f2a0 157 //printf("%f", joy.read());
acastiblancoc 3:c9c3a56609f9 158 if(cont>8)cont=8;
acastiblancoc 4:6f2a8091f2a0 159 break;
acastiblancoc 3:c9c3a56609f9 160
acastiblancoc 3:c9c3a56609f9 161 case 1:
acastiblancoc 4:6f2a8091f2a0 162 if( (matrix_temp[1]==2) || (matrix_temp[1]==1)|| (matrix_temp[1]==3))
acastiblancoc 4:6f2a8091f2a0 163 columna--;
acastiblancoc 4:6f2a8091f2a0 164 rotacion++;
acastiblancoc 4:6f2a8091f2a0 165 if(rotacion>4)
acastiblancoc 4:6f2a8091f2a0 166 rotacion=1;
acastiblancoc 4:6f2a8091f2a0 167 break;
acastiblancoc 3:c9c3a56609f9 168
acastiblancoc 4:6f2a8091f2a0 169 case 2:
acastiblancoc 4:6f2a8091f2a0 170 if((matrix_temp[2]==7 || matrix_temp[3]==7)||(matrix_temp[2]==3 && matrix_temp[3]==6)) {
acastiblancoc 3:c9c3a56609f9 171 command.printf("a");
acastiblancoc 4:6f2a8091f2a0 172 columna=6;
acastiblancoc 3:c9c3a56609f9 173 }
acastiblancoc 4:6f2a8091f2a0 174 else if((matrix_temp[1]==2 && matrix_temp[2]==2 && matrix_temp[3]==2)){
acastiblancoc 4:6f2a8091f2a0 175 command.printf("b");
acastiblancoc 4:6f2a8091f2a0 176 columna=8;
acastiblancoc 4:6f2a8091f2a0 177 }
acastiblancoc 4:6f2a8091f2a0 178 else if (columna<7){
acastiblancoc 4:6f2a8091f2a0 179 command.printf("c");
acastiblancoc 4:6f2a8091f2a0 180 columna++;
acastiblancoc 3:c9c3a56609f9 181 }
acastiblancoc 4:6f2a8091f2a0 182 //printf("%f", joy1.read());
acastiblancoc 4:6f2a8091f2a0 183 break;
acastiblancoc 4:6f2a8091f2a0 184
acastiblancoc 3:c9c3a56609f9 185 case 3:
acastiblancoc 4:6f2a8091f2a0 186 columna--;
acastiblancoc 4:6f2a8091f2a0 187 if(columna<1)
acastiblancoc 4:6f2a8091f2a0 188 columna=1;
acastiblancoc 4:6f2a8091f2a0 189 //printf("%f", joy.read());
acastiblancoc 4:6f2a8091f2a0 190 break;
acastiblancoc 4:6f2a8091f2a0 191
acastiblancoc 4:6f2a8091f2a0 192 }
acastiblancoc 3:c9c3a56609f9 193 }
acastiblancoc 3:c9c3a56609f9 194
acastiblancoc 3:c9c3a56609f9 195
acastiblancoc 4:6f2a8091f2a0 196 void print_matrix(uint8_t *temp){
acastiblancoc 4:6f2a8091f2a0 197 matrix_temp1[0]=(matrix_temp[0]+ matrix_act[cont-3]);
acastiblancoc 4:6f2a8091f2a0 198 matrix_temp1[1]=(matrix_temp[1]+ matrix_act[cont-2]);
acastiblancoc 4:6f2a8091f2a0 199 matrix_temp1[2]=(matrix_temp[2]+ matrix_act[cont-1]);
acastiblancoc 4:6f2a8091f2a0 200 matrix_temp1[3]=(matrix_temp[3]+ matrix_act[cont]);
acastiblancoc 4:6f2a8091f2a0 201 sendSPI(cont-3,matrix_temp1[0]);
acastiblancoc 4:6f2a8091f2a0 202 sendSPI(cont-2,matrix_temp1[1]);
acastiblancoc 4:6f2a8091f2a0 203 sendSPI(cont-1,matrix_temp1[2]);
acastiblancoc 4:6f2a8091f2a0 204 sendSPI(cont ,matrix_temp1[3]); //pasa de i+2 a i+1
acastiblancoc 5:63e225628634 205 if(punto%4==0)
acastiblancoc 5:63e225628634 206 {
acastiblancoc 5:63e225628634 207 VEL=VEL/3;
acastiblancoc 5:63e225628634 208 if(VEL<=50)
acastiblancoc 5:63e225628634 209 VEL=50;
acastiblancoc 5:63e225628634 210 }
acastiblancoc 3:c9c3a56609f9 211 wait_ms(VEL);
acastiblancoc 3:c9c3a56609f9 212 }
acastiblancoc 3:c9c3a56609f9 213
acastiblancoc 4:6f2a8091f2a0 214 void crear_fig(char type_fig,char rotacion,char columna)
acastiblancoc 3:c9c3a56609f9 215 {
acastiblancoc 3:c9c3a56609f9 216 switch (type_fig){
acastiblancoc 3:c9c3a56609f9 217 case 1: if(columna>7)
acastiblancoc 3:c9c3a56609f9 218 columna=7;
acastiblancoc 4:6f2a8091f2a0 219 copy_matrix(5,1,1,PLANT,columna); break;
acastiblancoc 3:c9c3a56609f9 220
acastiblancoc 4:6f2a8091f2a0 221 case 2: if(rotacion==1 || rotacion==3)
acastiblancoc 4:6f2a8091f2a0 222 copy_matrix(0,0,0,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 223 if(rotacion==2 || rotacion==4)
acastiblancoc 4:6f2a8091f2a0 224 copy_matrix(5,5,2,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 225 break;
acastiblancoc 3:c9c3a56609f9 226
acastiblancoc 4:6f2a8091f2a0 227 case 3: if(rotacion==1)
acastiblancoc 4:6f2a8091f2a0 228 copy_matrix(0,0,1,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 229 if(rotacion==2)
acastiblancoc 4:6f2a8091f2a0 230 copy_matrix(5,2,0,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 231 if(rotacion==3)
acastiblancoc 4:6f2a8091f2a0 232 copy_matrix(1,3,3,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 233 if(rotacion==4)
acastiblancoc 4:6f2a8091f2a0 234 copy_matrix(5,6,2,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 235 break;
acastiblancoc 3:c9c3a56609f9 236
acastiblancoc 4:6f2a8091f2a0 237 case 4: if(rotacion==1)
acastiblancoc 4:6f2a8091f2a0 238 copy_matrix(5,3,2,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 239 if(rotacion==2)
acastiblancoc 4:6f2a8091f2a0 240 copy_matrix(0,1,0,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 241 if(rotacion==3)
acastiblancoc 4:6f2a8091f2a0 242 copy_matrix(5,2,3,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 243 if(rotacion==4)
acastiblancoc 4:6f2a8091f2a0 244 copy_matrix(3,1,3,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 245 break;
acastiblancoc 3:c9c3a56609f9 246
acastiblancoc 4:6f2a8091f2a0 247 case 5: if(rotacion==1 || rotacion==3)
acastiblancoc 4:6f2a8091f2a0 248 copy_matrix(5,4,1,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 249 if(rotacion==2 || rotacion==4)
acastiblancoc 4:6f2a8091f2a0 250 copy_matrix(0,1,3,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 251 break;
acastiblancoc 3:c9c3a56609f9 252 }
acastiblancoc 3:c9c3a56609f9 253 }
acastiblancoc 3:c9c3a56609f9 254
acastiblancoc 4:6f2a8091f2a0 255 void figuras(){
acastiblancoc 4:6f2a8091f2a0 256 figura= rand() % 5+1;
acastiblancoc 4:6f2a8091f2a0 257 rotacion= rand() % 4+1;
acastiblancoc 4:6f2a8091f2a0 258 columna= 4;
acastiblancoc 3:c9c3a56609f9 259 }
acastiblancoc 3:c9c3a56609f9 260
acastiblancoc 4:6f2a8091f2a0 261 void elimina_linea(){
acastiblancoc 4:6f2a8091f2a0 262 for(uint8_t i=0;i<9;i++){
acastiblancoc 4:6f2a8091f2a0 263 if(matrix_act[i]==255){
acastiblancoc 3:c9c3a56609f9 264 for(uint8_t a=i;a>=1;a--){
acastiblancoc 4:6f2a8091f2a0 265 sendSPI(a,matrix_act[a-1]);
acastiblancoc 4:6f2a8091f2a0 266 matrix_act[a]=matrix_act[a-1];
acastiblancoc 3:c9c3a56609f9 267 }
acastiblancoc 5:63e225628634 268 punto++;
acastiblancoc 5:63e225628634 269 printf("%f", punto);
acastiblancoc 3:c9c3a56609f9 270 }
acastiblancoc 3:c9c3a56609f9 271 }
acastiblancoc 3:c9c3a56609f9 272 }
acastiblancoc 3:c9c3a56609f9 273
acastiblancoc 3:c9c3a56609f9 274 int main() {
acastiblancoc 4:6f2a8091f2a0 275 bajar.attach(&bajaf, 0.1);
acastiblancoc 4:6f2a8091f2a0 276 init_display();
acastiblancoc 3:c9c3a56609f9 277 borrar();
acastiblancoc 3:c9c3a56609f9 278 while(1){
acastiblancoc 4:6f2a8091f2a0 279 figuras();
acastiblancoc 4:6f2a8091f2a0 280 buscar_fila();
acastiblancoc 4:6f2a8091f2a0 281 cont=0;
acastiblancoc 5:63e225628634 282 elimina_linea();
acastiblancoc 3:c9c3a56609f9 283 while (cont<fila){
acastiblancoc 4:6f2a8091f2a0 284 movimiento_fig();
acastiblancoc 4:6f2a8091f2a0 285 crear_fig(figura,rotacion,columna);
acastiblancoc 4:6f2a8091f2a0 286 buscar_fila();
acastiblancoc 4:6f2a8091f2a0 287 print_matrix(matrix_temp);
acastiblancoc 4:6f2a8091f2a0 288 }
acastiblancoc 4:6f2a8091f2a0 289 guardar();
acastiblancoc 3:c9c3a56609f9 290 }
acastiblancoc 3:c9c3a56609f9 291 }