julian alvarez
/
moggo
Código simplificado, faltan grados, detenerse, columna.
moggo.cpp@1:78d5142da831, 2018-09-10 (annotated)
- Committer:
- jiuk
- Date:
- Mon Sep 10 20:19:56 2018 +0000
- Revision:
- 1:78d5142da831
- Parent:
- 0:3a60cf1c28ca
- Child:
- 2:eac5a192998a
ogg
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:3a60cf1c28ca | 12 | void debug_m(char *s , ... ); //depurador de errores, probador. |
jiuk | 0:3a60cf1c28ca | 13 | |
jiuk | 0:3a60cf1c28ca | 14 | uint8_t mat_act[8]={0,0,0,0,0,0,0,0}; //Matriz para mostrar en pantalla. |
jiuk | 1:78d5142da831 | 15 | uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0}; //Matriz temporal para arrojar las fichas. |
jiuk | 1:78d5142da831 | 16 | uint8_t cont=0; |
jiuk | 1:78d5142da831 | 17 | uint8_t fila=0; |
jiuk | 0:3a60cf1c28ca | 18 | |
jiuk | 0:3a60cf1c28ca | 19 | void sendSPI(uint8_t d1, uint8_t d2) |
jiuk | 0:3a60cf1c28ca | 20 | { |
jiuk | 0:3a60cf1c28ca | 21 | deviceM.unlock(); |
jiuk | 0:3a60cf1c28ca | 22 | ssel=0; |
jiuk | 0:3a60cf1c28ca | 23 | deviceM.write(d1); |
jiuk | 0:3a60cf1c28ca | 24 | deviceM.write(d2); |
jiuk | 0:3a60cf1c28ca | 25 | ssel=1; |
jiuk | 0:3a60cf1c28ca | 26 | deviceM.lock(); |
jiuk | 0:3a60cf1c28ca | 27 | } |
jiuk | 0:3a60cf1c28ca | 28 | |
jiuk | 0:3a60cf1c28ca | 29 | void test() //test |
jiuk | 0:3a60cf1c28ca | 30 | { |
jiuk | 0:3a60cf1c28ca | 31 | sendSPI(0x09,0); //no decodificacion |
jiuk | 1:78d5142da831 | 32 | sendSPI(0x0A,0x0); //intensidad |
jiuk | 0:3a60cf1c28ca | 33 | sendSPI(0x0B,0x07); //usa 7 leds |
jiuk | 0:3a60cf1c28ca | 34 | sendSPI(0x0C,1); //no apaga |
jiuk | 0:3a60cf1c28ca | 35 | sendSPI(0x0F,0); //operacion normal |
jiuk | 0:3a60cf1c28ca | 36 | } |
jiuk | 0:3a60cf1c28ca | 37 | |
jiuk | 1:78d5142da831 | 38 | /*void imp_mat(uint8_t* vC) // recibe matriz actualizada |
jiuk | 0:3a60cf1c28ca | 39 | |
jiuk | 1:78d5142da831 | 40 | { |
jiuk | 1:78d5142da831 | 41 | for (uint8_t i=0;i<8;i++){ //Realiza la impresion de la matriz. |
jiuk | 1:78d5142da831 | 42 | sendSPI(i,*(vC+i-1)); |
jiuk | 1:78d5142da831 | 43 | } |
jiuk | 1:78d5142da831 | 44 | }*/ |
jiuk | 1:78d5142da831 | 45 | |
jiuk | 1:78d5142da831 | 46 | /*void baja(uint8_t* vC) // recibe matriz actualizada |
jiuk | 0:3a60cf1c28ca | 47 | |
jiuk | 0:3a60cf1c28ca | 48 | { |
jiuk | 1:78d5142da831 | 49 | for (uint8_t i=0;i<8;i++){ //Realiza la impresion de la matriz. |
jiuk | 1:78d5142da831 | 50 | sendSPI(i,*(vC+i)); |
jiuk | 1:78d5142da831 | 51 | sendSPI(i+1,*(vC+(i+1))); |
jiuk | 1:78d5142da831 | 52 | sendSPI(i+2,*(vC+(i+2))); |
jiuk | 1:78d5142da831 | 53 | wait_ms(VEL); |
jiuk | 0:3a60cf1c28ca | 54 | } |
jiuk | 1:78d5142da831 | 55 | }*/ |
jiuk | 0:3a60cf1c28ca | 56 | |
jiuk | 1:78d5142da831 | 57 | |
jiuk | 1:78d5142da831 | 58 | /*void act_mat(uint8_t* mascara) //recibe matriz temporal |
jiuk | 0:3a60cf1c28ca | 59 | { |
jiuk | 0:3a60cf1c28ca | 60 | for (int i=0; i<8;i++) //Es la matriz que tenemos como resultado de las diferentes piezas. |
jiuk | 0:3a60cf1c28ca | 61 | mat_act[i]= (*(mascara+i)); |
jiuk | 1:78d5142da831 | 62 | } */ |
jiuk | 0:3a60cf1c28ca | 63 | |
jiuk | 1:78d5142da831 | 64 | void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig) |
jiuk | 0:3a60cf1c28ca | 65 | { |
jiuk | 1:78d5142da831 | 66 | //Realiza una copia de la matriz actual fila por fila. |
jiuk | 1:78d5142da831 | 67 | mat_tmp[0]= *(fig+a); |
jiuk | 1:78d5142da831 | 68 | mat_tmp[1]= *(fig+b); |
jiuk | 1:78d5142da831 | 69 | mat_tmp[2]= *(fig+c); |
jiuk | 0:3a60cf1c28ca | 70 | } |
jiuk | 0:3a60cf1c28ca | 71 | |
jiuk | 1:78d5142da831 | 72 | void borrar() //borrar toda la matriz; |
jiuk | 0:3a60cf1c28ca | 73 | { |
jiuk | 0:3a60cf1c28ca | 74 | int i; |
jiuk | 0:3a60cf1c28ca | 75 | for(i=0;i<=8;i++) |
jiuk | 0:3a60cf1c28ca | 76 | { |
jiuk | 0:3a60cf1c28ca | 77 | sendSPI(0x0+i,0x00); |
jiuk | 0:3a60cf1c28ca | 78 | } |
jiuk | 0:3a60cf1c28ca | 79 | }; |
jiuk | 0:3a60cf1c28ca | 80 | |
jiuk | 1:78d5142da831 | 81 | void buscar_fil(){ |
jiuk | 1:78d5142da831 | 82 | //uint8_t fil; |
jiuk | 1:78d5142da831 | 83 | for (uint8_t i=0;i<8;i++){ //Realiza la impresion de la matriz. |
jiuk | 1:78d5142da831 | 84 | if(mat_act[i]==0) |
jiuk | 1:78d5142da831 | 85 | fila=i; |
jiuk | 1:78d5142da831 | 86 | if(mat_act[i]!=0){ |
jiuk | 1:78d5142da831 | 87 | fila=i-2; |
jiuk | 1:78d5142da831 | 88 | i=9; |
jiuk | 1:78d5142da831 | 89 | } |
jiuk | 0:3a60cf1c28ca | 90 | } |
jiuk | 1:78d5142da831 | 91 | }; |
jiuk | 1:78d5142da831 | 92 | |
jiuk | 1:78d5142da831 | 93 | void guardar_mat(){ |
jiuk | 1:78d5142da831 | 94 | //borrar(); |
jiuk | 1:78d5142da831 | 95 | wait_ms(VEL); |
jiuk | 1:78d5142da831 | 96 | mat_act[fila-1]=mat_tmp[0]; |
jiuk | 1:78d5142da831 | 97 | mat_act[fila]=mat_tmp[1]; |
jiuk | 1:78d5142da831 | 98 | mat_act[fila+1]=mat_tmp[2]; |
jiuk | 1:78d5142da831 | 99 | sendSPI(fila-1,mat_act[fila-1]); |
jiuk | 1:78d5142da831 | 100 | sendSPI(fila,mat_act[fila]); |
jiuk | 1:78d5142da831 | 101 | sendSPI(fila+1,mat_act[fila+1]); |
jiuk | 1:78d5142da831 | 102 | }; |
jiuk | 1:78d5142da831 | 103 | |
jiuk | 1:78d5142da831 | 104 | void imp_mat(uint8_t *temp){ |
jiuk | 1:78d5142da831 | 105 | uint8_t i=0; |
jiuk | 1:78d5142da831 | 106 | buscar_fil(); |
jiuk | 1:78d5142da831 | 107 | for (i=0;i<fila;i++){ //Realiza la impresion de la matriz. |
jiuk | 1:78d5142da831 | 108 | sendSPI(i-1,0); |
jiuk | 1:78d5142da831 | 109 | sendSPI(i,*(temp+0)); |
jiuk | 1:78d5142da831 | 110 | sendSPI(i+1,*(temp+1)); |
jiuk | 1:78d5142da831 | 111 | sendSPI(i+2,*(temp+2)); |
jiuk | 1:78d5142da831 | 112 | wait_ms(VEL); |
jiuk | 1:78d5142da831 | 113 | } |
jiuk | 1:78d5142da831 | 114 | // if(cont==0){ |
jiuk | 1:78d5142da831 | 115 | // guardar_mat(i,mat_tmp); |
jiuk | 1:78d5142da831 | 116 | // cont=1; |
jiuk | 1:78d5142da831 | 117 | // } |
jiuk | 0:3a60cf1c28ca | 118 | } |
jiuk | 0:3a60cf1c28ca | 119 | |
jiuk | 1:78d5142da831 | 120 | |
jiuk | 1:78d5142da831 | 121 | /*void desplazar_fila(){ //Realiza el desplazamiento para copiar el vector |
jiuk | 1:78d5142da831 | 122 | for (int i=0;i<11;i++) |
jiuk | 1:78d5142da831 | 123 | mat_tmp[i]= mat_tmp[i]>>1; //Podemos indicar en que columna caer |
jiuk | 1:78d5142da831 | 124 | |
jiuk | 1:78d5142da831 | 125 | } */ |
jiuk | 1:78d5142da831 | 126 | |
jiuk | 1:78d5142da831 | 127 | void dibujar(char type_fig,char grados,char columna) |
jiuk | 1:78d5142da831 | 128 | { |
jiuk | 1:78d5142da831 | 129 | //borrar(); |
jiuk | 1:78d5142da831 | 130 | switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz. |
jiuk | 1:78d5142da831 | 131 | case 1: cop_mat(5,1,1,FIG_C); break; //1: cuadro; |
jiuk | 1:78d5142da831 | 132 | case 2: cop_mat(0,0,0,FIG_C); break; //2: I; |
jiuk | 1:78d5142da831 | 133 | case 3: cop_mat(0,0,1,FIG_C); break; //3: L; |
jiuk | 1:78d5142da831 | 134 | case 4: cop_mat(5,2,3,FIG_C); break; //4: T; |
jiuk | 1:78d5142da831 | 135 | case 5: cop_mat(5,4,1,FIG_C); break; //5: S; |
jiuk | 1:78d5142da831 | 136 | } |
jiuk | 1:78d5142da831 | 137 | } |
jiuk | 1:78d5142da831 | 138 | |
jiuk | 1:78d5142da831 | 139 | /*void dibujar(char type_fig,char grados,char columna) |
jiuk | 1:78d5142da831 | 140 | { |
jiuk | 1:78d5142da831 | 141 | //borrar(); |
jiuk | 1:78d5142da831 | 142 | switch (type_fig){ |
jiuk | 1:78d5142da831 | 143 | case C_TYPE: cop_mat(1,1,0); break; //Se envia el vector que contiene la figura a la funcion copiar matriz. |
jiuk | 1:78d5142da831 | 144 | case I_TYPE: cop_mat(3,1,2); break; |
jiuk | 1:78d5142da831 | 145 | case L_TYPE: cop_mat(1,1,2); break; |
jiuk | 1:78d5142da831 | 146 | case T_TYPE: cop_mat(1,1,2); break; |
jiuk | 1:78d5142da831 | 147 | case S_TYPE: cop_mat(1,1,2); break; |
jiuk | 1:78d5142da831 | 148 | } |
jiuk | 1:78d5142da831 | 149 | }*/ |
jiuk | 1:78d5142da831 | 150 | |
jiuk | 0:3a60cf1c28ca | 151 | int main() { |
jiuk | 1:78d5142da831 | 152 | char tipo_fig=1,ncolumna=1,grados=1,lee1=0,lee2=0; // enviar ficha y columna |
jiuk | 0:3a60cf1c28ca | 153 | test(); |
jiuk | 1:78d5142da831 | 154 | borrar(); |
jiuk | 0:3a60cf1c28ca | 155 | while(1){ |
jiuk | 1:78d5142da831 | 156 | dibujar(1,1,1); |
jiuk | 1:78d5142da831 | 157 | lee1=command.getc(); //recive '<' para iniciar la linea de comandos. |
jiuk | 0:3a60cf1c28ca | 158 | tipo_fig=command.getc(); |
jiuk | 0:3a60cf1c28ca | 159 | grados=command.getc(); |
jiuk | 0:3a60cf1c28ca | 160 | ncolumna=command.getc(); |
jiuk | 1:78d5142da831 | 161 | lee2=command.getc(); //recive '>' para terminar la linea de comandos. |
jiuk | 1:78d5142da831 | 162 | if(lee1==0x3c && lee2==0x3e) //solo imprime una figura si viene bajo el parametro '<(t_fig) (Grados) (Col)>'. |
jiuk | 0:3a60cf1c28ca | 163 | { |
jiuk | 0:3a60cf1c28ca | 164 | dibujar(tipo_fig,grados,ncolumna); |
jiuk | 1:78d5142da831 | 165 | //desplazar_fila(); |
jiuk | 1:78d5142da831 | 166 | //act_mat(mat_tmp); |
jiuk | 1:78d5142da831 | 167 | imp_mat(mat_tmp); |
jiuk | 1:78d5142da831 | 168 | guardar_mat(); |
jiuk | 1:78d5142da831 | 169 | //baja(mat_act); |
jiuk | 0:3a60cf1c28ca | 170 | } |
jiuk | 0:3a60cf1c28ca | 171 | } |
jiuk | 0:3a60cf1c28ca | 172 | } |
jiuk | 0:3a60cf1c28ca | 173 | |
jiuk | 0:3a60cf1c28ca | 174 | |
jiuk | 0:3a60cf1c28ca | 175 | |
jiuk | 0:3a60cf1c28ca | 176 | void debug_m(char *s , ... ){ |
jiuk | 0:3a60cf1c28ca | 177 | #if DEBUG |
jiuk | 0:3a60cf1c28ca | 178 | command.printf(s); //busca errores para depurar en el sistema datos guardados. |
jiuk | 0:3a60cf1c28ca | 179 | #endif |
jiuk | 0:3a60cf1c28ca | 180 | } |