test for a library

Committer:
hober
Date:
Wed Aug 15 02:12:10 2018 +0000
Revision:
0:43f2f3301904
2018/08/15 convert to library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hober 0:43f2f3301904 1 /**
hober 0:43f2f3301904 2 ******************************************************************************
hober 0:43f2f3301904 3 * @file altimu_10_v5.cpp
hober 0:43f2f3301904 4 * @author AST / EST
hober 0:43f2f3301904 5 * @version V0.0.1
hober 0:43f2f3301904 6 * @date 08-October-2014
hober 0:43f2f3301904 7 * @brief Implementation file for the AltIMU_10_v5 singleton class
hober 0:43f2f3301904 8 ******************************************************************************
hober 0:43f2f3301904 9 * @attention
hober 0:43f2f3301904 10 *
hober 0:43f2f3301904 11 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
hober 0:43f2f3301904 12 *
hober 0:43f2f3301904 13 * Redistribution and use in source and binary forms, with or without modification,
hober 0:43f2f3301904 14 * are permitted provided that the following conditions are met:
hober 0:43f2f3301904 15 * 1. Redistributions of source code must retain the above copyright notice,
hober 0:43f2f3301904 16 * this list of conditions and the following disclaimer.
hober 0:43f2f3301904 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
hober 0:43f2f3301904 18 * this list of conditions and the following disclaimer in the documentation
hober 0:43f2f3301904 19 * and/or other materials provided with the distribution.
hober 0:43f2f3301904 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
hober 0:43f2f3301904 21 * may be used to endorse or promote products derived from this software
hober 0:43f2f3301904 22 * without specific prior written permission.
hober 0:43f2f3301904 23 *
hober 0:43f2f3301904 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
hober 0:43f2f3301904 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
hober 0:43f2f3301904 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
hober 0:43f2f3301904 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
hober 0:43f2f3301904 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
hober 0:43f2f3301904 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
hober 0:43f2f3301904 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
hober 0:43f2f3301904 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
hober 0:43f2f3301904 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
hober 0:43f2f3301904 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
hober 0:43f2f3301904 34 *
hober 0:43f2f3301904 35 ******************************************************************************
hober 0:43f2f3301904 36 */
hober 0:43f2f3301904 37
hober 0:43f2f3301904 38 /* Includes ------------------------------------------------------------------*/
hober 0:43f2f3301904 39 #include "mbed.h"
hober 0:43f2f3301904 40 #include "altimu_10_v5.h"
hober 0:43f2f3301904 41
hober 0:43f2f3301904 42 /* Static variables ----------------------------------------------------------*/
hober 0:43f2f3301904 43 AltIMU_10_v5* AltIMU_10_v5::_instance0 = NULL;
hober 0:43f2f3301904 44 AltIMU_10_v5* AltIMU_10_v5::_instance1 = NULL;
hober 0:43f2f3301904 45
hober 0:43f2f3301904 46
hober 0:43f2f3301904 47 /* Methods -------------------------------------------------------------------*/
hober 0:43f2f3301904 48 /**
hober 0:43f2f3301904 49 * @brief Constructor
hober 0:43f2f3301904 50 */
hober 0:43f2f3301904 51 AltIMU_10_v5::AltIMU_10_v5(DevI2C *ext_i2c, uint8_t SA0) : dev_i2c(ext_i2c),
hober 0:43f2f3301904 52 magnetometer(new LIS3MDL(*dev_i2c,SA0)),
hober 0:43f2f3301904 53 pt_sensor(new LPS25H(*dev_i2c,SA0)),
hober 0:43f2f3301904 54 gyro_lsm6ds33(new LSM6DS33(*dev_i2c,SA0))
hober 0:43f2f3301904 55 {
hober 0:43f2f3301904 56 }
hober 0:43f2f3301904 57
hober 0:43f2f3301904 58 /**
hober 0:43f2f3301904 59 * @brief Get singleton instance
hober 0:43f2f3301904 60 * @return a pointer to the initialized singleton instance of class AltIMU_10_v5.
hober 0:43f2f3301904 61 * A return value of NULL indicates an out of memory situation.
hober 0:43f2f3301904 62 * @param[in] ext_i2c (optional) pointer to an instance of DevI2C to be used
hober 0:43f2f3301904 63 * for communication on the expansion board.
hober 0:43f2f3301904 64 * Defaults to NULL.
hober 0:43f2f3301904 65 * Taken into account only on the very first call of one of the 'Instance' functions.
hober 0:43f2f3301904 66 * If not provided a new DevI2C will be created with standard
hober 0:43f2f3301904 67 * configuration parameters.
hober 0:43f2f3301904 68 * The used DevI2C object gets saved in instance variable dev_i2c.
hober 0:43f2f3301904 69 * @param[in] ff_irq_pin (optional) PinName of the pin associated to asynchronous
hober 0:43f2f3301904 70 * (i.e. interrupt based) free fall detection in case a LSM6DS33 3D
hober 0:43f2f3301904 71 * Acceleromenter and 3D Gyroscope is mounted on top of the DIL 24-pin socket.
hober 0:43f2f3301904 72 * Defaults to F746ZG_PIN_FF.
hober 0:43f2f3301904 73 * Taken into account only on the very first call of one of the 'Instance' functions.
hober 0:43f2f3301904 74 * A value of 'NC' will avoid instantiation of the LSM6DS33 even if present.
hober 0:43f2f3301904 75 */
hober 0:43f2f3301904 76 AltIMU_10_v5* AltIMU_10_v5::Instance(DevI2C *ext_i2c, uint8_t SA0) {
hober 0:43f2f3301904 77 if(_instance0 == NULL && SA0 == 0) {
hober 0:43f2f3301904 78 if(ext_i2c == NULL)
hober 0:43f2f3301904 79 ext_i2c = new DevI2C(F746ZG_PIN_I2C_SDA, F746ZG_PIN_I2C_SCL);
hober 0:43f2f3301904 80
hober 0:43f2f3301904 81 if(ext_i2c != NULL)
hober 0:43f2f3301904 82 _instance0 = new AltIMU_10_v5(ext_i2c, 0);
hober 0:43f2f3301904 83
hober 0:43f2f3301904 84 if(_instance0 != NULL) {
hober 0:43f2f3301904 85 bool ret = _instance0->Init();
hober 0:43f2f3301904 86 if(!ret) {
hober 0:43f2f3301904 87 error("Failed to init AltIMU_10_v5 expansion board for SA0=0!\n");
hober 0:43f2f3301904 88 }
hober 0:43f2f3301904 89 }
hober 0:43f2f3301904 90 }
hober 0:43f2f3301904 91 else if(_instance1 == NULL && SA0 == 1) {
hober 0:43f2f3301904 92 if(ext_i2c == NULL)
hober 0:43f2f3301904 93 ext_i2c = new DevI2C(F746ZG_PIN_I2C_SDA, F746ZG_PIN_I2C_SCL);
hober 0:43f2f3301904 94
hober 0:43f2f3301904 95 if(ext_i2c != NULL)
hober 0:43f2f3301904 96 _instance1 = new AltIMU_10_v5(ext_i2c, 1);
hober 0:43f2f3301904 97
hober 0:43f2f3301904 98 if(_instance1 != NULL) {
hober 0:43f2f3301904 99 bool ret = _instance1->Init();
hober 0:43f2f3301904 100 if(!ret) {
hober 0:43f2f3301904 101 error("Failed to init AltIMU_10_v5 expansion board for SA0=1!\n");
hober 0:43f2f3301904 102 }
hober 0:43f2f3301904 103 }
hober 0:43f2f3301904 104 }
hober 0:43f2f3301904 105
hober 0:43f2f3301904 106 return (SA0 == 0 ? _instance0 :
hober 0:43f2f3301904 107 (SA0 == 1 ? _instance1 : NULL));
hober 0:43f2f3301904 108 }
hober 0:43f2f3301904 109
hober 0:43f2f3301904 110 /**
hober 0:43f2f3301904 111 * @brief Get singleton instance
hober 0:43f2f3301904 112 * @return a pointer to the initialized singleton instance of class AltIMU_10_v5.
hober 0:43f2f3301904 113 * A return value of NULL indicates an out of memory situation.
hober 0:43f2f3301904 114 * @param[in] sda I2C data line pin.
hober 0:43f2f3301904 115 * Taken into account only on the very first call of one of the 'Instance' functions.
hober 0:43f2f3301904 116 * A new DevI2C will be created based on parameters 'sda' and 'scl'.
hober 0:43f2f3301904 117 * The used DevI2C object gets saved in instance variable dev_i2c.
hober 0:43f2f3301904 118 * @param[in] scl I2C clock line pin.
hober 0:43f2f3301904 119 * Taken into account only on the very first call of one of the 'Instance' functions.
hober 0:43f2f3301904 120 * A new DevI2C will be created based on parameters 'sda' and 'scl'.
hober 0:43f2f3301904 121 * The used DevI2C object gets saved in instance variable dev_i2c.
hober 0:43f2f3301904 122 * @param[in] ff_irq_pin (optional) PinName of the pin associated to asynchronous
hober 0:43f2f3301904 123 * (i.e. interrupt based) free fall detection in case a LSM6DS33 3D
hober 0:43f2f3301904 124 * Acceleromenter and 3D Gyroscope is mounted on top of the DIL 24-pin socket.
hober 0:43f2f3301904 125 * Defaults to NC.
hober 0:43f2f3301904 126 * Taken into account only on the very first call of one of the 'Instance' functions.
hober 0:43f2f3301904 127 * A value of 'NC' will avoid instantiation of the LSM6DS33 even if present.
hober 0:43f2f3301904 128 */
hober 0:43f2f3301904 129 AltIMU_10_v5* AltIMU_10_v5::Instance(PinName sda, PinName scl, uint8_t SA0) {
hober 0:43f2f3301904 130 DevI2C *ext_i2c = new DevI2C(sda, scl);
hober 0:43f2f3301904 131
hober 0:43f2f3301904 132 if(ext_i2c != NULL) return Instance(ext_i2c, SA0);
hober 0:43f2f3301904 133
hober 0:43f2f3301904 134 return NULL;
hober 0:43f2f3301904 135 }
hober 0:43f2f3301904 136
hober 0:43f2f3301904 137
hober 0:43f2f3301904 138 /**
hober 0:43f2f3301904 139 * @brief Initialize the singleton's magnetometer
hober 0:43f2f3301904 140 * @retval true if initialization successful,
hober 0:43f2f3301904 141 * @retval false otherwise
hober 0:43f2f3301904 142 */
hober 0:43f2f3301904 143 bool AltIMU_10_v5::Init_LIS3MDL(void) {
hober 0:43f2f3301904 144 uint8_t m_id = 0;
hober 0:43f2f3301904 145 MAGNETO_InitTypeDef InitStructure;
hober 0:43f2f3301904 146
hober 0:43f2f3301904 147 /* Check presence */
hober 0:43f2f3301904 148 if((magnetometer->read_id(&m_id) != MAGNETO_OK) ||
hober 0:43f2f3301904 149 (m_id != I_AM_LIS3MDL_M))
hober 0:43f2f3301904 150 {
hober 0:43f2f3301904 151 delete magnetometer;
hober 0:43f2f3301904 152 magnetometer = NULL;
hober 0:43f2f3301904 153 return true;
hober 0:43f2f3301904 154 }
hober 0:43f2f3301904 155
hober 0:43f2f3301904 156 /* Configure sensor */
hober 0:43f2f3301904 157 InitStructure.M_FullScale = 4.0f;
hober 0:43f2f3301904 158 InitStructure.M_OperatingMode = LIS3MDL_M_MD_CONTINUOUS;
hober 0:43f2f3301904 159 InitStructure.M_XYOperativeMode = LIS3MDL_M_OM_HP;
hober 0:43f2f3301904 160 InitStructure.M_OutputDataRate = 1000.0f;
hober 0:43f2f3301904 161
hober 0:43f2f3301904 162 if(magnetometer->init(&InitStructure) != MAGNETO_OK)
hober 0:43f2f3301904 163 {
hober 0:43f2f3301904 164 return false;
hober 0:43f2f3301904 165 }
hober 0:43f2f3301904 166
hober 0:43f2f3301904 167 return true;
hober 0:43f2f3301904 168 }
hober 0:43f2f3301904 169
hober 0:43f2f3301904 170 /**
hober 0:43f2f3301904 171 * @brief Initialize the singleton's pressure sensor
hober 0:43f2f3301904 172 * @retval true if initialization successful,
hober 0:43f2f3301904 173 * @retval false otherwise
hober 0:43f2f3301904 174 */
hober 0:43f2f3301904 175 bool AltIMU_10_v5::Init_LPS25H(void) {
hober 0:43f2f3301904 176 uint8_t p_id = 0;
hober 0:43f2f3301904 177 PRESSURE_InitTypeDef InitStructure;
hober 0:43f2f3301904 178
hober 0:43f2f3301904 179 /* Check presence */
hober 0:43f2f3301904 180 if((pt_sensor->read_id(&p_id) != PRESSURE_OK) ||
hober 0:43f2f3301904 181 (p_id != I_AM_LPS25H))
hober 0:43f2f3301904 182 {
hober 0:43f2f3301904 183 delete pt_sensor;
hober 0:43f2f3301904 184 pt_sensor = NULL;
hober 0:43f2f3301904 185 return true;
hober 0:43f2f3301904 186 }
hober 0:43f2f3301904 187
hober 0:43f2f3301904 188 /* Configure sensor */
hober 0:43f2f3301904 189 InitStructure.OutputDataRate = LPS25H_ODR_1Hz;
hober 0:43f2f3301904 190 InitStructure.BlockDataUpdate = LPS25H_BDU_CONT;
hober 0:43f2f3301904 191 InitStructure.DiffEnable = LPS25H_DIFF_ENABLE;
hober 0:43f2f3301904 192 InitStructure.SPIMode = LPS25H_SPI_SIM_3W;
hober 0:43f2f3301904 193 InitStructure.PressureResolution = LPS25H_P_RES_AVG_32;
hober 0:43f2f3301904 194 InitStructure.TemperatureResolution = LPS25H_T_RES_AVG_16;
hober 0:43f2f3301904 195
hober 0:43f2f3301904 196 if(pt_sensor->init(&InitStructure) != PRESSURE_OK)
hober 0:43f2f3301904 197 {
hober 0:43f2f3301904 198 return false;
hober 0:43f2f3301904 199 }
hober 0:43f2f3301904 200
hober 0:43f2f3301904 201 return true;
hober 0:43f2f3301904 202 }
hober 0:43f2f3301904 203
hober 0:43f2f3301904 204 /**
hober 0:43f2f3301904 205 * @brief Initialize the singleton's LSM6DS33 gyroscope
hober 0:43f2f3301904 206 * @retval true if initialization successful,
hober 0:43f2f3301904 207 * @retval false otherwise
hober 0:43f2f3301904 208 */
hober 0:43f2f3301904 209 bool AltIMU_10_v5::Init_LSM6DS33(void) {
hober 0:43f2f3301904 210 IMU_6AXES_InitTypeDef InitStructure;
hober 0:43f2f3301904 211 uint8_t xg_id = 0;
hober 0:43f2f3301904 212 bool status=true;
hober 0:43f2f3301904 213
hober 0:43f2f3301904 214 /* Check presence */
hober 0:43f2f3301904 215 if((status=(gyro_lsm6ds33->read_id(&xg_id) != IMU_6AXES_OK)) ||
hober 0:43f2f3301904 216 (xg_id != I_AM_LSM6DS33_XG))
hober 0:43f2f3301904 217 {
hober 0:43f2f3301904 218 delete gyro_lsm6ds33;
hober 0:43f2f3301904 219 gyro_lsm6ds33 = NULL;
hober 0:43f2f3301904 220 return true;
hober 0:43f2f3301904 221 }
hober 0:43f2f3301904 222
hober 0:43f2f3301904 223 /* Configure sensor */
hober 0:43f2f3301904 224 InitStructure.G_FullScale = 2000.0f; /* 2000DPS */
hober 0:43f2f3301904 225 InitStructure.G_OutputDataRate = 119.0f; /* 119HZ */
hober 0:43f2f3301904 226 InitStructure.G_X_Axis = 1; /* Enable */
hober 0:43f2f3301904 227 InitStructure.G_Y_Axis = 1; /* Enable */
hober 0:43f2f3301904 228 InitStructure.G_Z_Axis = 1; /* Enable */
hober 0:43f2f3301904 229
hober 0:43f2f3301904 230 InitStructure.X_FullScale = 2.0f; /* 2G */
hober 0:43f2f3301904 231 InitStructure.X_OutputDataRate = 119.0f; /* 119HZ */
hober 0:43f2f3301904 232 InitStructure.X_X_Axis = 1; /* Enable */
hober 0:43f2f3301904 233 InitStructure.X_Y_Axis = 1; /* Enable */
hober 0:43f2f3301904 234 InitStructure.X_Z_Axis = 1; /* Enable */
hober 0:43f2f3301904 235
hober 0:43f2f3301904 236 if(gyro_lsm6ds33->init(&InitStructure) != IMU_6AXES_OK)
hober 0:43f2f3301904 237 {
hober 0:43f2f3301904 238 return false;
hober 0:43f2f3301904 239 }
hober 0:43f2f3301904 240
hober 0:43f2f3301904 241 return true;
hober 0:43f2f3301904 242 }