[PH]

Dependencies:   IRremouteAndButton QuadDisplayMy LIS3MDL_my sMotor

Files at this revision

API Documentation at this revision

Comitter:
allesh
Date:
Mon May 17 07:17:06 2021 +0000
Parent:
0:eb99909151eb
Commit message:
placeholder;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r eb99909151eb -r b24bac622a37 main.cpp
--- a/main.cpp	Mon May 17 05:34:19 2021 +0000
+++ b/main.cpp	Mon May 17 07:17:06 2021 +0000
@@ -26,15 +26,21 @@
 //-----------------------
 void polling_sensors_isr();
 void sensors_task();
+void FrameTimeoutCW_isr();
+void FrameTimeoutCCW_isr();
+void BounceTimeoutCW_isr();
+void BounceTimeoutCCW_isr();
+void CW_switch_isr();
+void CCW_switch_isr();
 //-----------------------
-void clk_speed_isr();
+//void clk_speed_isr();
 /*****************************************************************
 *                           Interface
 ******************************************************************/
 
 DigitalOut led1(LED1);
-DigitalIn CW_switch(D10);
-DigitalIn CCW_switch(D11);
+InterruptIn CW_switch(D10);
+InterruptIn CCW_switch(D11);
 //---
 RawSerial pc(USBTX, USBRX);
 DevI2C devI2c(D14,D15);
@@ -44,17 +50,22 @@
 QuadDisplayMy display(&spi,D6);
 //---
 sMotor motor(A5, A4, A3, A2); // creates new stepper motor: IN1, IN2, IN3, IN4
-InterruptIn BlueBatton(BUTTON)
+InterruptIn BlueBatton(USER_BUTTON);
 
 /*****************************************************************
  *                            Time
 ******************************************************************/
-//Ticker polling_sensors;
+Ticker polling_sensors;
 //Ticker clk_speed;
+Timeout FrameTimeoutCW;
+Timeout BounceTimeoutCW;
+Timeout FrameTimeoutCCW;
+Timeout BounceTimeoutCCW;
+
 /****************************************************************
 *                           Threads  
 ****************************************************************/
