Programa de MIP

Dependencies:   bloques ball mbed WS2812 PixelArray tsi_sensor TSI bloque MMA8451Q

main.cpp

Committer:
anna_dot
Date:
2021-05-24
Revision:
15:706dd4761fbe
Parent:
14:4adfd8e600fe
Child:
16:eab9a8d27969

File content as of revision 15:706dd4761fbe:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"
#include "TSISensor.h"
#include "MMA8451Q.h"
#include "ball.h"
#include "bloques.h"

#define WS2812_BUF 256
#define NUM_COLORS 6
#define NUM_LEDS_PER_COLOR 10
#define NUM_LEDS_BARRA 4
#define ACCEL_ADRESS_I2C (0X1D<<1)
#define DIMENSION_TABLERO 16
 
PixelArray px(WS2812_BUF);
 
// See the program page for information on the timing numbers
// The given numbers are for the K64F
WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
MMA8451Q acc(PTE25, PTE24, ACCEL_ADRESS_I2C );

///////
Serial pc(USBTX, USBRX);
//////

TSISensor tsi;
int barra[NUM_LEDS_BARRA]={6,7,8,9};
float tsi_oldRead=0;
int counter_tsi=0;
int posAcelerometerTemp, posAcelerometer=0, posAcelerometerOLD=0, posAcelerometerOLD1=0, contadorReboteA=0;
int posSliderTouchTemp, posSliderTouch=0, posSliderTouchOLD=0, posSliderTouchOLD1=0, contadorRebote=0;
int matrix_tablero[DIMENSION_TABLERO][DIMENSION_TABLERO]={};
ball m_ball;
bool direct_ball=0;
Ticker refresh_ball;
bloques Bloques;

int angle=1;
bool lose=false;

void update_barra(int led_apagar, int led_encender, int act_barra );
void tsi_ctr_modeBtn(float tsi_newRead);
void tsi_ctr_modeSlider(float tsi_newRead);
void accel_ctr();
void print_pixel(uint8_t valR, uint8_t valG, uint8_t valB, int numPixelMatrix_i, int numPixelMatrix_j, int numPixelMatrixOLD_i, int numPixelMatrixOLD_j, uint8_t size=1);
void setPixelColor(int numPixelMatrix, int r, int g, int b);
void ctr_ball();
void clear_matrix();
void angle_rebote();

int main()
{   
    int sel_opcio=0;
    pc.baud(115200);
    pc.printf("\r\n\r\nHola!\r\ncompilado el " __DATE__ ", " __TIME__ "\r\n");
    
    ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    for(int i=0; i<Bloques.getNumBloques(); i++){
        bloque temp_blq=Bloques.getBloque(i);
        print_pixel(temp_blq.getColR(),temp_blq.getColG(),temp_blq.getColB(), temp_blq.getCoordX(),temp_blq.getCoordY(), -1, -1, temp_blq.getSize());
    }
    // set up the colours we want to draw with
    //int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};

    // for each of the colours (j) write out 10 of them
    // the pixels are written at the colour*10, plus the colour position
    // all modulus 60 so it wraps around
    /*for (int i = 0; i < WS2812_BUF; i++) {
        px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
    }*/
    for(int i=0; i<NUM_LEDS_BARRA; i++){
        px.Set(barra[i], 0x2f0000);
    }

    for(int i=0; i<NUM_LEDS_BARRA; i++){
        px.SetI(barra[i]%WS2812_BUF, 255); //0xff
    }

 
    /*for(int i=0; i<NUM_LEDS_BARRA; i++){
        matrix_tablero[0][barra[i]]=1;
    }*/
    
    print_pixel(255, 255, 255, m_ball.getCoordX(), m_ball.getCoordY(),-1,-1);
    //print_pixel(255, 255, 255, 0, 0,-1,-1);

    // Now the buffer is written, rotate it
    // by writing it out with an increasing offset
    ws.write(px.getBuf());
    
    refresh_ball.attach(&ctr_ball, 0.4);
    while (1) {
        /*for (int z=WS2812_BUF; z >= 0 ; z--) {
            ws.write_offsets(px.getBuf(),z,z,z);
            wait(0.075);
        }*/
        if(sel_opcio==0){
            tsi_ctr_modeBtn(tsi.readPercentage());
        }
        else{
            if(sel_opcio==1){
                tsi_ctr_modeSlider(tsi.readPercentage());
            }
            else if(sel_opcio==2){
                accel_ctr();
            }
        }   
        //pc.printf("\r\n Touch %d", (rand()%(9-6 + 1) + 6));
        
    }
    
}

