Programa de MIP

Dependencies:   bloques ball mbed WS2812 PixelArray tsi_sensor TSI bloque MMA8451Q

main.cpp

Committer:
anna_dot
Date:
2021-05-19
Revision:
3:769c63f85711
Parent:
2:273f1db49b50
Child:
4:3f515bbfd20c

File content as of revision 3:769c63f85711:

#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 accel(PTE25, PTE24, ACCEL_ADRESS_I2C );

TSISensor tsi;
int barra[NUM_LEDS_BARRA]={6,7,8,9};
float tsi_oldRead=0;
int counter_tsi=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);


int main()
{
 
    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());
        
        control_accel();
        
    }
 
}

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 último led
                
    for(int i=0; i<NUM_LEDS_BARRA; i++){ //actualizamos valor del array
        barra[i]=barra[i]+act_barra;
    }
            
    px.Set(barra[led_encender], 0x2f0000); //encendemos el siguiente led a la izquierda
    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);
            }
        }
    }
}