code for read VL53l0x sensor with CAN

Dependencies:   mbed

Committer:
pablo_bmxrp
Date:
Mon Mar 18 22:45:39 2019 +0000
Revision:
1:7bbfe329c62b
Parent:
STM32F103C8T6_MPA/VL53L0X_simple/VL53L0X.h@0:44429c0a71d4
V1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pablo_bmxrp 0:44429c0a71d4 1 /*******************************************************************************
pablo_bmxrp 0:44429c0a71d4 2 Copyright © 2016, STMicroelectronics International N.V.
pablo_bmxrp 0:44429c0a71d4 3 All rights reserved.
pablo_bmxrp 0:44429c0a71d4 4
pablo_bmxrp 0:44429c0a71d4 5 Redistribution and use in source and binary forms, with or without
pablo_bmxrp 0:44429c0a71d4 6 modification, are permitted provided that the following conditions are met:
pablo_bmxrp 0:44429c0a71d4 7 * Redistributions of source code must retain the above copyright
pablo_bmxrp 0:44429c0a71d4 8 notice, this list of conditions and the following disclaimer.
pablo_bmxrp 0:44429c0a71d4 9 * Redistributions in binary form must reproduce the above copyright
pablo_bmxrp 0:44429c0a71d4 10 notice, this list of conditions and the following disclaimer in the
pablo_bmxrp 0:44429c0a71d4 11 documentation and/or other materials provided with the distribution.
pablo_bmxrp 0:44429c0a71d4 12 * Neither the name of STMicroelectronics nor the
pablo_bmxrp 0:44429c0a71d4 13 names of its contributors may be used to endorse or promote products
pablo_bmxrp 0:44429c0a71d4 14 derived from this software without specific prior written permission.
pablo_bmxrp 0:44429c0a71d4 15
pablo_bmxrp 0:44429c0a71d4 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
pablo_bmxrp 0:44429c0a71d4 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
pablo_bmxrp 0:44429c0a71d4 18 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
pablo_bmxrp 0:44429c0a71d4 19 NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
pablo_bmxrp 0:44429c0a71d4 20 IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
pablo_bmxrp 0:44429c0a71d4 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
pablo_bmxrp 0:44429c0a71d4 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
pablo_bmxrp 0:44429c0a71d4 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
pablo_bmxrp 0:44429c0a71d4 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
pablo_bmxrp 0:44429c0a71d4 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
pablo_bmxrp 0:44429c0a71d4 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pablo_bmxrp 0:44429c0a71d4 27 *****************************************************************************/
pablo_bmxrp 0:44429c0a71d4 28
pablo_bmxrp 0:44429c0a71d4 29 #ifndef __VL53L0X_CLASS_H
pablo_bmxrp 0:44429c0a71d4 30 #define __VL53L0X_CLASS_H
pablo_bmxrp 0:44429c0a71d4 31
pablo_bmxrp 0:44429c0a71d4 32 //------- Feburary 6th, 2018 by JH1PJL / K.Arai --------------------------------
pablo_bmxrp 0:44429c0a71d4 33 //ORIGINAL=1 does NOT mean anything but just keep original source code as it is!
pablo_bmxrp 0:44429c0a71d4 34 //You can NOT compile with ORIGINAL=1 !!
pablo_bmxrp 0:44429c0a71d4 35 #define ORIGINAL 0 // Just keep = 0
pablo_bmxrp 0:44429c0a71d4 36 #define USE_I2C_2V8
pablo_bmxrp 0:44429c0a71d4 37 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 38
pablo_bmxrp 0:44429c0a71d4 39 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 40 #ifdef _MSC_VER
pablo_bmxrp 0:44429c0a71d4 41 # ifdef VL53L0X_API_EXPORTS
pablo_bmxrp 0:44429c0a71d4 42 # define VL53L0X_API __declspec(dllexport)
pablo_bmxrp 0:44429c0a71d4 43 # else
pablo_bmxrp 0:44429c0a71d4 44 # define VL53L0X_API
pablo_bmxrp 0:44429c0a71d4 45 # endif
pablo_bmxrp 0:44429c0a71d4 46 #else
pablo_bmxrp 0:44429c0a71d4 47 # define VL53L0X_API
pablo_bmxrp 0:44429c0a71d4 48 #endif
pablo_bmxrp 0:44429c0a71d4 49 #endif
pablo_bmxrp 0:44429c0a71d4 50
pablo_bmxrp 0:44429c0a71d4 51 /* Includes ------------------------------------------------------------------*/
pablo_bmxrp 0:44429c0a71d4 52 #include "mbed.h"
pablo_bmxrp 0:44429c0a71d4 53 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 54 #include "RangeSensor.h"
pablo_bmxrp 0:44429c0a71d4 55 #include "DevI2C.h"
pablo_bmxrp 0:44429c0a71d4 56 #include "PinNames.h"
pablo_bmxrp 0:44429c0a71d4 57 #endif
pablo_bmxrp 0:44429c0a71d4 58 #include "VL53L0X_def.h"
pablo_bmxrp 0:44429c0a71d4 59 #include "VL53L0X_platform.h"
pablo_bmxrp 0:44429c0a71d4 60 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 61 #include "Stmpe1600.h"
pablo_bmxrp 0:44429c0a71d4 62 #endif
pablo_bmxrp 0:44429c0a71d4 63
pablo_bmxrp 0:44429c0a71d4 64 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 65 //------- Feburary 4th, 2018 by JH1PJL / K.Arai --------------------------------
pablo_bmxrp 0:44429c0a71d4 66 // moved to following header file
pablo_bmxrp 0:44429c0a71d4 67 #include "VL53L0X_1st.h"
pablo_bmxrp 0:44429c0a71d4 68 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 69 #endif
pablo_bmxrp 0:44429c0a71d4 70
pablo_bmxrp 0:44429c0a71d4 71 /* sensor operating modes */
pablo_bmxrp 0:44429c0a71d4 72 typedef enum {
pablo_bmxrp 0:44429c0a71d4 73 range_single_shot_polling = 1,
pablo_bmxrp 0:44429c0a71d4 74 range_continuous_polling,
pablo_bmxrp 0:44429c0a71d4 75 range_continuous_interrupt,
pablo_bmxrp 0:44429c0a71d4 76 range_continuous_polling_low_threshold,
pablo_bmxrp 0:44429c0a71d4 77 range_continuous_polling_high_threshold,
pablo_bmxrp 0:44429c0a71d4 78 range_continuous_polling_out_of_window,
pablo_bmxrp 0:44429c0a71d4 79 range_continuous_interrupt_low_threshold,
pablo_bmxrp 0:44429c0a71d4 80 range_continuous_interrupt_high_threshold,
pablo_bmxrp 0:44429c0a71d4 81 range_continuous_interrupt_out_of_window,
pablo_bmxrp 0:44429c0a71d4 82 } OperatingMode;
pablo_bmxrp 0:44429c0a71d4 83
pablo_bmxrp 0:44429c0a71d4 84 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 85 /* sensor range profiles */
pablo_bmxrp 0:44429c0a71d4 86 typedef enum {
pablo_bmxrp 0:44429c0a71d4 87 range_long_distance_33ms_200cm = 1,
pablo_bmxrp 0:44429c0a71d4 88 range_hi_accurate_200ms_120cm,
pablo_bmxrp 0:44429c0a71d4 89 range_hi_speed_20ms_120cm,
pablo_bmxrp 0:44429c0a71d4 90 } RangeProfile;
pablo_bmxrp 0:44429c0a71d4 91 #endif
pablo_bmxrp 0:44429c0a71d4 92
pablo_bmxrp 0:44429c0a71d4 93 /** default device address */
pablo_bmxrp 0:44429c0a71d4 94 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 95 #define VL53L0X_DEFAULT_ADDRESS 0x52 /* (8-bit) */
pablo_bmxrp 0:44429c0a71d4 96 #else
pablo_bmxrp 0:44429c0a71d4 97 // No other choice
pablo_bmxrp 0:44429c0a71d4 98 #define VL53L0X_DEFAULT_ADDRESS 0x52 // 8-bit(0x53/Read & 0x52?Write) 0x29(7bit)
pablo_bmxrp 0:44429c0a71d4 99 #endif
pablo_bmxrp 0:44429c0a71d4 100
pablo_bmxrp 0:44429c0a71d4 101 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 102 #if (MBED_MAJOR_VERSION == 2)
pablo_bmxrp 0:44429c0a71d4 103 #define WAIT_MS(x) wait_ms(x)
pablo_bmxrp 0:44429c0a71d4 104 #define VL53L0X_OsDelay(...) wait_ms(2)
pablo_bmxrp 0:44429c0a71d4 105 #elif (MBED_MAJOR_VERSION == 5)
pablo_bmxrp 0:44429c0a71d4 106 #define WAIT_MS(x) Thread::wait(x)
pablo_bmxrp 0:44429c0a71d4 107 #define VL53L0X_OsDelay(...) Thread::wait(2)
pablo_bmxrp 0:44429c0a71d4 108 #else
pablo_bmxrp 0:44429c0a71d4 109 #warning "I cannot control wait_ms()!!"
pablo_bmxrp 0:44429c0a71d4 110 #endif
pablo_bmxrp 0:44429c0a71d4 111 #endif // !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 112
pablo_bmxrp 0:44429c0a71d4 113 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 114
pablo_bmxrp 0:44429c0a71d4 115 //--------------- Simple debug -------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 116 //extern DigitalOut myled;
pablo_bmxrp 0:44429c0a71d4 117 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 118
pablo_bmxrp 0:44429c0a71d4 119 /** Interface for STMicronics VL53L0X
pablo_bmxrp 0:44429c0a71d4 120 * World smallest Time-of-Flight (ToF) ranging sensor
pablo_bmxrp 0:44429c0a71d4 121 *
pablo_bmxrp 0:44429c0a71d4 122 * @code
pablo_bmxrp 0:44429c0a71d4 123 * #include "mbed.h"
pablo_bmxrp 0:44429c0a71d4 124 *
pablo_bmxrp 0:44429c0a71d4 125 * // I2C Communication
pablo_bmxrp 0:44429c0a71d4 126 * VL53L0X sensor(I2C_SDA, I2C_SCL, D8); // SDA, SCL & XSHUT
pablo_bmxrp 0:44429c0a71d4 127 * // If you connected I2C line not only this device but also other devices,
pablo_bmxrp 0:44429c0a71d4 128 * // you need to declare following method.
pablo_bmxrp 0:44429c0a71d4 129 * I2C i2c(I2C_SDA, I2C_SCL);
pablo_bmxrp 0:44429c0a71d4 130 * VL53L0X sensor(i2c, D8); // I2C, XSHUT
pablo_bmxrp 0:44429c0a71d4 131 *
pablo_bmxrp 0:44429c0a71d4 132 * int main()
pablo_bmxrp 0:44429c0a71d4 133 * {
pablo_bmxrp 0:44429c0a71d4 134 * int status = VL53L0X_ERROR_NONE;
pablo_bmxrp 0:44429c0a71d4 135 * uint32_t data;
pablo_bmxrp 0:44429c0a71d4 136 *
pablo_bmxrp 0:44429c0a71d4 137 * status = sensor.init_sensor(0x33<<1U); //new addres(not equal others)
pablo_bmxrp 0:44429c0a71d4 138 * status = sensor.set_mode(range_long_distance_33ms_200cm);
pablo_bmxrp 0:44429c0a71d4 139 * //status = sensor.set_mode(range_hi_accurate_200ms_120cm);
pablo_bmxrp 0:44429c0a71d4 140 * //status = sensor.set_mode(range_hi_speed_20ms_120cm);
pablo_bmxrp 0:44429c0a71d4 141 * while (true) {
pablo_bmxrp 0:44429c0a71d4 142 * status = sensor.get_distance(&data);
pablo_bmxrp 0:44429c0a71d4 143 * if (status == VL53L0X_ERROR_NONE) {
pablo_bmxrp 0:44429c0a71d4 144 * printf("%5d\r\n", data);
pablo_bmxrp 0:44429c0a71d4 145 * } else {
pablo_bmxrp 0:44429c0a71d4 146 * printf("error\r\n");
pablo_bmxrp 0:44429c0a71d4 147 * }
pablo_bmxrp 0:44429c0a71d4 148 * }
pablo_bmxrp 0:44429c0a71d4 149 * }
pablo_bmxrp 0:44429c0a71d4 150 * @endcode
pablo_bmxrp 0:44429c0a71d4 151 */
pablo_bmxrp 0:44429c0a71d4 152
pablo_bmxrp 0:44429c0a71d4 153
pablo_bmxrp 0:44429c0a71d4 154 /* Classes -------------------------------------------------------------------*/
pablo_bmxrp 0:44429c0a71d4 155 /** Class representing a VL53L0 sensor component
pablo_bmxrp 0:44429c0a71d4 156 */
pablo_bmxrp 0:44429c0a71d4 157 class VL53L0X
pablo_bmxrp 0:44429c0a71d4 158 {
pablo_bmxrp 0:44429c0a71d4 159 public:
pablo_bmxrp 0:44429c0a71d4 160 /** Constructor
pablo_bmxrp 0:44429c0a71d4 161 * @param[in] &i2c device I2C to be used for communication
pablo_bmxrp 0:44429c0a71d4 162 * @param[in] pin Mbed DigitalInOut PinName to be used for XSHUT
pablo_bmxrp 0:44429c0a71d4 163 * @param[in] &pin_gpio1 pin Mbed InterruptIn PinName
pablo_bmxrp 0:44429c0a71d4 164 * to be used as component GPIO_1 INT
pablo_bmxrp 0:44429c0a71d4 165 */
pablo_bmxrp 0:44429c0a71d4 166 VL53L0X(I2C& i2c, PinName xshut, PinName pin_gpio1) : _i2c(i2c),
pablo_bmxrp 0:44429c0a71d4 167 _gpio0(xshut)
pablo_bmxrp 0:44429c0a71d4 168 {
pablo_bmxrp 0:44429c0a71d4 169 if (pin_gpio1 != NC) {
pablo_bmxrp 0:44429c0a71d4 170 _gpio1Int = new InterruptIn(pin_gpio1);
pablo_bmxrp 0:44429c0a71d4 171 } else {
pablo_bmxrp 0:44429c0a71d4 172 _gpio1Int = NULL;
pablo_bmxrp 0:44429c0a71d4 173 }
pablo_bmxrp 0:44429c0a71d4 174 preparation();
pablo_bmxrp 0:44429c0a71d4 175 }
pablo_bmxrp 0:44429c0a71d4 176
pablo_bmxrp 0:44429c0a71d4 177 /** Constructor 2 (another simple way)
pablo_bmxrp 0:44429c0a71d4 178 * @param[in] Pin for I2C SDA & SDL
pablo_bmxrp 0:44429c0a71d4 179 * @param[in] pin Mbed DigitalInOut PinName to be used for XSHUT
pablo_bmxrp 0:44429c0a71d4 180 */
pablo_bmxrp 0:44429c0a71d4 181 VL53L0X(PinName p_sda, PinName p_scl, PinName xshut)
pablo_bmxrp 0:44429c0a71d4 182 : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p), _gpio0(xshut) {
pablo_bmxrp 0:44429c0a71d4 183 _gpio1Int = NULL;
pablo_bmxrp 0:44429c0a71d4 184 preparation();
pablo_bmxrp 0:44429c0a71d4 185 }
pablo_bmxrp 0:44429c0a71d4 186
pablo_bmxrp 0:44429c0a71d4 187 /** Constructor 3 (another simple way)
pablo_bmxrp 0:44429c0a71d4 188 * @param[in] &i2c device I2C to be used for communication
pablo_bmxrp 0:44429c0a71d4 189 * @param[in] pin Mbed DigitalInOut PinName to be used for XSHUT
pablo_bmxrp 0:44429c0a71d4 190 */
pablo_bmxrp 0:44429c0a71d4 191 VL53L0X(I2C& p_i2c, PinName xshut) : _i2c(p_i2c), _gpio0(xshut) {
pablo_bmxrp 0:44429c0a71d4 192 _gpio1Int = NULL;
pablo_bmxrp 0:44429c0a71d4 193 preparation();
pablo_bmxrp 0:44429c0a71d4 194 }
pablo_bmxrp 0:44429c0a71d4 195
pablo_bmxrp 0:44429c0a71d4 196 /** Destructor
pablo_bmxrp 0:44429c0a71d4 197 */
pablo_bmxrp 0:44429c0a71d4 198 virtual ~VL53L0X()
pablo_bmxrp 0:44429c0a71d4 199 {
pablo_bmxrp 0:44429c0a71d4 200 if (_gpio1Int != NULL) {
pablo_bmxrp 0:44429c0a71d4 201 delete _gpio1Int;
pablo_bmxrp 0:44429c0a71d4 202 }
pablo_bmxrp 0:44429c0a71d4 203 }
pablo_bmxrp 0:44429c0a71d4 204 #else
pablo_bmxrp 0:44429c0a71d4 205 #warning "please recover original constractor form VL53L0X_header_original.txt"
pablo_bmxrp 0:44429c0a71d4 206 #endif
pablo_bmxrp 0:44429c0a71d4 207
pablo_bmxrp 0:44429c0a71d4 208 /*** Interface Methods ***/
pablo_bmxrp 0:44429c0a71d4 209 /*** High level API ***/
pablo_bmxrp 0:44429c0a71d4 210 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 211 /**
pablo_bmxrp 0:44429c0a71d4 212 * @brief initialize
pablo_bmxrp 0:44429c0a71d4 213 * @return void
pablo_bmxrp 0:44429c0a71d4 214 */
pablo_bmxrp 0:44429c0a71d4 215 /* turns on the sensor */
pablo_bmxrp 0:44429c0a71d4 216 void preparation(void)
pablo_bmxrp 0:44429c0a71d4 217 {
pablo_bmxrp 0:44429c0a71d4 218 _my_device.I2cDevAddr = VL53L0X_DEFAULT_ADDRESS;
pablo_bmxrp 0:44429c0a71d4 219 _my_device.comms_type = 1; // VL53L0X_COMMS_I2C
pablo_bmxrp 0:44429c0a71d4 220 _my_device.comms_speed_khz = 400;
pablo_bmxrp 0:44429c0a71d4 221 _device = &_my_device;
pablo_bmxrp 0:44429c0a71d4 222 _range_mode = range_long_distance_33ms_200cm;
pablo_bmxrp 0:44429c0a71d4 223 VL53L0X_off();
pablo_bmxrp 0:44429c0a71d4 224 }
pablo_bmxrp 0:44429c0a71d4 225 #endif
pablo_bmxrp 0:44429c0a71d4 226
pablo_bmxrp 0:44429c0a71d4 227 /**
pablo_bmxrp 0:44429c0a71d4 228 * @brief PowerOn the sensor
pablo_bmxrp 0:44429c0a71d4 229 * @return void
pablo_bmxrp 0:44429c0a71d4 230 */
pablo_bmxrp 0:44429c0a71d4 231 /* turns on the sensor */
pablo_bmxrp 0:44429c0a71d4 232 void VL53L0X_on(void)
pablo_bmxrp 0:44429c0a71d4 233 {
pablo_bmxrp 0:44429c0a71d4 234 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 235 if (_gpio0) {
pablo_bmxrp 0:44429c0a71d4 236 *_gpio0 = 1;
pablo_bmxrp 0:44429c0a71d4 237 } else {
pablo_bmxrp 0:44429c0a71d4 238 if (_expgpio0) {
pablo_bmxrp 0:44429c0a71d4 239 *_expgpio0 = 1;
pablo_bmxrp 0:44429c0a71d4 240 }
pablo_bmxrp 0:44429c0a71d4 241 }
pablo_bmxrp 0:44429c0a71d4 242 wait_ms(10);
pablo_bmxrp 0:44429c0a71d4 243 #else
pablo_bmxrp 0:44429c0a71d4 244 _gpio0.output();
pablo_bmxrp 0:44429c0a71d4 245 _gpio0 = 1;
pablo_bmxrp 0:44429c0a71d4 246 _gpio0.input();
pablo_bmxrp 0:44429c0a71d4 247 WAIT_MS(1);
pablo_bmxrp 0:44429c0a71d4 248 #endif
pablo_bmxrp 0:44429c0a71d4 249 }
pablo_bmxrp 0:44429c0a71d4 250
pablo_bmxrp 0:44429c0a71d4 251 /**
pablo_bmxrp 0:44429c0a71d4 252 * @brief PowerOff the sensor
pablo_bmxrp 0:44429c0a71d4 253 * @return void
pablo_bmxrp 0:44429c0a71d4 254 */
pablo_bmxrp 0:44429c0a71d4 255 /* turns off the sensor */
pablo_bmxrp 0:44429c0a71d4 256 void VL53L0X_off(void)
pablo_bmxrp 0:44429c0a71d4 257 {
pablo_bmxrp 0:44429c0a71d4 258 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 259 if (_gpio0) {
pablo_bmxrp 0:44429c0a71d4 260 *_gpio0 = 0;
pablo_bmxrp 0:44429c0a71d4 261 } else {
pablo_bmxrp 0:44429c0a71d4 262 if (_expgpio0) {
pablo_bmxrp 0:44429c0a71d4 263 *_expgpio0 = 0;
pablo_bmxrp 0:44429c0a71d4 264 }
pablo_bmxrp 0:44429c0a71d4 265 }
pablo_bmxrp 0:44429c0a71d4 266 wait_ms(10);
pablo_bmxrp 0:44429c0a71d4 267 #else
pablo_bmxrp 0:44429c0a71d4 268 _gpio0 = 0;
pablo_bmxrp 0:44429c0a71d4 269 _gpio0.output();
pablo_bmxrp 0:44429c0a71d4 270 WAIT_MS(10);
pablo_bmxrp 0:44429c0a71d4 271 #endif
pablo_bmxrp 0:44429c0a71d4 272 }
pablo_bmxrp 0:44429c0a71d4 273
pablo_bmxrp 0:44429c0a71d4 274 /**
pablo_bmxrp 0:44429c0a71d4 275 * @brief Initialize the sensor with default values
pablo_bmxrp 0:44429c0a71d4 276 * @param[in] new I2C address
pablo_bmxrp 0:44429c0a71d4 277 * @return "0" on success
pablo_bmxrp 0:44429c0a71d4 278 */
pablo_bmxrp 0:44429c0a71d4 279 int init_sensor(uint8_t new_addr);
pablo_bmxrp 0:44429c0a71d4 280
pablo_bmxrp 0:44429c0a71d4 281 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 282 /**
pablo_bmxrp 0:44429c0a71d4 283 * @brief Start the measure by single shot operating mode
pablo_bmxrp 0:44429c0a71d4 284 * @param[in] operating mode
pablo_bmxrp 0:44429c0a71d4 285 * @return "0" on success
pablo_bmxrp 0:44429c0a71d4 286 */
pablo_bmxrp 0:44429c0a71d4 287 int set_mode(RangeProfile range_mode) {
pablo_bmxrp 0:44429c0a71d4 288 _range_mode = range_mode;
pablo_bmxrp 0:44429c0a71d4 289 return start_measurement(range_single_shot_polling, NULL);
pablo_bmxrp 0:44429c0a71d4 290 }
pablo_bmxrp 0:44429c0a71d4 291
pablo_bmxrp 0:44429c0a71d4 292 /**
pablo_bmxrp 0:44429c0a71d4 293 * @brief Get ranging result and only that
pablo_bmxrp 0:44429c0a71d4 294 *
pablo_bmxrp 0:44429c0a71d4 295 * @par Function Description
pablo_bmxrp 0:44429c0a71d4 296 * Unlike @a VL53L0X_get_ranging_measurement_data()
pablo_bmxrp 0:44429c0a71d4 297 * this function only retrieves the range in millimeter \n
pablo_bmxrp 0:44429c0a71d4 298 * It does any required up-scale translation\n
pablo_bmxrp 0:44429c0a71d4 299 * It can be called after success status polling or in interrupt mode \n
pablo_bmxrp 0:44429c0a71d4 300 * @warning these function is not doing wrap around filtering \n
pablo_bmxrp 0:44429c0a71d4 301 * This function doesn't perform any data ready check!
pablo_bmxrp 0:44429c0a71d4 302 *
pablo_bmxrp 0:44429c0a71d4 303 * @param p_data Pointer to range distance
pablo_bmxrp 0:44429c0a71d4 304 * @return "0" on success
pablo_bmxrp 0:44429c0a71d4 305 */
pablo_bmxrp 0:44429c0a71d4 306 virtual int get_distance(uint32_t *p_data)
pablo_bmxrp 0:44429c0a71d4 307 {
pablo_bmxrp 0:44429c0a71d4 308 int status = 0;
pablo_bmxrp 0:44429c0a71d4 309 VL53L0X_RangingMeasurementData_t p_ranging_measurement_data;
pablo_bmxrp 0:44429c0a71d4 310
pablo_bmxrp 0:44429c0a71d4 311 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 312 status = start_measurement(range_single_shot_polling, NULL);
pablo_bmxrp 0:44429c0a71d4 313 #else
pablo_bmxrp 0:44429c0a71d4 314 status = VL53L0X_start_measurement(_device);
pablo_bmxrp 0:44429c0a71d4 315 #endif
pablo_bmxrp 0:44429c0a71d4 316 if (!status) {
pablo_bmxrp 0:44429c0a71d4 317 status = get_measurement(range_single_shot_polling,
pablo_bmxrp 0:44429c0a71d4 318 &p_ranging_measurement_data);
pablo_bmxrp 0:44429c0a71d4 319 }
pablo_bmxrp 0:44429c0a71d4 320 if (p_ranging_measurement_data.RangeStatus == 0) {
pablo_bmxrp 0:44429c0a71d4 321 // we have a valid range.
pablo_bmxrp 0:44429c0a71d4 322 *p_data = p_ranging_measurement_data.RangeMilliMeter;
pablo_bmxrp 0:44429c0a71d4 323 } else {
pablo_bmxrp 0:44429c0a71d4 324 *p_data = 0;
pablo_bmxrp 0:44429c0a71d4 325 status = VL53L0X_ERROR_RANGE_ERROR;
pablo_bmxrp 0:44429c0a71d4 326 }
pablo_bmxrp 0:44429c0a71d4 327 stop_measurement(range_single_shot_polling);
pablo_bmxrp 0:44429c0a71d4 328 return status;
pablo_bmxrp 0:44429c0a71d4 329 }
pablo_bmxrp 0:44429c0a71d4 330 #endif
pablo_bmxrp 0:44429c0a71d4 331
pablo_bmxrp 0:44429c0a71d4 332 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 333 //------- Feburary 4th, 2018 by JH1PJL / K.Arai --------------------------------
pablo_bmxrp 0:44429c0a71d4 334 // moved to following header file
pablo_bmxrp 0:44429c0a71d4 335 #include "VL53L0X_2nd.h"
pablo_bmxrp 0:44429c0a71d4 336 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 337 #else
pablo_bmxrp 0:44429c0a71d4 338 #warning "please recover original functions form VL53L0X_2nd.h"
pablo_bmxrp 0:44429c0a71d4 339 #endif
pablo_bmxrp 0:44429c0a71d4 340
pablo_bmxrp 0:44429c0a71d4 341 private:
pablo_bmxrp 0:44429c0a71d4 342
pablo_bmxrp 0:44429c0a71d4 343 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 344 //------- Feburary 4th, 2018 by JH1PJL / K.Arai --------------------------------
pablo_bmxrp 0:44429c0a71d4 345 // moved to following header file
pablo_bmxrp 0:44429c0a71d4 346 #include "VL53L0X_3rd.h"
pablo_bmxrp 0:44429c0a71d4 347 //------------------------------------------------------------------------------
pablo_bmxrp 0:44429c0a71d4 348 #else
pablo_bmxrp 0:44429c0a71d4 349 #warning "please recover original functions form VL53L0X_3rd.h"
pablo_bmxrp 0:44429c0a71d4 350 #endif
pablo_bmxrp 0:44429c0a71d4 351 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 352 static const unsigned int TEMP_BUF_SIZE = 32;
pablo_bmxrp 0:44429c0a71d4 353
pablo_bmxrp 0:44429c0a71d4 354 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
pablo_bmxrp 0:44429c0a71d4 355 uint16_t NumByteToWrite) {
pablo_bmxrp 0:44429c0a71d4 356 int ret;
pablo_bmxrp 0:44429c0a71d4 357 uint8_t tmp[TEMP_BUF_SIZE];
pablo_bmxrp 0:44429c0a71d4 358
pablo_bmxrp 0:44429c0a71d4 359 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
pablo_bmxrp 0:44429c0a71d4 360 /* First, send device address. Then, send data and STOP condition */
pablo_bmxrp 0:44429c0a71d4 361 tmp[0] = RegisterAddr;
pablo_bmxrp 0:44429c0a71d4 362 memcpy(tmp+1, pBuffer, NumByteToWrite);
pablo_bmxrp 0:44429c0a71d4 363 ret = _i2c.write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
pablo_bmxrp 0:44429c0a71d4 364 if(ret) return -1;
pablo_bmxrp 0:44429c0a71d4 365 return 0;
pablo_bmxrp 0:44429c0a71d4 366 }
pablo_bmxrp 0:44429c0a71d4 367
pablo_bmxrp 0:44429c0a71d4 368 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
pablo_bmxrp 0:44429c0a71d4 369 uint16_t NumByteToRead) {
pablo_bmxrp 0:44429c0a71d4 370 int ret;
pablo_bmxrp 0:44429c0a71d4 371
pablo_bmxrp 0:44429c0a71d4 372 //--debug--
pablo_bmxrp 0:44429c0a71d4 373 //myled = 1;
pablo_bmxrp 0:44429c0a71d4 374 /* Send device address, with no STOP condition */
pablo_bmxrp 0:44429c0a71d4 375 ret = _i2c.write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
pablo_bmxrp 0:44429c0a71d4 376 //--debug--
pablo_bmxrp 0:44429c0a71d4 377 //myled = 0;
pablo_bmxrp 0:44429c0a71d4 378 if(!ret) {
pablo_bmxrp 0:44429c0a71d4 379 /* Read data, with STOP condition */
pablo_bmxrp 0:44429c0a71d4 380 ret = _i2c.read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
pablo_bmxrp 0:44429c0a71d4 381 }
pablo_bmxrp 0:44429c0a71d4 382 if(ret) return -1;
pablo_bmxrp 0:44429c0a71d4 383 return 0;
pablo_bmxrp 0:44429c0a71d4 384 }
pablo_bmxrp 0:44429c0a71d4 385 #endif
pablo_bmxrp 0:44429c0a71d4 386
pablo_bmxrp 0:44429c0a71d4 387 VL53L0X_DeviceInfo_t _device_info;
pablo_bmxrp 0:44429c0a71d4 388
pablo_bmxrp 0:44429c0a71d4 389 #if !ORIGINAL
pablo_bmxrp 0:44429c0a71d4 390 RangeProfile _range_mode;
pablo_bmxrp 0:44429c0a71d4 391 #endif
pablo_bmxrp 0:44429c0a71d4 392
pablo_bmxrp 0:44429c0a71d4 393 #if ORIGINAL
pablo_bmxrp 0:44429c0a71d4 394 /* IO Device */
pablo_bmxrp 0:44429c0a71d4 395 DevI2C *_dev_i2c;
pablo_bmxrp 0:44429c0a71d4 396 /* Digital out pin */
pablo_bmxrp 0:44429c0a71d4 397 DigitalOut *_gpio0;
pablo_bmxrp 0:44429c0a71d4 398 /* GPIO expander */
pablo_bmxrp 0:44429c0a71d4 399 Stmpe1600DigiOut *_expgpio0;
pablo_bmxrp 0:44429c0a71d4 400 #else
pablo_bmxrp 0:44429c0a71d4 401 /* IO Device */
pablo_bmxrp 0:44429c0a71d4 402 I2C *_i2c_p;
pablo_bmxrp 0:44429c0a71d4 403 I2C &_i2c;
pablo_bmxrp 0:44429c0a71d4 404 /* Digital in and out pin */
pablo_bmxrp 0:44429c0a71d4 405 DigitalInOut _gpio0;
pablo_bmxrp 0:44429c0a71d4 406 #endif
pablo_bmxrp 0:44429c0a71d4 407 /* Measure detection IRQ */
pablo_bmxrp 0:44429c0a71d4 408 InterruptIn *_gpio1Int;
pablo_bmxrp 0:44429c0a71d4 409 /* Device data */
pablo_bmxrp 0:44429c0a71d4 410 VL53L0X_Dev_t _my_device;
pablo_bmxrp 0:44429c0a71d4 411 VL53L0X_DEV _device;
pablo_bmxrp 0:44429c0a71d4 412 };
pablo_bmxrp 0:44429c0a71d4 413
pablo_bmxrp 0:44429c0a71d4 414 #endif /* _VL53L0X_CLASS_H_ */