Programa de MIP

Dependencies:   bloques ball mbed WS2812 PixelArray tsi_sensor TSI bloque MMA8451Q

main.cpp

Committer:
aarmdlr
Date:
2021-05-21
Revision:
5:a0c1625fae57
Parent:
4:3f515bbfd20c
Child:
6:0004357c1faa

File content as of revision 5:a0c1625fae57:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"
#include "TSISensor.h"
#include "MMA8451Q.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)
 
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 pixel=8;

//void control_tsi(float tsi_newRead);
//void control_accel();
void update_barra(int led_apagar, int led_encender, int act_barra );
void tsi_ctr_modeBtn(float tsi_newRead);
void accel_ctr();


int main()
{
    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, 0xff);
    }
    
 
    
    
    // now all the colours are computed, add a fade effect using intensity scaling
    // compute and write the II value for each pixel
    /*for (int j=0; j<WS2812_BUF; j++) {
        // px.SetI(pixel position, II value)
        px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
    }
 
    */
    // Now the buffer is written, rotate it
    // by writing it out with an increasing offset
    ws.write(px.getBuf());
    while (1) {
        /*for (int z=WS2812_BUF; z >= 0 ; z--) {
            ws.write_offsets(px.getBuf(),z,z,z);
            wait(0.075);
        }*/
         
        //tsi_ctr_modeBtn(tsi.readPercentage());
        
        accel_ctr();
        
    }
 
}

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 control_tsi(float tsi_newRead){
    float percent= tsi_newRead; //tsi.readPercentage();
    if( (percent < 0.4) && percent!=0 ){ //if left
        if(barra[0]>0){
            px.Set(barra[NUM_LEDS_BARRA-1], 0x0);  //apagamos el último led
                //px.SetI(barra[NUM_LEDS_BARRA-1]%WS2812_BUF, 0x0);
            
            for(int i=0; i<NUM_LEDS_BARRA; i++){ //actualizamos valor del array
                barra[i]=barra[i]-1;
            }
            
            px.Set(barra[0], 0x2f0000); //encendemos el siguiente led a la izquierda
            px.SetI(barra[0]%WS2812_BUF, 0xff);
            
            //update_barra((NUM_LEDS_BARRA-1),0, -1 );
        }
    }
    else{
        if(percent > 0.6){ //if right
            if(barra[NUM_LEDS_BARRA-1]<15){
                px.Set(barra[0], 0x0);  //apagamos el primer led
                //px.SetI(barra[0]%WS2812_BUF, 0x0);
                
                for(int i=0; i<NUM_LEDS_BARRA; i++){ //actualizamos valor del array
                    barra[i]=barra[i]+1;
                }
                
                px.Set(barra[NUM_LEDS_BARRA-1], 0x2f0000); //encendemos el siguiente led a la derecha
                px.SetI(barra[NUM_LEDS_BARRA-1]%WS2812_BUF, 0xff);
                
                //update_barra(0,(NUM_LEDS_BARRA-1), 1 );
            }
            
        }
    }
    
    //ws.write(px.getBuf());
}*/
void update_barra(int led_apagar, int led_encender, int act_barra ){
    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, 0xff);
}

/*void control_accel(){
    float acY= accel.getAccY();
    if(acY>0.3){
        if(barra[NUM_LEDS_BARRA-1]<15){
            px.Set(barra[0], 0x0);  //apagamos el primer led
            //px.SetI(barra[0]%WS2812_BUF, 0x0);
            
            for(int i=0; i<NUM_LEDS_BARRA; i++){ //actualizamos valor del array
                barra[i]=barra[i]+1;
            }
            
            px.Set(barra[NUM_LEDS_BARRA-1], 0x2f0000); //encendemos el siguiente led a la derecha
            px.SetI(barra[NUM_LEDS_BARRA-1]%WS2812_BUF, 0xff);
        }
        
    }
    else{
        if(acY<-0.3){ //if left
            if(barra[0]>0){
                px.Set(barra[NUM_LEDS_BARRA-1], 0x0);  //apagamos el último led
                //px.SetI(barra[NUM_LEDS_BARRA-1]%WS2812_BUF, 0x0);
                
                for(int i=0; i<NUM_LEDS_BARRA; i++){ //actualizamos valor del array
                    barra[i]=barra[i]-1;
                }
                
                px.Set(barra[0], 0x2f0000); //encendemos el siguiente led a la izquierda
                px.SetI(barra[0]%WS2812_BUF, 0xff);
            }
        }
    }
}*/

/*    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 refreshLightStrips(){
        ws.write(px.getBuf());  
    }
    
 void printBar(uint8_t valR, uint8_t valG, uint8_t valB, int pos) {
    
    printf ("\r\nRGB=> %ld, %ld, %ld \r\n", valR, valG, valB);
    
    if(pos>=-6 and pos<=6){
        for (unsigned ii = 240; ii < 256; ii++) {
            setPixelColor(ii, 0, 0, 0);
        }
        setPixelColor(256-10+pos, valR, valG, valB);
        setPixelColor(256-9+pos, valR, valG, valB);
        setPixelColor(256-8+pos, valR, valG, valB);
        setPixelColor(256-7+pos, valR, valG, valB);

    }
    
    refreshLightStrips();
    
}

*/
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>=38){
            posAcelerometer=posAcelerometerTemp;
            pc.printf("\r\n NEWWWW =%d",posAcelerometer);
            pc.printf("\r\n OOOLD =%d",posAcelerometerOLD);
        }
             
    }
    if(posAcelerometer!=posAcelerometerOLD){
        if(posAcelerometer>posAcelerometerOLD){
            update_barra(0,(NUM_LEDS_BARRA-1), 1 ); //if rigth
            ws.write(px.getBuf());
            pc.printf("\r\n HOLAAAA DRETA");
            /*for(int p=posAcelerometerOLD; p<=posAcelerometer; p++){ //movemos la barrita hasta la posición actual, mostrando la animación
                if(barra[NUM_LEDS_BARRA-1]<15){
                    update_barra(0,(NUM_LEDS_BARRA-1), 1 ); //if rigth
                    ws.write(px.getBuf());
                }
                //pc.printf("\r\n HOLAAAA DRETA=%d",p);
                //printBar( 255, 0, 0, p);
                
            }*/
        }else if(posAcelerometer<posAcelerometerOLD){
            update_barra((NUM_LEDS_BARRA-1), 0, -1 ); //if left
            ws.write(px.getBuf());
            pc.printf("\r\n HOLAAAA ESQUERRA");
            /*for(int p=posAcelerometerOLD; p>=posAcelerometer; p--){
                //printBar( 255, 0, 0, p);
                if(barra[0]>0){
                    
                }
                //
                
            }*/
         
        }
            
       posAcelerometerOLD=posAcelerometer;
        
        //pc.printf("\r\n Acelerometer");
    }
}