Version 2 tetris

Dependencies:   mbed

Fork of Nucleo_pwm3 by julian alvarez

Committer:
acastiblancoc
Date:
Sun Oct 21 00:28:09 2018 +0000
Revision:
1:b8818726b16f
Codigo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
acastiblancoc 1:b8818726b16f 1 #include "mbed.h"
acastiblancoc 1:b8818726b16f 2 #include "figs.h"
acastiblancoc 1:b8818726b16f 3
acastiblancoc 1:b8818726b16f 4 SPI deviceM(PB_15, PB_14, PB_13);
acastiblancoc 1:b8818726b16f 5 DigitalOut ssel (PB_12);
acastiblancoc 1:b8818726b16f 6 Serial device(USBTX,USBRX);
acastiblancoc 1:b8818726b16f 7 #define VEL 80
acastiblancoc 1:b8818726b16f 8 uint8_t matrix_act[11]={0,0,0,0,0,0,0,0,0,0,0};
acastiblancoc 1:b8818726b16f 9 uint8_t matrix_tmp[8]={0,0,0,0,0,0,0,0};
acastiblancoc 1:b8818726b16f 10 uint8_t matrix_tmp1[8]={0,0,0,0,0,0,0,0};
acastiblancoc 1:b8818726b16f 11 uint8_t fila=0,i=0, cont=0, ncolumna=1, grados=0, output1=0;
acastiblancoc 1:b8818726b16f 12
acastiblancoc 1:b8818726b16f 13
acastiblancoc 1:b8818726b16f 14 void Rx_interrupt() {
acastiblancoc 1:b8818726b16f 15 output1=!output1;
acastiblancoc 1:b8818726b16f 16 char d = device.getc(); //obtiene lo que hay en el puerto serial
acastiblancoc 1:b8818726b16f 17
acastiblancoc 1:b8818726b16f 18 device.printf("irq %c\n",d);//hace el cambio en el led
acastiblancoc 1:b8818726b16f 19 void correr_fig();
acastiblancoc 1:b8818726b16f 20 return;
acastiblancoc 1:b8818726b16f 21 }
acastiblancoc 1:b8818726b16f 22
acastiblancoc 1:b8818726b16f 23 void sendSPI(uint8_t d1, uint8_t d2)
acastiblancoc 1:b8818726b16f 24 {
acastiblancoc 1:b8818726b16f 25 deviceM.unlock();
acastiblancoc 1:b8818726b16f 26 ssel=0;
acastiblancoc 1:b8818726b16f 27 deviceM.write(d1);
acastiblancoc 1:b8818726b16f 28 deviceM.write(d2);
acastiblancoc 1:b8818726b16f 29 ssel=1;
acastiblancoc 1:b8818726b16f 30 deviceM.lock();
acastiblancoc 1:b8818726b16f 31 }
acastiblancoc 1:b8818726b16f 32
acastiblancoc 1:b8818726b16f 33 void init_display()
acastiblancoc 1:b8818726b16f 34 {
acastiblancoc 1:b8818726b16f 35 sendSPI(0x0c,1);
acastiblancoc 1:b8818726b16f 36 sendSPI(0x0b,7);
acastiblancoc 1:b8818726b16f 37 sendSPI(0x09,0);
acastiblancoc 1:b8818726b16f 38 sendSPI(0x0A,0x0E);
acastiblancoc 1:b8818726b16f 39 int j;
acastiblancoc 1:b8818726b16f 40 for (j=0;j<4;j++){
acastiblancoc 1:b8818726b16f 41 sendSPI(0x0F,1);
acastiblancoc 1:b8818726b16f 42 wait (0.2);
acastiblancoc 1:b8818726b16f 43 sendSPI(0x0f,0);
acastiblancoc 1:b8818726b16f 44 wait (0.2);
acastiblancoc 1:b8818726b16f 45 }
acastiblancoc 1:b8818726b16f 46 }
acastiblancoc 1:b8818726b16f 47
acastiblancoc 1:b8818726b16f 48 void copy_matrix(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
acastiblancoc 1:b8818726b16f 49 {
acastiblancoc 1:b8818726b16f 50 matrix_tmp[0]= *(fig+5)>>columna-1;
acastiblancoc 1:b8818726b16f 51 matrix_tmp[1]= *(fig+a)>>columna-1;
acastiblancoc 1:b8818726b16f 52 matrix_tmp[2]= *(fig+b)>>columna-1;
acastiblancoc 1:b8818726b16f 53 matrix_tmp[3]= *(fig+c)>>columna-1;
acastiblancoc 1:b8818726b16f 54 }
acastiblancoc 1:b8818726b16f 55
acastiblancoc 1:b8818726b16f 56 void clean_mat()
acastiblancoc 1:b8818726b16f 57 {
acastiblancoc 1:b8818726b16f 58 int j;
acastiblancoc 1:b8818726b16f 59 for(j=0;j<=8;j++)
acastiblancoc 1:b8818726b16f 60 {
acastiblancoc 1:b8818726b16f 61 sendSPI(0x0+j,0x00);
acastiblancoc 1:b8818726b16f 62 }
acastiblancoc 1:b8818726b16f 63 };
acastiblancoc 1:b8818726b16f 64
acastiblancoc 1:b8818726b16f 65 void buscar_fil(){
acastiblancoc 1:b8818726b16f 66 for (uint8_t j=0;j<9;j++){
acastiblancoc 1:b8818726b16f 67 if((matrix_act[j]& matrix_tmp[3])==0){
acastiblancoc 1:b8818726b16f 68 fila=j;
acastiblancoc 1:b8818726b16f 69 }
acastiblancoc 1:b8818726b16f 70 if((matrix_act[j]& matrix_tmp[3])!=0){
acastiblancoc 1:b8818726b16f 71 fila=j-1;
acastiblancoc 1:b8818726b16f 72 j=9;
acastiblancoc 1:b8818726b16f 73 }
acastiblancoc 1:b8818726b16f 74 if((matrix_act[j]& matrix_tmp[2])!=0){
acastiblancoc 1:b8818726b16f 75 fila=j;
acastiblancoc 1:b8818726b16f 76 j=9;
acastiblancoc 1:b8818726b16f 77 }
acastiblancoc 1:b8818726b16f 78 if((matrix_act[j]& matrix_tmp[1])!=0){
acastiblancoc 1:b8818726b16f 79 fila=j+1;
acastiblancoc 1:b8818726b16f 80 j=9;
acastiblancoc 1:b8818726b16f 81 }
acastiblancoc 1:b8818726b16f 82 }
acastiblancoc 1:b8818726b16f 83 if(fila>8)
acastiblancoc 1:b8818726b16f 84 fila=8;
acastiblancoc 1:b8818726b16f 85
acastiblancoc 1:b8818726b16f 86 };
acastiblancoc 1:b8818726b16f 87
acastiblancoc 1:b8818726b16f 88 void guardar_mat(){
acastiblancoc 1:b8818726b16f 89 matrix_act[fila-3]=matrix_tmp1[0];
acastiblancoc 1:b8818726b16f 90 matrix_act[fila-2]=matrix_tmp1[1];
acastiblancoc 1:b8818726b16f 91 matrix_act[fila-1]=matrix_tmp1[2];
acastiblancoc 1:b8818726b16f 92 matrix_act[fila]=matrix_tmp1[3];
acastiblancoc 1:b8818726b16f 93 };
acastiblancoc 1:b8818726b16f 94
acastiblancoc 1:b8818726b16f 95
acastiblancoc 1:b8818726b16f 96 void imp_mat(uint8_t *temp){
acastiblancoc 1:b8818726b16f 97 buscar_fil();
acastiblancoc 1:b8818726b16f 98 for (int j=0;j<=fila;j++){
acastiblancoc 1:b8818726b16f 99 matrix_tmp1[0]=(matrix_tmp[0]+ matrix_act[j-3]);
acastiblancoc 1:b8818726b16f 100 matrix_tmp1[1]=(matrix_tmp[1]+ matrix_act[j-2]);
acastiblancoc 1:b8818726b16f 101 matrix_tmp1[2]=(matrix_tmp[2]+ matrix_act[j-1]);
acastiblancoc 1:b8818726b16f 102 matrix_tmp1[3]=(matrix_tmp[3]+ matrix_act[j]);
acastiblancoc 1:b8818726b16f 103 sendSPI(j-3,matrix_tmp1[0]);
acastiblancoc 1:b8818726b16f 104 sendSPI(j-2,matrix_tmp1[1]);
acastiblancoc 1:b8818726b16f 105 sendSPI(j-1,matrix_tmp1[2]);
acastiblancoc 1:b8818726b16f 106 sendSPI(j ,matrix_tmp1[3]);
acastiblancoc 1:b8818726b16f 107 wait_ms(VEL);
acastiblancoc 1:b8818726b16f 108 }
acastiblancoc 1:b8818726b16f 109 }
acastiblancoc 1:b8818726b16f 110 void correr_fig(){
acastiblancoc 1:b8818726b16f 111
acastiblancoc 1:b8818726b16f 112 uint8_t correr=device.getc();
acastiblancoc 1:b8818726b16f 113
acastiblancoc 1:b8818726b16f 114 if(correr==30)
acastiblancoc 1:b8818726b16f 115 correr=1;
acastiblancoc 1:b8818726b16f 116
acastiblancoc 1:b8818726b16f 117 if(correr==29)
acastiblancoc 1:b8818726b16f 118 correr=2;
acastiblancoc 1:b8818726b16f 119
acastiblancoc 1:b8818726b16f 120 if(correr==28)
acastiblancoc 1:b8818726b16f 121 correr=3;
acastiblancoc 1:b8818726b16f 122
acastiblancoc 1:b8818726b16f 123 if(correr==31)
acastiblancoc 1:b8818726b16f 124 correr=0;
acastiblancoc 1:b8818726b16f 125
acastiblancoc 1:b8818726b16f 126 switch (correr){
acastiblancoc 1:b8818726b16f 127 /*
acastiblancoc 1:b8818726b16f 128 case 0:
acastiblancoc 1:b8818726b16f 129 cont++;
acastiblancoc 1:b8818726b16f 130 if(cont>8)cont=8;
acastiblancoc 1:b8818726b16f 131 break;*/
acastiblancoc 1:b8818726b16f 132
acastiblancoc 1:b8818726b16f 133 case 1:
acastiblancoc 1:b8818726b16f 134 if( (matrix_tmp[1]==2) || (matrix_tmp[1]==1)|| (matrix_tmp[1]==3))
acastiblancoc 1:b8818726b16f 135 ncolumna--;
acastiblancoc 1:b8818726b16f 136 grados++;
acastiblancoc 1:b8818726b16f 137 if(grados>4)
acastiblancoc 1:b8818726b16f 138 grados=1;
acastiblancoc 1:b8818726b16f 139 break;
acastiblancoc 1:b8818726b16f 140
acastiblancoc 1:b8818726b16f 141 case 2:
acastiblancoc 1:b8818726b16f 142 if((matrix_tmp[2]==7 || matrix_tmp[3]==7)||(matrix_tmp[2]==3 && matrix_tmp[3]==6)) {
acastiblancoc 1:b8818726b16f 143 device.printf("a");
acastiblancoc 1:b8818726b16f 144 ncolumna=6;
acastiblancoc 1:b8818726b16f 145 }
acastiblancoc 1:b8818726b16f 146 else if((matrix_tmp[1]==2 && matrix_tmp[2]==2 && matrix_tmp[3]==2)){
acastiblancoc 1:b8818726b16f 147 device.printf("b");
acastiblancoc 1:b8818726b16f 148 ncolumna=8;
acastiblancoc 1:b8818726b16f 149 }
acastiblancoc 1:b8818726b16f 150 else if (ncolumna<7){
acastiblancoc 1:b8818726b16f 151 device.printf("c");
acastiblancoc 1:b8818726b16f 152 ncolumna++;
acastiblancoc 1:b8818726b16f 153 }
acastiblancoc 1:b8818726b16f 154 break;
acastiblancoc 1:b8818726b16f 155
acastiblancoc 1:b8818726b16f 156 case 3:
acastiblancoc 1:b8818726b16f 157 ncolumna--;
acastiblancoc 1:b8818726b16f 158 if(ncolumna<1)
acastiblancoc 1:b8818726b16f 159 ncolumna=1;
acastiblancoc 1:b8818726b16f 160 break;
acastiblancoc 1:b8818726b16f 161 }
acastiblancoc 1:b8818726b16f 162
acastiblancoc 1:b8818726b16f 163 }
acastiblancoc 1:b8818726b16f 164
acastiblancoc 1:b8818726b16f 165
acastiblancoc 1:b8818726b16f 166 void new_fig(char type_fig,char grados,char columna)
acastiblancoc 1:b8818726b16f 167 {
acastiblancoc 1:b8818726b16f 168 switch (type_fig){
acastiblancoc 1:b8818726b16f 169 case 1: if(columna>7)
acastiblancoc 1:b8818726b16f 170 columna=7;
acastiblancoc 1:b8818726b16f 171 copy_matrix(5,1,1,PLANT,columna); break;
acastiblancoc 1:b8818726b16f 172
acastiblancoc 1:b8818726b16f 173 case 2: if(grados==1 || grados==3)
acastiblancoc 1:b8818726b16f 174 copy_matrix(0,0,0,PLANT,columna);
acastiblancoc 1:b8818726b16f 175 if(grados==2 || grados==4)
acastiblancoc 1:b8818726b16f 176 copy_matrix(5,5,2,PLANT,columna);
acastiblancoc 1:b8818726b16f 177 break;
acastiblancoc 1:b8818726b16f 178
acastiblancoc 1:b8818726b16f 179 case 3: if(grados==1)
acastiblancoc 1:b8818726b16f 180 copy_matrix(0,0,1,PLANT,columna);
acastiblancoc 1:b8818726b16f 181 if(grados==2)
acastiblancoc 1:b8818726b16f 182 copy_matrix(5,2,0,PLANT,columna);
acastiblancoc 1:b8818726b16f 183 if(grados==3)
acastiblancoc 1:b8818726b16f 184 copy_matrix(1,3,3,PLANT,columna);
acastiblancoc 1:b8818726b16f 185 if(grados==4)
acastiblancoc 1:b8818726b16f 186 copy_matrix(5,6,2,PLANT,columna);
acastiblancoc 1:b8818726b16f 187 break;
acastiblancoc 1:b8818726b16f 188
acastiblancoc 1:b8818726b16f 189 case 4: if(grados==1)
acastiblancoc 1:b8818726b16f 190 copy_matrix(5,3,2,PLANT,columna);
acastiblancoc 1:b8818726b16f 191 if(grados==2)
acastiblancoc 1:b8818726b16f 192 copy_matrix(0,1,0,PLANT,columna);
acastiblancoc 1:b8818726b16f 193 if(grados==3)
acastiblancoc 1:b8818726b16f 194 copy_matrix(5,2,3,PLANT,columna);
acastiblancoc 1:b8818726b16f 195 if(grados==4)
acastiblancoc 1:b8818726b16f 196 copy_matrix(3,1,3,PLANT,columna);
acastiblancoc 1:b8818726b16f 197 break;
acastiblancoc 1:b8818726b16f 198
acastiblancoc 1:b8818726b16f 199 case 5: if(grados==1 || grados==3)
acastiblancoc 1:b8818726b16f 200 copy_matrix(5,4,1,PLANT,columna);
acastiblancoc 1:b8818726b16f 201 if(grados==2 || grados==4)
acastiblancoc 1:b8818726b16f 202 copy_matrix(0,1,3,PLANT,columna);
acastiblancoc 1:b8818726b16f 203 break;
acastiblancoc 1:b8818726b16f 204 }
acastiblancoc 1:b8818726b16f 205 }
acastiblancoc 1:b8818726b16f 206
acastiblancoc 1:b8818726b16f 207 int main() {
acastiblancoc 1:b8818726b16f 208 char tipo_fig=1,ncolumna=1,grados=1,lee1=0,lee2=0;
acastiblancoc 1:b8818726b16f 209 init_display();
acastiblancoc 1:b8818726b16f 210 clean_mat();
acastiblancoc 1:b8818726b16f 211 while(1){
acastiblancoc 1:b8818726b16f 212 lee1=device.getc();
acastiblancoc 1:b8818726b16f 213 tipo_fig=device.getc();
acastiblancoc 1:b8818726b16f 214 grados=device.getc();
acastiblancoc 1:b8818726b16f 215 ncolumna=device.getc();
acastiblancoc 1:b8818726b16f 216 lee2=device.getc();
acastiblancoc 1:b8818726b16f 217 if(lee1==0x3c && lee2==0x3e)
acastiblancoc 1:b8818726b16f 218 {
acastiblancoc 1:b8818726b16f 219 new_fig(tipo_fig,grados,ncolumna);
acastiblancoc 1:b8818726b16f 220 imp_mat(matrix_tmp);
acastiblancoc 1:b8818726b16f 221 guardar_mat();
acastiblancoc 1:b8818726b16f 222 }
acastiblancoc 1:b8818726b16f 223 }
acastiblancoc 1:b8818726b16f 224 }