alis

Dependencies:   mbed

Committer:
acastiblancoc
Date:
Sun Nov 11 08:40:45 2018 +0000
Revision:
4:6f2a8091f2a0
Parent:
3:c9c3a56609f9
Child:
5:63e225628634
ok

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 4:6f2a8091f2a0 13 int cc=0;
acastiblancoc 4:6f2a8091f2a0 14 uint8_t movimiento=0;
acastiblancoc 3:c9c3a56609f9 15
acastiblancoc 3:c9c3a56609f9 16 #define VEL 100
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 4:6f2a8091f2a0 70 void buscar_fila(){
acastiblancoc 3:c9c3a56609f9 71 for (uint8_t i=0;i<9;i++){
acastiblancoc 4:6f2a8091f2a0 72 if((matrix_act[i]& matrix_temp[3])==0){
acastiblancoc 3:c9c3a56609f9 73 fila=i;
acastiblancoc 3:c9c3a56609f9 74 }
acastiblancoc 4:6f2a8091f2a0 75 if(((matrix_act[i]& matrix_temp[3])!=0)&& (i!=9)){
acastiblancoc 4:6f2a8091f2a0 76 fila=i-1;
acastiblancoc 4:6f2a8091f2a0 77 command.printf("Fila pos 1: %d \n", fila);
acastiblancoc 3:c9c3a56609f9 78 i=9;
acastiblancoc 3:c9c3a56609f9 79 }
acastiblancoc 4:6f2a8091f2a0 80 if(((matrix_act[i]& matrix_temp[2])!=0)&& (i!=9)){
acastiblancoc 3:c9c3a56609f9 81 fila=i;
acastiblancoc 3:c9c3a56609f9 82 i=9;
acastiblancoc 4:6f2a8091f2a0 83 command.printf("Fila pos 2: %d \n", fila);
acastiblancoc 3:c9c3a56609f9 84 }
acastiblancoc 4:6f2a8091f2a0 85 if(((matrix_act[i]& matrix_temp[1])!=0)&& (i!=9)){
acastiblancoc 3:c9c3a56609f9 86 fila=i+1;
acastiblancoc 3:c9c3a56609f9 87 i=9;
acastiblancoc 4:6f2a8091f2a0 88 command.printf("Fila pos 3: %d \n", fila);
acastiblancoc 3:c9c3a56609f9 89 }
acastiblancoc 3:c9c3a56609f9 90 }
acastiblancoc 3:c9c3a56609f9 91 if(fila>8)
acastiblancoc 3:c9c3a56609f9 92 fila=8;
acastiblancoc 3:c9c3a56609f9 93 if(fila<=1){
acastiblancoc 3:c9c3a56609f9 94 wait_ms(VEL);
acastiblancoc 3:c9c3a56609f9 95 borrar();
acastiblancoc 3:c9c3a56609f9 96 fila=8;
acastiblancoc 3:c9c3a56609f9 97 }
acastiblancoc 3:c9c3a56609f9 98 };
acastiblancoc 3:c9c3a56609f9 99
acastiblancoc 4:6f2a8091f2a0 100 void guardar(){
acastiblancoc 4:6f2a8091f2a0 101 matrix_act[fila-3]=matrix_temp1[0];
acastiblancoc 4:6f2a8091f2a0 102 matrix_act[fila-2]=matrix_temp1[1];
acastiblancoc 4:6f2a8091f2a0 103 matrix_act[fila-1]=matrix_temp1[2];
acastiblancoc 4:6f2a8091f2a0 104 matrix_act[fila ]=matrix_temp1[3];
acastiblancoc 3:c9c3a56609f9 105 };
acastiblancoc 3:c9c3a56609f9 106
acastiblancoc 4:6f2a8091f2a0 107 void reada()
acastiblancoc 4:6f2a8091f2a0 108 { vx=0;
acastiblancoc 4:6f2a8091f2a0 109 vy=0;
acastiblancoc 4:6f2a8091f2a0 110 vx=joy.read();
acastiblancoc 4:6f2a8091f2a0 111 vy=joy1.read();
acastiblancoc 4:6f2a8091f2a0 112 cc=cambio.read();
acastiblancoc 4:6f2a8091f2a0 113 }
acastiblancoc 3:c9c3a56609f9 114
acastiblancoc 4:6f2a8091f2a0 115 void bajaf()
acastiblancoc 4:6f2a8091f2a0 116 {
acastiblancoc 4:6f2a8091f2a0 117 movimiento=4;}
acastiblancoc 4:6f2a8091f2a0 118 void movimiento_fig(){
acastiblancoc 4:6f2a8091f2a0 119 //movimiento=0;
acastiblancoc 4:6f2a8091f2a0 120 /*reada();
acastiblancoc 4:6f2a8091f2a0 121 command.printf("vy = %f \n", vy);
acastiblancoc 4:6f2a8091f2a0 122 command.printf("vx = %f \n", vx);
acastiblancoc 4:6f2a8091f2a0 123 command.printf("cc = %f \n", cambio.read());
acastiblancoc 4:6f2a8091f2a0 124 if(vy<0.2)
acastiblancoc 4:6f2a8091f2a0 125 movimiento=1;
acastiblancoc 3:c9c3a56609f9 126
acastiblancoc 4:6f2a8091f2a0 127 /* if(vy>0.8)
acastiblancoc 4:6f2a8091f2a0 128 movimiento=4;
acastiblancoc 3:c9c3a56609f9 129
acastiblancoc 4:6f2a8091f2a0 130 if(vx<0.2)
acastiblancoc 4:6f2a8091f2a0 131 movimiento=3;
acastiblancoc 3:c9c3a56609f9 132
acastiblancoc 4:6f2a8091f2a0 133 if(vx>0.80)
acastiblancoc 4:6f2a8091f2a0 134 movimiento=2;
acastiblancoc 4:6f2a8091f2a0 135 command.printf("movimiento = %d \n", movimiento);
acastiblancoc 4:6f2a8091f2a0 136 */
acastiblancoc 4:6f2a8091f2a0 137 char movimiento1=command.getc();
acastiblancoc 4:6f2a8091f2a0 138 if(movimiento1==30)
acastiblancoc 4:6f2a8091f2a0 139 movimiento=1;
acastiblancoc 3:c9c3a56609f9 140
acastiblancoc 4:6f2a8091f2a0 141 if(movimiento1==29)
acastiblancoc 4:6f2a8091f2a0 142 movimiento=2;
acastiblancoc 4:6f2a8091f2a0 143
acastiblancoc 4:6f2a8091f2a0 144 if(movimiento1==28)
acastiblancoc 4:6f2a8091f2a0 145 movimiento=3;
acastiblancoc 4:6f2a8091f2a0 146 /*
acastiblancoc 4:6f2a8091f2a0 147 if(movimiento==31)
acastiblancoc 4:6f2a8091f2a0 148 movimiento=0;*/
acastiblancoc 4:6f2a8091f2a0 149 switch (movimiento){
acastiblancoc 3:c9c3a56609f9 150
acastiblancoc 4:6f2a8091f2a0 151 case 4:
acastiblancoc 3:c9c3a56609f9 152 cont++;
acastiblancoc 4:6f2a8091f2a0 153 //printf("%f", joy.read());
acastiblancoc 3:c9c3a56609f9 154 if(cont>8)cont=8;
acastiblancoc 4:6f2a8091f2a0 155 break;
acastiblancoc 3:c9c3a56609f9 156
acastiblancoc 3:c9c3a56609f9 157 case 1:
acastiblancoc 4:6f2a8091f2a0 158 if( (matrix_temp[1]==2) || (matrix_temp[1]==1)|| (matrix_temp[1]==3))
acastiblancoc 4:6f2a8091f2a0 159 columna--;
acastiblancoc 4:6f2a8091f2a0 160 rotacion++;
acastiblancoc 4:6f2a8091f2a0 161 if(rotacion>4)
acastiblancoc 4:6f2a8091f2a0 162 rotacion=1;
acastiblancoc 4:6f2a8091f2a0 163 //printf("%f", joy1.read());
acastiblancoc 4:6f2a8091f2a0 164 break;
acastiblancoc 3:c9c3a56609f9 165
acastiblancoc 4:6f2a8091f2a0 166 case 2:
acastiblancoc 4:6f2a8091f2a0 167 if((matrix_temp[2]==7 || matrix_temp[3]==7)||(matrix_temp[2]==3 && matrix_temp[3]==6)) {
acastiblancoc 3:c9c3a56609f9 168 command.printf("a");
acastiblancoc 4:6f2a8091f2a0 169 columna=6;
acastiblancoc 3:c9c3a56609f9 170 }
acastiblancoc 4:6f2a8091f2a0 171 else if((matrix_temp[1]==2 && matrix_temp[2]==2 && matrix_temp[3]==2)){
acastiblancoc 4:6f2a8091f2a0 172 command.printf("b");
acastiblancoc 4:6f2a8091f2a0 173 columna=8;
acastiblancoc 4:6f2a8091f2a0 174 }
acastiblancoc 4:6f2a8091f2a0 175 else if (columna<7){
acastiblancoc 4:6f2a8091f2a0 176 command.printf("c");
acastiblancoc 4:6f2a8091f2a0 177 columna++;
acastiblancoc 3:c9c3a56609f9 178 }
acastiblancoc 4:6f2a8091f2a0 179 //printf("%f", joy1.read());
acastiblancoc 4:6f2a8091f2a0 180 break;
acastiblancoc 4:6f2a8091f2a0 181
acastiblancoc 3:c9c3a56609f9 182 case 3:
acastiblancoc 4:6f2a8091f2a0 183 columna--;
acastiblancoc 4:6f2a8091f2a0 184 if(columna<1)
acastiblancoc 4:6f2a8091f2a0 185 columna=1;
acastiblancoc 4:6f2a8091f2a0 186 //printf("%f", joy.read());
acastiblancoc 4:6f2a8091f2a0 187 break;
acastiblancoc 4:6f2a8091f2a0 188
acastiblancoc 4:6f2a8091f2a0 189 }
acastiblancoc 3:c9c3a56609f9 190 }
acastiblancoc 3:c9c3a56609f9 191
acastiblancoc 3:c9c3a56609f9 192
acastiblancoc 4:6f2a8091f2a0 193 void print_matrix(uint8_t *temp){
acastiblancoc 4:6f2a8091f2a0 194 matrix_temp1[0]=(matrix_temp[0]+ matrix_act[cont-3]);
acastiblancoc 4:6f2a8091f2a0 195 matrix_temp1[1]=(matrix_temp[1]+ matrix_act[cont-2]);
acastiblancoc 4:6f2a8091f2a0 196 matrix_temp1[2]=(matrix_temp[2]+ matrix_act[cont-1]);
acastiblancoc 4:6f2a8091f2a0 197 matrix_temp1[3]=(matrix_temp[3]+ matrix_act[cont]);
acastiblancoc 4:6f2a8091f2a0 198 sendSPI(cont-3,matrix_temp1[0]);
acastiblancoc 4:6f2a8091f2a0 199 sendSPI(cont-2,matrix_temp1[1]);
acastiblancoc 4:6f2a8091f2a0 200 sendSPI(cont-1,matrix_temp1[2]);
acastiblancoc 4:6f2a8091f2a0 201 sendSPI(cont ,matrix_temp1[3]); //pasa de i+2 a i+1
acastiblancoc 3:c9c3a56609f9 202 wait_ms(VEL);
acastiblancoc 3:c9c3a56609f9 203 }
acastiblancoc 3:c9c3a56609f9 204
acastiblancoc 4:6f2a8091f2a0 205 void crear_fig(char type_fig,char rotacion,char columna)
acastiblancoc 3:c9c3a56609f9 206 {
acastiblancoc 3:c9c3a56609f9 207 switch (type_fig){
acastiblancoc 3:c9c3a56609f9 208 case 1: if(columna>7)
acastiblancoc 3:c9c3a56609f9 209 columna=7;
acastiblancoc 4:6f2a8091f2a0 210 copy_matrix(5,1,1,PLANT,columna); break;
acastiblancoc 3:c9c3a56609f9 211
acastiblancoc 4:6f2a8091f2a0 212 case 2: if(rotacion==1 || rotacion==3)
acastiblancoc 4:6f2a8091f2a0 213 copy_matrix(0,0,0,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 214 if(rotacion==2 || rotacion==4)
acastiblancoc 4:6f2a8091f2a0 215 copy_matrix(5,5,2,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 216 break;
acastiblancoc 3:c9c3a56609f9 217
acastiblancoc 4:6f2a8091f2a0 218 case 3: if(rotacion==1)
acastiblancoc 4:6f2a8091f2a0 219 copy_matrix(0,0,1,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 220 if(rotacion==2)
acastiblancoc 4:6f2a8091f2a0 221 copy_matrix(5,2,0,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 222 if(rotacion==3)
acastiblancoc 4:6f2a8091f2a0 223 copy_matrix(1,3,3,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 224 if(rotacion==4)
acastiblancoc 4:6f2a8091f2a0 225 copy_matrix(5,6,2,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 226 break;
acastiblancoc 3:c9c3a56609f9 227
acastiblancoc 4:6f2a8091f2a0 228 case 4: if(rotacion==1)
acastiblancoc 4:6f2a8091f2a0 229 copy_matrix(5,3,2,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 230 if(rotacion==2)
acastiblancoc 4:6f2a8091f2a0 231 copy_matrix(0,1,0,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 232 if(rotacion==3)
acastiblancoc 4:6f2a8091f2a0 233 copy_matrix(5,2,3,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 234 if(rotacion==4)
acastiblancoc 4:6f2a8091f2a0 235 copy_matrix(3,1,3,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 236 break;
acastiblancoc 3:c9c3a56609f9 237
acastiblancoc 4:6f2a8091f2a0 238 case 5: if(rotacion==1 || rotacion==3)
acastiblancoc 4:6f2a8091f2a0 239 copy_matrix(5,4,1,PLANT,columna);
acastiblancoc 4:6f2a8091f2a0 240 if(rotacion==2 || rotacion==4)
acastiblancoc 4:6f2a8091f2a0 241 copy_matrix(0,1,3,PLANT,columna);
acastiblancoc 3:c9c3a56609f9 242 break;
acastiblancoc 3:c9c3a56609f9 243 }
acastiblancoc 3:c9c3a56609f9 244 }
acastiblancoc 3:c9c3a56609f9 245
acastiblancoc 4:6f2a8091f2a0 246 void figuras(){
acastiblancoc 4:6f2a8091f2a0 247 figura= rand() % 5+1;
acastiblancoc 4:6f2a8091f2a0 248 rotacion= rand() % 4+1;
acastiblancoc 4:6f2a8091f2a0 249 columna= 4;
acastiblancoc 3:c9c3a56609f9 250 }
acastiblancoc 3:c9c3a56609f9 251
acastiblancoc 4:6f2a8091f2a0 252 void elimina_linea(){
acastiblancoc 4:6f2a8091f2a0 253 for(uint8_t i=0;i<9;i++){
acastiblancoc 4:6f2a8091f2a0 254 if(matrix_act[i]==255){
acastiblancoc 3:c9c3a56609f9 255 for(uint8_t a=i;a>=1;a--){
acastiblancoc 4:6f2a8091f2a0 256 sendSPI(a,matrix_act[a-1]);
acastiblancoc 4:6f2a8091f2a0 257 matrix_act[a]=matrix_act[a-1];
acastiblancoc 3:c9c3a56609f9 258 }
acastiblancoc 3:c9c3a56609f9 259 }
acastiblancoc 3:c9c3a56609f9 260 }
acastiblancoc 3:c9c3a56609f9 261 }
acastiblancoc 3:c9c3a56609f9 262
acastiblancoc 3:c9c3a56609f9 263 int main() {
acastiblancoc 4:6f2a8091f2a0 264 bajar.attach(&bajaf, 0.1);
acastiblancoc 4:6f2a8091f2a0 265 init_display();
acastiblancoc 3:c9c3a56609f9 266 borrar();
acastiblancoc 3:c9c3a56609f9 267 while(1){
acastiblancoc 4:6f2a8091f2a0 268 figuras();
acastiblancoc 4:6f2a8091f2a0 269 buscar_fila();
acastiblancoc 4:6f2a8091f2a0 270 cont=0;
acastiblancoc 4:6f2a8091f2a0 271 elimina_linea();
acastiblancoc 3:c9c3a56609f9 272 while (cont<fila){
acastiblancoc 4:6f2a8091f2a0 273 movimiento_fig();
acastiblancoc 4:6f2a8091f2a0 274 crear_fig(figura,rotacion,columna);
acastiblancoc 4:6f2a8091f2a0 275 buscar_fila();
acastiblancoc 4:6f2a8091f2a0 276 print_matrix(matrix_temp);
acastiblancoc 4:6f2a8091f2a0 277 }
acastiblancoc 4:6f2a8091f2a0 278 guardar();
acastiblancoc 3:c9c3a56609f9 279 }
acastiblancoc 3:c9c3a56609f9 280 }