-//Thread sensor_daemon;
+Thread sensor_daemon;
 
 /*****************************************************************
 *                             Global variables
@@ -66,6 +77,7 @@
 uint8_t mode=0;
 uint8_t RotateDir;
 uint16_t RotateSpeed;
+
 //----------------
 uint8_t SensorsEn=1;
 
@@ -73,6 +85,12 @@
 uint8_t point;
 uint32_t rotate_cnt;
 //---------
+uint8_t DebounceCW=0;
+uint8_t cntCW=0;
+uint8_t NewCWMode=0;
+uint8_t DebounceCCW=0;
+uint8_t cntCCW=0;
+uint8_t NewCCWMode=0;
 //------------------
 
 /******************************************************************
@@ -106,7 +124,7 @@
 {
  pc.baud(115200);
  ButtonIni(&BlueBatton);
- button_event_ind=button_event=0;
+ //button_event_ind=button_event=0;
  
  
 //--------------
@@ -114,17 +132,19 @@
 sensor_daemon.start(sensors_task);
 SensorsEn=1;
 polling_sensors.attach(&polling_sensors_isr, 0.4);
-clk_speed.attach(&clk_speed_isr, 0.01);
+//clk_speed.attach(&clk_speed_isr, 0.01);
 mode=0;
 point=0;
 RotateSpeed=step_speed;
 rotate_cnt=0;
+CW_switch.fall(&CW_switch_isr);
+CCW_switch.fall(&CCW_switch_isr);
 }
 
 //------------------
 void HowManyClicks()
 {
-    WhatButtonMode(&button_event)
+    WhatButtonMode(&button_event);
     if(button_event) {
         switch (button_event) {
             case 1:
@@ -133,9 +153,9 @@
                 break;
             case 2:
                 mode=1;
-                point=0;
-                //RotateDir=1;
-                //RotateSpeed=step_speed;
+                point=1;
+                RotateDir=0;
+                RotateSpeed=step_speed;
                 break;                    
             case 3:
                 mode=2;
@@ -182,48 +202,30 @@
     while (true) {
         ThisThread::flags_wait_any(0x1,true);
         SensorsEn=0;                 
-        status = board->sensor_left->get_distance(&distance_l); 
-        if (status != VL53L0X_ERROR_NONE)
-            distance_l=8888;  
-        status = board->sensor_right->get_distance(&distance_r);
-        if (status != VL53L0X_ERROR_NONE)
-             distance_r=8888;      
-        status = board->sensor_centre->get_distance(&distance_c); 
-        if (status != VL53L0X_ERROR_NONE)
-             distance_c=8888; 
+        
         switch(mode){
             case 0:
-                sprintf(text,"c%3d",distance_c);  
+               // sprintf(text,"c%3d",distance_c);  
                 break;
             case 1:
-                sprintf(text,"l%3d", distance_l);                        
+                //sprintf(text,"l%3d", distance_l);                        
                 break;  
             case 2:      
-                sprintf(text,"r%3d", distance_r);
+                //sprintf(text,"r%3d", distance_r);
                 break;    
             case 3:
-                dif=((int)distance_r-(int)distance_l);   
-                sprintf(text,"d%3d",dif);
-                if (abs(dif)>4) {
-                    point=1;
-                    if(dif>0)
-                        RotateDir=1;
-                    else
-                        RotateDir=0;
-                }else    
-                    point=0;                       
+                    
                 break;   
             case 4:      
-                sprintf(text,"%d",G[0]);
+                //sprintf(text,"%d",G[0]);
                 break;
             case 5:      
-                sprintf(text,"%d",dur_time);
+                //sprintf(text,"%d",dur_time);
                 break;                 
             default: 
                 sprintf(text,"----");                    
                 break;                      
-        } 
-        board->display->display_string(text);                                 
+        }                         
         SensorsEn=1;
    }         
 }
@@ -231,28 +233,63 @@
 /**********************************************************
 *                    Interrupt Service Routines 
 ***********************************************************/
-void proximityR_isr()
+void FrameTimeoutCW_isr()
 {
-  prox=1;      
-} 
+    NewCWMode=cnt;
+    cntCW=0;                   
+}
+
 //---------------------
-void proximityF_isr()
-{ 
-  prox=0;      
-}    
+void BounceTimeoutCW_isr()
+{
+  DebounceCW=0;
+  
+}  
 
+void FrameTimeoutCCW_isr()
+{
+    NewCCWMode=cnt;
+    cntCCW=0;                   
+}
+
+//---------------------
+void BounceTimeoutCCW_isr()
+{
+  DebounceCCW=0;
+  
+}      
 //---------------------
 void polling_sensors_isr()
 {
  if (SensorsEn){
     sensor_daemon.flags_set(0x1);
-    led = !led; 
+    led1 = !led1; 
  }    
 }
+void CW_switch_isr()
+{
+     if(DebounceCW==0){
+        DebounceCW=1;
+        cntCW++;
+        FrameTimeoutCW.detach();
+        FrameTimeoutCW.attach(FrameTimeoutCW_isr,1.0);          
+        BounceTimeoutCW.attach(BounceTimeoutCW_isr,0.2);
+    }
+}
+void CCW_switch_isr()
+{
+     if(DebounceCCW==0){
+        DebounceCCW=1;
+        cntCCW++;
+        FrameTimeoutCCW.detach();
+        FrameTimeoutCCW.attach(FrameTimeoutCCW_isr,1.0);          
+        BounceTimeoutCCW.attach(BounceTimeoutCCW_isr,0.2);
+    }
+}
 //---------------------------------
-void clk_speed_isr()
+/*void clk_speed_isr()
 {
     rotate_cnt++; 
     if (rotate_cnt>10)
         dur_time_en=1;
-}
+}*/