Example of single tap and double tap detection for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of SingleDoubleTap_IKS01A2 by ST Expansion SW Team

Single and Double Tap Demo Application based on sensor expansion board X-NUCLEO-IKS01A2

Main function is to show how to detect the single and double tap events 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 tap the board and then view the notification using an hyper terminal. When the single tap is detected, the LED is switched on for a while.
- the user can press the user button to pass from the single tap detection to the double tap detection feature. The user can try to double tap the board and then view the notification using an hyper terminal. When the double tap is detected, the LED is switched on twice for a while.
- the user can press again the user button to disable the single and double tap detection feature.
- the user can press the user button to enable again the single tap detection feature and so on.

Committer:
cparata
Date:
Fri Aug 19 12:31:21 2016 +0000
Revision:
2:21a191bd1998
Add interfaces to all components

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 2:21a191bd1998 1 /**
cparata 2:21a191bd1998 2 ******************************************************************************
cparata 2:21a191bd1998 3 * @file HTS221Sensor.cpp
cparata 2:21a191bd1998 4 * @author AST
cparata 2:21a191bd1998 5 * @version V1.0.0
cparata 2:21a191bd1998 6 * @date 5 August 2016
cparata 2:21a191bd1998 7 * @brief Implementation of an HTS221 Humidity and Temperature sensor.
cparata 2:21a191bd1998 8 ******************************************************************************
cparata 2:21a191bd1998 9 * @attention
cparata 2:21a191bd1998 10 *
cparata 2:21a191bd1998 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
cparata 2:21a191bd1998 12 *
cparata 2:21a191bd1998 13 * Redistribution and use in source and binary forms, with or without modification,
cparata 2:21a191bd1998 14 * are permitted provided that the following conditions are met:
cparata 2:21a191bd1998 15 * 1. Redistributions of source code must retain the above copyright notice,
cparata 2:21a191bd1998 16 * this list of conditions and the following disclaimer.
cparata 2:21a191bd1998 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 2:21a191bd1998 18 * this list of conditions and the following disclaimer in the documentation
cparata 2:21a191bd1998 19 * and/or other materials provided with the distribution.
cparata 2:21a191bd1998 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 2:21a191bd1998 21 * may be used to endorse or promote products derived from this software
cparata 2:21a191bd1998 22 * without specific prior written permission.
cparata 2:21a191bd1998 23 *
cparata 2:21a191bd1998 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 2:21a191bd1998 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 2:21a191bd1998 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 2:21a191bd1998 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 2:21a191bd1998 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 2:21a191bd1998 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 2:21a191bd1998 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 2:21a191bd1998 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 2:21a191bd1998 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 2:21a191bd1998 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 2:21a191bd1998 34 *
cparata 2:21a191bd1998 35 ******************************************************************************
cparata 2:21a191bd1998 36 */
cparata 2:21a191bd1998 37
cparata 2:21a191bd1998 38
cparata 2:21a191bd1998 39 /* Includes ------------------------------------------------------------------*/
cparata 2:21a191bd1998 40
cparata 2:21a191bd1998 41 #include "mbed.h"
cparata 2:21a191bd1998 42 #include "DevI2C.h"
cparata 2:21a191bd1998 43 #include "HTS221Sensor.h"
cparata 2:21a191bd1998 44 #include "HTS221_Driver.h"
cparata 2:21a191bd1998 45
cparata 2:21a191bd1998 46
cparata 2:21a191bd1998 47 /* Class Implementation ------------------------------------------------------*/
cparata 2:21a191bd1998 48
cparata 2:21a191bd1998 49 /** Constructor
cparata 2:21a191bd1998 50 * @param i2c object of an helper class which handles the I2C peripheral
cparata 2:21a191bd1998 51 * @param address the address of the component's instance
cparata 2:21a191bd1998 52 */
cparata 2:21a191bd1998 53 HTS221Sensor::HTS221Sensor(DevI2C &i2c) : dev_i2c(i2c)
cparata 2:21a191bd1998 54 {
cparata 2:21a191bd1998 55 address = HTS221_I2C_ADDRESS;
cparata 2:21a191bd1998 56 };
cparata 2:21a191bd1998 57
cparata 2:21a191bd1998 58
cparata 2:21a191bd1998 59 /** Constructor
cparata 2:21a191bd1998 60 * @param i2c object of an helper class which handles the I2C peripheral
cparata 2:21a191bd1998 61 * @param address the address of the component's instance
cparata 2:21a191bd1998 62 */
cparata 2:21a191bd1998 63 HTS221Sensor::HTS221Sensor(DevI2C &i2c, uint8_t address) : dev_i2c(i2c), address(address)
cparata 2:21a191bd1998 64 {
cparata 2:21a191bd1998 65
cparata 2:21a191bd1998 66 };
cparata 2:21a191bd1998 67
cparata 2:21a191bd1998 68 /**
cparata 2:21a191bd1998 69 * @brief Initializing the component.
cparata 2:21a191bd1998 70 * @param[in] init pointer to device specific initalization structure.
cparata 2:21a191bd1998 71 * @retval "0" in case of success, an error code otherwise.
cparata 2:21a191bd1998 72 */
cparata 2:21a191bd1998 73 int HTS221Sensor::Init(void *init)
cparata 2:21a191bd1998 74 {
cparata 2:21a191bd1998 75 /* Power down the device */
cparata 2:21a191bd1998 76 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 2:21a191bd1998 77 {
cparata 2:21a191bd1998 78 return 1;
cparata 2:21a191bd1998 79 }
cparata 2:21a191bd1998 80
cparata 2:21a191bd1998 81 /* Enable BDU */
cparata 2:21a191bd1998 82 if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
cparata 2:21a191bd1998 83 {
cparata 2:21a191bd1998 84 return 1;
cparata 2:21a191bd1998 85 }
cparata 2:21a191bd1998 86
cparata 2:21a191bd1998 87 if(Set_ODR(1.0f) == 1)
cparata 2:21a191bd1998 88 {
cparata 2:21a191bd1998 89 return 1;
cparata 2:21a191bd1998 90 }
cparata 2:21a191bd1998 91
cparata 2:21a191bd1998 92 return 0;
cparata 2:21a191bd1998 93 }
cparata 2:21a191bd1998 94
cparata 2:21a191bd1998 95 /**
cparata 2:21a191bd1998 96 * @brief Enable HTS221
cparata 2:21a191bd1998 97 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 98 */
cparata 2:21a191bd1998 99 int HTS221Sensor::Enable(void)
cparata 2:21a191bd1998 100 {
cparata 2:21a191bd1998 101 /* Power up the device */
cparata 2:21a191bd1998 102 if ( HTS221_Activate( (void *)this ) == HTS221_ERROR )
cparata 2:21a191bd1998 103 {
cparata 2:21a191bd1998 104 return 1;
cparata 2:21a191bd1998 105 }
cparata 2:21a191bd1998 106
cparata 2:21a191bd1998 107 return 0;
cparata 2:21a191bd1998 108 }
cparata 2:21a191bd1998 109
cparata 2:21a191bd1998 110 /**
cparata 2:21a191bd1998 111 * @brief Disable HTS221
cparata 2:21a191bd1998 112 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 113 */
cparata 2:21a191bd1998 114 int HTS221Sensor::Disable(void)
cparata 2:21a191bd1998 115 {
cparata 2:21a191bd1998 116 /* Power up the device */
cparata 2:21a191bd1998 117 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 2:21a191bd1998 118 {
cparata 2:21a191bd1998 119 return 1;
cparata 2:21a191bd1998 120 }
cparata 2:21a191bd1998 121
cparata 2:21a191bd1998 122 return 0;
cparata 2:21a191bd1998 123 }
cparata 2:21a191bd1998 124
cparata 2:21a191bd1998 125 /**
cparata 2:21a191bd1998 126 * @brief Read ID address of HTS221
cparata 2:21a191bd1998 127 * @param id the pointer where the ID of the device is stored
cparata 2:21a191bd1998 128 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 129 */
cparata 2:21a191bd1998 130 int HTS221Sensor::ReadID(uint8_t *id)
cparata 2:21a191bd1998 131 {
cparata 2:21a191bd1998 132 if(!id)
cparata 2:21a191bd1998 133 {
cparata 2:21a191bd1998 134 return 1;
cparata 2:21a191bd1998 135 }
cparata 2:21a191bd1998 136
cparata 2:21a191bd1998 137 /* Read WHO AM I register */
cparata 2:21a191bd1998 138 if ( HTS221_Get_DeviceID( (void *)this, id ) == HTS221_ERROR )
cparata 2:21a191bd1998 139 {
cparata 2:21a191bd1998 140 return 1;
cparata 2:21a191bd1998 141 }
cparata 2:21a191bd1998 142
cparata 2:21a191bd1998 143 return 0;
cparata 2:21a191bd1998 144 }
cparata 2:21a191bd1998 145
cparata 2:21a191bd1998 146 /**
cparata 2:21a191bd1998 147 * @brief Reboot memory content of HTS221
cparata 2:21a191bd1998 148 * @param None
cparata 2:21a191bd1998 149 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 150 */
cparata 2:21a191bd1998 151 int HTS221Sensor::Reset(void)
cparata 2:21a191bd1998 152 {
cparata 2:21a191bd1998 153 uint8_t tmpreg;
cparata 2:21a191bd1998 154
cparata 2:21a191bd1998 155 /* Read CTRL_REG2 register */
cparata 2:21a191bd1998 156 if (ReadReg(HTS221_CTRL_REG2, &tmpreg) != 0)
cparata 2:21a191bd1998 157 {
cparata 2:21a191bd1998 158 return 1;
cparata 2:21a191bd1998 159 }
cparata 2:21a191bd1998 160
cparata 2:21a191bd1998 161 /* Enable or Disable the reboot memory */
cparata 2:21a191bd1998 162 tmpreg |= (0x01 << HTS221_BOOT_BIT);
cparata 2:21a191bd1998 163
cparata 2:21a191bd1998 164 /* Write value to MEMS CTRL_REG2 regsister */
cparata 2:21a191bd1998 165 if (WriteReg(HTS221_CTRL_REG2, tmpreg) != 0)
cparata 2:21a191bd1998 166 {
cparata 2:21a191bd1998 167 return 1;
cparata 2:21a191bd1998 168 }
cparata 2:21a191bd1998 169
cparata 2:21a191bd1998 170 return 0;
cparata 2:21a191bd1998 171 }
cparata 2:21a191bd1998 172
cparata 2:21a191bd1998 173 /**
cparata 2:21a191bd1998 174 * @brief Read HTS221 output register, and calculate the humidity
cparata 2:21a191bd1998 175 * @param pfData the pointer to data output
cparata 2:21a191bd1998 176 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 177 */
cparata 2:21a191bd1998 178 int HTS221Sensor::GetHumidity(float* pfData)
cparata 2:21a191bd1998 179 {
cparata 2:21a191bd1998 180 uint16_t uint16data = 0;
cparata 2:21a191bd1998 181
cparata 2:21a191bd1998 182 /* Read data from HTS221. */
cparata 2:21a191bd1998 183 if ( HTS221_Get_Humidity( (void *)this, &uint16data ) == HTS221_ERROR )
cparata 2:21a191bd1998 184 {
cparata 2:21a191bd1998 185 return 1;
cparata 2:21a191bd1998 186 }
cparata 2:21a191bd1998 187
cparata 2:21a191bd1998 188 *pfData = ( float )uint16data / 10.0f;
cparata 2:21a191bd1998 189
cparata 2:21a191bd1998 190 return 0;
cparata 2:21a191bd1998 191 }
cparata 2:21a191bd1998 192
cparata 2:21a191bd1998 193 /**
cparata 2:21a191bd1998 194 * @brief Read HTS221 output register, and calculate the temperature
cparata 2:21a191bd1998 195 * @param pfData the pointer to data output
cparata 2:21a191bd1998 196 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 197 */
cparata 2:21a191bd1998 198 int HTS221Sensor::GetTemperature(float* pfData)
cparata 2:21a191bd1998 199 {
cparata 2:21a191bd1998 200 int16_t int16data = 0;
cparata 2:21a191bd1998 201
cparata 2:21a191bd1998 202 /* Read data from HTS221. */
cparata 2:21a191bd1998 203 if ( HTS221_Get_Temperature( (void *)this, &int16data ) == HTS221_ERROR )
cparata 2:21a191bd1998 204 {
cparata 2:21a191bd1998 205 return 1;
cparata 2:21a191bd1998 206 }
cparata 2:21a191bd1998 207
cparata 2:21a191bd1998 208 *pfData = ( float )int16data / 10.0f;
cparata 2:21a191bd1998 209
cparata 2:21a191bd1998 210 return 0;
cparata 2:21a191bd1998 211 }
cparata 2:21a191bd1998 212
cparata 2:21a191bd1998 213 /**
cparata 2:21a191bd1998 214 * @brief Read HTS221 output register, and calculate the humidity
cparata 2:21a191bd1998 215 * @param odr the pointer to the output data rate
cparata 2:21a191bd1998 216 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 217 */
cparata 2:21a191bd1998 218 int HTS221Sensor::Get_ODR(float* odr)
cparata 2:21a191bd1998 219 {
cparata 2:21a191bd1998 220 HTS221_Odr_et odr_low_level;
cparata 2:21a191bd1998 221
cparata 2:21a191bd1998 222 if ( HTS221_Get_Odr( (void *)this, &odr_low_level ) == HTS221_ERROR )
cparata 2:21a191bd1998 223 {
cparata 2:21a191bd1998 224 return 1;
cparata 2:21a191bd1998 225 }
cparata 2:21a191bd1998 226
cparata 2:21a191bd1998 227 switch( odr_low_level )
cparata 2:21a191bd1998 228 {
cparata 2:21a191bd1998 229 case HTS221_ODR_ONE_SHOT:
cparata 2:21a191bd1998 230 *odr = 0.0f;
cparata 2:21a191bd1998 231 break;
cparata 2:21a191bd1998 232 case HTS221_ODR_1HZ :
cparata 2:21a191bd1998 233 *odr = 1.0f;
cparata 2:21a191bd1998 234 break;
cparata 2:21a191bd1998 235 case HTS221_ODR_7HZ :
cparata 2:21a191bd1998 236 *odr = 7.0f;
cparata 2:21a191bd1998 237 break;
cparata 2:21a191bd1998 238 case HTS221_ODR_12_5HZ :
cparata 2:21a191bd1998 239 *odr = 12.5f;
cparata 2:21a191bd1998 240 break;
cparata 2:21a191bd1998 241 default :
cparata 2:21a191bd1998 242 *odr = -1.0f;
cparata 2:21a191bd1998 243 return 1;
cparata 2:21a191bd1998 244 }
cparata 2:21a191bd1998 245
cparata 2:21a191bd1998 246 return 0;
cparata 2:21a191bd1998 247 }
cparata 2:21a191bd1998 248
cparata 2:21a191bd1998 249 /**
cparata 2:21a191bd1998 250 * @brief Set ODR
cparata 2:21a191bd1998 251 * @param odr the output data rate to be set
cparata 2:21a191bd1998 252 * @retval 0 in case of success, an error code otherwise
cparata 2:21a191bd1998 253 */
cparata 2:21a191bd1998 254 int HTS221Sensor::Set_ODR(float odr)
cparata 2:21a191bd1998 255 {
cparata 2:21a191bd1998 256 HTS221_Odr_et new_odr;
cparata 2:21a191bd1998 257
cparata 2:21a191bd1998 258 new_odr = ( odr <= 1.0f ) ? HTS221_ODR_1HZ
cparata 2:21a191bd1998 259 : ( odr <= 7.0f ) ? HTS221_ODR_7HZ
cparata 2:21a191bd1998 260 : HTS221_ODR_12_5HZ;
cparata 2:21a191bd1998 261
cparata 2:21a191bd1998 262 if ( HTS221_Set_Odr( (void *)this, new_odr ) == HTS221_ERROR )
cparata 2:21a191bd1998 263 {
cparata 2:21a191bd1998 264 return 1;
cparata 2:21a191bd1998 265 }
cparata 2:21a191bd1998 266
cparata 2:21a191bd1998 267 return 0;
cparata 2:21a191bd1998 268 }
cparata 2:21a191bd1998 269
cparata 2:21a191bd1998 270
cparata 2:21a191bd1998 271 /**
cparata 2:21a191bd1998 272 * @brief Read the data from register
cparata 2:21a191bd1998 273 * @param reg register address
cparata 2:21a191bd1998 274 * @param data register data
cparata 2:21a191bd1998 275 * @retval 0 in case of success
cparata 2:21a191bd1998 276 * @retval 1 in case of failure
cparata 2:21a191bd1998 277 */
cparata 2:21a191bd1998 278 int HTS221Sensor::ReadReg( uint8_t reg, uint8_t *data )
cparata 2:21a191bd1998 279 {
cparata 2:21a191bd1998 280
cparata 2:21a191bd1998 281 if ( HTS221_ReadReg( (void *)this, reg, 1, data ) == HTS221_ERROR )
cparata 2:21a191bd1998 282 {
cparata 2:21a191bd1998 283 return 1;
cparata 2:21a191bd1998 284 }
cparata 2:21a191bd1998 285
cparata 2:21a191bd1998 286 return 0;
cparata 2:21a191bd1998 287 }
cparata 2:21a191bd1998 288
cparata 2:21a191bd1998 289 /**
cparata 2:21a191bd1998 290 * @brief Write the data to register
cparata 2:21a191bd1998 291 * @param reg register address
cparata 2:21a191bd1998 292 * @param data register data
cparata 2:21a191bd1998 293 * @retval 0 in case of success
cparata 2:21a191bd1998 294 * @retval 1 in case of failure
cparata 2:21a191bd1998 295 */
cparata 2:21a191bd1998 296 int HTS221Sensor::WriteReg( uint8_t reg, uint8_t data )
cparata 2:21a191bd1998 297 {
cparata 2:21a191bd1998 298
cparata 2:21a191bd1998 299 if ( HTS221_WriteReg( (void *)this, reg, 1, &data ) == HTS221_ERROR )
cparata 2:21a191bd1998 300 {
cparata 2:21a191bd1998 301 return 1;
cparata 2:21a191bd1998 302 }
cparata 2:21a191bd1998 303
cparata 2:21a191bd1998 304 return 0;
cparata 2:21a191bd1998 305 }
cparata 2:21a191bd1998 306
cparata 2:21a191bd1998 307 uint8_t HTS221_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
cparata 2:21a191bd1998 308 {
cparata 2:21a191bd1998 309 return ((HTS221Sensor *)handle)->IO_Write(pBuffer, WriteAddr, nBytesToWrite);
cparata 2:21a191bd1998 310 }
cparata 2:21a191bd1998 311
cparata 2:21a191bd1998 312 uint8_t HTS221_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
cparata 2:21a191bd1998 313 {
cparata 2:21a191bd1998 314 return ((HTS221Sensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead);
cparata 2:21a191bd1998 315 }