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