Capteur de distance VL6180

Dependencies:   TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG VL6180x BUTTON_GROUP

Revision:
2:ffdb56ed9b36
Parent:
1:126b6cd0f4f5
--- a/main.cpp	Sun May 10 19:10:32 2015 +0000
+++ b/main.cpp	Fri Jun 26 01:58:28 2020 +0000
@@ -1,117 +1,206 @@
-/******************************************************************************
- * Developed from
- * VL6180X_demo.ino
- * Example Sketch for VL6180x time of flight range finder.
- * Casey Kuhns @ SparkFun Electronics
- * 10/29/2014
- * https://github.com/sparkfun/ToF_Range_Finder-VL6180_Library
- * 
- * The VL6180x by ST micro is a time of flight range finder that
- * uses pulsed IR light to determine distances from object at close
- * range.  The average range of a sensor is between 0-200mm
- * 
- * Resources:
- * This library uses the Arduino Wire.h to complete I2C transactions.
- * 
- * Development environment specifics:
- *  IDE: Arduino 1.0.5
- *  Hardware Platform: Arduino Pro 3.3V/8MHz
- *  VL6180x Breakout Version: 1.0
- * 
- * 
- * This code is beerware. If you see me (or any other SparkFun employee) at the
- * local pub, and you've found our code helpful, please buy us a round!
- * 
- * Distributed as-is; no warranty is given.
- ******************************************************************************/
 #include "mbed.h"
+#include <stdlib.h>
 #include <VL6180x.h>
+#include "stm32746g_discovery_lcd.h"
+#include "stm32746g_discovery_ts.h"
+#include "button_group.hpp"
+#include <string>
+#include "iut.h"
 
-/*const float GAIN_1    = 1.01;  // Actual ALS Gain of 1.01
-const float GAIN_1_25 = 1.28;  // Actual ALS Gain of 1.28
-const float GAIN_1_67 = 1.72;  // Actual ALS Gain of 1.72
-const float GAIN_2_5  = 2.6;   // Actual ALS Gain of 2.60
-const float GAIN_5    = 5.21;  // Actual ALS Gain of 5.21
-const float GAIN_10   = 10.32; // Actual ALS Gain of 10.32
-const float GAIN_20   = 20;    // Actual ALS Gain of 20
-const float GAIN_40   = 40;    // Actual ALS Gain of 40
-*/
-
+//Adresse du capteur
 #define VL6180X_ADDRESS 0x29
 
+// Procéde à l'identification du capteur
 VL6180xIdentification identification;
 // mbed uses 8bit addresses shift address by 1 bit left
 VL6180x sensor(D14, D15, VL6180X_ADDRESS<<1);
 
