Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Revision:
19:394c9a4a3046
Parent:
18:6312be3b32d9
Child:
20:b2e0b41a0e6b
diff -r 6312be3b32d9 -r 394c9a4a3046 main.cpp
--- a/main.cpp	Wed Oct 28 15:06:09 2015 +0000
+++ b/main.cpp	Fri Oct 30 11:59:52 2015 +0100
@@ -11,25 +11,8 @@
 #define VL6180X_I2C_SDA I2C_SDA
 #define VL6180X_I2C_SCL I2C_SCL
 
-//#define RANGE_SINGLE_SHOT_POLLING
-//#define ALS_SINGLE_SHOT_POLLING
-//#define RANGE_CONTINUOUS_POLLING
-//#define ALS_CONTINUOUS_POLLING
-#define RANGE_CONTINUOUS_INTERRUPT
-//#define ALS_CONTINUOUS_INTERRUPT
-//#define INTERLEAVED_MODE_INTERRUPT
-//#define RANGE_CONTINUOUS_POLLING_LOW_THRESHOLD
-//#define RANGE_CONTINUOUS_POLLING_HIGH_THRESHOLD
-//#define RANGE_CONTINUOUS_POLLING_OUT_OF_WINDOW
-//#define ALS_CONTINUOUS_POLLING_LOW_THRESHOLD
-//#define ALS_CONTINUOUS_POLLING_HIGH_THRESHOLD
-//#define ALS_CONTINUOUS_POLLING_OUT_OF_WINDOW
-//#define RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD
-//#define RANGE_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD
-//#define RANGE_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW	
-//#define ALS_CONTINUOUS_INTERRUPT_LOW_THRESHOLD
-//#define ALS_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD
-//#define ALS_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW
+#define RANGE   0
+#define ALS     1
 
 /* timer and digital out pin used for debugging */
 //Timer timer;
@@ -39,233 +22,133 @@
 DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
 static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c);
 
-MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right;
+MeasureData_t data_sensor_top;
 	
 /* flags that handle interrupt request */
-bool flag_sensor_top=false, flag_sensor_bottom=false, flag_sensor_left=false, flag_sensor_right=false, flag_stop_measure=false;	
+bool flag_sensor_top=false, flag_stop_measure=false;	
 
-/* user button used to stop measurements in interrupt mode */
-InterruptIn stop_button(USER_BUTTON);
-
-/* callback functions of the sensors */ 
+/* callback function of the sensor_top */
 void SensorTopIRQ(void)
 {
    flag_sensor_top=true;
    board->sensor_top->DisableInterruptMeasureDetectionIRQ();
 }	
 
-void SensorBottomIRQ(void)
-{
-   flag_sensor_bottom=true;	
-   board->sensor_bottom->DisableInterruptMeasureDetectionIRQ();
-}	
-
-void SensorLeftIRQ(void)
-{
-   flag_sensor_left=true;	
-   board->sensor_left->DisableInterruptMeasureDetectionIRQ();
-}
-
-void SensorRightIRQ(void)
-{
-   flag_sensor_right=true;	
-   board->sensor_right->DisableInterruptMeasureDetectionIRQ();
-}
-
+/* callback function of the user button to stop program */
 void StopMeasureIRQ(void)
 {
    flag_stop_measure=true;
 }
 
