Programa de MIP

Dependencies:   bloques ball mbed WS2812 PixelArray tsi_sensor TSI bloque MMA8451Q

Revision:
12:f9060e568ff9
Parent:
11:4908defcd9d9
Child:
13:3d207bd7f387
--- a/main.cpp	Sun May 23 17:08:47 2021 +0000
+++ b/main.cpp	Sun May 23 18:11:51 2021 +0000
@@ -35,7 +35,8 @@
 bool direct_ball=0;
 Ticker refresh_ball;
 
-
+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);
@@ -44,9 +45,11 @@
 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 clear_matrix();
 
 int main()
-{   int sel_opcio=1;
+{   
+    int sel_opcio=0;
     pc.baud(115200);
     pc.printf("\r\n\r\nHola!\r\ncompilado el " __DATE__ ", " __TIME__ "\r\n");
     
@@ -81,7 +84,7 @@
     // by writing it out with an increasing offset
     ws.write(px.getBuf());
     
-    refresh_ball.attach(&ctr_ball, 0.5);
+    refresh_ball.attach(&ctr_ball, 0.2);
     while (1) {
         /*for (int z=WS2812_BUF; z >= 0 ; z--) {
             ws.write_offsets(px.getBuf(),z,z,z);
@@ -121,7 +124,7 @@
         tsi_oldRead=tsi_newRead;
     
 
-        if(counter_tsi == 250){
+        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 );
@@ -308,22 +311,57 @@
         direct_ball=!direct_ball;
     }
     else{
-        while(!found_barra and i<NUM_LEDS_BARRA){
-            if(barra[i]==old_j and old_i==1){
-                found_barra=true;
-                direct_ball=!direct_ball;           
+        if( (old_j==15 or old_j==0) and (angle!=1) ){
+            
+            if(angle==0){
+                angle=2;
+            }else{
+                angle=0;
+            }
+            
+        }else{
+        
+            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++;
             }
-            i++;
+            if(found_barra){
+                if(barra[0]==old_j){
+                    angle=0;
+                }else{
+                    if(barra[3]==old_j){
+                        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; 
     }*/
-    m_ball.movement(direct_ball);   
     
-    print_pixel(255, 255, 255, m_ball.getCoordX(), m_ball.getCoordY(),old_i, old_j);
+    if(!lose){
+        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<255; i++){
+        setPixelColor(i,0,0,0);
+    }
+    ws.write(px.getBuf());
 }
 
 
-