asd

Dependencies:   TextLCD VL53L0X_simple mbed

Fork of Check_VL53L0X_simple_ToF_Sensor by Kenji Arai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Mbed Application program
00003  *   Time-of-Flight ranging and gesture detection sensor / STMicro VL53L0X
00004  *      http://www.st.com/ja/imaging-and-photonics-solutions/vl53l0x.html
00005  *
00006  *    1) AKIZUKI AE-VL53L0X
00007  *      http://akizukidenshi.com/catalog/g/gM-12590/
00008  *    2) SWITCH SCIENCE  Pololu VL53L0X (POLOLU-2490)
00009  *      https://www.switch-science.com/catalog/2869/
00010  *    3) SWITCH SCIENCE  VL53L0X (SSCI-028943)
00011  *      https://www.switch-science.com/catalog/2894/
00012  *    4) Strawberry Linux
00013  *      https://strawberry-linux.com/catalog/items?code=15310
00014  *
00015  *    ---- Tested AE-VL53L0X BOARD and handmade circuit ----
00016  *      Tested on below Mbed boards and works fine on mbed 2.0
00017  *          Nucleo-F303K8
00018  *          Nucleo-F334R8, -F401RE, -F411RE, -F446RE
00019  *          Nucleo-L053R8, -L073RZ, -L152RE, -L476RG
00020  *          FRDM-K64F
00021  *          TY51822r3
00022  *      Run also on mbed-os5 (Tested on Nucleo-F446RE)
00023  *
00024  * Copyright (c) 2018 Kenji Arai / JH1PJL
00025  *  http://www.page.sannet.ne.jp/kenjia/index.html
00026  *  http://mbed.org/users/kenjiArai/
00027  *      Created:    January   21st, 2018
00028  *      Revised:    Feburary   6th, 2018 (with updated VL53L0X_simple library)
00029  */
00030 
00031 //  Include --------------------------------------------------------------------
00032 #include "mbed.h"
00033 #include "VL53L0X.h"
00034 
00035 //  Definition -----------------------------------------------------------------
00036 #define USE_LCD     0
00037 #define USE_XSHUT   0
00038 
00039 //  Constructor ----------------------------------------------------------------
00040 DigitalOut  myled(LED1);
00041 Serial      pc(USBTX, USBRX);
00042 //I2C         i2c(P0_30, P0_7);     // only for TY51822r3
00043 I2C         i2c(I2C_SDA, I2C_SCL);
00044 #if USE_XSHUT
00045 VL53L0X     sensor(i2c, D8);        // XSHUT = D8
00046 #else
00047 VL53L0X     sensor(i2c, NC);        // XSHUT = NC
00048 #endif
00049 #if USE_LCD
00050 TextLCD_I2C_N lcd(&i2c, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki  AQM0802A)
00051 #endif
00052 Timer       t;
00053 
00054 //  RAM ------------------------------------------------------------------------
00055 
00056 //  ROM / Constant data --------------------------------------------------------
00057 char *const msg0  = "VL53L0X is running correctly!!\r\n";
00058 char *const msg1  = "VL53L0X -> something is wrong!!\r\n";
00059 char *const msg2  = "#,";
00060 char *const msg3  = "d[mm]=,";
00061 char *const msg4  = "d[mm]=,error,";
00062 char *const msg5  = "VL53[mS]=, ";
00063 char *const msg6  = "all[mS]=, ";
00064 
00065 //  Function prototypes --------------------------------------------------------
00066 
00067 //------------------------------------------------------------------------------
00068 //  Control Program
00069 //------------------------------------------------------------------------------
00070 int main()
00071 {
00072     int status = VL53L0X_ERROR_NONE;
00073     uint32_t data;
00074     uint32_t count = 0;
00075     uint32_t tm_sensor;
00076     uint32_t tm_all_work;
00077 
00078 #if USE_LCD
00079     lcd.locate(0, 0);
00080     //        12345678
00081     lcd.puts("12345678");
00082     lcd.locate(0, 1);
00083     //        12345678
00084     lcd.puts(" JH1PJL ");
00085     lcd.setCursor(TextLCD_Base::CurOff_BlkOff);
00086     lcd.setContrast(0x19);
00087     wait(2.0f);
00088 #endif
00089 #if USE_XSHUT
00090     status = sensor.init_sensor(0x33);  // set new I2C address
00091 #else
00092     // no control XSHUT then set default address (no other way)
00093     status = sensor.init_sensor(VL53L0X_DEFAULT_ADDRESS);
00094 #endif
00095     if (status == VL53L0X_ERROR_NONE) {
00096         pc.printf(msg0);
00097     } else {
00098         pc.printf(msg1);
00099     }
00100     status = sensor.set_mode(range_long_distance_33ms_200cm);
00101     //status = sensor.set_mode(range_hi_accurate_200ms_120cm);
00102     //status = sensor.set_mode(range_hi_speed_20ms_120cm);
00103     if (status == VL53L0X_ERROR_NONE) {
00104         pc.printf(msg0);
00105     } else {
00106         pc.printf(msg1);
00107     }
00108     while(true) {
00109         t.reset();
00110         t.start();
00111         //myled = !myled;
00112         status = sensor.get_distance(&data);
00113         tm_sensor = t.read_ms();
00114         if (status == VL53L0X_ERROR_NONE) {
00115             pc.printf("%s%5d,%s%5d,", msg2, count++, msg3, data);
00116         } else {
00117             pc.printf("%s%5d,%s", msg2, count++, msg4);
00118         }
00119 #if USE_LCD
00120         if ((count % 4) == 0){
00121             lcd.locate(0, 0);
00122             lcd.printf(" %5d  ", data);
00123             lcd.locate(0, 1);
00124             lcd.printf(" %5d  ", count);
00125         }
00126 #endif
00127         pc.printf("%s%d,%s%d\r\n", msg5, tm_sensor, msg6, tm_all_work);
00128         tm_all_work = t.read_ms();
00129         if (tm_all_work < 99){
00130             wait_ms(100 - tm_all_work);
00131         }
00132     }
00133 }