Vijayaraghavan Narayanan / VLX6180X_API
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vl6180x_high_level_api.cpp Source File

vl6180x_high_level_api.cpp

00001 /*******************************************************************************
00002 ################################################################################
00003 #                             (C) STMicroelectronics 2014
00004 #
00005 # This program is free software; you can redistribute it and/or modify it under
00006 # the terms of the GNU General Public License version 2 and only version 2 as
00007 # published by the Free Software Foundation.
00008 #
00009 # This program is distributed in the hope that it will be useful, but WITHOUT
00010 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00012 # details.
00013 #
00014 # You should have received a copy of the GNU General Public License along with
00015 # this program; if not, write to the Free Software Foundation, Inc.,
00016 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 #
00018 #------------------------------------------------------------------------------
00019 #                             Imaging Division
00020 ################################################################################
00021 ********************************************************************************/
00022 
00023 
00024 /**
00025  * @file high_level_api.cpp
00026  *
00027  * Copyright (C) 2014 ST MicroElectronics
00028  *
00029  * @brief High level interface definition for device, providing methods for basic
00030  * ranging.
00031  *
00032  */
00033 
00034 #include "vl6180x_high_level_api.h"
00035 #include "definitions.h"
00036 #include "common_driver.h"
00037 #include "ranging_driver.h"
00038 #include "als_driver.h"
00039 #include "range_upscaling_driver.h"
00040 #include "debug.h"
00041 #include "utilities.h"
00042 
00043 static uint8_t _device_i2c_addr = 0;
00044 
00045 sensor_error get_vendor(uint8_t *pVendorStr)
00046 {
00047     LOG_FUNCTION_START((void*)pVendorStr);
00048 
00049     LOG_FUNCTION_END(NULL);
00050     return SENSOR_ERROR_NONE;
00051 }
00052 
00053 sensor_error get_version(int32_t id, uint8_t *pVersionStr)
00054 {
00055     LOG_FUNCTION_START((void*)&id,(void*)pVersionStr);
00056 
00057     LOG_FUNCTION_END(NULL);
00058     return SENSOR_ERROR_NONE;
00059 }
00060 
00061 uint32_t get_max_range()
00062 {
00063     uint32_t maxRange = 0;
00064 
00065     maxRange = range_get_upper_limit(_device_i2c_addr);
00066 
00067     return maxRange;
00068 }
00069 
00070 uint32_t get_min_range()
00071 {
00072     return range_get_lower_limit(_device_i2c_addr);;
00073 }
00074 
00075 sensor_error initialise(uint8_t device_base_address)
00076 {
00077 
00078     LOG_FUNCTION_START((void*)&device_base_address);
00079 
00080     _device_i2c_addr = device_base_address;
00081     common_initialise(_device_i2c_addr);
00082 
00083     LOG_FUNCTION_END(NULL);
00084 
00085     return SENSOR_ERROR_NONE;
00086 }
00087 
00088 sensor_error start_ranging(int32_t id)
00089 {
00090     LOG_FUNCTION_START((void*)&id);
00091 
00092     // load settings
00093     common_set_static_config(_device_i2c_addr);
00094 
00095     // ranging setup
00096     range_set_low_threshold(_device_i2c_addr, 10); // threshold defaults are 0mm and 255mm
00097     range_set_high_threshold(_device_i2c_addr, 200);//200mm
00098     range_set_max_convergence_time(_device_i2c_addr, 50); // set max convergence time to 50ms;
00099 
00100     //set ranging in InterruptMode
00101     common_set_gpio1_polarity(_device_i2c_addr, GPIOx_POLARITY_SELECT_OFF);
00102     common_set_gpio1_select(_device_i2c_addr, GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT);
00103     common_set_gpio1_mode(_device_i2c_addr, GPIOx_MODE_SELECT_RANGING);
00104     range_set_system_interrupt_config_gpio(_device_i2c_addr, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
00105 
00106     //range_set_systemMode(_device_i2c_addr, RANGE_START_SINGLESHOT);
00107     LOG_FUNCTION_END(NULL);
00108 
00109     return SENSOR_ERROR_NONE;
00110 }
00111 
00112 sensor_error start_extended_ranging(int32_t id)
00113 {
00114     LOG_FUNCTION_START((void*)&id);
00115     // load settings onto
00116     er_set_static_config(_device_i2c_addr);
00117 
00118     // ranging setup
00119     // range_set_low_threshold(_device_i2c_addr, 10); // threshold defaults are 0mm and 255mm
00120     // range_set_high_threshold(_device_i2c_addr, 200);//200mm
00121     range_set_max_convergence_time(_device_i2c_addr, 63); // set max convergence time to 50ms;
00122     range_set_crosstalk_compensation_rate(_device_i2c_addr, 0);
00123 
00124     //set ranging in InterruptMode
00125     common_set_gpio1_polarity(_device_i2c_addr, GPIOx_POLARITY_SELECT_OFF);
00126     common_set_gpio1_select(_device_i2c_addr, GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT);
00127     common_set_gpio1_mode(_device_i2c_addr, GPIOx_MODE_SELECT_RANGING);
00128     range_set_system_interrupt_config_gpio(_device_i2c_addr, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
00129 
00130     LOG_FUNCTION_END(NULL);
00131 
00132     return SENSOR_ERROR_NONE;
00133 }
00134 
00135 sensor_error get_range_measurement(int32_t id, sensor_RangeData *pRangeData)
00136 {
00137     const int cErrorDataShift = 4;
00138 
00139     LOG_FUNCTION_START((void*)&id,(void*)pRangeData);
00140     // start single range measurement
00141     range_set_systemMode(_device_i2c_addr, RANGE_START_SINGLESHOT);
00142 
00143     // poll the till new sample ready
00144     while (range_get_result_interrupt_status_gpio(_device_i2c_addr) != RES_INT_STAT_GPIO_NEW_SAMPLE_READY)
00145     {
00146         timer_wait_us(100);
00147     }
00148 
00149     pRangeData->range_mm        = range_get_result(_device_i2c_addr);
00150     pRangeData->signalRate_mcps = decodeFrom9_7_Format(range_get_signal_rate(_device_i2c_addr));
00151     pRangeData->errorStatus     = (range_get_result_status(_device_i2c_addr)) >> cErrorDataShift;
00152 
00153     // clear the interrupt on
00154     range_set_system_interrupt_clear(_device_i2c_addr);
00155     LOG_FUNCTION_END(NULL);
00156 
00157     return SENSOR_ERROR_NONE;
00158 }
00159 
00160 sensor_error start_als(int32_t id)
00161 {
00162     LOG_FUNCTION_START((void*)&id);
00163     common_set_static_config(_device_i2c_addr);
00164 
00165     als_set_integration_period(_device_i2c_addr, 100); //100ms
00166     als_set_interMeasurement_period(_device_i2c_addr, 200); //200ms
00167     als_set_analogue_gain(_device_i2c_addr, 0);
00168 
00169     als_set_low_threshold(_device_i2c_addr, 0);
00170     als_set_high_threshold(_device_i2c_addr, 0x0FFFF);
00171 
00172     common_set_gpio1_polarity(_device_i2c_addr, GPIOx_POLARITY_SELECT_OFF);
00173     common_set_gpio1_select(_device_i2c_addr, GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT);
00174     common_set_gpio1_mode(_device_i2c_addr, GPIOx_MODE_SELECT_ALS);
00175     als_set_system_interrupt_config_gpio(_device_i2c_addr, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY);
00176 
00177 //    als_set_systemMode(_device_i2c_addr, ALS_START_SINGLESHOT);
00178     LOG_FUNCTION_END(NULL);
00179     
00180     return SENSOR_ERROR_NONE;
00181 }
00182 
00183 sensor_error get_als_measurement(int32_t id, sensor_AlsData *pAlsData)
00184 {
00185     const int cErrorDataShift = 4;
00186     
00187     LOG_FUNCTION_START((void*)&id,(void*)pAlsData);
00188     als_set_systemMode(_device_i2c_addr, ALS_START_SINGLESHOT);
00189 
00190     while (als_get_result_interrupt_status_gpio(_device_i2c_addr) != RES_INT_STAT_GPIO_NEW_SAMPLE_READY)
00191     {
00192         timer_wait_us(100);
00193     }
00194     pAlsData->lux         = als_get_lux(_device_i2c_addr);
00195     pAlsData->errorStatus = (als_get_result_status(_device_i2c_addr)) >> cErrorDataShift;
00196 
00197     als_set_system_interrupt_clear(_device_i2c_addr);
00198     LOG_FUNCTION_END(NULL);
00199         
00200     return SENSOR_ERROR_NONE;
00201 }
00202 
00203 sensor_error get_minimum_delay()
00204 {
00205 
00206     return SENSOR_ERROR_NONE;
00207 }
00208