-void printIdentification(struct VL6180xIdentification *temp){
-  printf("Model ID = ");
-  printf("%d\n",temp->idModel);
+//Communication avec le pc par USB
+Serial pc(USBTX, USBRX);
 
-  printf("Model Rev = ");
-  printf("%d",temp->idModelRevMajor);
-  printf(".");
-  printf("%d\n",temp->idModelRevMinor);
+//Mikami : pour la gestion des boutons
+using namespace Mikami;
 
-  printf("Module Rev = ");
-  printf("%d",temp->idModuleRevMajor);
-  printf(".");
-  printf("%d\n",temp->idModuleRevMinor);  
+TS_DISCO_F746NG ts_;
+LCD_DISCO_F746NG lcd_;
+
 
-  printf("Manufacture Date = ");
-  printf("%d",((temp->idDate >> 3) & 0x001F));
-  printf("/");
-  printf("%d",((temp->idDate >> 8) & 0x000F));
-  printf("/1");
-  printf("%d\n",((temp->idDate >> 12) & 0x000F));
-  printf(" Phase: ");
-  printf("%d\n",(temp->idDate & 0x0007));
+//fonction printIdentification qui permet l'identification du capteur et l'affichage de ces caréctéristiques(modéle , version)
+void printIdentification(struct VL6180xIdentification *temp)
+{
+    printf("\rModel ID = ");
+    printf("%d\n",temp->idModel);
 
-  printf("Manufacture Time (s)= ");
-  printf("%d\n",(temp->idTime * 2));
-  printf("\n\n");
-}
-int main() {
+    printf("\rModel Rev = ");
+    printf("%d",temp->idModelRevMajor);
+    printf(".");
+    printf("%d\n",temp->idModelRevMinor);
 
-  wait_ms(100); // delay .1s
+    printf("\rModule Rev = ");
+    printf("%d",temp->idModuleRevMajor);
+    printf(".");
+    printf("%d\n",temp->idModuleRevMinor);
 
-  sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
-  printIdentification(&identification); // Helper function to print all the Module information
-
-  if(sensor.VL6180xInit() != 0){
-        printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
-  }; 
-
-  sensor.VL6180xDefautSettings(); //Load default settings to get started.
-  
-    wait_ms(1000); // delay 1s
+    printf("\rManufacture Date = ");
+    printf("%d",((temp->idDate >> 3) & 0x001F));
+    printf("/");
+    printf("%d",((temp->idDate >> 8) & 0x000F));
+    printf("/1");
+    printf("%d\n",((temp->idDate >> 12) & 0x000F));
+    printf(" \rPhase: ");
+    printf("%d\n",(temp->idDate & 0x0007));
 
-    
-    
-    while(1) {
-  //Get Ambient Light level and report in LUX
-      printf("Ambient Light Level (Lux) = ");
-  
-  //Input GAIN for light levels, 
-  // GAIN_20     // Actual ALS Gain of 20
-  // GAIN_10     // Actual ALS Gain of 10.32
-  // GAIN_5      // Actual ALS Gain of 5.21
-  // GAIN_2_5    // Actual ALS Gain of 2.60
-  // GAIN_1_67   // Actual ALS Gain of 1.72
-  // GAIN_1_25   // Actual ALS Gain of 1.28
-  // GAIN_1      // Actual ALS Gain of 1.01
-  // GAIN_40     // Actual ALS Gain of 40
-  
-      printf("%f\n",sensor.getAmbientLight(GAIN_1) );
-
-  //Get Distance and report in mm
-      printf("Distance measured (mm) = ");
-      printf("%d\n", sensor.getDistance() ); 
-
-      wait_ms(500);  
+    printf("\rManufacture Time (s)= ");
+    printf("%d\n",(temp->idTime * 2));
+    printf("\n\n");
+}
+//Logo iut
+void drawImage_iut(int offsetX, int offsetY)
+{
+    int x = 0;
+    int y = 0;
+    uint32_t* dataPtr = (uint32_t*)iut.data;
+    while(y < iut.height) {
+        while(x < iut.width) {
+            BSP_LCD_DrawPixel(x + offsetX, y + offsetY, *dataPtr);
+            dataPtr++;
+            x++;
+        }
+        x = 0;
+        y++;
     }
 }
 
+int main()
+{
+
+    wait_ms(2000);
+
+    pc.baud(115200);//Pour régler la vitesse
+    pc.printf("\r\nRUN\r\n");//envoie un message
+
+    //unsigned int Button_reset = 0 ;
+    unsigned  int valeur_capteur ;
+    char buffer [50] ;
+
+    unsigned int valeur_min = 30 ;
+    unsigned int valeur_max = 150 ;
+    int etat = 0 ; //si etat = 0 alors rien ne se passe / si etat =1 alors c'est la valeur min / si etat = 2 alors c'est la valeur max 
+
+    
+    //Initialisation de l'ecran
+    BSP_LCD_Init();
+    BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
+    BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
+
+    BSP_LCD_Clear(LCD_COLOR_BLUE);
+    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
+    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+    BSP_LCD_SetFont(&Font16);
+
+    //Affichage de chaines de caréctérs (message de bienvenue et mesure)
+    BSP_LCD_DisplayStringAt(35, 30, (uint8_t *)"BIENVENUE SUR UN CAPTEUR DE DISTANCE", CENTER_MODE);
+    BSP_LCD_DisplayStringAt(5, 90, (uint8_t *)"Mesure", CENTER_MODE);
+
+    //affichage du logo de l'iut
+    drawImage_iut(0,0);
+
+    //affichage des carectéristiques du capteur
+    sensor.getIdentification(&identification); 
+    printIdentification(&identification); 
+ 
+    //test pour vérifier si le capteur a été bien initialisé
+    if(sensor.VL6180xInit() != 0) 
+    {
+        printf("FAILED TO INITALIZE\n"); 
+    };
+
+    sensor.VL6180xDefautSettings(); 
+
+    wait_ms(1000); // delay 1s
+
+
+    //Switch : bouton min , max
+    const string STR_SW[2] = {"MIN", "MAX"};
+    BSP_LCD_SetFont(&Font20);
+    ButtonGroup sw(lcd_, ts_, 10, 160, 66, 40,
+                   LCD_COLOR_DARKBLUE,   LCD_COLOR_DARKBLUE, 2, STR_SW, 5, 0, 2);
 
 
+    while(1) {
+        //affichage de la lumiére ambiante en LUX
+        printf("\r\nAmbient Light Level (Lux) = ");
+        printf("%f",sensor.getAmbientLight(GAIN_1) );
 
+        //Affichage de la distance en milimétres
+        printf("\r\nDistance measured (mm) = ");
+        printf("%d\n", sensor.getDistance() );
 
+        //Stocker la distance dans la variable "valeur_capteur"
+        valeur_capteur = sensor.getDistance() ;
+
+        //Conversion
+        sprintf(buffer, "%d", valeur_capteur);
+        
+        //paramétrage de l'écran et affichage de la distance
+        BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
+        BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
+        BSP_LCD_SetFont(&Font20);
+        BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)255, CENTER_MODE);
+        
+        //condition pour tester si le switch "MIN" est appuyé
+        if (sw.Touched(0, LCD_COLOR_GREEN)) {
+            BSP_LCD_SetFont(&Font20);
+            BSP_LCD_DisplayStringAt(0, 210, (uint8_t *)"MIN = 30  ", CENTER_MODE);
+            etat = 1 ; // etat 1 = etat MIN
+
+        }
+        
+        //condition pour tester si le switch "MAX" est appuyé
+        else if (sw.Touched(1, LCD_COLOR_RED)) {
+            BSP_LCD_SetFont(&Font20);
+            BSP_LCD_DisplayStringAt(0, 210, (uint8_t *)"MAX = 150  ", CENTER_MODE);
+            etat =2 ; // etat 2 = etat MAX
+        }
+
+        
+        else {
+            // si etat=0 alors on affiche la mesure en continu 
+            if(etat == 0) {
+                BSP_LCD_SetBackColor(LCD_COLOR_YELLOW);
+                BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+                BSP_LCD_SetFont(&Font20);
+                BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)buffer, CENTER_MODE);
+                wait_ms(500);
+                
+            //sinon si etat=1 on teste alors si la valeur du capteur est supérieure ou égale à la valeur min
+            } else if (etat ==1) {
+                if(valeur_capteur >= valeur_min) {
+                    BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
+                    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+                    BSP_LCD_SetFont(&Font20);
+                    BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)buffer, CENTER_MODE);
+                } else {
+                    BSP_LCD_SetBackColor(LCD_COLOR_RED);
+                    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+                    BSP_LCD_SetFont(&Font20);
+                    BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)buffer, CENTER_MODE);
+                }
+                //sinon si la valeur=2 n teste alors si la valeur du capteur est inférieure à la valeur max 
+            } else if(etat == 2) {
+                if(valeur_capteur <= valeur_max) {
+                    BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
+                    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+                    BSP_LCD_SetFont(&Font20);
+                    BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)buffer, CENTER_MODE);
+
+                } else {
+                    BSP_LCD_SetBackColor(LCD_COLOR_RED);
+                    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+                    BSP_LCD_SetFont(&Font20);
+                    BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)buffer, CENTER_MODE);
+                }
+            }
+        }
+    }
+}