void tsi_ctr_modeBtn(float tsi_newRead){
    
    if(tsi_newRead!=0.0){
        if(tsi_newRead>0.6 and tsi_oldRead>0.6){
            counter_tsi++;
        }
        else {
            if(tsi_newRead<0.4 and tsi_oldRead<0.4){
                counter_tsi++;
            }
            else{
                counter_tsi=0;
            }
        }
        tsi_oldRead=tsi_newRead;
    

        if(counter_tsi == 10450){
            if( (tsi_newRead < 0.4) && tsi_newRead!=0 ){ //if left
                if(barra[0]>0){
                    update_barra((NUM_LEDS_BARRA-1),0, -1 );
                }
            }
            else{
                if(tsi_newRead > 0.6){ //if right
                    if(barra[NUM_LEDS_BARRA-1]<15){
                        update_barra(0,(NUM_LEDS_BARRA-1), 1 );
                    }
                }
            }
            //control_tsi(tsi_newRead);
            ws.write(px.getBuf());
            counter_tsi=0;
        }
    }
    else{
        counter_tsi=0;
    }
    

    //tsi_oldRead=tsi_newRead;
}

void tsi_ctr_modeSlider(float tsi_newRead){
    //Multiplicamos lo que lee el tsi.readPercentage() por 100 para tener un porcentaje del 0 al 100% en lugar del 0 al 1 que devuelve dicha función tsi.readPercentage().
    //Lo multiplicamos por 1.4 para obtener un ranto de entre 0 y 140.
    //Le restamos 70 para que el 50% leido por el tsi represente el 0, de esta forma ahora el rango es de -70 a +70.
    //Dividimos entre 10 para obtener unos valores entre -7 y +7 que son las posiciones que puede tomar la barra inferior si se trata de una barra de 4 pixeles como es el caso.
    posSliderTouchTemp=(((tsi_newRead*100)*1.4)-70)/10;
     
    //Controlamos que no se salga de las posiciones posibles de la barra inferior.
    if(posSliderTouchTemp>-7 and posSliderTouchTemp<7){
        //pc.printf("\r\n Porcentaje=%d",posSliderTouchTemp);
        
        //Se controla los rebotes del TSI ya que sino da saltos, por eso se incrementa un contador cuando detecta una misma posicion mas de una vez seguida.
        if(posSliderTouchTemp==posSliderTouchOLD1){
            contadorRebote=contadorRebote+1;
        }else{
            contadorRebote=0;
        }
        posSliderTouchOLD1=posSliderTouchTemp;
        
        
        if(contadorRebote>=140){
            posSliderTouch=posSliderTouchTemp;
        }
        
         
    }
    
    if(posSliderTouch!=posSliderTouchOLD){
        //Se suaviza el movimiento de la barra inferior para que de un efecto de scroll mas bonito y no tan brusco, 
        //por eso se realiza un for para que pase por todas las posiciones hasta la posicoon de la barra objetivo.
        if(posSliderTouch>posSliderTouchOLD){
            for(int p=posSliderTouchOLD; p<posSliderTouch; p++){
                update_barra(0,(NUM_LEDS_BARRA-1), 1 ); //if rigth
                ws.write(px.getBuf());
                //printBar( 249, 0, 0, p);
            }
        }else if(posSliderTouch<posSliderTouchOLD){
            for(int p=posSliderTouchOLD; p>posSliderTouch; p--){
                update_barra((NUM_LEDS_BARRA-1), 0, -1 ); //if left
                ws.write(px.getBuf());
                //printBar( 249, 0, 0, p);
            }
        }
        posSliderTouchOLD=posSliderTouch;
        //pc.printf("\r\n Touch");
        
    }
    
}


