Ultra-compact high-performance eCompass module: ultra-low power 3D accelerometer and 3D magnetometer.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client LSM303AGR_JS ... more
LSM303AGRAccSensor.cpp@1:86d530a7f949, 2017-09-27 (annotated)
- Committer:
- mapellil
- Date:
- Wed Sep 27 16:49:51 2017 +0200
- Revision:
- 1:86d530a7f949
- Parent:
- 0:ec6e59cc6f40
- Child:
- 2:e37be5550633
Fixed NonCopyable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:ec6e59cc6f40 | 1 | /** |
nikapov | 0:ec6e59cc6f40 | 2 | ****************************************************************************** |
nikapov | 0:ec6e59cc6f40 | 3 | * @file LSM303AGRAccSensor.cpp |
nikapov | 0:ec6e59cc6f40 | 4 | * @author CLab |
nikapov | 0:ec6e59cc6f40 | 5 | * @version V1.0.0 |
nikapov | 0:ec6e59cc6f40 | 6 | * @date 5 August 2016 |
nikapov | 0:ec6e59cc6f40 | 7 | * @brief Implementation an LSM303AGR accelerometer sensor. |
nikapov | 0:ec6e59cc6f40 | 8 | ****************************************************************************** |
nikapov | 0:ec6e59cc6f40 | 9 | * @attention |
nikapov | 0:ec6e59cc6f40 | 10 | * |
nikapov | 0:ec6e59cc6f40 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nikapov | 0:ec6e59cc6f40 | 12 | * |
nikapov | 0:ec6e59cc6f40 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:ec6e59cc6f40 | 14 | * are permitted provided that the following conditions are met: |
nikapov | 0:ec6e59cc6f40 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:ec6e59cc6f40 | 16 | * this list of conditions and the following disclaimer. |
nikapov | 0:ec6e59cc6f40 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:ec6e59cc6f40 | 18 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:ec6e59cc6f40 | 19 | * and/or other materials provided with the distribution. |
nikapov | 0:ec6e59cc6f40 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:ec6e59cc6f40 | 21 | * may be used to endorse or promote products derived from this software |
nikapov | 0:ec6e59cc6f40 | 22 | * without specific prior written permission. |
nikapov | 0:ec6e59cc6f40 | 23 | * |
nikapov | 0:ec6e59cc6f40 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:ec6e59cc6f40 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:ec6e59cc6f40 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:ec6e59cc6f40 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:ec6e59cc6f40 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:ec6e59cc6f40 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:ec6e59cc6f40 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:ec6e59cc6f40 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:ec6e59cc6f40 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:ec6e59cc6f40 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:ec6e59cc6f40 | 34 | * |
nikapov | 0:ec6e59cc6f40 | 35 | ****************************************************************************** |
nikapov | 0:ec6e59cc6f40 | 36 | */ |
nikapov | 0:ec6e59cc6f40 | 37 | |
nikapov | 0:ec6e59cc6f40 | 38 | |
nikapov | 0:ec6e59cc6f40 | 39 | /* Includes ------------------------------------------------------------------*/ |
nikapov | 0:ec6e59cc6f40 | 40 | |
nikapov | 0:ec6e59cc6f40 | 41 | #include "LSM303AGRAccSensor.h" |
nikapov | 0:ec6e59cc6f40 | 42 | |
nikapov | 0:ec6e59cc6f40 | 43 | |
nikapov | 0:ec6e59cc6f40 | 44 | /* Class Implementation ------------------------------------------------------*/ |
nikapov | 0:ec6e59cc6f40 | 45 | |
nikapov | 0:ec6e59cc6f40 | 46 | /** Constructor |
nikapov | 0:ec6e59cc6f40 | 47 | * @param i2c object of an helper class which handles the I2C peripheral |
nikapov | 0:ec6e59cc6f40 | 48 | * @param address the address of the component's instance |
nikapov | 0:ec6e59cc6f40 | 49 | */ |
mapellil | 1:86d530a7f949 | 50 | LSM303AGRAccSensor::LSM303AGRAccSensor(DevI2C *i2c, uint8_t address, PinName int1_pin, PinName int2_pin) : |
mapellil | 1:86d530a7f949 | 51 | _dev_i2c(i2c), _address(address), _cs_pin(NC), _int1_pin(int1_pin), _int2_pin(int2_pin) |
nikapov | 0:ec6e59cc6f40 | 52 | { |
mapellil | 1:86d530a7f949 | 53 | assert (i2c); |
nikapov | 0:ec6e59cc6f40 | 54 | }; |
nikapov | 0:ec6e59cc6f40 | 55 | |
nikapov | 0:ec6e59cc6f40 | 56 | /** |
nikapov | 0:ec6e59cc6f40 | 57 | * @brief Initializing the component. |
nikapov | 0:ec6e59cc6f40 | 58 | * @param[in] init pointer to device specific initalization structure. |
nikapov | 0:ec6e59cc6f40 | 59 | * @retval "0" in case of success, an error code otherwise. |
nikapov | 0:ec6e59cc6f40 | 60 | */ |
nikapov | 0:ec6e59cc6f40 | 61 | int LSM303AGRAccSensor::init(void *init) |
nikapov | 0:ec6e59cc6f40 | 62 | { |
nikapov | 0:ec6e59cc6f40 | 63 | /* Enable BDU */ |
nikapov | 0:ec6e59cc6f40 | 64 | if ( LSM303AGR_ACC_W_BlockDataUpdate( (void *)this, LSM303AGR_ACC_BDU_ENABLED ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 65 | { |
nikapov | 0:ec6e59cc6f40 | 66 | return 1; |
nikapov | 0:ec6e59cc6f40 | 67 | } |
nikapov | 0:ec6e59cc6f40 | 68 | |
nikapov | 0:ec6e59cc6f40 | 69 | /* FIFO mode selection */ |
nikapov | 0:ec6e59cc6f40 | 70 | if ( LSM303AGR_ACC_W_FifoMode( (void *)this, LSM303AGR_ACC_FM_BYPASS ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 71 | { |
nikapov | 0:ec6e59cc6f40 | 72 | return 1; |
nikapov | 0:ec6e59cc6f40 | 73 | } |
nikapov | 0:ec6e59cc6f40 | 74 | |
nikapov | 0:ec6e59cc6f40 | 75 | /* Output data rate selection - power down. */ |
nikapov | 0:ec6e59cc6f40 | 76 | if ( LSM303AGR_ACC_W_ODR( (void *)this, LSM303AGR_ACC_ODR_DO_PWR_DOWN ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 77 | { |
nikapov | 0:ec6e59cc6f40 | 78 | return 1; |
nikapov | 0:ec6e59cc6f40 | 79 | } |
nikapov | 0:ec6e59cc6f40 | 80 | |
nikapov | 0:ec6e59cc6f40 | 81 | /* Full scale selection. */ |
nikapov | 0:ec6e59cc6f40 | 82 | if ( set_x_fs( 2.0f ) == 1 ) |
nikapov | 0:ec6e59cc6f40 | 83 | { |
nikapov | 0:ec6e59cc6f40 | 84 | return 1; |
nikapov | 0:ec6e59cc6f40 | 85 | } |
nikapov | 0:ec6e59cc6f40 | 86 | |
nikapov | 0:ec6e59cc6f40 | 87 | /* Enable axes. */ |
nikapov | 0:ec6e59cc6f40 | 88 | if ( LSM303AGR_ACC_W_XEN( (void *)this, LSM303AGR_ACC_XEN_ENABLED ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 89 | { |
nikapov | 0:ec6e59cc6f40 | 90 | return 1; |
nikapov | 0:ec6e59cc6f40 | 91 | } |
nikapov | 0:ec6e59cc6f40 | 92 | |
nikapov | 0:ec6e59cc6f40 | 93 | if ( LSM303AGR_ACC_W_YEN ( (void *)this, LSM303AGR_ACC_YEN_ENABLED ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 94 | { |
nikapov | 0:ec6e59cc6f40 | 95 | return 1; |
nikapov | 0:ec6e59cc6f40 | 96 | } |
nikapov | 0:ec6e59cc6f40 | 97 | |
nikapov | 0:ec6e59cc6f40 | 98 | if ( LSM303AGR_ACC_W_ZEN ( (void *)this, LSM303AGR_ACC_ZEN_ENABLED ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 99 | { |
nikapov | 0:ec6e59cc6f40 | 100 | return 1; |
nikapov | 0:ec6e59cc6f40 | 101 | } |
nikapov | 0:ec6e59cc6f40 | 102 | |
nikapov | 0:ec6e59cc6f40 | 103 | /* Select default output data rate. */ |
nikapov | 0:ec6e59cc6f40 | 104 | _last_odr = 100.0f; |
nikapov | 0:ec6e59cc6f40 | 105 | |
nikapov | 0:ec6e59cc6f40 | 106 | _is_enabled = 0; |
nikapov | 0:ec6e59cc6f40 | 107 | |
nikapov | 0:ec6e59cc6f40 | 108 | return 0; |
nikapov | 0:ec6e59cc6f40 | 109 | } |
nikapov | 0:ec6e59cc6f40 | 110 | |
nikapov | 0:ec6e59cc6f40 | 111 | /** |
nikapov | 0:ec6e59cc6f40 | 112 | * @brief Enable LSM303AGR Accelerator |
nikapov | 0:ec6e59cc6f40 | 113 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 114 | */ |
nikapov | 0:ec6e59cc6f40 | 115 | int LSM303AGRAccSensor::enable(void) |
nikapov | 0:ec6e59cc6f40 | 116 | { |
nikapov | 0:ec6e59cc6f40 | 117 | /* Check if the component is already enabled */ |
nikapov | 0:ec6e59cc6f40 | 118 | if ( _is_enabled == 1 ) |
nikapov | 0:ec6e59cc6f40 | 119 | { |
nikapov | 0:ec6e59cc6f40 | 120 | return 0; |
nikapov | 0:ec6e59cc6f40 | 121 | } |
nikapov | 0:ec6e59cc6f40 | 122 | |
nikapov | 0:ec6e59cc6f40 | 123 | /* Output data rate selection. */ |
nikapov | 0:ec6e59cc6f40 | 124 | if ( set_x_odr_when_enabled( _last_odr ) == 1 ) |
nikapov | 0:ec6e59cc6f40 | 125 | { |
nikapov | 0:ec6e59cc6f40 | 126 | return 1; |
nikapov | 0:ec6e59cc6f40 | 127 | } |
nikapov | 0:ec6e59cc6f40 | 128 | |
nikapov | 0:ec6e59cc6f40 | 129 | _is_enabled = 1; |
nikapov | 0:ec6e59cc6f40 | 130 | |
nikapov | 0:ec6e59cc6f40 | 131 | return 0; |
nikapov | 0:ec6e59cc6f40 | 132 | } |
nikapov | 0:ec6e59cc6f40 | 133 | |
nikapov | 0:ec6e59cc6f40 | 134 | /** |
nikapov | 0:ec6e59cc6f40 | 135 | * @brief Disable LSM303AGR Accelerator |
nikapov | 0:ec6e59cc6f40 | 136 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 137 | */ |
nikapov | 0:ec6e59cc6f40 | 138 | int LSM303AGRAccSensor::disable(void) |
nikapov | 0:ec6e59cc6f40 | 139 | { |
nikapov | 0:ec6e59cc6f40 | 140 | /* Check if the component is already disabled */ |
nikapov | 0:ec6e59cc6f40 | 141 | if ( _is_enabled == 0 ) |
nikapov | 0:ec6e59cc6f40 | 142 | { |
nikapov | 0:ec6e59cc6f40 | 143 | return 0; |
nikapov | 0:ec6e59cc6f40 | 144 | } |
nikapov | 0:ec6e59cc6f40 | 145 | |
nikapov | 0:ec6e59cc6f40 | 146 | /* Store actual output data rate. */ |
nikapov | 0:ec6e59cc6f40 | 147 | if ( get_x_odr( &_last_odr ) == 1 ) |
nikapov | 0:ec6e59cc6f40 | 148 | { |
nikapov | 0:ec6e59cc6f40 | 149 | return 1; |
nikapov | 0:ec6e59cc6f40 | 150 | } |
nikapov | 0:ec6e59cc6f40 | 151 | |
nikapov | 0:ec6e59cc6f40 | 152 | /* Output data rate selection - power down. */ |
nikapov | 0:ec6e59cc6f40 | 153 | if ( LSM303AGR_ACC_W_ODR( (void *)this, LSM303AGR_ACC_ODR_DO_PWR_DOWN ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 154 | { |
nikapov | 0:ec6e59cc6f40 | 155 | return 1; |
nikapov | 0:ec6e59cc6f40 | 156 | } |
nikapov | 0:ec6e59cc6f40 | 157 | |
nikapov | 0:ec6e59cc6f40 | 158 | _is_enabled = 0; |
nikapov | 0:ec6e59cc6f40 | 159 | |
nikapov | 0:ec6e59cc6f40 | 160 | return 0; |
nikapov | 0:ec6e59cc6f40 | 161 | } |
nikapov | 0:ec6e59cc6f40 | 162 | |
nikapov | 0:ec6e59cc6f40 | 163 | /** |
nikapov | 0:ec6e59cc6f40 | 164 | * @brief Read ID of LSM303AGR Accelerometer |
nikapov | 0:ec6e59cc6f40 | 165 | * @param p_id the pointer where the ID of the device is stored |
nikapov | 0:ec6e59cc6f40 | 166 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 167 | */ |
nikapov | 0:ec6e59cc6f40 | 168 | int LSM303AGRAccSensor::read_id(uint8_t *id) |
nikapov | 0:ec6e59cc6f40 | 169 | { |
nikapov | 0:ec6e59cc6f40 | 170 | if(!id) |
nikapov | 0:ec6e59cc6f40 | 171 | { |
nikapov | 0:ec6e59cc6f40 | 172 | return 1; |
nikapov | 0:ec6e59cc6f40 | 173 | } |
nikapov | 0:ec6e59cc6f40 | 174 | |
nikapov | 0:ec6e59cc6f40 | 175 | /* Read WHO AM I register */ |
nikapov | 0:ec6e59cc6f40 | 176 | if ( LSM303AGR_ACC_R_WHO_AM_I( (void *)this, id ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 177 | { |
nikapov | 0:ec6e59cc6f40 | 178 | return 1; |
nikapov | 0:ec6e59cc6f40 | 179 | } |
nikapov | 0:ec6e59cc6f40 | 180 | |
nikapov | 0:ec6e59cc6f40 | 181 | return 0; |
nikapov | 0:ec6e59cc6f40 | 182 | } |
nikapov | 0:ec6e59cc6f40 | 183 | |
nikapov | 0:ec6e59cc6f40 | 184 | /** |
nikapov | 0:ec6e59cc6f40 | 185 | * @brief Read data from LSM303AGR Accelerometer |
nikapov | 0:ec6e59cc6f40 | 186 | * @param pData the pointer where the accelerometer data are stored |
nikapov | 0:ec6e59cc6f40 | 187 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 188 | */ |
nikapov | 0:ec6e59cc6f40 | 189 | int LSM303AGRAccSensor::get_x_axes(int32_t *pData) |
nikapov | 0:ec6e59cc6f40 | 190 | { |
nikapov | 0:ec6e59cc6f40 | 191 | int data[3]; |
nikapov | 0:ec6e59cc6f40 | 192 | |
nikapov | 0:ec6e59cc6f40 | 193 | /* Read data from LSM303AGR. */ |
nikapov | 0:ec6e59cc6f40 | 194 | if ( !LSM303AGR_ACC_Get_Acceleration((void *)this, data) ) |
nikapov | 0:ec6e59cc6f40 | 195 | { |
nikapov | 0:ec6e59cc6f40 | 196 | return 1; |
nikapov | 0:ec6e59cc6f40 | 197 | } |
nikapov | 0:ec6e59cc6f40 | 198 | |
nikapov | 0:ec6e59cc6f40 | 199 | /* Calculate the data. */ |
nikapov | 0:ec6e59cc6f40 | 200 | pData[0] = (int32_t)data[0]; |
nikapov | 0:ec6e59cc6f40 | 201 | pData[1] = (int32_t)data[1]; |
nikapov | 0:ec6e59cc6f40 | 202 | pData[2] = (int32_t)data[2]; |
nikapov | 0:ec6e59cc6f40 | 203 | |
nikapov | 0:ec6e59cc6f40 | 204 | return 0; |
nikapov | 0:ec6e59cc6f40 | 205 | } |
nikapov | 0:ec6e59cc6f40 | 206 | |
nikapov | 0:ec6e59cc6f40 | 207 | /** |
nikapov | 0:ec6e59cc6f40 | 208 | * @brief Read Accelerometer Sensitivity |
nikapov | 0:ec6e59cc6f40 | 209 | * @param pfData the pointer where the accelerometer sensitivity is stored |
nikapov | 0:ec6e59cc6f40 | 210 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 211 | */ |
nikapov | 0:ec6e59cc6f40 | 212 | int LSM303AGRAccSensor::get_x_sensitivity(float *pfData) |
nikapov | 0:ec6e59cc6f40 | 213 | { |
nikapov | 0:ec6e59cc6f40 | 214 | LSM303AGR_ACC_LPEN_t lp_value; |
nikapov | 0:ec6e59cc6f40 | 215 | LSM303AGR_ACC_HR_t hr_value; |
nikapov | 0:ec6e59cc6f40 | 216 | |
nikapov | 0:ec6e59cc6f40 | 217 | /* Read low power flag */ |
nikapov | 0:ec6e59cc6f40 | 218 | if( LSM303AGR_ACC_R_LOWPWR_EN( (void *)this, &lp_value ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 219 | { |
nikapov | 0:ec6e59cc6f40 | 220 | return 1; |
nikapov | 0:ec6e59cc6f40 | 221 | } |
nikapov | 0:ec6e59cc6f40 | 222 | |
nikapov | 0:ec6e59cc6f40 | 223 | /* Read high performance flag */ |
nikapov | 0:ec6e59cc6f40 | 224 | if( LSM303AGR_ACC_R_HiRes( (void *)this, &hr_value ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 225 | { |
nikapov | 0:ec6e59cc6f40 | 226 | return 1; |
nikapov | 0:ec6e59cc6f40 | 227 | } |
nikapov | 0:ec6e59cc6f40 | 228 | |
nikapov | 0:ec6e59cc6f40 | 229 | if( lp_value == LSM303AGR_ACC_LPEN_DISABLED && hr_value == LSM303AGR_ACC_HR_DISABLED ) |
nikapov | 0:ec6e59cc6f40 | 230 | { |
nikapov | 0:ec6e59cc6f40 | 231 | /* Normal Mode */ |
nikapov | 0:ec6e59cc6f40 | 232 | return get_x_sensitivity_normal_mode( pfData ); |
nikapov | 0:ec6e59cc6f40 | 233 | } else if ( lp_value == LSM303AGR_ACC_LPEN_ENABLED && hr_value == LSM303AGR_ACC_HR_DISABLED ) |
nikapov | 0:ec6e59cc6f40 | 234 | { |
nikapov | 0:ec6e59cc6f40 | 235 | /* Low Power Mode */ |
nikapov | 0:ec6e59cc6f40 | 236 | return get_x_sensitivity_lp_mode( pfData ); |
nikapov | 0:ec6e59cc6f40 | 237 | } else if ( lp_value == LSM303AGR_ACC_LPEN_DISABLED && hr_value == LSM303AGR_ACC_HR_ENABLED ) |
nikapov | 0:ec6e59cc6f40 | 238 | { |
nikapov | 0:ec6e59cc6f40 | 239 | /* High Resolution Mode */ |
nikapov | 0:ec6e59cc6f40 | 240 | return get_x_sensitivity_hr_mode( pfData ); |
nikapov | 0:ec6e59cc6f40 | 241 | } else |
nikapov | 0:ec6e59cc6f40 | 242 | { |
nikapov | 0:ec6e59cc6f40 | 243 | /* Not allowed */ |
nikapov | 0:ec6e59cc6f40 | 244 | return 1; |
nikapov | 0:ec6e59cc6f40 | 245 | } |
nikapov | 0:ec6e59cc6f40 | 246 | } |
nikapov | 0:ec6e59cc6f40 | 247 | |
nikapov | 0:ec6e59cc6f40 | 248 | /** |
nikapov | 0:ec6e59cc6f40 | 249 | * @brief Read Accelerometer Sensitivity in Normal Mode |
nikapov | 0:ec6e59cc6f40 | 250 | * @param sensitivity the pointer where the accelerometer sensitivity is stored |
nikapov | 0:ec6e59cc6f40 | 251 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 252 | */ |
nikapov | 0:ec6e59cc6f40 | 253 | int LSM303AGRAccSensor::get_x_sensitivity_normal_mode( float *sensitivity ) |
nikapov | 0:ec6e59cc6f40 | 254 | { |
nikapov | 0:ec6e59cc6f40 | 255 | LSM303AGR_ACC_FS_t fullScale; |
nikapov | 0:ec6e59cc6f40 | 256 | |
nikapov | 0:ec6e59cc6f40 | 257 | /* Read actual full scale selection from sensor. */ |
nikapov | 0:ec6e59cc6f40 | 258 | if ( LSM303AGR_ACC_R_FullScale( (void *)this, &fullScale ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 259 | { |
nikapov | 0:ec6e59cc6f40 | 260 | return 1; |
nikapov | 0:ec6e59cc6f40 | 261 | } |
nikapov | 0:ec6e59cc6f40 | 262 | |
nikapov | 0:ec6e59cc6f40 | 263 | /* Store the sensitivity based on actual full scale. */ |
nikapov | 0:ec6e59cc6f40 | 264 | switch( fullScale ) |
nikapov | 0:ec6e59cc6f40 | 265 | { |
nikapov | 0:ec6e59cc6f40 | 266 | case LSM303AGR_ACC_FS_2G: |
nikapov | 0:ec6e59cc6f40 | 267 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_NORMAL_MODE; |
nikapov | 0:ec6e59cc6f40 | 268 | break; |
nikapov | 0:ec6e59cc6f40 | 269 | case LSM303AGR_ACC_FS_4G: |
nikapov | 0:ec6e59cc6f40 | 270 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_NORMAL_MODE; |
nikapov | 0:ec6e59cc6f40 | 271 | break; |
nikapov | 0:ec6e59cc6f40 | 272 | case LSM303AGR_ACC_FS_8G: |
nikapov | 0:ec6e59cc6f40 | 273 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_NORMAL_MODE; |
nikapov | 0:ec6e59cc6f40 | 274 | break; |
nikapov | 0:ec6e59cc6f40 | 275 | case LSM303AGR_ACC_FS_16G: |
nikapov | 0:ec6e59cc6f40 | 276 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_NORMAL_MODE; |
nikapov | 0:ec6e59cc6f40 | 277 | break; |
nikapov | 0:ec6e59cc6f40 | 278 | default: |
nikapov | 0:ec6e59cc6f40 | 279 | *sensitivity = -1.0f; |
nikapov | 0:ec6e59cc6f40 | 280 | return 1; |
nikapov | 0:ec6e59cc6f40 | 281 | } |
nikapov | 0:ec6e59cc6f40 | 282 | |
nikapov | 0:ec6e59cc6f40 | 283 | return 0; |
nikapov | 0:ec6e59cc6f40 | 284 | } |
nikapov | 0:ec6e59cc6f40 | 285 | |
nikapov | 0:ec6e59cc6f40 | 286 | /** |
nikapov | 0:ec6e59cc6f40 | 287 | * @brief Read Accelerometer Sensitivity in LP Mode |
nikapov | 0:ec6e59cc6f40 | 288 | * @param sensitivity the pointer where the accelerometer sensitivity is stored |
nikapov | 0:ec6e59cc6f40 | 289 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 290 | */ |
nikapov | 0:ec6e59cc6f40 | 291 | int LSM303AGRAccSensor::get_x_sensitivity_lp_mode( float *sensitivity ) |
nikapov | 0:ec6e59cc6f40 | 292 | { |
nikapov | 0:ec6e59cc6f40 | 293 | LSM303AGR_ACC_FS_t fullScale; |
nikapov | 0:ec6e59cc6f40 | 294 | |
nikapov | 0:ec6e59cc6f40 | 295 | /* Read actual full scale selection from sensor. */ |
nikapov | 0:ec6e59cc6f40 | 296 | if ( LSM303AGR_ACC_R_FullScale( (void *)this, &fullScale ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 297 | { |
nikapov | 0:ec6e59cc6f40 | 298 | return 1; |
nikapov | 0:ec6e59cc6f40 | 299 | } |
nikapov | 0:ec6e59cc6f40 | 300 | |
nikapov | 0:ec6e59cc6f40 | 301 | /* Store the sensitivity based on actual full scale. */ |
nikapov | 0:ec6e59cc6f40 | 302 | switch( fullScale ) |
nikapov | 0:ec6e59cc6f40 | 303 | { |
nikapov | 0:ec6e59cc6f40 | 304 | case LSM303AGR_ACC_FS_2G: |
nikapov | 0:ec6e59cc6f40 | 305 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_LOW_POWER_MODE; |
nikapov | 0:ec6e59cc6f40 | 306 | break; |
nikapov | 0:ec6e59cc6f40 | 307 | case LSM303AGR_ACC_FS_4G: |
nikapov | 0:ec6e59cc6f40 | 308 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_LOW_POWER_MODE; |
nikapov | 0:ec6e59cc6f40 | 309 | break; |
nikapov | 0:ec6e59cc6f40 | 310 | case LSM303AGR_ACC_FS_8G: |
nikapov | 0:ec6e59cc6f40 | 311 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_LOW_POWER_MODE; |
nikapov | 0:ec6e59cc6f40 | 312 | break; |
nikapov | 0:ec6e59cc6f40 | 313 | case LSM303AGR_ACC_FS_16G: |
nikapov | 0:ec6e59cc6f40 | 314 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_LOW_POWER_MODE; |
nikapov | 0:ec6e59cc6f40 | 315 | break; |
nikapov | 0:ec6e59cc6f40 | 316 | default: |
nikapov | 0:ec6e59cc6f40 | 317 | *sensitivity = -1.0f; |
nikapov | 0:ec6e59cc6f40 | 318 | return 1; |
nikapov | 0:ec6e59cc6f40 | 319 | } |
nikapov | 0:ec6e59cc6f40 | 320 | |
nikapov | 0:ec6e59cc6f40 | 321 | return 0; |
nikapov | 0:ec6e59cc6f40 | 322 | } |
nikapov | 0:ec6e59cc6f40 | 323 | |
nikapov | 0:ec6e59cc6f40 | 324 | /** |
nikapov | 0:ec6e59cc6f40 | 325 | * @brief Read Accelerometer Sensitivity in HR Mode |
nikapov | 0:ec6e59cc6f40 | 326 | * @param sensitivity the pointer where the accelerometer sensitivity is stored |
nikapov | 0:ec6e59cc6f40 | 327 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 328 | */ |
nikapov | 0:ec6e59cc6f40 | 329 | int LSM303AGRAccSensor::get_x_sensitivity_hr_mode( float *sensitivity ) |
nikapov | 0:ec6e59cc6f40 | 330 | { |
nikapov | 0:ec6e59cc6f40 | 331 | LSM303AGR_ACC_FS_t fullScale; |
nikapov | 0:ec6e59cc6f40 | 332 | |
nikapov | 0:ec6e59cc6f40 | 333 | /* Read actual full scale selection from sensor. */ |
nikapov | 0:ec6e59cc6f40 | 334 | if ( LSM303AGR_ACC_R_FullScale( (void *)this, &fullScale ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 335 | { |
nikapov | 0:ec6e59cc6f40 | 336 | return 1; |
nikapov | 0:ec6e59cc6f40 | 337 | } |
nikapov | 0:ec6e59cc6f40 | 338 | |
nikapov | 0:ec6e59cc6f40 | 339 | /* Store the sensitivity based on actual full scale. */ |
nikapov | 0:ec6e59cc6f40 | 340 | switch( fullScale ) |
nikapov | 0:ec6e59cc6f40 | 341 | { |
nikapov | 0:ec6e59cc6f40 | 342 | case LSM303AGR_ACC_FS_2G: |
nikapov | 0:ec6e59cc6f40 | 343 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_HIGH_RESOLUTION_MODE; |
nikapov | 0:ec6e59cc6f40 | 344 | break; |
nikapov | 0:ec6e59cc6f40 | 345 | case LSM303AGR_ACC_FS_4G: |
nikapov | 0:ec6e59cc6f40 | 346 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_HIGH_RESOLUTION_MODE; |
nikapov | 0:ec6e59cc6f40 | 347 | break; |
nikapov | 0:ec6e59cc6f40 | 348 | case LSM303AGR_ACC_FS_8G: |
nikapov | 0:ec6e59cc6f40 | 349 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_HIGH_RESOLUTION_MODE; |
nikapov | 0:ec6e59cc6f40 | 350 | break; |
nikapov | 0:ec6e59cc6f40 | 351 | case LSM303AGR_ACC_FS_16G: |
nikapov | 0:ec6e59cc6f40 | 352 | *sensitivity = ( float )LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_HIGH_RESOLUTION_MODE; |
nikapov | 0:ec6e59cc6f40 | 353 | break; |
nikapov | 0:ec6e59cc6f40 | 354 | default: |
nikapov | 0:ec6e59cc6f40 | 355 | *sensitivity = -1.0f; |
nikapov | 0:ec6e59cc6f40 | 356 | return 1; |
nikapov | 0:ec6e59cc6f40 | 357 | } |
nikapov | 0:ec6e59cc6f40 | 358 | |
nikapov | 0:ec6e59cc6f40 | 359 | return 0; |
nikapov | 0:ec6e59cc6f40 | 360 | } |
nikapov | 0:ec6e59cc6f40 | 361 | |
nikapov | 0:ec6e59cc6f40 | 362 | /** |
nikapov | 0:ec6e59cc6f40 | 363 | * @brief Read raw data from LSM303AGR Accelerometer |
nikapov | 0:ec6e59cc6f40 | 364 | * @param pData the pointer where the accelerometer raw data are stored |
nikapov | 0:ec6e59cc6f40 | 365 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 366 | */ |
nikapov | 0:ec6e59cc6f40 | 367 | int LSM303AGRAccSensor::get_x_axes_raw(int16_t *pData) |
nikapov | 0:ec6e59cc6f40 | 368 | { |
nikapov | 0:ec6e59cc6f40 | 369 | uint8_t regValue[6] = {0, 0, 0, 0, 0, 0}; |
nikapov | 0:ec6e59cc6f40 | 370 | u8_t shift = 0; |
nikapov | 0:ec6e59cc6f40 | 371 | LSM303AGR_ACC_LPEN_t lp; |
nikapov | 0:ec6e59cc6f40 | 372 | LSM303AGR_ACC_HR_t hr; |
nikapov | 0:ec6e59cc6f40 | 373 | |
nikapov | 0:ec6e59cc6f40 | 374 | /* Determine which operational mode the acc is set */ |
nikapov | 0:ec6e59cc6f40 | 375 | if(!LSM303AGR_ACC_R_HiRes( (void *)this, &hr )) { |
nikapov | 0:ec6e59cc6f40 | 376 | return 1; |
nikapov | 0:ec6e59cc6f40 | 377 | } |
nikapov | 0:ec6e59cc6f40 | 378 | |
nikapov | 0:ec6e59cc6f40 | 379 | if(!LSM303AGR_ACC_R_LOWPWR_EN( (void *)this, &lp )) { |
nikapov | 0:ec6e59cc6f40 | 380 | return 1; |
nikapov | 0:ec6e59cc6f40 | 381 | } |
nikapov | 0:ec6e59cc6f40 | 382 | |
nikapov | 0:ec6e59cc6f40 | 383 | if (lp == LSM303AGR_ACC_LPEN_ENABLED && hr == LSM303AGR_ACC_HR_DISABLED) { |
nikapov | 0:ec6e59cc6f40 | 384 | /* op mode is LP 8-bit */ |
nikapov | 0:ec6e59cc6f40 | 385 | shift = 8; |
nikapov | 0:ec6e59cc6f40 | 386 | } else if (lp == LSM303AGR_ACC_LPEN_DISABLED && hr == LSM303AGR_ACC_HR_DISABLED) { |
nikapov | 0:ec6e59cc6f40 | 387 | /* op mode is Normal 10-bit */ |
nikapov | 0:ec6e59cc6f40 | 388 | shift = 6; |
nikapov | 0:ec6e59cc6f40 | 389 | } else if (lp == LSM303AGR_ACC_LPEN_DISABLED && hr == LSM303AGR_ACC_HR_ENABLED) { |
nikapov | 0:ec6e59cc6f40 | 390 | /* op mode is HR 12-bit */ |
nikapov | 0:ec6e59cc6f40 | 391 | shift = 4; |
nikapov | 0:ec6e59cc6f40 | 392 | } else { |
nikapov | 0:ec6e59cc6f40 | 393 | return 1; |
nikapov | 0:ec6e59cc6f40 | 394 | } |
nikapov | 0:ec6e59cc6f40 | 395 | |
nikapov | 0:ec6e59cc6f40 | 396 | /* Read output registers from LSM303AGR_ACC_GYRO_OUTX_L_XL to LSM303AGR_ACC_GYRO_OUTZ_H_XL. */ |
nikapov | 0:ec6e59cc6f40 | 397 | if (!LSM303AGR_ACC_Get_Raw_Acceleration( (void *)this, ( uint8_t* )regValue )) |
nikapov | 0:ec6e59cc6f40 | 398 | { |
nikapov | 0:ec6e59cc6f40 | 399 | return 1; |
nikapov | 0:ec6e59cc6f40 | 400 | } |
nikapov | 0:ec6e59cc6f40 | 401 | |
nikapov | 0:ec6e59cc6f40 | 402 | /* Format the data. */ |
nikapov | 0:ec6e59cc6f40 | 403 | pData[0] = ( ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] ) >> shift ); |
nikapov | 0:ec6e59cc6f40 | 404 | pData[1] = ( ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] ) >> shift ); |
nikapov | 0:ec6e59cc6f40 | 405 | pData[2] = ( ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] ) >> shift ); |
nikapov | 0:ec6e59cc6f40 | 406 | |
nikapov | 0:ec6e59cc6f40 | 407 | return 0; |
nikapov | 0:ec6e59cc6f40 | 408 | } |
nikapov | 0:ec6e59cc6f40 | 409 | |
nikapov | 0:ec6e59cc6f40 | 410 | /** |
nikapov | 0:ec6e59cc6f40 | 411 | * @brief Read LSM303AGR Accelerometer output data rate |
nikapov | 0:ec6e59cc6f40 | 412 | * @param odr the pointer to the output data rate |
nikapov | 0:ec6e59cc6f40 | 413 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 414 | */ |
nikapov | 0:ec6e59cc6f40 | 415 | int LSM303AGRAccSensor::get_x_odr(float* odr) |
nikapov | 0:ec6e59cc6f40 | 416 | { |
nikapov | 0:ec6e59cc6f40 | 417 | LSM303AGR_ACC_ODR_t odr_low_level; |
nikapov | 0:ec6e59cc6f40 | 418 | |
nikapov | 0:ec6e59cc6f40 | 419 | if ( LSM303AGR_ACC_R_ODR( (void *)this, &odr_low_level ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 420 | { |
nikapov | 0:ec6e59cc6f40 | 421 | return 1; |
nikapov | 0:ec6e59cc6f40 | 422 | } |
nikapov | 0:ec6e59cc6f40 | 423 | |
nikapov | 0:ec6e59cc6f40 | 424 | switch( odr_low_level ) |
nikapov | 0:ec6e59cc6f40 | 425 | { |
nikapov | 0:ec6e59cc6f40 | 426 | case LSM303AGR_ACC_ODR_DO_PWR_DOWN: |
nikapov | 0:ec6e59cc6f40 | 427 | *odr = 0.0f; |
nikapov | 0:ec6e59cc6f40 | 428 | break; |
nikapov | 0:ec6e59cc6f40 | 429 | case LSM303AGR_ACC_ODR_DO_1Hz: |
nikapov | 0:ec6e59cc6f40 | 430 | *odr = 1.0f; |
nikapov | 0:ec6e59cc6f40 | 431 | break; |
nikapov | 0:ec6e59cc6f40 | 432 | case LSM303AGR_ACC_ODR_DO_10Hz: |
nikapov | 0:ec6e59cc6f40 | 433 | *odr = 10.0f; |
nikapov | 0:ec6e59cc6f40 | 434 | break; |
nikapov | 0:ec6e59cc6f40 | 435 | case LSM303AGR_ACC_ODR_DO_25Hz: |
nikapov | 0:ec6e59cc6f40 | 436 | *odr = 25.0f; |
nikapov | 0:ec6e59cc6f40 | 437 | break; |
nikapov | 0:ec6e59cc6f40 | 438 | case LSM303AGR_ACC_ODR_DO_50Hz: |
nikapov | 0:ec6e59cc6f40 | 439 | *odr = 50.0f; |
nikapov | 0:ec6e59cc6f40 | 440 | break; |
nikapov | 0:ec6e59cc6f40 | 441 | case LSM303AGR_ACC_ODR_DO_100Hz: |
nikapov | 0:ec6e59cc6f40 | 442 | *odr = 100.0f; |
nikapov | 0:ec6e59cc6f40 | 443 | break; |
nikapov | 0:ec6e59cc6f40 | 444 | case LSM303AGR_ACC_ODR_DO_200Hz: |
nikapov | 0:ec6e59cc6f40 | 445 | *odr = 200.0f; |
nikapov | 0:ec6e59cc6f40 | 446 | break; |
nikapov | 0:ec6e59cc6f40 | 447 | case LSM303AGR_ACC_ODR_DO_400Hz: |
nikapov | 0:ec6e59cc6f40 | 448 | *odr = 400.0f; |
nikapov | 0:ec6e59cc6f40 | 449 | break; |
nikapov | 0:ec6e59cc6f40 | 450 | default: |
nikapov | 0:ec6e59cc6f40 | 451 | *odr = -1.0f; |
nikapov | 0:ec6e59cc6f40 | 452 | return 1; |
nikapov | 0:ec6e59cc6f40 | 453 | } |
nikapov | 0:ec6e59cc6f40 | 454 | |
nikapov | 0:ec6e59cc6f40 | 455 | return 0; |
nikapov | 0:ec6e59cc6f40 | 456 | } |
nikapov | 0:ec6e59cc6f40 | 457 | |
nikapov | 0:ec6e59cc6f40 | 458 | /** |
nikapov | 0:ec6e59cc6f40 | 459 | * @brief Set ODR |
nikapov | 0:ec6e59cc6f40 | 460 | * @param odr the output data rate to be set |
nikapov | 0:ec6e59cc6f40 | 461 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 462 | */ |
nikapov | 0:ec6e59cc6f40 | 463 | int LSM303AGRAccSensor::set_x_odr(float odr) |
nikapov | 0:ec6e59cc6f40 | 464 | { |
nikapov | 0:ec6e59cc6f40 | 465 | if(_is_enabled == 1) |
nikapov | 0:ec6e59cc6f40 | 466 | { |
nikapov | 0:ec6e59cc6f40 | 467 | if(set_x_odr_when_enabled(odr) == 1) |
nikapov | 0:ec6e59cc6f40 | 468 | { |
nikapov | 0:ec6e59cc6f40 | 469 | return 1; |
nikapov | 0:ec6e59cc6f40 | 470 | } |
nikapov | 0:ec6e59cc6f40 | 471 | } |
nikapov | 0:ec6e59cc6f40 | 472 | else |
nikapov | 0:ec6e59cc6f40 | 473 | { |
nikapov | 0:ec6e59cc6f40 | 474 | if(set_x_odr_when_disabled(odr) == 1) |
nikapov | 0:ec6e59cc6f40 | 475 | { |
nikapov | 0:ec6e59cc6f40 | 476 | return 1; |
nikapov | 0:ec6e59cc6f40 | 477 | } |
nikapov | 0:ec6e59cc6f40 | 478 | } |
nikapov | 0:ec6e59cc6f40 | 479 | |
nikapov | 0:ec6e59cc6f40 | 480 | return 0; |
nikapov | 0:ec6e59cc6f40 | 481 | } |
nikapov | 0:ec6e59cc6f40 | 482 | |
nikapov | 0:ec6e59cc6f40 | 483 | /** |
nikapov | 0:ec6e59cc6f40 | 484 | * @brief Set ODR when enabled |
nikapov | 0:ec6e59cc6f40 | 485 | * @param odr the output data rate to be set |
nikapov | 0:ec6e59cc6f40 | 486 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 487 | */ |
nikapov | 0:ec6e59cc6f40 | 488 | int LSM303AGRAccSensor::set_x_odr_when_enabled(float odr) |
nikapov | 0:ec6e59cc6f40 | 489 | { |
nikapov | 0:ec6e59cc6f40 | 490 | LSM303AGR_ACC_ODR_t new_odr; |
nikapov | 0:ec6e59cc6f40 | 491 | |
nikapov | 0:ec6e59cc6f40 | 492 | new_odr = ( odr <= 1.0f ) ? LSM303AGR_ACC_ODR_DO_1Hz |
nikapov | 0:ec6e59cc6f40 | 493 | : ( odr <= 10.0f ) ? LSM303AGR_ACC_ODR_DO_10Hz |
nikapov | 0:ec6e59cc6f40 | 494 | : ( odr <= 25.0f ) ? LSM303AGR_ACC_ODR_DO_25Hz |
nikapov | 0:ec6e59cc6f40 | 495 | : ( odr <= 50.0f ) ? LSM303AGR_ACC_ODR_DO_50Hz |
nikapov | 0:ec6e59cc6f40 | 496 | : ( odr <= 100.0f ) ? LSM303AGR_ACC_ODR_DO_100Hz |
nikapov | 0:ec6e59cc6f40 | 497 | : ( odr <= 200.0f ) ? LSM303AGR_ACC_ODR_DO_200Hz |
nikapov | 0:ec6e59cc6f40 | 498 | : LSM303AGR_ACC_ODR_DO_400Hz; |
nikapov | 0:ec6e59cc6f40 | 499 | |
nikapov | 0:ec6e59cc6f40 | 500 | if ( LSM303AGR_ACC_W_ODR( (void *)this, new_odr ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 501 | { |
nikapov | 0:ec6e59cc6f40 | 502 | return 1; |
nikapov | 0:ec6e59cc6f40 | 503 | } |
nikapov | 0:ec6e59cc6f40 | 504 | |
nikapov | 0:ec6e59cc6f40 | 505 | return 0; |
nikapov | 0:ec6e59cc6f40 | 506 | } |
nikapov | 0:ec6e59cc6f40 | 507 | |
nikapov | 0:ec6e59cc6f40 | 508 | /** |
nikapov | 0:ec6e59cc6f40 | 509 | * @brief Set ODR when disabled |
nikapov | 0:ec6e59cc6f40 | 510 | * @param odr the output data rate to be set |
nikapov | 0:ec6e59cc6f40 | 511 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 512 | */ |
nikapov | 0:ec6e59cc6f40 | 513 | int LSM303AGRAccSensor::set_x_odr_when_disabled(float odr) |
nikapov | 0:ec6e59cc6f40 | 514 | { |
nikapov | 0:ec6e59cc6f40 | 515 | _last_odr = ( odr <= 1.0f ) ? 1.0f |
nikapov | 0:ec6e59cc6f40 | 516 | : ( odr <= 10.0f ) ? 10.0f |
nikapov | 0:ec6e59cc6f40 | 517 | : ( odr <= 25.0f ) ? 25.0f |
nikapov | 0:ec6e59cc6f40 | 518 | : ( odr <= 50.0f ) ? 50.0f |
nikapov | 0:ec6e59cc6f40 | 519 | : ( odr <= 100.0f ) ? 100.0f |
nikapov | 0:ec6e59cc6f40 | 520 | : ( odr <= 200.0f ) ? 200.0f |
nikapov | 0:ec6e59cc6f40 | 521 | : 400.0f; |
nikapov | 0:ec6e59cc6f40 | 522 | |
nikapov | 0:ec6e59cc6f40 | 523 | return 0; |
nikapov | 0:ec6e59cc6f40 | 524 | } |
nikapov | 0:ec6e59cc6f40 | 525 | |
nikapov | 0:ec6e59cc6f40 | 526 | |
nikapov | 0:ec6e59cc6f40 | 527 | /** |
nikapov | 0:ec6e59cc6f40 | 528 | * @brief Read LSM303AGR Accelerometer full scale |
nikapov | 0:ec6e59cc6f40 | 529 | * @param fullScale the pointer to the full scale |
nikapov | 0:ec6e59cc6f40 | 530 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 531 | */ |
nikapov | 0:ec6e59cc6f40 | 532 | int LSM303AGRAccSensor::get_x_fs(float* fullScale) |
nikapov | 0:ec6e59cc6f40 | 533 | { |
nikapov | 0:ec6e59cc6f40 | 534 | LSM303AGR_ACC_FS_t fs_low_level; |
nikapov | 0:ec6e59cc6f40 | 535 | |
nikapov | 0:ec6e59cc6f40 | 536 | if ( LSM303AGR_ACC_R_FullScale( (void *)this, &fs_low_level ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 537 | { |
nikapov | 0:ec6e59cc6f40 | 538 | return 1; |
nikapov | 0:ec6e59cc6f40 | 539 | } |
nikapov | 0:ec6e59cc6f40 | 540 | |
nikapov | 0:ec6e59cc6f40 | 541 | switch( fs_low_level ) |
nikapov | 0:ec6e59cc6f40 | 542 | { |
nikapov | 0:ec6e59cc6f40 | 543 | case LSM303AGR_ACC_FS_2G: |
nikapov | 0:ec6e59cc6f40 | 544 | *fullScale = 2.0f; |
nikapov | 0:ec6e59cc6f40 | 545 | break; |
nikapov | 0:ec6e59cc6f40 | 546 | case LSM303AGR_ACC_FS_4G: |
nikapov | 0:ec6e59cc6f40 | 547 | *fullScale = 4.0f; |
nikapov | 0:ec6e59cc6f40 | 548 | break; |
nikapov | 0:ec6e59cc6f40 | 549 | case LSM303AGR_ACC_FS_8G: |
nikapov | 0:ec6e59cc6f40 | 550 | *fullScale = 8.0f; |
nikapov | 0:ec6e59cc6f40 | 551 | break; |
nikapov | 0:ec6e59cc6f40 | 552 | case LSM303AGR_ACC_FS_16G: |
nikapov | 0:ec6e59cc6f40 | 553 | *fullScale = 16.0f; |
nikapov | 0:ec6e59cc6f40 | 554 | break; |
nikapov | 0:ec6e59cc6f40 | 555 | default: |
nikapov | 0:ec6e59cc6f40 | 556 | *fullScale = -1.0f; |
nikapov | 0:ec6e59cc6f40 | 557 | return 1; |
nikapov | 0:ec6e59cc6f40 | 558 | } |
nikapov | 0:ec6e59cc6f40 | 559 | |
nikapov | 0:ec6e59cc6f40 | 560 | return 0; |
nikapov | 0:ec6e59cc6f40 | 561 | } |
nikapov | 0:ec6e59cc6f40 | 562 | |
nikapov | 0:ec6e59cc6f40 | 563 | /** |
nikapov | 0:ec6e59cc6f40 | 564 | * @brief Set full scale |
nikapov | 0:ec6e59cc6f40 | 565 | * @param fullScale the full scale to be set |
nikapov | 0:ec6e59cc6f40 | 566 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:ec6e59cc6f40 | 567 | */ |
nikapov | 0:ec6e59cc6f40 | 568 | int LSM303AGRAccSensor::set_x_fs(float fullScale) |
nikapov | 0:ec6e59cc6f40 | 569 | { |
nikapov | 0:ec6e59cc6f40 | 570 | LSM303AGR_ACC_FS_t new_fs; |
nikapov | 0:ec6e59cc6f40 | 571 | |
nikapov | 0:ec6e59cc6f40 | 572 | new_fs = ( fullScale <= 2.0f ) ? LSM303AGR_ACC_FS_2G |
nikapov | 0:ec6e59cc6f40 | 573 | : ( fullScale <= 4.0f ) ? LSM303AGR_ACC_FS_4G |
nikapov | 0:ec6e59cc6f40 | 574 | : ( fullScale <= 8.0f ) ? LSM303AGR_ACC_FS_8G |
nikapov | 0:ec6e59cc6f40 | 575 | : LSM303AGR_ACC_FS_16G; |
nikapov | 0:ec6e59cc6f40 | 576 | |
nikapov | 0:ec6e59cc6f40 | 577 | if ( LSM303AGR_ACC_W_FullScale( (void *)this, new_fs ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 578 | { |
nikapov | 0:ec6e59cc6f40 | 579 | return 1; |
nikapov | 0:ec6e59cc6f40 | 580 | } |
nikapov | 0:ec6e59cc6f40 | 581 | |
nikapov | 0:ec6e59cc6f40 | 582 | return 0; |
nikapov | 0:ec6e59cc6f40 | 583 | } |
nikapov | 0:ec6e59cc6f40 | 584 | |
nikapov | 0:ec6e59cc6f40 | 585 | /** |
nikapov | 0:ec6e59cc6f40 | 586 | * @brief Read accelerometer data from register |
nikapov | 0:ec6e59cc6f40 | 587 | * @param reg register address |
nikapov | 0:ec6e59cc6f40 | 588 | * @param data register data |
nikapov | 0:ec6e59cc6f40 | 589 | * @retval 0 in case of success |
nikapov | 0:ec6e59cc6f40 | 590 | * @retval 1 in case of failure |
nikapov | 0:ec6e59cc6f40 | 591 | */ |
nikapov | 0:ec6e59cc6f40 | 592 | int LSM303AGRAccSensor::read_reg( uint8_t reg, uint8_t *data ) |
nikapov | 0:ec6e59cc6f40 | 593 | { |
nikapov | 0:ec6e59cc6f40 | 594 | |
nikapov | 0:ec6e59cc6f40 | 595 | if ( LSM303AGR_ACC_read_reg( (void *)this, reg, data ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 596 | { |
nikapov | 0:ec6e59cc6f40 | 597 | return 1; |
nikapov | 0:ec6e59cc6f40 | 598 | } |
nikapov | 0:ec6e59cc6f40 | 599 | |
nikapov | 0:ec6e59cc6f40 | 600 | return 0; |
nikapov | 0:ec6e59cc6f40 | 601 | } |
nikapov | 0:ec6e59cc6f40 | 602 | |
nikapov | 0:ec6e59cc6f40 | 603 | /** |
nikapov | 0:ec6e59cc6f40 | 604 | * @brief Write accelerometer data to register |
nikapov | 0:ec6e59cc6f40 | 605 | * @param reg register address |
nikapov | 0:ec6e59cc6f40 | 606 | * @param data register data |
nikapov | 0:ec6e59cc6f40 | 607 | * @retval 0 in case of success |
nikapov | 0:ec6e59cc6f40 | 608 | * @retval 1 in case of failure |
nikapov | 0:ec6e59cc6f40 | 609 | */ |
nikapov | 0:ec6e59cc6f40 | 610 | int LSM303AGRAccSensor::write_reg( uint8_t reg, uint8_t data ) |
nikapov | 0:ec6e59cc6f40 | 611 | { |
nikapov | 0:ec6e59cc6f40 | 612 | |
nikapov | 0:ec6e59cc6f40 | 613 | if ( LSM303AGR_ACC_write_reg( (void *)this, reg, data ) == MEMS_ERROR ) |
nikapov | 0:ec6e59cc6f40 | 614 | { |
nikapov | 0:ec6e59cc6f40 | 615 | return 1; |
nikapov | 0:ec6e59cc6f40 | 616 | } |
nikapov | 0:ec6e59cc6f40 | 617 | |
nikapov | 0:ec6e59cc6f40 | 618 | return 0; |
nikapov | 0:ec6e59cc6f40 | 619 | } |
nikapov | 0:ec6e59cc6f40 | 620 | |
nikapov | 0:ec6e59cc6f40 | 621 | uint8_t LSM303AGR_ACC_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
nikapov | 0:ec6e59cc6f40 | 622 | { |
nikapov | 0:ec6e59cc6f40 | 623 | return ((LSM303AGRAccSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
nikapov | 0:ec6e59cc6f40 | 624 | } |
nikapov | 0:ec6e59cc6f40 | 625 | |
nikapov | 0:ec6e59cc6f40 | 626 | uint8_t LSM303AGR_ACC_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
nikapov | 0:ec6e59cc6f40 | 627 | { |
nikapov | 0:ec6e59cc6f40 | 628 | return ((LSM303AGRAccSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
nikapov | 0:ec6e59cc6f40 | 629 | } |