Programa de MIP

Dependencies:   bloques ball mbed WS2812 PixelArray tsi_sensor TSI bloque MMA8451Q

main.cpp

Committer:
anna_dot
Date:
2021-05-22
Revision:
10:dccb2be41fbd
Parent:
9:777b0535d3bb
Child:
11:4908defcd9d9

File content as of revision 10:dccb2be41fbd:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"
#include "TSISensor.h"
#include "MMA8451Q.h"
#include "ball.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;



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);
void setPixelColor(int numPixelMatrix, int r, int g, int b);
void ctr_ball();
void ctr_ball_barra();

int main()
{   int sel_opcio=1;
    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
    
    // 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);


    // 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.5);
    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 == 250){
            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>=70){
            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>=10){
            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) {
    
    //printf ("\r\nRGB=> %ld, %ld, %ld \r\n", valR, valG, valB);
    int numPixelMatrix;
    if(numPixelMatrix_i%2==0){
        numPixelMatrix=((numPixelMatrix_i*16)+15)-numPixelMatrix_j;
    }else{
        numPixelMatrix=(numPixelMatrix_i*16)+numPixelMatrix_j;
    }
    
    if(numPixelMatrixOLD_i!=-1 and numPixelMatrixOLD_j!=-1){
        
        int numPixelMatrixOLD;
        if(numPixelMatrixOLD_i%2==0){
            numPixelMatrixOLD=((numPixelMatrixOLD_i*16)+15)-numPixelMatrixOLD_j;
        }else{
            numPixelMatrixOLD=(numPixelMatrixOLD_i*16)+numPixelMatrixOLD_j;
        }        
        
        setPixelColor(numPixelMatrixOLD, 0, 0, 0);
            
    }
        
    
    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;
    /*while(!found_barra and i<NUM_LEDS_BARRA){
        if(barra[i]==old_j and old_i==1){
            found_barra=true;
            direct_ball=!direct_ball;           
        }
        i++;
    }*/
    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; 
    }
    m_ball.movement(direct_ball);   
    
    print_pixel(255, 255, 255, m_ball.getCoordX(), m_ball.getCoordY(),old_i, old_j);
}

void ctr_ball_barra(){
    int old_i=m_ball.getCoordX();
    int old_j=m_ball.getCoordY();
    
    bool found_barra=false;
    int i=0;
    while(!found_barra and i<NUM_LEDS_BARRA){
        if(barra[i]==old_j and old_i==1){
            found_barra=true;
            direct_ball=!direct_ball;           
        }
        i++;
    }
    m_ball.movement(direct_ball);
}