void update_barra(int led_apagar, int led_encender, int act_barra ){
    if(m_ball.getCoordX()!=0){
        px.Set(barra[led_apagar], 0x0);  //apagamos el led
                    
        for(int i=0; i<NUM_LEDS_BARRA; i++){ //actualizamos valor del array
            barra[i]=barra[i]+act_barra;
            //pc.printf("\r\n UPDATEEE ------- =%d", barra[i]);
        }
                
        px.Set(barra[led_encender], 0x2f0000); //encendemos el siguiente led
        px.SetI(barra[led_encender]%WS2812_BUF, 255); //0xff
        //ctr_ball_barra();
    }
}


void accel_ctr(){
    float nearest = floor(abs(((acc.getAccY()+1)*100)/2)); //para obtener un valor 0 y 100 
    posAcelerometerTemp=(((nearest)*2.8)-140)/10;
    //pc.printf("\r\n Porcentaje=%d",posAcelerometerTemp);
    if(posAcelerometerTemp<=-7){
        posAcelerometerTemp=-6;
    }else if(posAcelerometerTemp>=7){
        posAcelerometerTemp=6;
    }
    if(posAcelerometerTemp>-7 and posAcelerometerTemp<7){
        //pc.printf("\r\n Porcentaje=%d",posAcelerometerTemp);
        //pc.printf("\r\n Ha entrat");
        if(posAcelerometerTemp==posAcelerometerOLD1){
            contadorReboteA=contadorReboteA+1;
        }else{
            contadorReboteA=0;
        }
        posAcelerometerOLD1=posAcelerometerTemp;
        if(contadorReboteA>=40){
            posAcelerometer=posAcelerometerTemp;
        }
             
    }
    if(posAcelerometer!=posAcelerometerOLD){
        if(posAcelerometer>posAcelerometerOLD){
            for(int p=posAcelerometerOLD; p<posAcelerometer; p++){ //movemos la barrita hasta la posición actual, mostrando la animación
                update_barra(0,(NUM_LEDS_BARRA-1), 1 ); //if rigth
                ws.write(px.getBuf());
                //pc.printf("\r\n HOLAAAA DRETA=%d",p);
                
            }
        }else if(posAcelerometer<posAcelerometerOLD){
            //pc.printf("\r\n HOLAAAA ESQUERRA");
            for(int p=posAcelerometerOLD; p>posAcelerometer; p--){
                update_barra((NUM_LEDS_BARRA-1), 0, -1 ); //if left
                ws.write(px.getBuf());
                //pc.printf("\r\n HOLAAAA ESQUERRA=%d",p);
                
            }
         
        }
            
       posAcelerometerOLD=posAcelerometer;
        
        //pc.printf("\r\n Acelerometer");
    }
}

void print_pixel(uint8_t valR, uint8_t valG, uint8_t valB, int numPixelMatrix_i, int numPixelMatrix_j, int numPixelMatrixOLD_i, int numPixelMatrixOLD_j, uint8_t size) {
    
    //printf ("\r\nRGB=> %ld, %ld, %ld \r\n", valR, valG, valB);
    
    if(numPixelMatrixOLD_i!=-1 and numPixelMatrixOLD_j!=-1){
        for(int j=numPixelMatrixOLD_j; j<(numPixelMatrixOLD_j+size); j++){
            uint8_t numPixelMatrixOLD;
            if(numPixelMatrixOLD_i%2==0){
                numPixelMatrixOLD=(numPixelMatrixOLD_i*16)+j;
            }else{
                numPixelMatrixOLD=((numPixelMatrixOLD_i*16)+15)-j;
            }        
            
            setPixelColor(numPixelMatrixOLD, 0, 0, 0);
                
        }
    }
    if(numPixelMatrix_i!=-1 or numPixelMatrix_j!=-1){
        for(int j=numPixelMatrix_j; j<(numPixelMatrix_j+size); j++){
            uint8_t numPixelMatrix;
            if(numPixelMatrix_i%2==0){
                numPixelMatrix=(numPixelMatrix_i*16)+j;
            }else{
                numPixelMatrix=((numPixelMatrix_i*16)+15)-j;
            }            
            
            setPixelColor(numPixelMatrix, valR, valG, valB);   
        } 
    }
    ws.write(px.getBuf());
    
}

