Example of tilt detection for LSM6DSL in X-NUCLEO-IKS01A2
Dependencies: X_NUCLEO_IKS01A2 mbed
Fork of Tilt_IKS01A2 by
Tilt Detection Demo Application based on sensor expansion board X-NUCLEO-IKS01A2
Main function is to show how to detect the tilt event using the sensor expansion board and send a notification using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can try to tilt the board and then view the notification using an hyper terminal. When the tilt event is detected, the LED is switched on for a while.
- the user button can be used to enable/disable the tilt detection feature.
X_NUCLEO_IKS01A2/Components/LSM303AGRSensor/LSM303AGR_MAG_Sensor.cpp@0:489965565a0d, 2016-08-12 (annotated)
- Committer:
- cparata
- Date:
- Fri Aug 12 13:42:49 2016 +0000
- Revision:
- 0:489965565a0d
First release of Tilt Detection for LSM6DSL in IKS01A2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cparata | 0:489965565a0d | 1 | /** |
| cparata | 0:489965565a0d | 2 | ****************************************************************************** |
| cparata | 0:489965565a0d | 3 | * @file LSM303AGR_MAG_Sensor.cpp |
| cparata | 0:489965565a0d | 4 | * @author AST |
| cparata | 0:489965565a0d | 5 | * @version V1.0.0 |
| cparata | 0:489965565a0d | 6 | * @date 5 August 2016 |
| cparata | 0:489965565a0d | 7 | * @brief Implementation an LSM303AGR magnetometer sensor. |
| cparata | 0:489965565a0d | 8 | ****************************************************************************** |
| cparata | 0:489965565a0d | 9 | * @attention |
| cparata | 0:489965565a0d | 10 | * |
| cparata | 0:489965565a0d | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| cparata | 0:489965565a0d | 12 | * |
| cparata | 0:489965565a0d | 13 | * Redistribution and use in source and binary forms, with or without modification, |
| cparata | 0:489965565a0d | 14 | * are permitted provided that the following conditions are met: |
| cparata | 0:489965565a0d | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
| cparata | 0:489965565a0d | 16 | * this list of conditions and the following disclaimer. |
| cparata | 0:489965565a0d | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| cparata | 0:489965565a0d | 18 | * this list of conditions and the following disclaimer in the documentation |
| cparata | 0:489965565a0d | 19 | * and/or other materials provided with the distribution. |
| cparata | 0:489965565a0d | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| cparata | 0:489965565a0d | 21 | * may be used to endorse or promote products derived from this software |
| cparata | 0:489965565a0d | 22 | * without specific prior written permission. |
| cparata | 0:489965565a0d | 23 | * |
| cparata | 0:489965565a0d | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| cparata | 0:489965565a0d | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| cparata | 0:489965565a0d | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| cparata | 0:489965565a0d | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| cparata | 0:489965565a0d | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| cparata | 0:489965565a0d | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| cparata | 0:489965565a0d | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| cparata | 0:489965565a0d | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| cparata | 0:489965565a0d | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| cparata | 0:489965565a0d | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| cparata | 0:489965565a0d | 34 | * |
| cparata | 0:489965565a0d | 35 | ****************************************************************************** |
| cparata | 0:489965565a0d | 36 | */ |
| cparata | 0:489965565a0d | 37 | |
| cparata | 0:489965565a0d | 38 | |
| cparata | 0:489965565a0d | 39 | /* Includes ------------------------------------------------------------------*/ |
| cparata | 0:489965565a0d | 40 | |
| cparata | 0:489965565a0d | 41 | #include "mbed.h" |
| cparata | 0:489965565a0d | 42 | #include "DevI2C.h" |
| cparata | 0:489965565a0d | 43 | #include "LSM303AGR_MAG_Sensor.h" |
| cparata | 0:489965565a0d | 44 | #include "LSM303AGR_MAG_driver.h" |
| cparata | 0:489965565a0d | 45 | |
| cparata | 0:489965565a0d | 46 | |
| cparata | 0:489965565a0d | 47 | /* Class Implementation ------------------------------------------------------*/ |
| cparata | 0:489965565a0d | 48 | |
| cparata | 0:489965565a0d | 49 | /** Constructor |
| cparata | 0:489965565a0d | 50 | * @param i2c object of an helper class which handles the I2C peripheral |
| cparata | 0:489965565a0d | 51 | * @param address the address of the component's instance |
| cparata | 0:489965565a0d | 52 | */ |
| cparata | 0:489965565a0d | 53 | LSM303AGR_MAG_Sensor::LSM303AGR_MAG_Sensor(DevI2C &i2c) : dev_i2c(i2c) |
| cparata | 0:489965565a0d | 54 | { |
| cparata | 0:489965565a0d | 55 | address = LSM303AGR_MAG_I2C_ADDRESS; |
| cparata | 0:489965565a0d | 56 | |
| cparata | 0:489965565a0d | 57 | /* Operating mode selection - power down */ |
| cparata | 0:489965565a0d | 58 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 59 | { |
| cparata | 0:489965565a0d | 60 | return; |
| cparata | 0:489965565a0d | 61 | } |
| cparata | 0:489965565a0d | 62 | |
| cparata | 0:489965565a0d | 63 | /* Enable BDU */ |
| cparata | 0:489965565a0d | 64 | if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 65 | { |
| cparata | 0:489965565a0d | 66 | return; |
| cparata | 0:489965565a0d | 67 | } |
| cparata | 0:489965565a0d | 68 | |
| cparata | 0:489965565a0d | 69 | if ( SetODR( 100.0f ) == LSM303AGR_MAG_STATUS_ERROR ) |
| cparata | 0:489965565a0d | 70 | { |
| cparata | 0:489965565a0d | 71 | return; |
| cparata | 0:489965565a0d | 72 | } |
| cparata | 0:489965565a0d | 73 | |
| cparata | 0:489965565a0d | 74 | if ( SetFS( 50.0f ) == LSM303AGR_MAG_STATUS_ERROR ) |
| cparata | 0:489965565a0d | 75 | { |
| cparata | 0:489965565a0d | 76 | return; |
| cparata | 0:489965565a0d | 77 | } |
| cparata | 0:489965565a0d | 78 | |
| cparata | 0:489965565a0d | 79 | if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 80 | { |
| cparata | 0:489965565a0d | 81 | return; |
| cparata | 0:489965565a0d | 82 | } |
| cparata | 0:489965565a0d | 83 | }; |
| cparata | 0:489965565a0d | 84 | |
| cparata | 0:489965565a0d | 85 | /** Constructor |
| cparata | 0:489965565a0d | 86 | * @param i2c object of an helper class which handles the I2C peripheral |
| cparata | 0:489965565a0d | 87 | * @param address the address of the component's instance |
| cparata | 0:489965565a0d | 88 | */ |
| cparata | 0:489965565a0d | 89 | LSM303AGR_MAG_Sensor::LSM303AGR_MAG_Sensor(DevI2C &i2c, uint8_t address) : dev_i2c(i2c), address(address) |
| cparata | 0:489965565a0d | 90 | { |
| cparata | 0:489965565a0d | 91 | /* Operating mode selection - power down */ |
| cparata | 0:489965565a0d | 92 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 93 | { |
| cparata | 0:489965565a0d | 94 | return; |
| cparata | 0:489965565a0d | 95 | } |
| cparata | 0:489965565a0d | 96 | |
| cparata | 0:489965565a0d | 97 | /* Enable BDU */ |
| cparata | 0:489965565a0d | 98 | if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 99 | { |
| cparata | 0:489965565a0d | 100 | return; |
| cparata | 0:489965565a0d | 101 | } |
| cparata | 0:489965565a0d | 102 | |
| cparata | 0:489965565a0d | 103 | if ( SetODR( 100.0f ) == LSM303AGR_MAG_STATUS_ERROR ) |
| cparata | 0:489965565a0d | 104 | { |
| cparata | 0:489965565a0d | 105 | return; |
| cparata | 0:489965565a0d | 106 | } |
| cparata | 0:489965565a0d | 107 | |
| cparata | 0:489965565a0d | 108 | if ( SetFS( 50.0f ) == LSM303AGR_MAG_STATUS_ERROR ) |
| cparata | 0:489965565a0d | 109 | { |
| cparata | 0:489965565a0d | 110 | return; |
| cparata | 0:489965565a0d | 111 | } |
| cparata | 0:489965565a0d | 112 | |
| cparata | 0:489965565a0d | 113 | if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 114 | { |
| cparata | 0:489965565a0d | 115 | return; |
| cparata | 0:489965565a0d | 116 | } |
| cparata | 0:489965565a0d | 117 | }; |
| cparata | 0:489965565a0d | 118 | |
| cparata | 0:489965565a0d | 119 | /** |
| cparata | 0:489965565a0d | 120 | * @brief Enable LSM303AGR magnetometer |
| cparata | 0:489965565a0d | 121 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 122 | */ |
| cparata | 0:489965565a0d | 123 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::Enable(void) |
| cparata | 0:489965565a0d | 124 | { |
| cparata | 0:489965565a0d | 125 | /* Operating mode selection */ |
| cparata | 0:489965565a0d | 126 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_CONTINUOS_MODE ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 127 | { |
| cparata | 0:489965565a0d | 128 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 129 | } |
| cparata | 0:489965565a0d | 130 | |
| cparata | 0:489965565a0d | 131 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 132 | } |
| cparata | 0:489965565a0d | 133 | |
| cparata | 0:489965565a0d | 134 | /** |
| cparata | 0:489965565a0d | 135 | * @brief Disable LSM303AGR magnetometer |
| cparata | 0:489965565a0d | 136 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 137 | */ |
| cparata | 0:489965565a0d | 138 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::Disable(void) |
| cparata | 0:489965565a0d | 139 | { |
| cparata | 0:489965565a0d | 140 | /* Operating mode selection - power down */ |
| cparata | 0:489965565a0d | 141 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 142 | { |
| cparata | 0:489965565a0d | 143 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 144 | } |
| cparata | 0:489965565a0d | 145 | |
| cparata | 0:489965565a0d | 146 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 147 | } |
| cparata | 0:489965565a0d | 148 | |
| cparata | 0:489965565a0d | 149 | /** |
| cparata | 0:489965565a0d | 150 | * @brief Read ID of LSM303AGR Magnetometer |
| cparata | 0:489965565a0d | 151 | * @param p_id the pointer where the ID of the device is stored |
| cparata | 0:489965565a0d | 152 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 153 | */ |
| cparata | 0:489965565a0d | 154 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::ReadID(uint8_t *p_id) |
| cparata | 0:489965565a0d | 155 | { |
| cparata | 0:489965565a0d | 156 | if(!p_id) |
| cparata | 0:489965565a0d | 157 | { |
| cparata | 0:489965565a0d | 158 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 159 | } |
| cparata | 0:489965565a0d | 160 | |
| cparata | 0:489965565a0d | 161 | /* Read WHO AM I register */ |
| cparata | 0:489965565a0d | 162 | if ( LSM303AGR_MAG_R_WHO_AM_I( (void *)this, p_id ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 163 | { |
| cparata | 0:489965565a0d | 164 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 165 | } |
| cparata | 0:489965565a0d | 166 | |
| cparata | 0:489965565a0d | 167 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 168 | } |
| cparata | 0:489965565a0d | 169 | |
| cparata | 0:489965565a0d | 170 | /** |
| cparata | 0:489965565a0d | 171 | * @brief Read data from LSM303AGR Magnetometer |
| cparata | 0:489965565a0d | 172 | * @param pData the pointer where the magnetometer data are stored |
| cparata | 0:489965565a0d | 173 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 174 | */ |
| cparata | 0:489965565a0d | 175 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::GetAxes(int32_t *pData) |
| cparata | 0:489965565a0d | 176 | { |
| cparata | 0:489965565a0d | 177 | int16_t pDataRaw[3]; |
| cparata | 0:489965565a0d | 178 | float sensitivity = 0; |
| cparata | 0:489965565a0d | 179 | |
| cparata | 0:489965565a0d | 180 | /* Read raw data from LSM303AGR output register. */ |
| cparata | 0:489965565a0d | 181 | if ( GetAxesRaw( pDataRaw ) == LSM303AGR_MAG_STATUS_ERROR ) |
| cparata | 0:489965565a0d | 182 | { |
| cparata | 0:489965565a0d | 183 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 184 | } |
| cparata | 0:489965565a0d | 185 | |
| cparata | 0:489965565a0d | 186 | /* Get LSM303AGR actual sensitivity. */ |
| cparata | 0:489965565a0d | 187 | if ( GetSensitivity( &sensitivity ) == LSM303AGR_MAG_STATUS_ERROR ) |
| cparata | 0:489965565a0d | 188 | { |
| cparata | 0:489965565a0d | 189 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 190 | } |
| cparata | 0:489965565a0d | 191 | |
| cparata | 0:489965565a0d | 192 | /* Calculate the data. */ |
| cparata | 0:489965565a0d | 193 | pData[0] = ( int32_t )( pDataRaw[0] * sensitivity ); |
| cparata | 0:489965565a0d | 194 | pData[1] = ( int32_t )( pDataRaw[1] * sensitivity ); |
| cparata | 0:489965565a0d | 195 | pData[2] = ( int32_t )( pDataRaw[2] * sensitivity ); |
| cparata | 0:489965565a0d | 196 | |
| cparata | 0:489965565a0d | 197 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 198 | } |
| cparata | 0:489965565a0d | 199 | |
| cparata | 0:489965565a0d | 200 | /** |
| cparata | 0:489965565a0d | 201 | * @brief Read Magnetometer Sensitivity |
| cparata | 0:489965565a0d | 202 | * @param pfData the pointer where the magnetometer sensitivity is stored |
| cparata | 0:489965565a0d | 203 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 204 | */ |
| cparata | 0:489965565a0d | 205 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::GetSensitivity(float *pfData) |
| cparata | 0:489965565a0d | 206 | { |
| cparata | 0:489965565a0d | 207 | *pfData = 1.5f; |
| cparata | 0:489965565a0d | 208 | |
| cparata | 0:489965565a0d | 209 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 210 | } |
| cparata | 0:489965565a0d | 211 | |
| cparata | 0:489965565a0d | 212 | /** |
| cparata | 0:489965565a0d | 213 | * @brief Read raw data from LSM303AGR Magnetometer |
| cparata | 0:489965565a0d | 214 | * @param pData the pointer where the magnetomer raw data are stored |
| cparata | 0:489965565a0d | 215 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 216 | */ |
| cparata | 0:489965565a0d | 217 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::GetAxesRaw(int16_t *pData) |
| cparata | 0:489965565a0d | 218 | { |
| cparata | 0:489965565a0d | 219 | uint8_t regValue[6] = {0, 0, 0, 0, 0, 0}; |
| cparata | 0:489965565a0d | 220 | int16_t *regValueInt16; |
| cparata | 0:489965565a0d | 221 | |
| cparata | 0:489965565a0d | 222 | /* Read output registers from LSM303AGR_MAG_OUTX_L to LSM303AGR_MAG_OUTZ_H. */ |
| cparata | 0:489965565a0d | 223 | if ( LSM303AGR_MAG_Get_Raw_Magnetic( (void *)this, regValue ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 224 | { |
| cparata | 0:489965565a0d | 225 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 226 | } |
| cparata | 0:489965565a0d | 227 | |
| cparata | 0:489965565a0d | 228 | regValueInt16 = (int16_t *)regValue; |
| cparata | 0:489965565a0d | 229 | |
| cparata | 0:489965565a0d | 230 | /* Format the data. */ |
| cparata | 0:489965565a0d | 231 | pData[0] = regValueInt16[0]; |
| cparata | 0:489965565a0d | 232 | pData[1] = regValueInt16[1]; |
| cparata | 0:489965565a0d | 233 | pData[2] = regValueInt16[2]; |
| cparata | 0:489965565a0d | 234 | |
| cparata | 0:489965565a0d | 235 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 236 | } |
| cparata | 0:489965565a0d | 237 | |
| cparata | 0:489965565a0d | 238 | /** |
| cparata | 0:489965565a0d | 239 | * @brief Read LSM303AGR Magnetometer output data rate |
| cparata | 0:489965565a0d | 240 | * @param odr the pointer to the output data rate |
| cparata | 0:489965565a0d | 241 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 242 | */ |
| cparata | 0:489965565a0d | 243 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::GetODR(float* odr) |
| cparata | 0:489965565a0d | 244 | { |
| cparata | 0:489965565a0d | 245 | LSM303AGR_MAG_ODR_t odr_low_level; |
| cparata | 0:489965565a0d | 246 | |
| cparata | 0:489965565a0d | 247 | if ( LSM303AGR_MAG_R_ODR( (void *)this, &odr_low_level ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 248 | { |
| cparata | 0:489965565a0d | 249 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 250 | } |
| cparata | 0:489965565a0d | 251 | |
| cparata | 0:489965565a0d | 252 | switch( odr_low_level ) |
| cparata | 0:489965565a0d | 253 | { |
| cparata | 0:489965565a0d | 254 | case LSM303AGR_MAG_ODR_10Hz: |
| cparata | 0:489965565a0d | 255 | *odr = 10.000f; |
| cparata | 0:489965565a0d | 256 | break; |
| cparata | 0:489965565a0d | 257 | case LSM303AGR_MAG_ODR_20Hz: |
| cparata | 0:489965565a0d | 258 | *odr = 20.000f; |
| cparata | 0:489965565a0d | 259 | break; |
| cparata | 0:489965565a0d | 260 | case LSM303AGR_MAG_ODR_50Hz: |
| cparata | 0:489965565a0d | 261 | *odr = 50.000f; |
| cparata | 0:489965565a0d | 262 | break; |
| cparata | 0:489965565a0d | 263 | case LSM303AGR_MAG_ODR_100Hz: |
| cparata | 0:489965565a0d | 264 | *odr = 100.000f; |
| cparata | 0:489965565a0d | 265 | break; |
| cparata | 0:489965565a0d | 266 | default: |
| cparata | 0:489965565a0d | 267 | *odr = -1.000f; |
| cparata | 0:489965565a0d | 268 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 269 | } |
| cparata | 0:489965565a0d | 270 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 271 | } |
| cparata | 0:489965565a0d | 272 | |
| cparata | 0:489965565a0d | 273 | /** |
| cparata | 0:489965565a0d | 274 | * @brief Set ODR |
| cparata | 0:489965565a0d | 275 | * @param odr the output data rate to be set |
| cparata | 0:489965565a0d | 276 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 277 | */ |
| cparata | 0:489965565a0d | 278 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::SetODR(float odr) |
| cparata | 0:489965565a0d | 279 | { |
| cparata | 0:489965565a0d | 280 | LSM303AGR_MAG_ODR_t new_odr; |
| cparata | 0:489965565a0d | 281 | |
| cparata | 0:489965565a0d | 282 | new_odr = ( odr <= 10.000f ) ? LSM303AGR_MAG_ODR_10Hz |
| cparata | 0:489965565a0d | 283 | : ( odr <= 20.000f ) ? LSM303AGR_MAG_ODR_20Hz |
| cparata | 0:489965565a0d | 284 | : ( odr <= 50.000f ) ? LSM303AGR_MAG_ODR_50Hz |
| cparata | 0:489965565a0d | 285 | : LSM303AGR_MAG_ODR_100Hz; |
| cparata | 0:489965565a0d | 286 | |
| cparata | 0:489965565a0d | 287 | if ( LSM303AGR_MAG_W_ODR( (void *)this, new_odr ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 288 | { |
| cparata | 0:489965565a0d | 289 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 290 | } |
| cparata | 0:489965565a0d | 291 | |
| cparata | 0:489965565a0d | 292 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 293 | } |
| cparata | 0:489965565a0d | 294 | |
| cparata | 0:489965565a0d | 295 | |
| cparata | 0:489965565a0d | 296 | /** |
| cparata | 0:489965565a0d | 297 | * @brief Read LSM303AGR Magnetometer full scale |
| cparata | 0:489965565a0d | 298 | * @param fullScale the pointer to the output data rate |
| cparata | 0:489965565a0d | 299 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 300 | */ |
| cparata | 0:489965565a0d | 301 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::GetFS(float* fullScale) |
| cparata | 0:489965565a0d | 302 | { |
| cparata | 0:489965565a0d | 303 | *fullScale = 50.0f; |
| cparata | 0:489965565a0d | 304 | |
| cparata | 0:489965565a0d | 305 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 306 | } |
| cparata | 0:489965565a0d | 307 | |
| cparata | 0:489965565a0d | 308 | /** |
| cparata | 0:489965565a0d | 309 | * @brief Set full scale |
| cparata | 0:489965565a0d | 310 | * @param fullScale the full scale to be set |
| cparata | 0:489965565a0d | 311 | * @retval LSM303AGR_MAG_STATUS_OK in case of success, an error code otherwise |
| cparata | 0:489965565a0d | 312 | */ |
| cparata | 0:489965565a0d | 313 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::SetFS(float fullScale) |
| cparata | 0:489965565a0d | 314 | { |
| cparata | 0:489965565a0d | 315 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 316 | } |
| cparata | 0:489965565a0d | 317 | |
| cparata | 0:489965565a0d | 318 | |
| cparata | 0:489965565a0d | 319 | /** |
| cparata | 0:489965565a0d | 320 | * @brief Read magnetometer data from register |
| cparata | 0:489965565a0d | 321 | * @param reg register address |
| cparata | 0:489965565a0d | 322 | * @param data register data |
| cparata | 0:489965565a0d | 323 | * @retval LSM303AGR_MAG_STATUS_OK in case of success |
| cparata | 0:489965565a0d | 324 | * @retval LSM303AGR_MAG_STATUS_ERROR in case of failure |
| cparata | 0:489965565a0d | 325 | */ |
| cparata | 0:489965565a0d | 326 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::ReadReg( uint8_t reg, uint8_t *data ) |
| cparata | 0:489965565a0d | 327 | { |
| cparata | 0:489965565a0d | 328 | if ( LSM303AGR_MAG_ReadReg( (void *)this, reg, data ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 329 | { |
| cparata | 0:489965565a0d | 330 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 331 | } |
| cparata | 0:489965565a0d | 332 | |
| cparata | 0:489965565a0d | 333 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 334 | } |
| cparata | 0:489965565a0d | 335 | |
| cparata | 0:489965565a0d | 336 | |
| cparata | 0:489965565a0d | 337 | /** |
| cparata | 0:489965565a0d | 338 | * @brief Write magnetometer data to register |
| cparata | 0:489965565a0d | 339 | * @param reg register address |
| cparata | 0:489965565a0d | 340 | * @param data register data |
| cparata | 0:489965565a0d | 341 | * @retval LSM303AGR_MAG_STATUS_OK in case of success |
| cparata | 0:489965565a0d | 342 | * @retval LSM303AGR_MAG_STATUS_ERROR in case of failure |
| cparata | 0:489965565a0d | 343 | */ |
| cparata | 0:489965565a0d | 344 | LSM303AGR_MAG_StatusTypeDef LSM303AGR_MAG_Sensor::WriteReg( uint8_t reg, uint8_t data ) |
| cparata | 0:489965565a0d | 345 | { |
| cparata | 0:489965565a0d | 346 | if ( LSM303AGR_MAG_WriteReg( (void *)this, reg, data ) == MEMS_ERROR ) |
| cparata | 0:489965565a0d | 347 | { |
| cparata | 0:489965565a0d | 348 | return LSM303AGR_MAG_STATUS_ERROR; |
| cparata | 0:489965565a0d | 349 | } |
| cparata | 0:489965565a0d | 350 | |
| cparata | 0:489965565a0d | 351 | return LSM303AGR_MAG_STATUS_OK; |
| cparata | 0:489965565a0d | 352 | } |
| cparata | 0:489965565a0d | 353 | |
| cparata | 0:489965565a0d | 354 | uint8_t LSM303AGR_MAG_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
| cparata | 0:489965565a0d | 355 | { |
| cparata | 0:489965565a0d | 356 | return ((LSM303AGR_MAG_Sensor *)handle)->IO_Write(pBuffer, WriteAddr, nBytesToWrite); |
| cparata | 0:489965565a0d | 357 | } |
| cparata | 0:489965565a0d | 358 | |
| cparata | 0:489965565a0d | 359 | uint8_t LSM303AGR_MAG_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
| cparata | 0:489965565a0d | 360 | { |
| cparata | 0:489965565a0d | 361 | return ((LSM303AGR_MAG_Sensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead); |
| cparata | 0:489965565a0d | 362 | } |
