Programa de MIP

Dependencies:   bloques ball mbed WS2812 PixelArray tsi_sensor TSI bloque MMA8451Q

Revision:
10:dccb2be41fbd
Parent:
9:777b0535d3bb
Child:
11:4908defcd9d9
--- a/main.cpp	Sat May 22 18:02:43 2021 +0000
+++ b/main.cpp	Sat May 22 19:12:30 2021 +0000
@@ -32,6 +32,8 @@
 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;
 
 
 
@@ -41,9 +43,11 @@
 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=7;
+{   int sel_opcio=1;
     pc.baud(115200);
     pc.printf("\r\n\r\nHola!\r\ncompilado el " __DATE__ ", " __TIME__ "\r\n");
     
@@ -77,6 +81,8 @@
     // 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);
@@ -93,11 +99,10 @@
                 accel_ctr();
             }
         }   
-        
-        pc.printf("\r\n Touch %d", (rand()%(9-6 + 1) + 6));
+        //pc.printf("\r\n Touch %d", (rand()%(9-6 + 1) + 6));
         
     }
- 
+    
 }
 
 void tsi_ctr_modeBtn(float tsi_newRead){
@@ -195,15 +200,18 @@
 
 
 void update_barra(int led_apagar, int led_encender, int act_barra ){
-    px.Set(barra[led_apagar], 0x0);  //apagamos el led
+    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]);
+        }
                 
-    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();
     }
-            
-    px.Set(barra[led_encender], 0x2f0000); //encendemos el siguiente led
-    px.SetI(barra[led_encender]%WS2812_BUF, 255); //0xff
 }
 
 
@@ -291,5 +299,40 @@
     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);
+}