Yosef Guevara Salamanca
/
TETS_PC
bien spi no se queda pegado
Diff: tetris_pc.cpp
- Revision:
- 10:d4d4b9e9ed92
- Parent:
- 9:0cf4c5f7a1d8
- Child:
- 11:2dc0f84c600a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tetris_pc.cpp Thu Sep 13 07:49:55 2018 +0000 @@ -0,0 +1,191 @@ +#include "mbed.h" +#include "piezas.h" +//#define DEBUG 1 + +Serial pc(USBTX,USBRX); +SPI deviceM(PB_15, PB_14, PB_13); //SE DEFINE EL LED QUE ACTIVARA EL DISPOSITIVO +DigitalOut ssel (PB_12); + +int memoria[8]={0}; + +void debuging(char*s,...){ + + #if DEBUG + pc.printf(s); + #endif + + } + +// FUNCION EL CUAL ENVIA EL DATO JUNTO CON LA DIRECCION DE LA COLUMNA CORRESPONDIENTE AL PRIMER DISPOSITIVO +void sendSPI(uint8_t d1, uint8_t d2) +{ + deviceM.unlock(); + ssel=0; // SELECCIONA EL PRIMER DISPOSOTIVO + deviceM.write(d1); // ESCRIBE EL ADDRESS + deviceM.write(d2); // ESCRIBE EL DATA + ssel=1; + deviceM.lock(); +}; + +// Toma el vecor reorganizado , lo imprime y lo almacena +void captura_matriz(uint16_t* imprimir){ +int i=0; // inicia el contador i en 0 + + while(i <8){ //se encarga de desplazar los bits dentro del vector + int j=1; // inicia el contador j en 1 + + while(j<=8){ //se encarga de seleccionar que posicion del vector imprimira + + sendSPI(j, memoria[j-1]|(imprimir[j-1]>>i)); //Imprime el resultado de aplicar OR a la memoria y al desplamamiento de imprimir + + + if(i==7|| (memoria[j-1]&(imprimir[j-1]>>i+1))!=0){ //Detiene el desplazamiento cuando los bits tocan fondo //AQUI esta el error + + memoria[j-1]= memoria[j-1]|(imprimir[j-1]>>i); //Almacena el nuevo valor de la operacion OR en la memoria + + } + j++; + } + wait(1.0); // espera para tomar el otro valor de lectura + i++; + } + + } + + + void desplazar(uint16_t* datos, char des){ // tomar la figura y la desplaza segun la posicion ingresada inicialmente + + uint16_t desplazamiento[8]={0}; // Inicia un vector auxiliar con solo Ceros + int j= 0; + + for(int i=des; i<(des+3);i++){ // Inicia un vector auxiliar con solo Ceros + desplazamiento[i]=datos[j]; // Alamcena los nuevos datos tomados en el vector + j++; + } + captura_matriz(desplazamiento); //Envia el Vector desplazado la funcion de captura de matriz + } + + void read(char pos, char figura, char giro){ + + switch(figura){ //Este switch escoje la figura con que trabajaar + case 0: // L + switch(giro){ //Este switch rota la figura a trabajar + case 0: + desplazar(PZA_LD,pos); //LLAMA A LA FUNCION DESPLAZAMIENTO Y PONE LA POSICION + case 1: + desplazar(PZA_LDN,pos); + + break; + case 2: + desplazar(PZA_LDO,pos); + + break; + case 3: + desplazar(PZA_LDD, pos); + + break; + } + break; + case 1://T + switch(giro){ + case 0: + desplazar(PZA_T, pos); + break; + case 1: + desplazar(PZA_TN, pos); + break; + case 2: + desplazar(PZA_TO, pos); + break; + case 3: + desplazar(PZA_TD, pos); + break; + } + + break; + case 2://I + switch(giro){ + case 0: case 2: + desplazar(PZA_I, pos); + break; + case 1: case 3: + desplazar(PZA_IR, pos); + break; + } + break; + case 3://Cuadrado + switch(giro){ + case 1: case 2: case 3: case 4: + desplazar(PZA_C,pos); + break; + } + break; + case 5: //Z + switch(giro){ + case 0: + desplazar(PZA_Z,pos); + break; + case 1: + desplazar(PZA_ZN,pos); + break; + case 2: + desplazar(PZA_ZO,pos); + break; + case 3: + desplazar(PZA_ZD, pos); + break; + } + + break; + + } + } + +void captura_datos(){ + + pc.baud(38400); //Inicializa la velocidad de comunicacion + + char pos= 0; + char figura= 0; + char giro= 0; + + debuging("\n Ingrese el posicion. "); + pos=pc.getc(); + debuging("\n Seleccione la Figura. "); + figura=pc.getc(); + debuging("\n Seleccione la rotacion. "); + giro=pc.getc(); + + read(pos,figura,giro); + + } + +void inicializar(){ //INICIALIZA LA MATRIZ + + sendSPI(0x0c,1); + sendSPI(0x0b,7); + sendSPI(0x09,0); //SELECCIONA LA LUMINOSIDAD DE LA MATRIZ + sendSPI(0x0A,0x0f); //SELECCIONA LA LUMINOSIDAD DE LA MATRIZ + int i; + for (i=0;i<2;i++){ + sendSPI(0x0F,1); + wait (0.5); + sendSPI(0x0f,0); + wait (0.5); + } + + for (int j= 1; j<=8;j++){ // limpia la pantalla al encenderce o reiniarcea asi + //no quedan leds encendidos cuando se ejecute el programa nuevamente + + sendSPI(j, 0x00); //pone cada columna y vecto en blanco al inicializar + } + } + + +int main() { + inicializar(); // Inicializa el programa para establecer los modos de trabajo liminosidad + while(1){ + captura_datos(); // Inicia la lectura de la informacion + } + +}