+void DisplayRefresh(OperatingMode op_mode)
+{
+   char str[4];
+   
+   if(op_mode==range_continuous_interrupt)
+   {
+      if(data_sensor_top.range_mm!=0xFFFFFFFF)
+      {
+         sprintf(str,"%d",data_sensor_top.range_mm);
+         board->display->DisplayString(str, strlen(str));
+      }
+   }
+   else if(op_mode==als_continuous_interrupt)
+   {
+      if(data_sensor_top.lux!=0xFFFFFFFF)
+      {
+         sprintf(str,"%d",data_sensor_top.lux);
+         board->display->DisplayString(str, strlen(str));
+      }
+   }    
+}
+
 int main()
 {   
-   int status, status_t, status_b, status_l, status_r;
-   
-   //device_i2c.frequency(400000); //change i2c frequncy from 100kHz to 400kHz
-   pc.baud(115200); //change baudrate of the printf
+   OperatingMode operating_mode, prev_operating_mode;
+   /* user button used to stop measurements in interrupt mode */
+   InterruptIn stop_button(USER_BUTTON);
+   STMPE1600DigiIn measure_type(device_i2c, GPIO_11);
+   int status, measure;
+   char str[4];
+ 
+   pc.baud(115200); //change the printf baudrate
    
    status=board->InitBoard();
    if(status)
       printf("Failed to init the board!\n\r");
-
-#ifdef RANGE_SINGLE_SHOT_POLLING	 
-   status_t=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &data_sensor_top, NULL, NULL);
-   if(!status_t)
-      printf("Top Range: %dmm\n\r",data_sensor_top.range_mm);
-   else if(status==INVALID_PARAMS)
-      printf("Failed to start measurement!\n\r");
-   else
-      printf("Invalid range value!\n\r");
-   status_t=board->sensor_top->StopMeasurement(range_single_shot_polling);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r");
-#endif
-	 
-#ifdef ALS_SINGLE_SHOT_POLLING
-   status_t=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &data_sensor_top, NULL, NULL);
-   if(!status_t)
-      printf("Top Light: %d lux\n\r",data_sensor_top.lux);
-   else if(status==INVALID_PARAMS)
-      printf("Failed to start measurement!\n\r");
-   else
-      printf("Invalid light value!\n\r");
-   status_t=board->sensor_top->StopMeasurement(als_single_shot_polling);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r");
-#endif
-
-#ifdef RANGE_CONTINUOUS_POLLING
-   status_t=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &data_sensor_top, NULL, NULL);
-   if(!status_t)
-   {
-      int i;
-      for(i=0;i<10;i++)
-      {
-         status_t=board->sensor_top->GetMeasurement(range_continuous_polling, &data_sensor_top);
-	 if(!status_t)
-	    printf("Top Range measure %d: %dmm\n\r",i+1,data_sensor_top.range_mm);
-	 else
-	    printf("Invalid range value!\n\r");
-      }
-   }
-   else
-      printf("Failed to start measurement!\n\r");
-   status_t=board->sensor_top->StopMeasurement(range_continuous_polling);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r");
-#endif 
-	
-#ifdef ALS_CONTINUOUS_POLLING	 
-   status_t=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &data_sensor_top, NULL, NULL);
-   if(!status_t)
-   {
-      int i;
-      for(i=0;i<10;i++)
-      {
-         status=board->sensor_top->GetMeasurement(als_continuous_polling, &data_sensor_top);
-	 if(!status_t)
-	    printf("Top Light measure %d: %d lux\n\r",i,data_sensor_top.lux);
-	 else
-	    printf("Invalid light value!\n\r");
-      }
-   }
-   else
-      printf("Failed to start measurement!\n\r");
-   status_t=board->sensor_top->StopMeasurement(als_continuous_polling);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r");
-#endif
-
-#ifdef RANGE_CONTINUOUS_INTERRUPT
+   
    stop_button.rise(&StopMeasureIRQ);
