Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 4:6f2a8091f2a0, committed 2018-11-11
- Comitter:
- acastiblancoc
- Date:
- Sun Nov 11 08:40:45 2018 +0000
- Parent:
- 3:c9c3a56609f9
- Commit message:
- ok
Changed in this revision
| Tetrix.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Tetrix.cpp Thu Oct 18 23:55:58 2018 +0000
+++ b/Tetrix.cpp Sun Nov 11 08:40:45 2018 +0000
@@ -1,16 +1,24 @@
#include "mbed.h"
#include "figs.h"
+Ticker bajar;
SPI deviceM(PB_15, PB_14, PB_13);
DigitalOut ssel (PB_12);
-Serial command(USBTX,USBRX);
+Serial command(USBTX,USBRX);
+AnalogIn joy(A0);
+AnalogIn joy1(A1);
+float vx=0;
+float vy=0;
+DigitalIn cambio(PB_8);
+int cc=0;
+uint8_t movimiento=0;
#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;
+uint8_t matrix_act [11]={0,0,0,0,0,0,0,0,0,0,0};
+uint8_t matrix_temp [8]={0,0,0,0,0,0,0,0};
+uint8_t matrix_temp1[8]={0,0,0,0,0,0,0,0};
+uint8_t fila=1,cont=0,columna=1,rotacion=1,figura=0;
void sendSPI(uint8_t d1, uint8_t d2)
{
@@ -24,7 +32,7 @@
-void test()
+void init_display()
{
sendSPI(0x0c,1);
sendSPI(0x0b,7);
@@ -34,18 +42,18 @@
for (j=0;j<4;j++){
sendSPI(0x0F,1);
wait (0.2);
- sendSPI(0x0F,0);
+ sendSPI(0x0f,0);
wait (0.2);
- }
+ }
}
-void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
+void copy_matrix(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;
+ 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)){
+ matrix_temp[0]= *(fig+5)>>columna-1;
+ matrix_temp[1]= *(fig+a)>>columna-1;
+ matrix_temp[2]= *(fig+b)>>columna-1;
+ matrix_temp[3]= *(fig+c)>>columna-1;
}
}
@@ -55,27 +63,29 @@
for(i=0;i<=8;i++)
{
sendSPI(i,0);
- mat_act[i]=0;
+ matrix_act[i]=0;
}
};
-void buscar_fil(){
+void buscar_fila(){
for (uint8_t i=0;i<9;i++){
- if((mat_act[i]& mat_tmp[3])==0){
+ if((matrix_act[i]& matrix_temp[3])==0){
fila=i;
}
- if((mat_act[i]& mat_tmp[3])!=0){
- fila=i-1;
+ if(((matrix_act[i]& matrix_temp[3])!=0)&& (i!=9)){
+ fila=i-1;
+ command.printf("Fila pos 1: %d \n", fila);
i=9;
}
- if((mat_act[i]& mat_tmp[2])!=0){
+ if(((matrix_act[i]& matrix_temp[2])!=0)&& (i!=9)){
fila=i;
i=9;
+ command.printf("Fila pos 2: %d \n", fila);
}
- if((mat_act[i]& mat_tmp[1])!=0){
+ if(((matrix_act[i]& matrix_temp[1])!=0)&& (i!=9)){
fila=i+1;
i=9;
- //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]);
+ command.printf("Fila pos 3: %d \n", fila);
}
}
if(fila>8)
@@ -87,167 +97,184 @@
}
};
-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 guardar(){
+ matrix_act[fila-3]=matrix_temp1[0];
+ matrix_act[fila-2]=matrix_temp1[1];
+ matrix_act[fila-1]=matrix_temp1[2];
+ matrix_act[fila ]=matrix_temp1[3];
};
-void correr_fig(){
-
- uint8_t correr=command.getc();
+void reada()
+{ vx=0;
+ vy=0;
+ vx=joy.read();
+ vy=joy1.read();
+ cc=cambio.read();
+ }
- if(correr==30)
- correr=1;
+void bajaf()
+{
+ movimiento=4;}
+void movimiento_fig(){
+ //movimiento=0;
+ /*reada();
+ command.printf("vy = %f \n", vy);
+ command.printf("vx = %f \n", vx);
+ command.printf("cc = %f \n", cambio.read());
+ if(vy<0.2)
+ movimiento=1;
- if(correr==29)
- correr=2;
+ /* if(vy>0.8)
+ movimiento=4;
- if(correr==28)
- correr=3;
+ if(vx<0.2)
+ movimiento=3;
- if(correr==31)
- correr=0;
+ if(vx>0.80)
+ movimiento=2;
+ command.printf("movimiento = %d \n", movimiento);
+ */
+ char movimiento1=command.getc();
+ if(movimiento1==30)
+ movimiento=1;
- switch (correr){
+ if(movimiento1==29)
+ movimiento=2;
+
+ if(movimiento1==28)
+ movimiento=3;
+ /*
+ if(movimiento==31)
+ movimiento=0;*/
+ switch (movimiento){
- case 0:
+ case 4:
cont++;
+ //printf("%f", joy.read());
if(cont>8)cont=8;
- break;
+ break;
case 1:
- if( (mat_tmp[1]==2) || (mat_tmp[1]==1)|| (mat_tmp[1]==3))
- ncolumna--;
- grados++;
- if(grados>4)
- grados=1;
- break;
+ if( (matrix_temp[1]==2) || (matrix_temp[1]==1)|| (matrix_temp[1]==3))
+ columna--;
+ rotacion++;
+ if(rotacion>4)
+ rotacion=1;
+ //printf("%f", joy1.read());
+ break;
- case 2:
- if((mat_tmp[2]==7 || mat_tmp[3]==7)||(mat_tmp[2]==3 && mat_tmp[3]==6)) {
+ case 2:
+ if((matrix_temp[2]==7 || matrix_temp[3]==7)||(matrix_temp[2]==3 && matrix_temp[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++;
+ columna=6;
}
- 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;
+ else if((matrix_temp[1]==2 && matrix_temp[2]==2 && matrix_temp[3]==2)){
+ command.printf("b");
+ columna=8;
+ }
+ else if (columna<7){
+ command.printf("c");
+ columna++;
}
- */
-
- /*if(ncolumna>7)
- {command.printf("d");
- ncolumna=7;}*/
- //correr a la Derecha;
-
+ //printf("%f", joy1.read());
+ break;
+
case 3:
- ncolumna--;
- if(ncolumna<1)
- ncolumna=1;
- break;
- }
-
+ columna--;
+ if(columna<1)
+ columna=1;
+ //printf("%f", joy.read());
+ 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
+void print_matrix(uint8_t *temp){
+ matrix_temp1[0]=(matrix_temp[0]+ matrix_act[cont-3]);
+ matrix_temp1[1]=(matrix_temp[1]+ matrix_act[cont-2]);
+ matrix_temp1[2]=(matrix_temp[2]+ matrix_act[cont-1]);
+ matrix_temp1[3]=(matrix_temp[3]+ matrix_act[cont]);
+ sendSPI(cont-3,matrix_temp1[0]);
+ sendSPI(cont-2,matrix_temp1[1]);
+ sendSPI(cont-1,matrix_temp1[2]);
+ sendSPI(cont ,matrix_temp1[3]); //pasa de i+2 a i+1
wait_ms(VEL);
}
-void dibujar(char type_fig,char grados,char columna)
+void crear_fig(char type_fig,char rotacion,char columna)
{
switch (type_fig){
case 1: if(columna>7)
columna=7;
- cop_mat(5,1,1,PLANT,columna); break;
+ copy_matrix(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);
+ case 2: if(rotacion==1 || rotacion==3)
+ copy_matrix(0,0,0,PLANT,columna);
+ if(rotacion==2 || rotacion==4)
+ copy_matrix(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);
+ case 3: if(rotacion==1)
+ copy_matrix(0,0,1,PLANT,columna);
+ if(rotacion==2)
+ copy_matrix(5,2,0,PLANT,columna);
+ if(rotacion==3)
+ copy_matrix(1,3,3,PLANT,columna);
+ if(rotacion==4)
+ copy_matrix(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);
+ case 4: if(rotacion==1)
+ copy_matrix(5,3,2,PLANT,columna);
+ if(rotacion==2)
+ copy_matrix(0,1,0,PLANT,columna);
+ if(rotacion==3)
+ copy_matrix(5,2,3,PLANT,columna);
+ if(rotacion==4)
+ copy_matrix(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);
+ case 5: if(rotacion==1 || rotacion==3)
+ copy_matrix(5,4,1,PLANT,columna);
+ if(rotacion==2 || rotacion==4)
+ copy_matrix(0,1,3,PLANT,columna);
break;
}
}
-void fichas(){
- ficha= rand() % 5+1;
- grados= rand() % 4+1;
- ncolumna= 4;
+void figuras(){
+ figura= rand() % 5+1;
+ rotacion= rand() % 4+1;
+ columna= 4;
}
-void buscar_linea(){
- for(uint8_t i=0;i<9;i++){
- if(mat_act[i]==255){
+void elimina_linea(){
+ for(uint8_t i=0;i<9;i++){
+ if(matrix_act[i]==255){
for(uint8_t a=i;a>=1;a--){
- sendSPI(a,mat_act[a-1]);
+ sendSPI(a,matrix_act[a-1]);
+ matrix_act[a]=matrix_act[a-1];
}
}
}
}
int main() {
- test();
+ bajar.attach(&bajaf, 0.1);
+ init_display();
borrar();
while(1){
- fichas();
- buscar_fil();
- cont=0;
+ figuras();
+ buscar_fila();
+ cont=0;
+ elimina_linea();
while (cont<fila){
- correr_fig();
- dibujar(ficha,grados,ncolumna);
- buscar_fil();
- imp_mat(mat_tmp);
- }
- buscar_linea();
- guardar_mat();
+ movimiento_fig();
+ crear_fig(figura,rotacion,columna);
+ buscar_fila();
+ print_matrix(matrix_temp);
+ }
+ guardar();
}
}
\ No newline at end of file