void setPixelColor(int numPixelMatrix, int r, int g, int b){
    px.SetI(numPixelMatrix,255);
    px.SetR(numPixelMatrix,r);
    px.SetG(numPixelMatrix,g);
    px.SetB(numPixelMatrix,b);
}

void ctr_ball(){
    int old_i=m_ball.getCoordX();
    int old_j=m_ball.getCoordY();
    
    bool found_barra=false;
    int i=0;
    if(old_i==15){
        
        direct_ball=!direct_ball;
        angle_rebote();
        
    }
    else{
        if( (old_j==15 or old_j==0) and (angle!=1) and old_i!=1){
            pc.printf("Angle ANTES REBOTE --> %d \r\n", angle);
            angle_rebote();
            pc.printf("Angle DESPUES REBOTE --> %d \r\n", angle);
            
        }else{
            int predict_Y=m_ball.predict_Y_axis_barra(direct_ball, angle);
            
            while(!found_barra and i<NUM_LEDS_BARRA){
                if(barra[i]==predict_Y and old_i==1){
                    found_barra=true;
                    direct_ball=!direct_ball;           
                }
                i++;
            }
            if(found_barra){
                if(barra[0]==predict_Y){
                    angle=0;
                }else{
                    if(barra[3]==predict_Y){
                        angle=2;
                    }else{
                        angle=1;
                    }
                }
            }else if(old_i==0){
                  refresh_ball.detach();  
                  clear_matrix();
                  lose=true;
            }
            
        }
    }
        
    /*if((barra[0]==old_j or barra[1]==old_j or barra[2]==old_j or barra[3]==old_j) and old_i==1){
        direct_ball=!direct_ball; 
    }*/
    
    if(!lose){
        bool found_blq_i=false;
        int i=0;
        
        while(!found_blq_i and i<Bloques.getNumBloques() ){
            bloque temp_blq=Bloques.getBloque(i);
            if((temp_blq.getCoordX()==(m_ball.getCoordX()+1))){
                found_blq_i=true;
            }
            i++;
        }
        
        bool found_blq_j=false;
        int j=0;
        if(found_blq_i){
            int predict_Y=m_ball.predict_Y_axis(direct_ball, angle);
            pc.printf("\r\n Coord X %d, Coord Y %d y predictY %d --> \n",m_ball.getCoordX(), m_ball.getCoordY() , predict_Y );
            while(!found_blq_j and j<Bloques.getNumBloques() ){
                bloque temp_blq=Bloques.getBloque(j);
                int blq_pos_Y=(temp_blq.getCoordY()+temp_blq.getSize());
                if((blq_pos_Y>predict_Y and predict_Y>=temp_blq.getCoordY()) and temp_blq.getEnabled()){
                    found_blq_j=true;
                    direct_ball=!direct_ball;
                    angle_rebote();
                    print_pixel(temp_blq.getColR(), temp_blq.getColG(), temp_blq.getColB(), -1, -1, temp_blq.getCoordX(), temp_blq.getCoordY(), temp_blq.getSize());
                    Bloques.disable_blq(j);
                    //pc.printf("BloqY %d, temp_bloq.CoordY %d y predictY %d --> %d", blq_pos_Y, temp_blq.getCoordY() , predict_Y );
                }
                j++;
            }
        }
        m_ball.movement(direct_ball, angle);   
        print_pixel(255, 255, 255, m_ball.getCoordX(), m_ball.getCoordY(),old_i, old_j);
    }
}

void clear_matrix(){
    for(int i=0; i<256; i++){
        setPixelColor(i,0,0,0);
    }
    ws.write(px.getBuf());
}

void angle_rebote(){
    if(angle==0){
        angle=2;
    }else if(angle==2){
        angle=0;
    }
}