-   status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+   measure=measure_type;
+   if(measure==RANGE)
+      operating_mode=range_continuous_interrupt;
+   else if(measure==ALS)
+      operating_mode=als_continuous_interrupt;
+   status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+   prev_operating_mode=operating_mode;
    if(!status)
    {
+      printf("\nStart measurement\n\r");
       while(1)
       {
          if(flag_sensor_top)
          {
             flag_sensor_top=false;
-            status=board->sensor_top->HandleIRQ(range_continuous_interrupt, &data_sensor_top);
+            status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top);
             if(!status)
-               printf("Range: %dmm\n\r",data_sensor_top.range_mm);
-            else
-               printf("Invalid range value!\n\r");
+            {
+              if(operating_mode==range_continuous_interrupt)
+                 sprintf(str,"%d",data_sensor_top.range_mm);
+              else if(operating_mode==als_continuous_interrupt)
+                 sprintf(str,"%d",data_sensor_top.lux);
+              board->display->DisplayString(str, strlen(str));
+            }
+            /*else
+            {
+               sprintf(str,"%s","----");
+               board->display->DisplayString(str, strlen(str));
+            }*/
          }
          if(flag_stop_measure)
-           break;
-      }
-   }
-   else 
-      printf("Failed to start measurement!\n\r");
-   status=board->sensor_top->StopMeasurement(range_continuous_interrupt);
-   if(status)
-      printf("Failed to stop measurement!\n\r");
-#endif
-
-#ifdef ALS_CONTINUOUS_INTERRUPT
-   stop_button.rise(&StopMeasureIRQ);
-   status_t=board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
-   if(!status_t)
-   {
-      while(1)
-      {
-         if(flag_sensor_top)
+         {
+            status=board->sensor_top->StopMeasurement(prev_operating_mode);
+            if(status)
+               printf("Failed to stop measurement!\n\r");
+            else
+               printf("Measurement stopped!\n\r");
+            printf("\nProgram stopped!\n\n\r");
+            break;
+         }
+         measure=measure_type;
+         if(measure==RANGE)
+            operating_mode=range_continuous_interrupt;
+         else if(measure==ALS)
+            operating_mode=als_continuous_interrupt;
+         if(operating_mode!=prev_operating_mode)
          {
- 	    flag_sensor_top=false;
-            status_t=board->sensor_top->HandleIRQ(als_continuous_interrupt, &data_sensor_top);
-	    if(!status_t)
-	       printf("Top Light: %d lux\n\r",data_sensor_top.lux);
-	    else
-               printf("Invalid light value!\n\r");
-	 }
-         if(flag_stop_measure)
-           break;
+            DisplayRefresh(prev_operating_mode);
+            status=board->sensor_top->StopMeasurement(prev_operating_mode);
+            prev_operating_mode=operating_mode;
+            if(status)
+               printf("Failed to stop measurement!\n\r");
+            else
+               printf("Measurement stopped!\n\r");
+            status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+            if(status)
+            {
+               printf("\nFailed to start measurement!\n\r");
+               break;
+            }
+            else
+               printf("\nStart measurement\n\r");
+         }
+         else
+            DisplayRefresh(operating_mode);
       }
    }
    else
-      printf("Failed to stop measurement!\n\r");
-   status_t=board->sensor_top->StopMeasurement(als_continuous_interrupt);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r");
-#endif
-	 
-#ifdef INTERLEAVED_MODE_INTERRUPT
-   stop_button.rise(&StopMeasureIRQ);
-   status_t=board->sensor_top->StartMeasurement(interleaved_mode_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
-   if(!status_t)
-   {
-      while(1)
-      {
-         if(flag_sensor_top)
-         {
-            flag_sensor_top=false;
-            status_t=board->sensor_top->HandleIRQ(interleaved_mode_interrupt, &data_sensor_top);
-            if(!status_t)
-	       printf("Top Range: %dmm\tTop Light: %d lux\n\r",data_sensor_top.range_mm, data_sensor_top.lux);
-	    else
-	       printf("Invalid range or light value!\n\r");
-	 }
-         if(flag_stop_measure)
-           break;
-      }
-   }
-   else
-      printf("Failed to stop measurement!\n\r");
-   status_t=board->sensor_top->StopMeasurement(interleaved_mode_interrupt);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r"); 
-#endif
+      printf("\nFailed to start measurement!\n\r");
 
-#ifdef RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD
-   stop_button.rise(&StopMeasureIRQ);
-   status_t=board->sensor_top->StartMeasurement(range_continuous_interrupt_low_threshold, SensorTopIRQ, &data_sensor_top, 80, NULL);
-   if(!status_t)
-   {
-      while(1)
-      {
-         if(flag_sensor_top)
-         {
-            flag_sensor_top=false;
-	    status_t=board->sensor_top->HandleIRQ(range_continuous_interrupt_low_threshold, &data_sensor_top);
-            if(!status_t)
-	       printf("Range int low threshold: %dmm\n\r",data_sensor_top.range_mm);
-            else
-	       printf("Invalid range value!\n\r");
-         }
-         if(flag_stop_measure)
-           break;
-      }
-   }
-   else 
-      printf("Failed to start measurement!\n\r");
-   status_t=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold);
-   if(status_t)
-      printf("Failed to stop measurement!\n\r");
-#endif
-
-delete board;   
-   
+   delete board;  
 }