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