Example of tilt detection for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of Tilt_IKS01A2 by ST Expansion SW Team

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.

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?

UserRevisionLine numberNew contents of line
cparata 0:489965565a0d 1 /**
cparata 0:489965565a0d 2 ******************************************************************************
cparata 0:489965565a0d 3 * @file HTS221Sensor.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 of an HTS221 Humidity and Temperature sensor.
cparata 0:489965565a0d 8 ******************************************************************************
cparata 0:489965565a0d 9 * @attention
cparata 0:489965565a0d 10 *
cparata 0:489965565a0d 11 * <h2><center>&copy; 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 "HTS221Sensor.h"
cparata 0:489965565a0d 44 #include "HTS221_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 HTS221Sensor::HTS221Sensor(DevI2C &i2c) : dev_i2c(i2c)
cparata 0:489965565a0d 54 {
cparata 0:489965565a0d 55 address = HTS221_I2C_ADDRESS;
cparata 0:489965565a0d 56
cparata 0:489965565a0d 57 /* Power down the device */
cparata 0:489965565a0d 58 if ( HTS221_DeActivate( (void *)this ) == HTS221_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 ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
cparata 0:489965565a0d 65 {
cparata 0:489965565a0d 66 return;
cparata 0:489965565a0d 67 }
cparata 0:489965565a0d 68
cparata 0:489965565a0d 69 if(SetODR(1.0f) == HTS221_STATUS_ERROR)
cparata 0:489965565a0d 70 {
cparata 0:489965565a0d 71 return;
cparata 0:489965565a0d 72 }
cparata 0:489965565a0d 73 };
cparata 0:489965565a0d 74
cparata 0:489965565a0d 75
cparata 0:489965565a0d 76 /** Constructor
cparata 0:489965565a0d 77 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:489965565a0d 78 * @param address the address of the component's instance
cparata 0:489965565a0d 79 */
cparata 0:489965565a0d 80 HTS221Sensor::HTS221Sensor(DevI2C &i2c, uint8_t address) : dev_i2c(i2c), address(address)
cparata 0:489965565a0d 81 {
cparata 0:489965565a0d 82 /* Power down the device */
cparata 0:489965565a0d 83 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 0:489965565a0d 84 {
cparata 0:489965565a0d 85 return;
cparata 0:489965565a0d 86 }
cparata 0:489965565a0d 87
cparata 0:489965565a0d 88 /* Enable BDU */
cparata 0:489965565a0d 89 if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
cparata 0:489965565a0d 90 {
cparata 0:489965565a0d 91 return;
cparata 0:489965565a0d 92 }
cparata 0:489965565a0d 93
cparata 0:489965565a0d 94 if(SetODR(1.0f) == HTS221_STATUS_ERROR)
cparata 0:489965565a0d 95 {
cparata 0:489965565a0d 96 return;
cparata 0:489965565a0d 97 }
cparata 0:489965565a0d 98 };
cparata 0:489965565a0d 99
cparata 0:489965565a0d 100 /**
cparata 0:489965565a0d 101 * @brief Enable HTS221
cparata 0:489965565a0d 102 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 103 */
cparata 0:489965565a0d 104 HTS221StatusTypeDef HTS221Sensor::Enable(void)
cparata 0:489965565a0d 105 {
cparata 0:489965565a0d 106 /* Power up the device */
cparata 0:489965565a0d 107 if ( HTS221_Activate( (void *)this ) == HTS221_ERROR )
cparata 0:489965565a0d 108 {
cparata 0:489965565a0d 109 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 110 }
cparata 0:489965565a0d 111
cparata 0:489965565a0d 112 return HTS221_STATUS_OK;
cparata 0:489965565a0d 113 }
cparata 0:489965565a0d 114
cparata 0:489965565a0d 115 /**
cparata 0:489965565a0d 116 * @brief Disable HTS221
cparata 0:489965565a0d 117 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 118 */
cparata 0:489965565a0d 119 HTS221StatusTypeDef HTS221Sensor::Disable(void)
cparata 0:489965565a0d 120 {
cparata 0:489965565a0d 121 /* Power up the device */
cparata 0:489965565a0d 122 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 0:489965565a0d 123 {
cparata 0:489965565a0d 124 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 125 }
cparata 0:489965565a0d 126
cparata 0:489965565a0d 127 return HTS221_STATUS_OK;
cparata 0:489965565a0d 128 }
cparata 0:489965565a0d 129
cparata 0:489965565a0d 130 /**
cparata 0:489965565a0d 131 * @brief Read ID address of HTS221
cparata 0:489965565a0d 132 * @param ht_id the pointer where the ID of the device is stored
cparata 0:489965565a0d 133 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 134 */
cparata 0:489965565a0d 135 HTS221StatusTypeDef HTS221Sensor::ReadID(uint8_t *ht_id)
cparata 0:489965565a0d 136 {
cparata 0:489965565a0d 137 /* Read WHO AM I register */
cparata 0:489965565a0d 138 if ( HTS221_Get_DeviceID( (void *)this, ht_id ) == HTS221_ERROR )
cparata 0:489965565a0d 139 {
cparata 0:489965565a0d 140 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 141 }
cparata 0:489965565a0d 142
cparata 0:489965565a0d 143 return HTS221_STATUS_OK;
cparata 0:489965565a0d 144 }
cparata 0:489965565a0d 145
cparata 0:489965565a0d 146 /**
cparata 0:489965565a0d 147 * @brief Reboot memory content of HTS221
cparata 0:489965565a0d 148 * @param None
cparata 0:489965565a0d 149 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 150 */
cparata 0:489965565a0d 151 HTS221StatusTypeDef HTS221Sensor::Reset(void)
cparata 0:489965565a0d 152 {
cparata 0:489965565a0d 153 uint8_t tmpreg;
cparata 0:489965565a0d 154
cparata 0:489965565a0d 155 /* Read CTRL_REG2 register */
cparata 0:489965565a0d 156 if (ReadReg(HTS221_CTRL_REG2, &tmpreg) != HTS221_STATUS_OK)
cparata 0:489965565a0d 157 {
cparata 0:489965565a0d 158 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 159 }
cparata 0:489965565a0d 160
cparata 0:489965565a0d 161 /* Enable or Disable the reboot memory */
cparata 0:489965565a0d 162 tmpreg |= (0x01 << HTS221_BOOT_BIT);
cparata 0:489965565a0d 163
cparata 0:489965565a0d 164 /* Write value to MEMS CTRL_REG2 regsister */
cparata 0:489965565a0d 165 if (WriteReg(HTS221_CTRL_REG2, tmpreg) != HTS221_STATUS_OK)
cparata 0:489965565a0d 166 {
cparata 0:489965565a0d 167 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 168 }
cparata 0:489965565a0d 169
cparata 0:489965565a0d 170 return HTS221_STATUS_OK;
cparata 0:489965565a0d 171 }
cparata 0:489965565a0d 172
cparata 0:489965565a0d 173 /**
cparata 0:489965565a0d 174 * @brief Read HTS221 output register, and calculate the humidity
cparata 0:489965565a0d 175 * @param pfData the pointer to data output
cparata 0:489965565a0d 176 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 177 */
cparata 0:489965565a0d 178 HTS221StatusTypeDef HTS221Sensor::GetHumidity(float* pfData)
cparata 0:489965565a0d 179 {
cparata 0:489965565a0d 180 uint16_t uint16data = 0;
cparata 0:489965565a0d 181
cparata 0:489965565a0d 182 /* Read data from HTS221. */
cparata 0:489965565a0d 183 if ( HTS221_Get_Humidity( (void *)this, &uint16data ) == HTS221_ERROR )
cparata 0:489965565a0d 184 {
cparata 0:489965565a0d 185 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 186 }
cparata 0:489965565a0d 187
cparata 0:489965565a0d 188 *pfData = ( float )uint16data / 10.0f;
cparata 0:489965565a0d 189
cparata 0:489965565a0d 190 return HTS221_STATUS_OK;
cparata 0:489965565a0d 191 }
cparata 0:489965565a0d 192
cparata 0:489965565a0d 193 /**
cparata 0:489965565a0d 194 * @brief Read HTS221 output register, and calculate the temperature
cparata 0:489965565a0d 195 * @param pfData the pointer to data output
cparata 0:489965565a0d 196 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 197 */
cparata 0:489965565a0d 198 HTS221StatusTypeDef HTS221Sensor::GetTemperature(float* pfData)
cparata 0:489965565a0d 199 {
cparata 0:489965565a0d 200 int16_t int16data = 0;
cparata 0:489965565a0d 201
cparata 0:489965565a0d 202 /* Read data from HTS221. */
cparata 0:489965565a0d 203 if ( HTS221_Get_Temperature( (void *)this, &int16data ) == HTS221_ERROR )
cparata 0:489965565a0d 204 {
cparata 0:489965565a0d 205 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 206 }
cparata 0:489965565a0d 207
cparata 0:489965565a0d 208 *pfData = ( float )int16data / 10.0f;
cparata 0:489965565a0d 209
cparata 0:489965565a0d 210 return HTS221_STATUS_OK;
cparata 0:489965565a0d 211 }
cparata 0:489965565a0d 212
cparata 0:489965565a0d 213 /**
cparata 0:489965565a0d 214 * @brief Read HTS221 output register, and calculate the humidity
cparata 0:489965565a0d 215 * @param odr the pointer to the output data rate
cparata 0:489965565a0d 216 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 217 */
cparata 0:489965565a0d 218 HTS221StatusTypeDef HTS221Sensor::GetODR(float* odr)
cparata 0:489965565a0d 219 {
cparata 0:489965565a0d 220 HTS221_Odr_et odr_low_level;
cparata 0:489965565a0d 221
cparata 0:489965565a0d 222 if ( HTS221_Get_Odr( (void *)this, &odr_low_level ) == HTS221_ERROR )
cparata 0:489965565a0d 223 {
cparata 0:489965565a0d 224 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 225 }
cparata 0:489965565a0d 226
cparata 0:489965565a0d 227 switch( odr_low_level )
cparata 0:489965565a0d 228 {
cparata 0:489965565a0d 229 case HTS221_ODR_ONE_SHOT:
cparata 0:489965565a0d 230 *odr = 0.0f;
cparata 0:489965565a0d 231 break;
cparata 0:489965565a0d 232 case HTS221_ODR_1HZ :
cparata 0:489965565a0d 233 *odr = 1.0f;
cparata 0:489965565a0d 234 break;
cparata 0:489965565a0d 235 case HTS221_ODR_7HZ :
cparata 0:489965565a0d 236 *odr = 7.0f;
cparata 0:489965565a0d 237 break;
cparata 0:489965565a0d 238 case HTS221_ODR_12_5HZ :
cparata 0:489965565a0d 239 *odr = 12.5f;
cparata 0:489965565a0d 240 break;
cparata 0:489965565a0d 241 default :
cparata 0:489965565a0d 242 *odr = -1.0f;
cparata 0:489965565a0d 243 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 244 }
cparata 0:489965565a0d 245
cparata 0:489965565a0d 246 return HTS221_STATUS_OK;
cparata 0:489965565a0d 247 }
cparata 0:489965565a0d 248
cparata 0:489965565a0d 249 /**
cparata 0:489965565a0d 250 * @brief Set ODR
cparata 0:489965565a0d 251 * @param odr the output data rate to be set
cparata 0:489965565a0d 252 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:489965565a0d 253 */
cparata 0:489965565a0d 254 HTS221StatusTypeDef HTS221Sensor::SetODR(float odr)
cparata 0:489965565a0d 255 {
cparata 0:489965565a0d 256 HTS221_Odr_et new_odr;
cparata 0:489965565a0d 257
cparata 0:489965565a0d 258 new_odr = ( odr <= 1.0f ) ? HTS221_ODR_1HZ
cparata 0:489965565a0d 259 : ( odr <= 7.0f ) ? HTS221_ODR_7HZ
cparata 0:489965565a0d 260 : HTS221_ODR_12_5HZ;
cparata 0:489965565a0d 261
cparata 0:489965565a0d 262 if ( HTS221_Set_Odr( (void *)this, new_odr ) == HTS221_ERROR )
cparata 0:489965565a0d 263 {
cparata 0:489965565a0d 264 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 265 }
cparata 0:489965565a0d 266
cparata 0:489965565a0d 267 return HTS221_STATUS_OK;
cparata 0:489965565a0d 268 }
cparata 0:489965565a0d 269
cparata 0:489965565a0d 270
cparata 0:489965565a0d 271 /**
cparata 0:489965565a0d 272 * @brief Read the data from register
cparata 0:489965565a0d 273 * @param reg register address
cparata 0:489965565a0d 274 * @param data register data
cparata 0:489965565a0d 275 * @retval HTS221_STATUS_OK in case of success
cparata 0:489965565a0d 276 * @retval HTS221_STATUS_ERROR in case of failure
cparata 0:489965565a0d 277 */
cparata 0:489965565a0d 278 HTS221StatusTypeDef HTS221Sensor::ReadReg( uint8_t reg, uint8_t *data )
cparata 0:489965565a0d 279 {
cparata 0:489965565a0d 280
cparata 0:489965565a0d 281 if ( HTS221_ReadReg( (void *)this, reg, 1, data ) == HTS221_ERROR )
cparata 0:489965565a0d 282 {
cparata 0:489965565a0d 283 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 284 }
cparata 0:489965565a0d 285
cparata 0:489965565a0d 286 return HTS221_STATUS_OK;
cparata 0:489965565a0d 287 }
cparata 0:489965565a0d 288
cparata 0:489965565a0d 289 /**
cparata 0:489965565a0d 290 * @brief Write the data to register
cparata 0:489965565a0d 291 * @param reg register address
cparata 0:489965565a0d 292 * @param data register data
cparata 0:489965565a0d 293 * @retval HTS221_STATUS_OK in case of success
cparata 0:489965565a0d 294 * @retval HTS221_STATUS_ERROR in case of failure
cparata 0:489965565a0d 295 */
cparata 0:489965565a0d 296 HTS221StatusTypeDef HTS221Sensor::WriteReg( uint8_t reg, uint8_t data )
cparata 0:489965565a0d 297 {
cparata 0:489965565a0d 298
cparata 0:489965565a0d 299 if ( HTS221_WriteReg( (void *)this, reg, 1, &data ) == HTS221_ERROR )
cparata 0:489965565a0d 300 {
cparata 0:489965565a0d 301 return HTS221_STATUS_ERROR;
cparata 0:489965565a0d 302 }
cparata 0:489965565a0d 303
cparata 0:489965565a0d 304 return HTS221_STATUS_OK;
cparata 0:489965565a0d 305 }
cparata 0:489965565a0d 306
cparata 0:489965565a0d 307 uint8_t HTS221_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
cparata 0:489965565a0d 308 {
cparata 0:489965565a0d 309 return ((HTS221Sensor *)handle)->IO_Write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:489965565a0d 310 }
cparata 0:489965565a0d 311
cparata 0:489965565a0d 312 uint8_t HTS221_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
cparata 0:489965565a0d 313 {
cparata 0:489965565a0d 314 return ((HTS221Sensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:489965565a0d 315 }