Código simplificado, faltan grados, detenerse, columna.

Dependencies:   mbed mbed

Committer:
jiuk
Date:
Wed Sep 12 18:10:17 2018 +0000
Revision:
3:990a096b8120
Parent:
2:eac5a192998a
revision 12-09-18

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiuk 0:3a60cf1c28ca 1 #include "mbed.h"
jiuk 0:3a60cf1c28ca 2 #include "moggo.h" //agrega la libreria que contiene las figuras.
jiuk 0:3a60cf1c28ca 3
jiuk 1:78d5142da831 4 SPI deviceM(PB_15, PB_14, PB_13);
jiuk 1:78d5142da831 5 DigitalOut ssel (PB_12);
jiuk 1:78d5142da831 6 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
jiuk 1:78d5142da831 7 //DigitalOut ssel (PB_9); //Chip Select para el controlador.
jiuk 0:3a60cf1c28ca 8 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:3a60cf1c28ca 9
jiuk 1:78d5142da831 10 #define VEL 250 //Velocidad de actualizacion de dato en el controlador.
jiuk 0:3a60cf1c28ca 11
jiuk 2:eac5a192998a 12 uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0}; //Matriz para mostrar en pantalla.
jiuk 2:eac5a192998a 13 uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0}; //Matriz temporal para arrojar las fichas.
jiuk 1:78d5142da831 14 uint8_t fila=0;
jiuk 0:3a60cf1c28ca 15
jiuk 0:3a60cf1c28ca 16 void sendSPI(uint8_t d1, uint8_t d2)
jiuk 0:3a60cf1c28ca 17 {
jiuk 0:3a60cf1c28ca 18 deviceM.unlock();
jiuk 0:3a60cf1c28ca 19 ssel=0;
jiuk 2:eac5a192998a 20 deviceM.write(d1);
jiuk 0:3a60cf1c28ca 21 deviceM.write(d2);
jiuk 0:3a60cf1c28ca 22 ssel=1;
jiuk 0:3a60cf1c28ca 23 deviceM.lock();
jiuk 0:3a60cf1c28ca 24 }
jiuk 0:3a60cf1c28ca 25
jiuk 0:3a60cf1c28ca 26 void test() //test
jiuk 0:3a60cf1c28ca 27 {
jiuk 0:3a60cf1c28ca 28 sendSPI(0x09,0); //no decodificacion
jiuk 1:78d5142da831 29 sendSPI(0x0A,0x0); //intensidad
jiuk 0:3a60cf1c28ca 30 sendSPI(0x0B,0x07); //usa 7 leds
jiuk 0:3a60cf1c28ca 31 sendSPI(0x0C,1); //no apaga
jiuk 0:3a60cf1c28ca 32 sendSPI(0x0F,0); //operacion normal
jiuk 0:3a60cf1c28ca 33 }
jiuk 0:3a60cf1c28ca 34
jiuk 2:eac5a192998a 35 void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
jiuk 2:eac5a192998a 36 {
jiuk 2:eac5a192998a 37 mat_tmp[0]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila.
jiuk 2:eac5a192998a 38 mat_tmp[1]= *(fig+b)>>columna-1;
jiuk 2:eac5a192998a 39 mat_tmp[2]= *(fig+c)>>columna-1;
jiuk 0:3a60cf1c28ca 40 }
jiuk 0:3a60cf1c28ca 41
jiuk 1:78d5142da831 42 void borrar() //borrar toda la matriz;
jiuk 0:3a60cf1c28ca 43 {
jiuk 0:3a60cf1c28ca 44 int i;
jiuk 0:3a60cf1c28ca 45 for(i=0;i<=8;i++)
jiuk 0:3a60cf1c28ca 46 {
jiuk 0:3a60cf1c28ca 47 sendSPI(0x0+i,0x00);
jiuk 0:3a60cf1c28ca 48 }
jiuk 0:3a60cf1c28ca 49 };
jiuk 0:3a60cf1c28ca 50
jiuk 1:78d5142da831 51 void buscar_fil(){
jiuk 1:78d5142da831 52 for (uint8_t i=0;i<8;i++){ //Realiza la impresion de la matriz.
jiuk 1:78d5142da831 53 if(mat_act[i]==0)
jiuk 1:78d5142da831 54 fila=i;
jiuk 1:78d5142da831 55 if(mat_act[i]!=0){
jiuk 1:78d5142da831 56 fila=i-2;
jiuk 2:eac5a192998a 57 i=8;
jiuk 1:78d5142da831 58 }
jiuk 0:3a60cf1c28ca 59 }
jiuk 2:eac5a192998a 60 command.printf("\n buscar fila \n %d",fila);
jiuk 2:eac5a192998a 61 command.printf("\n ******************************* \n ");
jiuk 2:eac5a192998a 62 };
jiuk 2:eac5a192998a 63
jiuk 2:eac5a192998a 64 void buscar_col(){
jiuk 2:eac5a192998a 65 uint8_t y;
jiuk 2:eac5a192998a 66 y= mat_act[fila] & mat_tmp[2];
jiuk 2:eac5a192998a 67 if(y != 0){
jiuk 2:eac5a192998a 68 fila=0;
jiuk 2:eac5a192998a 69 }
jiuk 2:eac5a192998a 70 else{
jiuk 2:eac5a192998a 71 fila++;
jiuk 2:eac5a192998a 72 }
jiuk 2:eac5a192998a 73 command.printf("\n fila cambiada en columna: y=%d",y);
jiuk 1:78d5142da831 74 };
jiuk 1:78d5142da831 75
jiuk 1:78d5142da831 76 void guardar_mat(){
jiuk 1:78d5142da831 77 //borrar();
jiuk 1:78d5142da831 78 wait_ms(VEL);
jiuk 1:78d5142da831 79 mat_act[fila-1]=mat_tmp[0];
jiuk 1:78d5142da831 80 mat_act[fila]=mat_tmp[1];
jiuk 1:78d5142da831 81 mat_act[fila+1]=mat_tmp[2];
jiuk 1:78d5142da831 82 };
jiuk 1:78d5142da831 83
jiuk 2:eac5a192998a 84
jiuk 1:78d5142da831 85 void imp_mat(uint8_t *temp){
jiuk 1:78d5142da831 86 uint8_t i=0;
jiuk 1:78d5142da831 87 buscar_fil();
jiuk 3:990a096b8120 88 for (i=0;i<fila;i++){
jiuk 3:990a096b8120 89
jiuk 1:78d5142da831 90 sendSPI(i-1,0);
jiuk 1:78d5142da831 91 sendSPI(i,*(temp+0));
jiuk 1:78d5142da831 92 sendSPI(i+1,*(temp+1));
jiuk 1:78d5142da831 93 sendSPI(i+2,*(temp+2));
jiuk 1:78d5142da831 94 wait_ms(VEL);
jiuk 1:78d5142da831 95 }
jiuk 0:3a60cf1c28ca 96 }
jiuk 0:3a60cf1c28ca 97
jiuk 1:78d5142da831 98 void dibujar(char type_fig,char grados,char columna)
jiuk 1:78d5142da831 99 {
jiuk 1:78d5142da831 100 //borrar();
jiuk 1:78d5142da831 101 switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz.
jiuk 2:eac5a192998a 102 case 1: cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro;
jiuk 2:eac5a192998a 103
jiuk 2:eac5a192998a 104 case 2: if(grados==1 || grados==3)
jiuk 2:eac5a192998a 105 cop_mat(0,0,0,FIG_ALL,columna);
jiuk 2:eac5a192998a 106 if(grados==2 || grados==4)
jiuk 2:eac5a192998a 107 cop_mat(5,5,2,FIG_ALL,columna); //2: I;
jiuk 2:eac5a192998a 108 break;
jiuk 2:eac5a192998a 109
jiuk 2:eac5a192998a 110 case 3: if(grados==1)
jiuk 2:eac5a192998a 111 cop_mat(0,0,1,FIG_ALL,columna); //3: L;
jiuk 2:eac5a192998a 112 if(grados==2)
jiuk 2:eac5a192998a 113 cop_mat(5,2,0,FIG_ALL,columna);
jiuk 2:eac5a192998a 114 if(grados==3)
jiuk 2:eac5a192998a 115 cop_mat(1,0,0,FIG_ALL,columna);
jiuk 2:eac5a192998a 116 if(grados==4)
jiuk 2:eac5a192998a 117 cop_mat(5,0,2,FIG_ALL,columna);
jiuk 2:eac5a192998a 118 break; //2: I;
jiuk 2:eac5a192998a 119
jiuk 2:eac5a192998a 120 case 4: if(grados==1)
jiuk 2:eac5a192998a 121 cop_mat(5,2,0,FIG_ALL,columna); //4: T;
jiuk 2:eac5a192998a 122 if(grados==2)
jiuk 2:eac5a192998a 123 cop_mat(3,1,3,FIG_ALL,columna);
jiuk 2:eac5a192998a 124 if(grados==3)
jiuk 2:eac5a192998a 125 cop_mat(5,3,2,FIG_ALL,columna);
jiuk 2:eac5a192998a 126 if(grados==4)
jiuk 2:eac5a192998a 127 cop_mat(0,1,0,FIG_ALL,columna);
jiuk 2:eac5a192998a 128 break;
jiuk 2:eac5a192998a 129
jiuk 2:eac5a192998a 130 case 5: if(grados==1 || grados==3)
jiuk 2:eac5a192998a 131 cop_mat(5,4,1,FIG_ALL,columna); //5: S;
jiuk 2:eac5a192998a 132 if(grados==2 || grados==4)
jiuk 2:eac5a192998a 133 cop_mat(0,1,3,FIG_ALL,columna);
jiuk 2:eac5a192998a 134 break;
jiuk 1:78d5142da831 135 }
jiuk 1:78d5142da831 136 }
jiuk 1:78d5142da831 137
jiuk 0:3a60cf1c28ca 138 int main() {
jiuk 1:78d5142da831 139 char tipo_fig=1,ncolumna=1,grados=1,lee1=0,lee2=0; // enviar ficha y columna
jiuk 0:3a60cf1c28ca 140 test();
jiuk 1:78d5142da831 141 borrar();
jiuk 0:3a60cf1c28ca 142 while(1){
jiuk 1:78d5142da831 143 lee1=command.getc(); //recive '<' para iniciar la linea de comandos.
jiuk 0:3a60cf1c28ca 144 tipo_fig=command.getc();
jiuk 0:3a60cf1c28ca 145 grados=command.getc();
jiuk 0:3a60cf1c28ca 146 ncolumna=command.getc();
jiuk 1:78d5142da831 147 lee2=command.getc(); //recive '>' para terminar la linea de comandos.
jiuk 1:78d5142da831 148 if(lee1==0x3c && lee2==0x3e) //solo imprime una figura si viene bajo el parametro '<(t_fig) (Grados) (Col)>'.
jiuk 0:3a60cf1c28ca 149 {
jiuk 0:3a60cf1c28ca 150 dibujar(tipo_fig,grados,ncolumna);
jiuk 2:eac5a192998a 151 imp_mat(mat_tmp);
jiuk 2:eac5a192998a 152 guardar_mat();
jiuk 0:3a60cf1c28ca 153 }
jiuk 0:3a60cf1c28ca 154 }
jiuk 2:eac5a192998a 155 }