Emilio Salomone / X_NUCLEO_IKS01A2

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_IKS01A2 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM6DSLSensor.cpp Source File

LSM6DSLSensor.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    LSM6DSLSensor.cpp
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    5 August 2016
00007  * @brief   Implementation of an LSM6DSL Inertial Measurement Unit (IMU) 6 axes
00008  *          sensor.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 #include "mbed.h"
00043 #include "DevI2C.h"
00044 #include "LSM6DSLSensor.h"
00045 #include "LSM6DSL_acc_gyro_driver.h"
00046 
00047 
00048 /* Class Implementation ------------------------------------------------------*/
00049 
00050 /** Constructor
00051  * @param i2c object of an helper class which handles the I2C peripheral
00052  * @param address the address of the component's instance
00053  */
00054 LSM6DSLSensor::LSM6DSLSensor(DevI2C &i2c, PinName int1_pin, PinName int2_pin) : _dev_i2c(i2c), _int1_irq(int1_pin), _int2_irq(int2_pin)
00055 {
00056   _address = LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH; 
00057 };
00058 
00059 /** Constructor
00060  * @param i2c object of an helper class which handles the I2C peripheral
00061  * @param address the address of the component's instance
00062  */
00063 LSM6DSLSensor::LSM6DSLSensor(DevI2C &i2c, PinName int1_pin, PinName int2_pin, uint8_t address) : _dev_i2c(i2c), _int1_irq(int1_pin), _int2_irq(int2_pin), _address(address)
00064 {
00065 
00066 };
00067 
00068 /**
00069  * @brief     Initializing the component.
00070  * @param[in] init pointer to device specific initalization structure.
00071  * @retval    "0" in case of success, an error code otherwise.
00072  */
00073 int LSM6DSLSensor::init(void *init)
00074 {
00075   /* Enable register address automatically incremented during a multiple byte
00076      access with a serial interface. */
00077   if ( LSM6DSL_ACC_GYRO_W_IF_Addr_Incr( (void *)this, LSM6DSL_ACC_GYRO_IF_INC_ENABLED ) == MEMS_ERROR )
00078   {
00079     return 1;
00080   }
00081   
00082   /* Enable BDU */
00083   if ( LSM6DSL_ACC_GYRO_W_BDU( (void *)this, LSM6DSL_ACC_GYRO_BDU_BLOCK_UPDATE ) == MEMS_ERROR )
00084   {
00085     return 1;
00086   }
00087   
00088   /* FIFO mode selection */
00089   if ( LSM6DSL_ACC_GYRO_W_FIFO_MODE( (void *)this, LSM6DSL_ACC_GYRO_FIFO_MODE_BYPASS ) == MEMS_ERROR )
00090   {
00091     return 1;
00092   }
00093   
00094   /* Output data rate selection - power down. */
00095   if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
00096   {
00097     return 1;
00098   }
00099   
00100   /* Full scale selection. */
00101   if ( set_x_fs( 2.0f ) == 1 )
00102   {
00103     return 1;
00104   }
00105 
00106   /* Output data rate selection - power down */
00107   if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
00108   {
00109     return 1;
00110   }
00111 
00112   /* Full scale selection. */
00113   if ( set_g_fs( 2000.0f ) == 1 )
00114   {
00115     return 1;
00116   }
00117   
00118   _x_last_odr = 104.0f;
00119 
00120   _x_is_enabled = 0;
00121   
00122   _g_last_odr = 104.0f;
00123 
00124   _g_is_enabled = 0;
00125   
00126   return 0;
00127 }
00128 
00129 /**
00130  * @brief  Enable LSM6DSL Accelerator
00131  * @retval 0 in case of success, an error code otherwise
00132  */
00133 int LSM6DSLSensor::enable_x(void)
00134 { 
00135   /* Check if the component is already enabled */
00136   if ( _x_is_enabled == 1 )
00137   {
00138     return 0;
00139   }
00140   
00141   /* Output data rate selection. */
00142   if ( set_x_odr_when_enabled( _x_last_odr ) == 1 )
00143   {
00144     return 1;
00145   }
00146   
00147   _x_is_enabled = 1;
00148   
00149   return 0;
00150 }
00151 
00152 /**
00153  * @brief  Enable LSM6DSL Gyroscope
00154  * @retval 0 in case of success, an error code otherwise
00155  */
00156 int LSM6DSLSensor::enable_g(void)
00157 { 
00158   /* Check if the component is already enabled */
00159   if ( _g_is_enabled == 1 )
00160   {
00161     return 0;
00162   }
00163   
00164   /* Output data rate selection. */
00165   if ( set_g_odr_when_enabled( _g_last_odr ) == 1 )
00166   {
00167     return 1;
00168   }
00169   
00170   _g_is_enabled = 1;
00171   
00172   return 0;
00173 }
00174 
00175 /**
00176  * @brief  Disable LSM6DSL Accelerator
00177  * @retval 0 in case of success, an error code otherwise
00178  */
00179 int LSM6DSLSensor::disable_x(void)
00180 { 
00181   /* Check if the component is already disabled */
00182   if ( _x_is_enabled == 0 )
00183   {
00184     return 0;
00185   }
00186   
00187   /* Store actual output data rate. */
00188   if ( get_x_odr( &_x_last_odr ) == 1 )
00189   {
00190     return 1;
00191   }
00192   
00193   /* Output data rate selection - power down. */
00194   if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
00195   {
00196     return 1;
00197   }
00198   
00199   _x_is_enabled = 0;
00200   
00201   return 0;
00202 }
00203 
00204 /**
00205  * @brief  Disable LSM6DSL Gyroscope
00206  * @retval 0 in case of success, an error code otherwise
00207  */
00208 int LSM6DSLSensor::disable_g(void)
00209 { 
00210   /* Check if the component is already disabled */
00211   if ( _g_is_enabled == 0 )
00212   {
00213     return 0;
00214   }
00215   
00216   /* Store actual output data rate. */
00217   if ( get_g_odr( &_g_last_odr ) == 1 )
00218   {
00219     return 1;
00220   }
00221   
00222   /* Output data rate selection - power down */
00223   if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
00224   {
00225     return 1;
00226   }
00227   
00228   _g_is_enabled = 0;
00229   
00230   return 0;
00231 }
00232 
00233 /**
00234  * @brief  Read ID of LSM6DSL Accelerometer and Gyroscope
00235  * @param  p_id the pointer where the ID of the device is stored
00236  * @retval 0 in case of success, an error code otherwise
00237  */
00238 int LSM6DSLSensor::read_id(uint8_t *id)
00239 {
00240   if(!id)
00241   { 
00242     return 1;
00243   }
00244 
00245   /* Read WHO AM I register */
00246   if ( LSM6DSL_ACC_GYRO_R_WHO_AM_I( (void *)this, id ) == MEMS_ERROR )
00247   {
00248     return 1;
00249   }
00250   
00251   return 0;
00252 }
00253 
00254 /**
00255  * @brief  Read data from LSM6DSL Accelerometer
00256  * @param  pData the pointer where the accelerometer data are stored
00257  * @retval 0 in case of success, an error code otherwise
00258  */
00259 int LSM6DSLSensor::get_x_axes(int32_t *pData)
00260 {
00261   int16_t dataRaw[3];
00262   float sensitivity = 0;
00263   
00264   /* Read raw data from LSM6DSL output register. */
00265   if ( get_x_axes_raw( dataRaw ) == 1 )
00266   {
00267     return 1;
00268   }
00269   
00270   /* Get LSM6DSL actual sensitivity. */
00271   if ( get_x_sensitivity( &sensitivity ) == 1 )
00272   {
00273     return 1;
00274   }
00275   
00276   /* Calculate the data. */
00277   pData[0] = ( int32_t )( dataRaw[0] * sensitivity );
00278   pData[1] = ( int32_t )( dataRaw[1] * sensitivity );
00279   pData[2] = ( int32_t )( dataRaw[2] * sensitivity );
00280   
00281   return 0;
00282 }
00283 
00284 /**
00285  * @brief  Read data from LSM6DSL Gyroscope
00286  * @param  pData the pointer where the gyroscope data are stored
00287  * @retval 0 in case of success, an error code otherwise
00288  */
00289 int LSM6DSLSensor::get_g_axes(int32_t *pData)
00290 {
00291   int16_t dataRaw[3];
00292   float sensitivity = 0;
00293   
00294   /* Read raw data from LSM6DSL output register. */
00295   if ( get_g_axes_raw( dataRaw ) == 1 )
00296   {
00297     return 1;
00298   }
00299   
00300   /* Get LSM6DSL actual sensitivity. */
00301   if ( get_g_sensitivity( &sensitivity ) == 1 )
00302   {
00303     return 1;
00304   }
00305   
00306   /* Calculate the data. */
00307   pData[0] = ( int32_t )( dataRaw[0] * sensitivity );
00308   pData[1] = ( int32_t )( dataRaw[1] * sensitivity );
00309   pData[2] = ( int32_t )( dataRaw[2] * sensitivity );
00310   
00311   return 0;
00312 }
00313 
00314 /**
00315  * @brief  Read Accelerometer Sensitivity
00316  * @param  pfData the pointer where the accelerometer sensitivity is stored
00317  * @retval 0 in case of success, an error code otherwise
00318  */
00319 int LSM6DSLSensor::get_x_sensitivity(float *pfData)
00320 {
00321   LSM6DSL_ACC_GYRO_FS_XL_t fullScale;
00322   
00323   /* Read actual full scale selection from sensor. */
00324   if ( LSM6DSL_ACC_GYRO_R_FS_XL( (void *)this, &fullScale ) == MEMS_ERROR )
00325   {
00326     return 1;
00327   }
00328   
00329   /* Store the sensitivity based on actual full scale. */
00330   switch( fullScale )
00331   {
00332     case LSM6DSL_ACC_GYRO_FS_XL_2g:
00333       *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_2G;
00334       break;
00335     case LSM6DSL_ACC_GYRO_FS_XL_4g:
00336       *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_4G;
00337       break;
00338     case LSM6DSL_ACC_GYRO_FS_XL_8g:
00339       *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_8G;
00340       break;
00341     case LSM6DSL_ACC_GYRO_FS_XL_16g:
00342       *pfData = ( float )LSM6DSL_ACC_SENSITIVITY_FOR_FS_16G;
00343       break;
00344     default:
00345       *pfData = -1.0f;
00346       return 1;
00347   }
00348   
00349   return 0;
00350 }
00351 
00352 /**
00353  * @brief  Read Gyroscope Sensitivity
00354  * @param  pfData the pointer where the gyroscope sensitivity is stored
00355  * @retval 0 in case of success, an error code otherwise
00356  */
00357 int LSM6DSLSensor::get_g_sensitivity(float *pfData)
00358 {
00359   LSM6DSL_ACC_GYRO_FS_125_t fullScale125;
00360   LSM6DSL_ACC_GYRO_FS_G_t   fullScale;
00361   
00362   /* Read full scale 125 selection from sensor. */
00363   if ( LSM6DSL_ACC_GYRO_R_FS_125( (void *)this, &fullScale125 ) == MEMS_ERROR )
00364   {
00365     return 1;
00366   }
00367   
00368   if ( fullScale125 == LSM6DSL_ACC_GYRO_FS_125_ENABLED )
00369   {
00370     *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_125DPS;
00371   }
00372   
00373   else
00374   {
00375   
00376     /* Read actual full scale selection from sensor. */
00377     if ( LSM6DSL_ACC_GYRO_R_FS_G( (void *)this, &fullScale ) == MEMS_ERROR )
00378     {
00379       return 1;
00380     }
00381     
00382     /* Store the sensitivity based on actual full scale. */
00383     switch( fullScale )
00384     {
00385       case LSM6DSL_ACC_GYRO_FS_G_245dps:
00386         *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_245DPS;
00387         break;
00388       case LSM6DSL_ACC_GYRO_FS_G_500dps:
00389         *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_500DPS;
00390         break;
00391       case LSM6DSL_ACC_GYRO_FS_G_1000dps:
00392         *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_1000DPS;
00393         break;
00394       case LSM6DSL_ACC_GYRO_FS_G_2000dps:
00395         *pfData = ( float )LSM6DSL_GYRO_SENSITIVITY_FOR_FS_2000DPS;
00396         break;
00397       default:
00398         *pfData = -1.0f;
00399         return 1;
00400     }
00401   }
00402   
00403   return 0;
00404 }
00405 
00406 /**
00407  * @brief  Read raw data from LSM6DSL Accelerometer
00408  * @param  pData the pointer where the accelerometer raw data are stored
00409  * @retval 0 in case of success, an error code otherwise
00410  */
00411 int LSM6DSLSensor::get_x_axes_raw(int16_t *pData)
00412 {
00413   uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
00414   
00415   /* Read output registers from LSM6DSL_ACC_GYRO_OUTX_L_XL to LSM6DSL_ACC_GYRO_OUTZ_H_XL. */
00416   if ( LSM6DSL_ACC_GYRO_GetRawAccData( (void *)this, regValue ) == MEMS_ERROR )
00417   {
00418     return 1;
00419   }
00420   
00421   /* Format the data. */
00422   pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] );
00423   pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] );
00424   pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] );
00425   
00426   return 0;
00427 }
00428 
00429 /**
00430  * @brief  Read raw data from LSM6DSL Gyroscope
00431  * @param  pData the pointer where the gyroscope raw data are stored
00432  * @retval 0 in case of success, an error code otherwise
00433  */
00434 int LSM6DSLSensor::get_g_axes_raw(int16_t *pData)
00435 {
00436   uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
00437   
00438   /* Read output registers from LSM6DSL_ACC_GYRO_OUTX_L_G to LSM6DSL_ACC_GYRO_OUTZ_H_G. */
00439   if ( LSM6DSL_ACC_GYRO_GetRawGyroData( (void *)this, regValue ) == MEMS_ERROR )
00440   {
00441     return 1;
00442   }
00443   
00444   /* Format the data. */
00445   pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] );
00446   pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] );
00447   pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] );
00448   
00449   return 0;
00450 }
00451 
00452 /**
00453  * @brief  Read LSM6DSL Accelerometer output data rate
00454  * @param  odr the pointer to the output data rate
00455  * @retval 0 in case of success, an error code otherwise
00456  */
00457 int LSM6DSLSensor::get_x_odr(float* odr)
00458 {
00459   LSM6DSL_ACC_GYRO_ODR_XL_t odr_low_level;
00460   
00461   if ( LSM6DSL_ACC_GYRO_R_ODR_XL( (void *)this, &odr_low_level ) == MEMS_ERROR )
00462   {
00463     return 1;
00464   }
00465   
00466   switch( odr_low_level )
00467   {
00468     case LSM6DSL_ACC_GYRO_ODR_XL_POWER_DOWN:
00469       *odr = 0.0f;
00470       break;
00471     case LSM6DSL_ACC_GYRO_ODR_XL_13Hz:
00472       *odr = 13.0f;
00473       break;
00474     case LSM6DSL_ACC_GYRO_ODR_XL_26Hz:
00475       *odr = 26.0f;
00476       break;
00477     case LSM6DSL_ACC_GYRO_ODR_XL_52Hz:
00478       *odr = 52.0f;
00479       break;
00480     case LSM6DSL_ACC_GYRO_ODR_XL_104Hz:
00481       *odr = 104.0f;
00482       break;
00483     case LSM6DSL_ACC_GYRO_ODR_XL_208Hz:
00484       *odr = 208.0f;
00485       break;
00486     case LSM6DSL_ACC_GYRO_ODR_XL_416Hz:
00487       *odr = 416.0f;
00488       break;
00489     case LSM6DSL_ACC_GYRO_ODR_XL_833Hz:
00490       *odr = 833.0f;
00491       break;
00492     case LSM6DSL_ACC_GYRO_ODR_XL_1660Hz:
00493       *odr = 1660.0f;
00494       break;
00495     case LSM6DSL_ACC_GYRO_ODR_XL_3330Hz:
00496       *odr = 3330.0f;
00497       break;
00498     case LSM6DSL_ACC_GYRO_ODR_XL_6660Hz:
00499       *odr = 6660.0f;
00500       break;
00501     default:
00502       *odr = -1.0f;
00503       return 1;
00504   }
00505   
00506   return 0;
00507 }
00508 
00509 /**
00510  * @brief  Read LSM6DSL Gyroscope output data rate
00511  * @param  odr the pointer to the output data rate
00512  * @retval 0 in case of success, an error code otherwise
00513  */
00514 int LSM6DSLSensor::get_g_odr(float* odr)
00515 {
00516   LSM6DSL_ACC_GYRO_ODR_G_t odr_low_level;
00517   
00518   if ( LSM6DSL_ACC_GYRO_R_ODR_G( (void *)this, &odr_low_level ) == MEMS_ERROR )
00519   {
00520     return 1;
00521   }
00522   
00523   switch( odr_low_level )
00524   {
00525     case LSM6DSL_ACC_GYRO_ODR_G_POWER_DOWN:
00526       *odr = 0.0f;
00527       break;
00528     case LSM6DSL_ACC_GYRO_ODR_G_13Hz:
00529       *odr = 13.0f;
00530       break;
00531     case LSM6DSL_ACC_GYRO_ODR_G_26Hz:
00532       *odr = 26.0f;
00533       break;
00534     case LSM6DSL_ACC_GYRO_ODR_G_52Hz:
00535       *odr = 52.0f;
00536       break;
00537     case LSM6DSL_ACC_GYRO_ODR_G_104Hz:
00538       *odr = 104.0f;
00539       break;
00540     case LSM6DSL_ACC_GYRO_ODR_G_208Hz:
00541       *odr = 208.0f;
00542       break;
00543     case LSM6DSL_ACC_GYRO_ODR_G_416Hz:
00544       *odr = 416.0f;
00545       break;
00546     case LSM6DSL_ACC_GYRO_ODR_G_833Hz:
00547       *odr = 833.0f;
00548       break;
00549     case LSM6DSL_ACC_GYRO_ODR_G_1660Hz:
00550       *odr = 1660.0f;
00551       break;
00552     case LSM6DSL_ACC_GYRO_ODR_G_3330Hz:
00553       *odr = 3330.0f;
00554       break;
00555     case LSM6DSL_ACC_GYRO_ODR_G_6660Hz:
00556       *odr = 6660.0f;
00557       break;
00558     default:
00559       *odr = -1.0f;
00560       return 1;
00561   }
00562   
00563   return 0;
00564 }
00565 
00566 /**
00567  * @brief  Set LSM6DSL Accelerometer output data rate
00568  * @param  odr the output data rate to be set
00569  * @retval 0 in case of success, an error code otherwise
00570  */
00571 int LSM6DSLSensor::set_x_odr(float odr)
00572 {
00573   if(_x_is_enabled == 1)
00574   {
00575     if(set_x_odr_when_enabled(odr) == 1)
00576     {
00577       return 1;
00578     }
00579   }
00580   else
00581   {
00582     if(set_x_odr_when_disabled(odr) == 1)
00583     {
00584       return 1;
00585     }
00586   }
00587   
00588   return 0;
00589 }
00590 
00591 /**
00592  * @brief  Set LSM6DSL Accelerometer output data rate when enabled
00593  * @param  odr the output data rate to be set
00594  * @retval 0 in case of success, an error code otherwise
00595  */
00596 int LSM6DSLSensor::set_x_odr_when_enabled(float odr)
00597 {
00598   LSM6DSL_ACC_GYRO_ODR_XL_t new_odr;
00599   
00600   new_odr = ( odr <=   13.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_13Hz
00601           : ( odr <=   26.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_26Hz
00602           : ( odr <=   52.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_52Hz
00603           : ( odr <=  104.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_104Hz
00604           : ( odr <=  208.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_208Hz
00605           : ( odr <=  416.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_416Hz
00606           : ( odr <=  833.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_833Hz
00607           : ( odr <= 1660.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_1660Hz
00608           : ( odr <= 3330.0f ) ? LSM6DSL_ACC_GYRO_ODR_XL_3330Hz
00609           :                      LSM6DSL_ACC_GYRO_ODR_XL_6660Hz;
00610             
00611   if ( LSM6DSL_ACC_GYRO_W_ODR_XL( (void *)this, new_odr ) == MEMS_ERROR )
00612   {
00613     return 1;
00614   }
00615   
00616   return 0;
00617 }
00618 
00619 /**
00620  * @brief  Set LSM6DSL Accelerometer output data rate when disabled
00621  * @param  odr the output data rate to be set
00622  * @retval 0 in case of success, an error code otherwise
00623  */
00624 int LSM6DSLSensor::set_x_odr_when_disabled(float odr)
00625 { 
00626   _x_last_odr = ( odr <=   13.0f ) ? 13.0f
00627              : ( odr <=   26.0f ) ? 26.0f
00628              : ( odr <=   52.0f ) ? 52.0f
00629              : ( odr <=  104.0f ) ? 104.0f
00630              : ( odr <=  208.0f ) ? 208.0f
00631              : ( odr <=  416.0f ) ? 416.0f
00632              : ( odr <=  833.0f ) ? 833.0f
00633              : ( odr <= 1660.0f ) ? 1660.0f
00634              : ( odr <= 3330.0f ) ? 3330.0f
00635              :                      6660.0f;
00636                                  
00637   return 0;
00638 }
00639 
00640 /**
00641  * @brief  Set LSM6DSL Gyroscope output data rate
00642  * @param  odr the output data rate to be set
00643  * @retval 0 in case of success, an error code otherwise
00644  */
00645 int LSM6DSLSensor::set_g_odr(float odr)
00646 {
00647   if(_g_is_enabled == 1)
00648   {
00649     if(set_g_odr_when_enabled(odr) == 1)
00650     {
00651       return 1;
00652     }
00653   }
00654   else
00655   {
00656     if(set_g_odr_when_disabled(odr) == 1)
00657     {
00658       return 1;
00659     }
00660   }
00661   
00662   return 0;
00663 }
00664 
00665 /**
00666  * @brief  Set LSM6DSL Gyroscope output data rate when enabled
00667  * @param  odr the output data rate to be set
00668  * @retval 0 in case of success, an error code otherwise
00669  */
00670 int LSM6DSLSensor::set_g_odr_when_enabled(float odr)
00671 {
00672   LSM6DSL_ACC_GYRO_ODR_G_t new_odr;
00673   
00674   new_odr = ( odr <=  13.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_13Hz
00675           : ( odr <=  26.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_26Hz
00676           : ( odr <=  52.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_52Hz
00677           : ( odr <= 104.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_104Hz
00678           : ( odr <= 208.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_208Hz
00679           : ( odr <= 416.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_416Hz
00680           : ( odr <= 833.0f )  ? LSM6DSL_ACC_GYRO_ODR_G_833Hz
00681           : ( odr <= 1660.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_1660Hz
00682           : ( odr <= 3330.0f ) ? LSM6DSL_ACC_GYRO_ODR_G_3330Hz
00683           :                      LSM6DSL_ACC_GYRO_ODR_G_6660Hz;
00684             
00685   if ( LSM6DSL_ACC_GYRO_W_ODR_G( (void *)this, new_odr ) == MEMS_ERROR )
00686   {
00687     return 1;
00688   }
00689   
00690   return 0;
00691 }
00692 
00693 /**
00694  * @brief  Set LSM6DSL Gyroscope output data rate when disabled
00695  * @param  odr the output data rate to be set
00696  * @retval 0 in case of success, an error code otherwise
00697  */
00698 int LSM6DSLSensor::set_g_odr_when_disabled(float odr)
00699 {
00700   _g_last_odr = ( odr <=  13.0f )  ? 13.0f
00701              : ( odr <=  26.0f )  ? 26.0f
00702              : ( odr <=  52.0f )  ? 52.0f
00703              : ( odr <= 104.0f )  ? 104.0f
00704              : ( odr <= 208.0f )  ? 208.0f
00705              : ( odr <= 416.0f )  ? 416.0f
00706              : ( odr <= 833.0f )  ? 833.0f
00707              : ( odr <= 1660.0f ) ? 1660.0f
00708              : ( odr <= 3330.0f ) ? 3330.0f
00709              :                      6660.0f;
00710                                  
00711   return 0;
00712 }
00713 
00714 /**
00715  * @brief  Read LSM6DSL Accelerometer full scale
00716  * @param  fullScale the pointer to the full scale
00717  * @retval 0 in case of success, an error code otherwise
00718  */
00719 int LSM6DSLSensor::get_x_fs(float* fullScale)
00720 {
00721   LSM6DSL_ACC_GYRO_FS_XL_t fs_low_level;
00722   
00723   if ( LSM6DSL_ACC_GYRO_R_FS_XL( (void *)this, &fs_low_level ) == MEMS_ERROR )
00724   {
00725     return 1;
00726   }
00727   
00728   switch( fs_low_level )
00729   {
00730     case LSM6DSL_ACC_GYRO_FS_XL_2g:
00731       *fullScale = 2.0f;
00732       break;
00733     case LSM6DSL_ACC_GYRO_FS_XL_4g:
00734       *fullScale = 4.0f;
00735       break;
00736     case LSM6DSL_ACC_GYRO_FS_XL_8g:
00737       *fullScale = 8.0f;
00738       break;
00739     case LSM6DSL_ACC_GYRO_FS_XL_16g:
00740       *fullScale = 16.0f;
00741       break;
00742     default:
00743       *fullScale = -1.0f;
00744       return 1;
00745   }
00746   
00747   return 0;
00748 }
00749 
00750 /**
00751  * @brief  Read LSM6DSL Gyroscope full scale
00752  * @param  fullScale the pointer to the full scale
00753  * @retval 0 in case of success, an error code otherwise
00754  */
00755 int LSM6DSLSensor::get_g_fs(float* fullScale)
00756 {
00757   LSM6DSL_ACC_GYRO_FS_G_t fs_low_level;
00758   LSM6DSL_ACC_GYRO_FS_125_t fs_125;
00759   
00760   if ( LSM6DSL_ACC_GYRO_R_FS_125( (void *)this, &fs_125 ) == MEMS_ERROR )
00761   {
00762     return 1;
00763   }
00764   if ( LSM6DSL_ACC_GYRO_R_FS_G( (void *)this, &fs_low_level ) == MEMS_ERROR )
00765   {
00766     return 1;
00767   }
00768   
00769   if ( fs_125 == LSM6DSL_ACC_GYRO_FS_125_ENABLED )
00770   {
00771     *fullScale = 125.0f;
00772   }
00773   
00774   else
00775   {
00776     switch( fs_low_level )
00777     {
00778       case LSM6DSL_ACC_GYRO_FS_G_245dps:
00779         *fullScale = 245.0f;
00780         break;
00781       case LSM6DSL_ACC_GYRO_FS_G_500dps:
00782         *fullScale = 500.0f;
00783         break;
00784       case LSM6DSL_ACC_GYRO_FS_G_1000dps:
00785         *fullScale = 1000.0f;
00786         break;
00787       case LSM6DSL_ACC_GYRO_FS_G_2000dps:
00788         *fullScale = 2000.0f;
00789         break;
00790       default:
00791         *fullScale = -1.0f;
00792         return 1;
00793     }
00794   }
00795   
00796   return 0;
00797 }
00798 
00799 /**
00800  * @brief  Set LSM6DSL Accelerometer full scale
00801  * @param  fullScale the full scale to be set
00802  * @retval 0 in case of success, an error code otherwise
00803  */
00804 int LSM6DSLSensor::set_x_fs(float fullScale)
00805 {
00806   LSM6DSL_ACC_GYRO_FS_XL_t new_fs;
00807   
00808   new_fs = ( fullScale <= 2.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_2g
00809          : ( fullScale <= 4.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_4g
00810          : ( fullScale <= 8.0f ) ? LSM6DSL_ACC_GYRO_FS_XL_8g
00811          :                         LSM6DSL_ACC_GYRO_FS_XL_16g;
00812            
00813   if ( LSM6DSL_ACC_GYRO_W_FS_XL( (void *)this, new_fs ) == MEMS_ERROR )
00814   {
00815     return 1;
00816   }
00817   
00818   return 0;
00819 }
00820 
00821 /**
00822  * @brief  Set LSM6DSL Gyroscope full scale
00823  * @param  fullScale the full scale to be set
00824  * @retval 0 in case of success, an error code otherwise
00825  */
00826 int LSM6DSLSensor::set_g_fs(float fullScale)
00827 {
00828   LSM6DSL_ACC_GYRO_FS_G_t new_fs;
00829   
00830   if ( fullScale <= 125.0f )
00831   {
00832     if ( LSM6DSL_ACC_GYRO_W_FS_125( (void *)this, LSM6DSL_ACC_GYRO_FS_125_ENABLED ) == MEMS_ERROR )
00833     {
00834       return 1;
00835     }
00836   }
00837   else
00838   {
00839     new_fs = ( fullScale <=  245.0f ) ? LSM6DSL_ACC_GYRO_FS_G_245dps
00840            : ( fullScale <=  500.0f ) ? LSM6DSL_ACC_GYRO_FS_G_500dps
00841            : ( fullScale <= 1000.0f ) ? LSM6DSL_ACC_GYRO_FS_G_1000dps
00842            :                            LSM6DSL_ACC_GYRO_FS_G_2000dps;
00843              
00844     if ( LSM6DSL_ACC_GYRO_W_FS_125( (void *)this, LSM6DSL_ACC_GYRO_FS_125_DISABLED ) == MEMS_ERROR )
00845     {
00846       return 1;
00847     }
00848     if ( LSM6DSL_ACC_GYRO_W_FS_G( (void *)this, new_fs ) == MEMS_ERROR )
00849     {
00850       return 1;
00851     }
00852   }
00853   
00854   return 0;
00855 }
00856 
00857 /**
00858  * @brief  Enable free fall detection
00859  * @param pin the interrupt pin to be used
00860  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
00861  * @retval 0 in case of success, an error code otherwise
00862 */
00863 int LSM6DSLSensor::enable_free_fall_detection(LSM6DSL_Interrupt_Pin_t pin)
00864 {
00865   /* Output Data Rate selection */
00866   if(set_x_odr(416.0f) == 1)
00867   {
00868     return 1;
00869   }
00870   
00871   /* Full scale selection */
00872   if ( LSM6DSL_ACC_GYRO_W_FS_XL( (void *)this, LSM6DSL_ACC_GYRO_FS_XL_2g ) == MEMS_ERROR )
00873   {
00874     return 1;
00875   }
00876   
00877   /* FF_DUR setting */
00878   if ( LSM6DSL_ACC_GYRO_W_FF_Duration( (void *)this, 0x06 ) == MEMS_ERROR )
00879   {
00880     return 1;
00881   }
00882   
00883   /* WAKE_DUR setting */
00884   if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
00885   {
00886     return 1;
00887   }
00888   
00889   /* TIMER_HR setting */
00890   if ( LSM6DSL_ACC_GYRO_W_TIMER_HR( (void *)this, LSM6DSL_ACC_GYRO_TIMER_HR_6_4ms ) == MEMS_ERROR )
00891   {
00892     return 1;
00893   }
00894   
00895   /* SLEEP_DUR setting */
00896   if ( LSM6DSL_ACC_GYRO_W_SLEEP_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
00897   {
00898     return 1;
00899   }
00900   
00901   /* FF_THS setting */
00902   if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, LSM6DSL_ACC_GYRO_FF_THS_312mg ) == MEMS_ERROR )
00903   {
00904     return 1;
00905   }
00906   
00907   /* Enable basic Interrupts */
00908   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR )
00909   {
00910     return 1;
00911   }
00912   
00913   /* Enable free fall event on either INT1 or INT2 pin */
00914   switch (pin)
00915   {
00916   case LSM6DSL_INT1_PIN:
00917     if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_FF_ENABLED ) == MEMS_ERROR )
00918     {
00919       return 1;
00920     }
00921     break;
00922 
00923   case LSM6DSL_INT2_PIN:
00924     if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_FF_ENABLED ) == MEMS_ERROR )
00925     {
00926       return 1;
00927     }
00928     break;
00929 
00930   default:
00931     return 1;
00932   }
00933   
00934   return 0;
00935 }
00936 
00937 /**
00938  * @brief  Disable free fall detection
00939  * @param  None
00940  * @retval 0 in case of success, an error code otherwise
00941 */
00942 int LSM6DSLSensor::disable_free_fall_detection(void)
00943 {
00944   /* Disable free fall event on INT1 pin */
00945   if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_FF_DISABLED ) == MEMS_ERROR )
00946   {
00947     return 1;
00948   }
00949   
00950   /* Disable free fall event on INT2 pin */
00951   if ( LSM6DSL_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_FF_DISABLED ) == MEMS_ERROR )
00952   {
00953     return 1;
00954   }
00955   
00956   /* Disable basic Interrupts */
00957   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR )
00958   {
00959     return 1;
00960   }
00961   
00962   /* FF_DUR setting */
00963   if ( LSM6DSL_ACC_GYRO_W_FF_Duration( (void *)this, 0x00 ) == MEMS_ERROR )
00964   {
00965     return 1;
00966   }
00967   
00968   /* FF_THS setting */
00969   if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, LSM6DSL_ACC_GYRO_FF_THS_156mg ) == MEMS_ERROR )
00970   {
00971     return 1;
00972   }
00973   
00974   return 0;
00975 }
00976 
00977 /**
00978  * @brief Set the free fall detection threshold for LSM6DSL accelerometer sensor
00979  * @param thr the threshold to be set
00980  * @retval 0 in case of success, an error code otherwise
00981  */
00982 int LSM6DSLSensor::set_free_fall_threshold(uint8_t thr)
00983 {
00984 
00985   if ( LSM6DSL_ACC_GYRO_W_FF_THS( (void *)this, (LSM6DSL_ACC_GYRO_FF_THS_t)thr ) == MEMS_ERROR )
00986   {
00987     return 1;
00988   }
00989   
00990   return 0;
00991 }
00992 
00993 /**
00994  * @brief Enable the pedometer feature for LSM6DSL accelerometer sensor
00995  * @note  This function sets the LSM6DSL accelerometer ODR to 26Hz and the LSM6DSL accelerometer full scale to 2g
00996  * @retval 0 in case of success, an error code otherwise
00997  */
00998 int LSM6DSLSensor::enable_pedometer(void)
00999 {
01000   /* Output Data Rate selection */
01001   if( set_x_odr(26.0f) == 1 )
01002   {
01003     return 1;
01004   }
01005   
01006   /* Full scale selection. */
01007   if( set_x_fs(2.0f) == 1 )
01008   {
01009     return 1;
01010   }
01011   
01012   /* Set pedometer threshold. */
01013   if ( set_pedometer_threshold(LSM6DSL_PEDOMETER_THRESHOLD_MID_HIGH) == 1 )
01014   {
01015     return 1;
01016   }
01017   
01018   /* Enable embedded functionalities. */
01019   if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_ENABLED ) == MEMS_ERROR )
01020   {
01021     return 1;
01022   }
01023   
01024   /* Enable pedometer algorithm. */
01025   if ( LSM6DSL_ACC_GYRO_W_PEDO( (void *)this, LSM6DSL_ACC_GYRO_PEDO_ENABLED ) == MEMS_ERROR )
01026   {
01027     return 1;
01028   }
01029   
01030   /* Enable pedometer on INT1. */
01031   if ( LSM6DSL_ACC_GYRO_W_STEP_DET_on_INT1( (void *)this, LSM6DSL_ACC_GYRO_INT1_PEDO_ENABLED ) == MEMS_ERROR )
01032   {
01033     return 1;
01034   }
01035   
01036   return 0;
01037 }
01038 
01039 /**
01040  * @brief Disable the pedometer feature for LSM6DSL accelerometer sensor
01041  * @retval 0 in case of success, an error code otherwise
01042  */
01043 int LSM6DSLSensor::disable_pedometer(void)
01044 {
01045   /* Disable pedometer on INT1. */
01046   if ( LSM6DSL_ACC_GYRO_W_STEP_DET_on_INT1( (void *)this, LSM6DSL_ACC_GYRO_INT1_PEDO_DISABLED ) == MEMS_ERROR )
01047   {
01048     return 1;
01049   }
01050   
01051   /* Disable pedometer algorithm. */
01052   if ( LSM6DSL_ACC_GYRO_W_PEDO( (void *)this, LSM6DSL_ACC_GYRO_PEDO_DISABLED ) == MEMS_ERROR )
01053   {
01054     return 1;
01055   }
01056   
01057   /* Disable embedded functionalities. */
01058   if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_DISABLED ) == MEMS_ERROR )
01059   {
01060     return 1;
01061   }
01062   
01063   /* Reset pedometer threshold. */
01064   if ( set_pedometer_threshold(0x0) == 1 )
01065   {
01066     return 1;
01067   }
01068   
01069   return 0;
01070 }
01071 
01072 /**
01073  * @brief Get the step counter for LSM6DSL accelerometer sensor
01074  * @param step_count the pointer to the step counter
01075  * @retval 0 in case of success, an error code otherwise
01076  */
01077 int LSM6DSLSensor::get_step_counter(uint16_t *step_count)
01078 {
01079   if ( LSM6DSL_ACC_GYRO_Get_GetStepCounter( (void *)this, ( uint8_t* )step_count ) == MEMS_ERROR )
01080   {
01081     return 1;
01082   }
01083   
01084   return 0;
01085 }
01086 
01087 /**
01088  * @brief Reset of the step counter for LSM6DSL accelerometer sensor
01089  * @retval 0 in case of success, an error code otherwise
01090  */
01091 int LSM6DSLSensor::reset_step_counter(void)
01092 {
01093   if ( LSM6DSL_ACC_GYRO_W_PedoStepReset( (void *)this, LSM6DSL_ACC_GYRO_PEDO_RST_STEP_ENABLED ) == MEMS_ERROR )
01094   {
01095     return 1;
01096   }
01097   
01098   wait_ms(10);
01099   
01100   if ( LSM6DSL_ACC_GYRO_W_PedoStepReset( (void *)this, LSM6DSL_ACC_GYRO_PEDO_RST_STEP_DISABLED ) == MEMS_ERROR )
01101   {
01102     return 1;
01103   }
01104   
01105   return 0;
01106 }
01107 
01108 /**
01109  * @brief Set the pedometer threshold for LSM6DSL accelerometer sensor
01110  * @param thr the threshold to be set
01111  * @retval 0 in case of success, an error code otherwise
01112  */
01113 int LSM6DSLSensor::set_pedometer_threshold(uint8_t thr)
01114 {
01115   if ( LSM6DSL_ACC_GYRO_W_PedoThreshold( (void *)this, thr ) == MEMS_ERROR )
01116   {
01117     return 1;
01118   }
01119   
01120   return 0;
01121 }
01122 
01123 /**
01124  * @brief Enable the tilt detection for LSM6DSL accelerometer sensor
01125  * @param pin the interrupt pin to be used
01126  * @note  This function sets the LSM6DSL accelerometer ODR to 26Hz and the LSM6DSL accelerometer full scale to 2g
01127  * @retval 0 in case of success, an error code otherwise
01128  */
01129 int LSM6DSLSensor::enable_tilt_detection(LSM6DSL_Interrupt_Pin_t pin)
01130 {
01131   /* Output Data Rate selection */
01132   if( set_x_odr(26.0f) == 1 )
01133   {
01134     return 1;
01135   }
01136   
01137   /* Full scale selection. */
01138   if( set_x_fs(2.0f) == 1 )
01139   {
01140     return 1;
01141   }
01142   
01143   /* Enable embedded functionalities */
01144   if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_ENABLED ) == MEMS_ERROR )
01145   {
01146     return 1;
01147   }
01148   
01149   /* Enable tilt calculation. */
01150   if ( LSM6DSL_ACC_GYRO_W_TILT( (void *)this, LSM6DSL_ACC_GYRO_TILT_ENABLED ) == MEMS_ERROR )
01151   {
01152     return 1;
01153   }
01154 
01155   /* Enable tilt detection on either INT1 or INT2 pin */
01156   switch (pin)
01157   {
01158   case LSM6DSL_INT1_PIN:
01159     if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TILT_ENABLED ) == MEMS_ERROR )
01160     {
01161       return 1;
01162     }
01163     break;
01164 
01165   case LSM6DSL_INT2_PIN:
01166     if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TILT_ENABLED ) == MEMS_ERROR )
01167     {
01168       return 1;
01169     }
01170     break;
01171 
01172   default:
01173     return 1;
01174   }
01175 
01176   return 0;
01177 }
01178 
01179 /**
01180  * @brief Disable the tilt detection for LSM6DSL accelerometer sensor
01181  * @retval 0 in case of success, an error code otherwise
01182  */
01183 int LSM6DSLSensor::disable_tilt_detection(void)
01184 {
01185   /* Disable tilt event on INT1. */
01186   if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TILT_DISABLED ) == MEMS_ERROR )
01187   {
01188     return 1;
01189   }
01190 
01191   /* Disable tilt event on INT2. */
01192   if ( LSM6DSL_ACC_GYRO_W_TiltEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TILT_DISABLED ) == MEMS_ERROR )
01193   {
01194     return 1;
01195   }
01196   
01197   /* Disable tilt calculation. */
01198   if ( LSM6DSL_ACC_GYRO_W_TILT( (void *)this, LSM6DSL_ACC_GYRO_TILT_DISABLED ) == MEMS_ERROR )
01199   {
01200     return 1;
01201   }
01202   
01203   /* Disable embedded functionalities */
01204   if ( LSM6DSL_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DSL_ACC_GYRO_FUNC_EN_DISABLED ) == MEMS_ERROR )
01205   {
01206     return 1;
01207   }
01208   
01209   return 0;
01210 }
01211 
01212 /**
01213  * @brief Enable the wake up detection for LSM6DSL accelerometer sensor
01214  * @param pin the interrupt pin to be used
01215  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
01216  * @retval 0 in case of success, an error code otherwise
01217  */
01218 int LSM6DSLSensor::enable_wake_up_detection(LSM6DSL_Interrupt_Pin_t pin)
01219 {
01220   /* Output Data Rate selection */
01221   if( set_x_odr(416.0f) == 1 )
01222   {
01223     return 1;
01224   }
01225   
01226   /* Full scale selection. */
01227   if( set_x_fs(2.0f) == 1 )
01228   {
01229     return 1;
01230   }
01231   
01232   /* WAKE_DUR setting */
01233   if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
01234   {
01235     return 1;
01236   }
01237   
01238   /* Set wake up threshold. */
01239   if ( LSM6DSL_ACC_GYRO_W_WK_THS( (void *)this, 0x02 ) == MEMS_ERROR )
01240   {
01241     return 1;
01242   }
01243   
01244   /* Enable basic Interrupts */
01245   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR )
01246   {
01247     return 1;
01248   }
01249 
01250   /* Enable wake up detection on either INT1 or INT2 pin */
01251   switch (pin)
01252   {
01253   case LSM6DSL_INT1_PIN:
01254     if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_WU_ENABLED ) == MEMS_ERROR )
01255     {
01256       return 1;
01257     }
01258     break;
01259 
01260   case LSM6DSL_INT2_PIN:
01261     if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_WU_ENABLED ) == MEMS_ERROR )
01262     {
01263       return 1;
01264     }
01265     break;
01266 
01267   default:
01268     return 1;
01269   }
01270   
01271   return 0;
01272 }
01273 
01274 /**
01275  * @brief Disable the wake up detection for LSM6DSL accelerometer sensor
01276  * @retval 0 in case of success, an error code otherwise
01277  */
01278 int LSM6DSLSensor::disable_wake_up_detection(void)
01279 {
01280   /* Disable wake up event on INT1 */
01281   if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_WU_DISABLED ) == MEMS_ERROR )
01282   {
01283     return 1;
01284   }
01285 
01286   /* Disable wake up event on INT2 */
01287   if ( LSM6DSL_ACC_GYRO_W_WUEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_WU_DISABLED ) == MEMS_ERROR )
01288   {
01289     return 1;
01290   }
01291   
01292   /* Disable basic Interrupts */
01293   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR )
01294   {
01295     return 1;
01296   }
01297   
01298   /* WU_DUR setting */
01299   if ( LSM6DSL_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
01300   {
01301     return 1;
01302   }
01303   
01304   /* WU_THS setting */
01305   if ( LSM6DSL_ACC_GYRO_W_WK_THS( (void *)this, 0x00 ) == MEMS_ERROR )
01306   {
01307     return 1;
01308   }
01309   
01310   return 0;
01311 }
01312 
01313 /**
01314  * @brief Set the wake up threshold for LSM6DSL accelerometer sensor
01315  * @param thr the threshold to be set
01316  * @retval 0 in case of success, an error code otherwise
01317  */
01318 int LSM6DSLSensor::set_wake_up_threshold(uint8_t thr)
01319 {
01320   if ( LSM6DSL_ACC_GYRO_W_WK_THS( (void *)this, thr ) == MEMS_ERROR )
01321   {
01322     return 1;
01323   }
01324   
01325   return 0;
01326 }
01327 
01328 /**
01329  * @brief Enable the single tap detection for LSM6DSL accelerometer sensor
01330  * @param pin the interrupt pin to be used
01331  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
01332  * @retval 0 in case of success, an error code otherwise
01333  */
01334 int LSM6DSLSensor::enable_single_tap_detection(LSM6DSL_Interrupt_Pin_t pin)
01335 {
01336   /* Output Data Rate selection */
01337   if( set_x_odr(416.0f) == 1 )
01338   {
01339     return 1;
01340   }
01341   
01342   /* Full scale selection. */
01343   if( set_x_fs(2.0f) == 1 )
01344   {
01345     return 1;
01346   }
01347 
01348   /* Enable X direction in tap recognition. */
01349   if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_ENABLED ) == MEMS_ERROR )
01350   {
01351     return 1;
01352   }
01353   
01354   /* Enable Y direction in tap recognition. */
01355   if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_ENABLED ) == MEMS_ERROR )
01356   {
01357     return 1;
01358   }
01359   
01360   /* Enable Z direction in tap recognition. */
01361   if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_ENABLED ) == MEMS_ERROR )
01362   {
01363     return 1;
01364   }
01365   
01366   /* Set tap threshold. */
01367   if ( set_tap_threshold( LSM6DSL_TAP_THRESHOLD_MID_LOW ) == 1 )
01368   {
01369     return 1;
01370   }
01371   
01372   /* Set tap shock time window. */
01373   if ( set_tap_shock_time( LSM6DSL_TAP_SHOCK_TIME_MID_HIGH ) == 1 )
01374   {
01375     return 1;
01376   }
01377   
01378   /* Set tap quiet time window. */
01379   if ( set_tap_quiet_time( LSM6DSL_TAP_QUIET_TIME_MID_LOW ) == 1 )
01380   {
01381     return 1;
01382   }
01383   
01384   /* _NOTE_: Tap duration time window - don't care for single tap. */
01385   
01386   /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
01387   
01388   /* Enable basic Interrupts */
01389   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR )
01390   {
01391     return 1;
01392   }
01393   
01394   /* Enable single tap on either INT1 or INT2 pin */
01395   switch (pin)
01396   {
01397   case LSM6DSL_INT1_PIN:
01398     if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_SINGLE_TAP_ENABLED ) == MEMS_ERROR )
01399     {
01400       return 1;
01401     }
01402     break;
01403 
01404   case LSM6DSL_INT2_PIN:
01405     if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_SINGLE_TAP_ENABLED ) == MEMS_ERROR )
01406     {
01407       return 1;
01408     }
01409     break;
01410 
01411   default:
01412     return 1;
01413   }
01414   
01415   return 0;
01416 }
01417 
01418 /**
01419  * @brief Disable the single tap detection for LSM6DSL accelerometer sensor
01420  * @retval 0 in case of success, an error code otherwise
01421  */
01422 int LSM6DSLSensor::disable_single_tap_detection(void)
01423 {
01424   /* Disable single tap interrupt on INT1 pin. */
01425   if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_SINGLE_TAP_DISABLED ) == MEMS_ERROR )
01426   {
01427     return 1;
01428   }
01429   
01430   /* Disable single tap interrupt on INT2 pin. */
01431   if ( LSM6DSL_ACC_GYRO_W_SingleTapOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_SINGLE_TAP_DISABLED ) == MEMS_ERROR )
01432   {
01433     return 1;
01434   }
01435   
01436   /* Disable basic Interrupts */
01437   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR )
01438   {
01439     return 1;
01440   }
01441   
01442   /* Reset tap threshold. */
01443   if ( set_tap_threshold( 0x0 ) == 1 )
01444   {
01445     return 1;
01446   }
01447   
01448   /* Reset tap shock time window. */
01449   if ( set_tap_shock_time( 0x0 ) == 1 )
01450   {
01451     return 1;
01452   }
01453   
01454   /* Reset tap quiet time window. */
01455   if ( set_tap_quiet_time( 0x0 ) == 1 )
01456   {
01457     return 1;
01458   }
01459   
01460   /* _NOTE_: Tap duration time window - don't care for single tap. */
01461   
01462   /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
01463   
01464   /* Disable Z direction in tap recognition. */
01465   if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_DISABLED ) == MEMS_ERROR )
01466   {
01467     return 1;
01468   }
01469   
01470   /* Disable Y direction in tap recognition. */
01471   if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_DISABLED ) == MEMS_ERROR )
01472   {
01473     return 1;
01474   }
01475   
01476   /* Disable X direction in tap recognition. */
01477   if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_DISABLED ) == MEMS_ERROR )
01478   {
01479     return 1;
01480   }
01481   
01482   return 0;
01483 }
01484 
01485 /**
01486  * @brief Enable the double tap detection for LSM6DSL accelerometer sensor
01487  * @param pin the interrupt pin to be used
01488  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
01489  * @retval 0 in case of success, an error code otherwise
01490  */
01491 int LSM6DSLSensor::enable_double_tap_detection(LSM6DSL_Interrupt_Pin_t pin)
01492 {
01493   /* Output Data Rate selection */
01494   if( set_x_odr(416.0f) == 1 )
01495   {
01496     return 1;
01497   }
01498   
01499   /* Full scale selection. */
01500   if( set_x_fs(2.0f) == 1 )
01501   {
01502     return 1;
01503   }
01504 
01505   /* Enable X direction in tap recognition. */
01506   if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_ENABLED ) == MEMS_ERROR )
01507   {
01508     return 1;
01509   }
01510   
01511   /* Enable Y direction in tap recognition. */
01512   if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_ENABLED ) == MEMS_ERROR )
01513   {
01514     return 1;
01515   }
01516   
01517   /* Enable Z direction in tap recognition. */
01518   if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_ENABLED ) == MEMS_ERROR )
01519   {
01520     return 1;
01521   }
01522   
01523   /* Set tap threshold. */
01524   if ( set_tap_threshold( LSM6DSL_TAP_THRESHOLD_MID_LOW ) == 1 )
01525   {
01526     return 1;
01527   }
01528   
01529   /* Set tap shock time window. */
01530   if ( set_tap_shock_time( LSM6DSL_TAP_SHOCK_TIME_HIGH ) == 1 )
01531   {
01532     return 1;
01533   }
01534   
01535   /* Set tap quiet time window. */
01536   if ( set_tap_quiet_time( LSM6DSL_TAP_QUIET_TIME_HIGH ) == 1 )
01537   {
01538     return 1;
01539   }
01540   
01541   /* Set tap duration time window. */
01542   if ( set_tap_duration_time( LSM6DSL_TAP_DURATION_TIME_MID ) == 1 )
01543   {
01544     return 1;
01545   }
01546   
01547   /* Single and double tap enabled. */
01548   if ( LSM6DSL_ACC_GYRO_W_SINGLE_DOUBLE_TAP_EV( (void *)this, LSM6DSL_ACC_GYRO_SINGLE_DOUBLE_TAP_DOUBLE_TAP ) == MEMS_ERROR )
01549   {
01550     return 1;
01551   }
01552   
01553   /* Enable basic Interrupts */
01554   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR )
01555   {
01556     return 1;
01557   }
01558   
01559   /* Enable double tap on either INT1 or INT2 pin */
01560   switch (pin)
01561   {
01562   case LSM6DSL_INT1_PIN:
01563     if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TAP_ENABLED ) == MEMS_ERROR )
01564     {
01565       return 1;
01566     }
01567     break;
01568 
01569   case LSM6DSL_INT2_PIN:
01570     if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TAP_ENABLED ) == MEMS_ERROR )
01571     {
01572       return 1;
01573     }
01574     break;
01575 
01576   default:
01577     return 1;
01578   }
01579   
01580   return 0;
01581 }
01582 
01583 /**
01584  * @brief Disable the double tap detection for LSM6DSL accelerometer sensor
01585  * @retval 0 in case of success, an error code otherwise
01586  */
01587 int LSM6DSLSensor::disable_double_tap_detection(void)
01588 {
01589   /* Disable double tap interrupt on INT1 pin. */
01590   if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_TAP_DISABLED ) == MEMS_ERROR )
01591   {
01592     return 1;
01593   }
01594   
01595   /* Disable double tap interrupt on INT2 pin. */
01596   if ( LSM6DSL_ACC_GYRO_W_TapEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_TAP_DISABLED ) == MEMS_ERROR )
01597   {
01598     return 1;
01599   }
01600   
01601   /* Disable basic Interrupts */
01602   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR )
01603   {
01604     return 1;
01605   }
01606   
01607   /* Reset tap threshold. */
01608   if ( set_tap_threshold( 0x0 ) == 1 )
01609   {
01610     return 1;
01611   }
01612   
01613   /* Reset tap shock time window. */
01614   if ( set_tap_shock_time( 0x0 ) == 1 )
01615   {
01616     return 1;
01617   }
01618   
01619   /* Reset tap quiet time window. */
01620   if ( set_tap_quiet_time( 0x0 ) == 1 )
01621   {
01622     return 1;
01623   }
01624   
01625   /* Reset tap duration time window. */
01626   if ( set_tap_duration_time( 0x0 ) == 1 )
01627   {
01628     return 1;
01629   }
01630   
01631   /* Only single tap enabled. */
01632   if ( LSM6DSL_ACC_GYRO_W_SINGLE_DOUBLE_TAP_EV( (void *)this, LSM6DSL_ACC_GYRO_SINGLE_DOUBLE_TAP_SINGLE_TAP ) == MEMS_ERROR )
01633   {
01634     return 1;
01635   }
01636   
01637   /* Disable Z direction in tap recognition. */
01638   if ( LSM6DSL_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Z_EN_DISABLED ) == MEMS_ERROR )
01639   {
01640     return 1;
01641   }
01642   
01643   /* Disable Y direction in tap recognition. */
01644   if ( LSM6DSL_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_Y_EN_DISABLED ) == MEMS_ERROR )
01645   {
01646     return 1;
01647   }
01648   
01649   /* Disable X direction in tap recognition. */
01650   if ( LSM6DSL_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DSL_ACC_GYRO_TAP_X_EN_DISABLED ) == MEMS_ERROR )
01651   {
01652     return 1;
01653   }
01654   
01655   return 0;
01656 }
01657 
01658 /**
01659  * @brief Set the tap threshold for LSM6DSL accelerometer sensor
01660  * @param thr the threshold to be set
01661  * @retval 0 in case of success, an error code otherwise
01662  */
01663 int LSM6DSLSensor::set_tap_threshold(uint8_t thr)
01664 {
01665   if ( LSM6DSL_ACC_GYRO_W_TAP_THS( (void *)this, thr ) == MEMS_ERROR )
01666   {
01667     return 1;
01668   }
01669   
01670   return 0;
01671 }
01672 
01673 /**
01674  * @brief Set the tap shock time window for LSM6DSL accelerometer sensor
01675  * @param time the shock time window to be set
01676  * @retval 0 in case of success, an error code otherwise
01677  */
01678 int LSM6DSLSensor::set_tap_shock_time(uint8_t time)
01679 {
01680   if ( LSM6DSL_ACC_GYRO_W_SHOCK_Duration( (void *)this, time ) == MEMS_ERROR )
01681   {
01682     return 1;
01683   }
01684   
01685   return 0;
01686 }
01687 
01688 /**
01689  * @brief Set the tap quiet time window for LSM6DSL accelerometer sensor
01690  * @param time the quiet time window to be set
01691  * @retval 0 in case of success, an error code otherwise
01692  */
01693 int LSM6DSLSensor::set_tap_quiet_time(uint8_t time)
01694 {
01695   if ( LSM6DSL_ACC_GYRO_W_QUIET_Duration( (void *)this, time ) == MEMS_ERROR )
01696   {
01697     return 1;
01698   }
01699   
01700   return 0;
01701 }
01702 
01703 /**
01704  * @brief Set the tap duration of the time window for LSM6DSL accelerometer sensor
01705  * @param time the duration of the time window to be set
01706  * @retval 0 in case of success, an error code otherwise
01707  */
01708 int LSM6DSLSensor::set_tap_duration_time(uint8_t time)
01709 {
01710   if ( LSM6DSL_ACC_GYRO_W_DUR( (void *)this, time ) == MEMS_ERROR )
01711   {
01712     return 1;
01713   }
01714   
01715   return 0;
01716 }
01717 
01718 /**
01719  * @brief Enable the 6D orientation detection for LSM6DSL accelerometer sensor
01720  * @param pin the interrupt pin to be used
01721  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
01722  * @retval 0 in case of success, an error code otherwise
01723  */
01724 int LSM6DSLSensor::enable_6d_orientation(LSM6DSL_Interrupt_Pin_t pin)
01725 {
01726   /* Output Data Rate selection */
01727   if( set_x_odr(416.0f) == 1 )
01728   {
01729     return 1;
01730   }
01731   
01732   /* Full scale selection. */
01733   if( set_x_fs(2.0f) == 1 )
01734   {
01735     return 1;
01736   }
01737 
01738   /* Set 6D threshold. */
01739   if ( LSM6DSL_ACC_GYRO_W_SIXD_THS( (void *)this, LSM6DSL_ACC_GYRO_SIXD_THS_60_degree ) == MEMS_ERROR )
01740   {
01741     return 1;
01742   }
01743   
01744   /* Enable basic Interrupts */
01745   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_ENABLED ) == MEMS_ERROR )
01746   {
01747     return 1;
01748   }
01749   
01750   /* Enable 6D orientation on either INT1 or INT2 pin */
01751   switch (pin)
01752   {
01753   case LSM6DSL_INT1_PIN:
01754     if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_6D_ENABLED ) == MEMS_ERROR )
01755     {
01756       return 1;
01757     }
01758     break;
01759 
01760   case LSM6DSL_INT2_PIN:
01761     if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_6D_ENABLED ) == MEMS_ERROR )
01762     {
01763       return 1;
01764     }
01765     break;
01766 
01767   default:
01768     return 1;
01769   }
01770   
01771   return 0;
01772 }
01773 
01774 /**
01775  * @brief Disable the 6D orientation detection for LSM6DSL accelerometer sensor
01776  * @retval 0 in case of success, an error code otherwise
01777  */
01778 int LSM6DSLSensor::disable_6d_orientation(void)
01779 {
01780   /* Disable 6D orientation interrupt on INT1 pin. */
01781   if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt1( (void *)this, LSM6DSL_ACC_GYRO_INT1_6D_DISABLED ) == MEMS_ERROR )
01782   {
01783     return 1;
01784   }
01785   
01786   /* Disable 6D orientation interrupt on INT2 pin. */
01787   if ( LSM6DSL_ACC_GYRO_W_6DEvOnInt2( (void *)this, LSM6DSL_ACC_GYRO_INT2_6D_DISABLED ) == MEMS_ERROR )
01788   {
01789     return 1;
01790   }
01791   
01792   /* Disable basic Interrupts */
01793   if ( LSM6DSL_ACC_GYRO_W_BASIC_INT( (void *)this, LSM6DSL_ACC_GYRO_BASIC_INT_DISABLED ) == MEMS_ERROR )
01794   {
01795     return 1;
01796   }
01797   
01798   /* Reset 6D threshold. */
01799   if ( LSM6DSL_ACC_GYRO_W_SIXD_THS( (void *)this, LSM6DSL_ACC_GYRO_SIXD_THS_80_degree ) == MEMS_ERROR )
01800   {
01801     return 1;
01802   }
01803   
01804   return 0;
01805 }
01806 
01807 /**
01808  * @brief Get the 6D orientation XL axis for LSM6DSL accelerometer sensor
01809  * @param xl the pointer to the 6D orientation XL axis
01810  * @retval 0 in case of success, an error code otherwise
01811  */
01812 int LSM6DSLSensor::get_6d_orientation_xl(uint8_t *xl)
01813 {
01814   LSM6DSL_ACC_GYRO_DSD_XL_t xl_raw;
01815   
01816   if ( LSM6DSL_ACC_GYRO_R_DSD_XL( (void *)this, &xl_raw ) == MEMS_ERROR )
01817   {
01818     return 1;
01819   }
01820   
01821   switch( xl_raw )
01822   {
01823     case LSM6DSL_ACC_GYRO_DSD_XL_DETECTED:
01824       *xl = 1;
01825       break;
01826     case LSM6DSL_ACC_GYRO_DSD_XL_NOT_DETECTED:
01827       *xl = 0;
01828       break;
01829     default:
01830       return 1;
01831   }
01832   
01833   return 0;
01834 }
01835 
01836 /**
01837  * @brief Get the 6D orientation XH axis for LSM6DSL accelerometer sensor
01838  * @param xh the pointer to the 6D orientation XH axis
01839  * @retval 0 in case of success, an error code otherwise
01840  */
01841 int LSM6DSLSensor::get_6d_orientation_xh(uint8_t *xh)
01842 {
01843   LSM6DSL_ACC_GYRO_DSD_XH_t xh_raw;
01844   
01845   if ( LSM6DSL_ACC_GYRO_R_DSD_XH( (void *)this, &xh_raw ) == MEMS_ERROR )
01846   {
01847     return 1;
01848   }
01849   
01850   switch( xh_raw )
01851   {
01852     case LSM6DSL_ACC_GYRO_DSD_XH_DETECTED:
01853       *xh = 1;
01854       break;
01855     case LSM6DSL_ACC_GYRO_DSD_XH_NOT_DETECTED:
01856       *xh = 0;
01857       break;
01858     default:
01859       return 1;
01860   }
01861   
01862   return 0;
01863 }
01864 
01865 /**
01866  * @brief Get the 6D orientation YL axis for LSM6DSL accelerometer sensor
01867  * @param yl the pointer to the 6D orientation YL axis
01868  * @retval 0 in case of success, an error code otherwise
01869  */
01870 int LSM6DSLSensor::get_6d_orientation_yl(uint8_t *yl)
01871 {
01872   LSM6DSL_ACC_GYRO_DSD_YL_t yl_raw;
01873   
01874   if ( LSM6DSL_ACC_GYRO_R_DSD_YL( (void *)this, &yl_raw ) == MEMS_ERROR )
01875   {
01876     return 1;
01877   }
01878   
01879   switch( yl_raw )
01880   {
01881     case LSM6DSL_ACC_GYRO_DSD_YL_DETECTED:
01882       *yl = 1;
01883       break;
01884     case LSM6DSL_ACC_GYRO_DSD_YL_NOT_DETECTED:
01885       *yl = 0;
01886       break;
01887     default:
01888       return 1;
01889   }
01890   
01891   return 0;
01892 }
01893 
01894 /**
01895  * @brief Get the 6D orientation YH axis for LSM6DSL accelerometer sensor
01896  * @param yh the pointer to the 6D orientation YH axis
01897  * @retval 0 in case of success, an error code otherwise
01898  */
01899 int LSM6DSLSensor::get_6d_orientation_yh(uint8_t *yh)
01900 {
01901   LSM6DSL_ACC_GYRO_DSD_YH_t yh_raw;
01902   
01903   if ( LSM6DSL_ACC_GYRO_R_DSD_YH( (void *)this, &yh_raw ) == MEMS_ERROR )
01904   {
01905     return 1;
01906   }
01907   
01908   switch( yh_raw )
01909   {
01910     case LSM6DSL_ACC_GYRO_DSD_YH_DETECTED:
01911       *yh = 1;
01912       break;
01913     case LSM6DSL_ACC_GYRO_DSD_YH_NOT_DETECTED:
01914       *yh = 0;
01915       break;
01916     default:
01917       return 1;
01918   }
01919   
01920   return 0;
01921 }
01922 
01923 /**
01924  * @brief Get the 6D orientation ZL axis for LSM6DSL accelerometer sensor
01925  * @param zl the pointer to the 6D orientation ZL axis
01926  * @retval 0 in case of success, an error code otherwise
01927  */
01928 int LSM6DSLSensor::get_6d_orientation_zl(uint8_t *zl)
01929 {
01930   LSM6DSL_ACC_GYRO_DSD_ZL_t zl_raw;
01931   
01932   if ( LSM6DSL_ACC_GYRO_R_DSD_ZL( (void *)this, &zl_raw ) == MEMS_ERROR )
01933   {
01934     return 1;
01935   }
01936   
01937   switch( zl_raw )
01938   {
01939     case LSM6DSL_ACC_GYRO_DSD_ZL_DETECTED:
01940       *zl = 1;
01941       break;
01942     case LSM6DSL_ACC_GYRO_DSD_ZL_NOT_DETECTED:
01943       *zl = 0;
01944       break;
01945     default:
01946       return 1;
01947   }
01948   
01949   return 0;
01950 }
01951 
01952 /**
01953  * @brief Get the 6D orientation ZH axis for LSM6DSL accelerometer sensor
01954  * @param zh the pointer to the 6D orientation ZH axis
01955  * @retval 0 in case of success, an error code otherwise
01956  */
01957 int LSM6DSLSensor::get_6d_orientation_zh(uint8_t *zh)
01958 {
01959   LSM6DSL_ACC_GYRO_DSD_ZH_t zh_raw;
01960   
01961   if ( LSM6DSL_ACC_GYRO_R_DSD_ZH( (void *)this, &zh_raw ) == MEMS_ERROR )
01962   {
01963     return 1;
01964   }
01965   
01966   switch( zh_raw )
01967   {
01968     case LSM6DSL_ACC_GYRO_DSD_ZH_DETECTED:
01969       *zh = 1;
01970       break;
01971     case LSM6DSL_ACC_GYRO_DSD_ZH_NOT_DETECTED:
01972       *zh = 0;
01973       break;
01974     default:
01975       return 1;
01976   }
01977   
01978   return 0;
01979 }
01980 
01981 /**
01982  * @brief Get the status of all hardware events for LSM6DSL accelerometer sensor
01983  * @param status the pointer to the status of all hardware events
01984  * @retval 0 in case of success, an error code otherwise
01985  */
01986 int LSM6DSLSensor::get_event_status(LSM6DSL_Event_Status_t *status)
01987 {
01988   uint8_t Wake_Up_Src = 0, Tap_Src = 0, D6D_Src = 0, Func_Src = 0, Md1_Cfg = 0, Md2_Cfg = 0, Int1_Ctrl = 0;
01989 
01990   memset((void *)status, 0x0, sizeof(LSM6DSL_Event_Status_t));
01991 
01992   if(read_reg(LSM6DSL_ACC_GYRO_WAKE_UP_SRC, &Wake_Up_Src) != 0)
01993   {
01994     return 1;
01995   }
01996 
01997   if(read_reg(LSM6DSL_ACC_GYRO_TAP_SRC, &Tap_Src) != 0)
01998   {
01999     return 1;
02000   }
02001 
02002   if(read_reg(LSM6DSL_ACC_GYRO_D6D_SRC, &D6D_Src) != 0)
02003   {
02004     return 1;
02005   }
02006 
02007   if(read_reg(LSM6DSL_ACC_GYRO_FUNC_SRC, &Func_Src) != 0)
02008   {
02009     return 1;
02010   }
02011 
02012   if(read_reg(LSM6DSL_ACC_GYRO_MD1_CFG, &Md1_Cfg ) != 0 )
02013   {
02014     return 1;
02015   }
02016 
02017   if(read_reg(LSM6DSL_ACC_GYRO_MD2_CFG, &Md2_Cfg ) != 0)
02018   {
02019     return 1;
02020   }
02021 
02022   if(read_reg(LSM6DSL_ACC_GYRO_INT1_CTRL, &Int1_Ctrl ) != 0)
02023   {
02024     return 1;
02025   }
02026 
02027   if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_FF_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_FF_MASK))
02028   {
02029     if((Wake_Up_Src & LSM6DSL_ACC_GYRO_FF_EV_STATUS_MASK))
02030     {
02031       status->FreeFallStatus = 1;  
02032     }
02033   }
02034 
02035   if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_WU_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_WU_MASK))
02036   {
02037     if((Wake_Up_Src & LSM6DSL_ACC_GYRO_WU_EV_STATUS_MASK))
02038     {
02039       status->WakeUpStatus = 1;  
02040     }
02041   }
02042 
02043   if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_SINGLE_TAP_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_SINGLE_TAP_MASK))
02044   {
02045     if((Tap_Src & LSM6DSL_ACC_GYRO_SINGLE_TAP_EV_STATUS_MASK))
02046     {
02047       status->TapStatus = 1;  
02048     }
02049   }
02050 
02051   if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_TAP_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_TAP_MASK))
02052   {
02053     if((Tap_Src & LSM6DSL_ACC_GYRO_DOUBLE_TAP_EV_STATUS_MASK))
02054     {
02055       status->DoubleTapStatus = 1;  
02056     }
02057   }
02058 
02059   if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_6D_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_6D_MASK))
02060   {
02061     if((D6D_Src & LSM6DSL_ACC_GYRO_D6D_EV_STATUS_MASK))
02062     {
02063       status->D6DOrientationStatus = 1;  
02064     }
02065   }
02066 
02067   if((Int1_Ctrl & LSM6DSL_ACC_GYRO_INT1_PEDO_MASK))
02068   {
02069     if((Func_Src & LSM6DSL_ACC_GYRO_PEDO_EV_STATUS_MASK))
02070     {
02071       status->StepStatus = 1;  
02072     }
02073   }
02074 
02075   if((Md1_Cfg & LSM6DSL_ACC_GYRO_INT1_TILT_MASK) || (Md2_Cfg & LSM6DSL_ACC_GYRO_INT2_TILT_MASK))
02076   {
02077     if((Func_Src & LSM6DSL_ACC_GYRO_TILT_EV_STATUS_MASK))
02078     {
02079       status->TiltStatus = 1;  
02080     }
02081   }
02082 
02083   return 0;
02084 }
02085 
02086 /**
02087  * @brief Read the data from register
02088  * @param reg register address
02089  * @param data register data
02090  * @retval 0 in case of success, an error code otherwise
02091  */
02092 int LSM6DSLSensor::read_reg( uint8_t reg, uint8_t *data )
02093 {
02094 
02095   if ( LSM6DSL_ACC_GYRO_read_reg( (void *)this, reg, data, 1 ) == MEMS_ERROR )
02096   {
02097     return 1;
02098   }
02099 
02100   return 0;
02101 }
02102 
02103 /**
02104  * @brief Write the data to register
02105  * @param reg register address
02106  * @param data register data
02107  * @retval 0 in case of success, an error code otherwise
02108  */
02109 int LSM6DSLSensor::write_reg( uint8_t reg, uint8_t data )
02110 {
02111 
02112   if ( LSM6DSL_ACC_GYRO_write_reg( (void *)this, reg, &data, 1 ) == MEMS_ERROR )
02113   {
02114     return 1;
02115   }
02116 
02117   return 0;
02118 }
02119 
02120 
02121 uint8_t LSM6DSL_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
02122 {
02123   return ((LSM6DSLSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
02124 }
02125 
02126 uint8_t LSM6DSL_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
02127 {
02128   return ((LSM6DSLSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
02129 }