Beta
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of X_NUCLEO_IKS01A2 by
Components/LSM303AGRSensor/LSM303AGRAccSensor.h@11:8f4e5789b6b3, 2017-11-02 (annotated)
- Committer:
- Qksalo
- Date:
- Thu Nov 02 10:58:12 2017 +0000
- Revision:
- 11:8f4e5789b6b3
- Parent:
- 9:038121268b07
Beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davide.aliprandi@st.com | 6:671fd10a51b7 | 1 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 2 | ****************************************************************************** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 3 | * @file LSM303AGRAccSensor.h |
davide.aliprandi@st.com | 6:671fd10a51b7 | 4 | * @author CLab |
davide.aliprandi@st.com | 6:671fd10a51b7 | 5 | * @version V1.0.0 |
davide.aliprandi@st.com | 6:671fd10a51b7 | 6 | * @date 5 August 2016 |
davide.aliprandi@st.com | 6:671fd10a51b7 | 7 | * @brief Abstract Class of an LSM303AGR accelerometer sensor. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 8 | ****************************************************************************** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 9 | * @attention |
davide.aliprandi@st.com | 6:671fd10a51b7 | 10 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
davide.aliprandi@st.com | 6:671fd10a51b7 | 12 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 14 | * are permitted provided that the following conditions are met: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 16 | * this list of conditions and the following disclaimer. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 18 | * this list of conditions and the following disclaimer in the documentation |
davide.aliprandi@st.com | 6:671fd10a51b7 | 19 | * and/or other materials provided with the distribution. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
davide.aliprandi@st.com | 6:671fd10a51b7 | 21 | * may be used to endorse or promote products derived from this software |
davide.aliprandi@st.com | 6:671fd10a51b7 | 22 | * without specific prior written permission. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 23 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
davide.aliprandi@st.com | 6:671fd10a51b7 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
davide.aliprandi@st.com | 6:671fd10a51b7 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
davide.aliprandi@st.com | 6:671fd10a51b7 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 34 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 35 | ****************************************************************************** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 36 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 37 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 38 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 39 | /* Prevent recursive inclusion -----------------------------------------------*/ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 40 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 41 | #ifndef __LSM303AGRAccSensor_H__ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 42 | #define __LSM303AGRAccSensor_H__ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 43 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 44 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 45 | /* Includes ------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 46 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 47 | #include "DevI2C.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 48 | #include "LSM303AGR_acc_driver.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 49 | #include "MotionSensor.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 50 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 51 | /* Defines -------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 52 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_NORMAL_MODE 3.900f /**< Sensitivity value for 2 g full scale and normal mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 53 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_HIGH_RESOLUTION_MODE 0.980f /**< Sensitivity value for 2 g full scale and high resolution mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 54 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_2G_LOW_POWER_MODE 15.630f /**< Sensitivity value for 2 g full scale and low power mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 55 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_NORMAL_MODE 7.820f /**< Sensitivity value for 4 g full scale and normal mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 56 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_HIGH_RESOLUTION_MODE 1.950f /**< Sensitivity value for 4 g full scale and high resolution mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 57 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_4G_LOW_POWER_MODE 31.260f /**< Sensitivity value for 4 g full scale and low power mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 58 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_NORMAL_MODE 15.630f /**< Sensitivity value for 8 g full scale and normal mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 59 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_HIGH_RESOLUTION_MODE 3.900f /**< Sensitivity value for 8 g full scale and high resolution mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 60 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_8G_LOW_POWER_MODE 62.520f /**< Sensitivity value for 8 g full scale and low power mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 61 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_NORMAL_MODE 46.900f /**< Sensitivity value for 16 g full scale and normal mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 62 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_HIGH_RESOLUTION_MODE 11.720f /**< Sensitivity value for 16 g full scale and high resolution mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 63 | #define LSM303AGR_ACC_SENSITIVITY_FOR_FS_16G_LOW_POWER_MODE 187.580f /**< Sensitivity value for 16 g full scale and low power mode [mg/LSB] */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 64 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 65 | /* Class Declaration ---------------------------------------------------------*/ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 66 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 67 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 68 | * Abstract class of an LSM303AGR Inertial Measurement Unit (IMU) 6 axes |
davide.aliprandi@st.com | 6:671fd10a51b7 | 69 | * sensor. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 70 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 71 | class LSM303AGRAccSensor : public MotionSensor |
davide.aliprandi@st.com | 6:671fd10a51b7 | 72 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 73 | public: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 74 | LSM303AGRAccSensor(DevI2C &i2c); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 75 | LSM303AGRAccSensor(DevI2C &i2c, uint8_t address); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 76 | virtual int init(void *init); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 77 | virtual int read_id(uint8_t *id); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 78 | virtual int get_x_axes(int32_t *pData); |
Davidroid | 9:038121268b07 | 79 | virtual int get_x_axes_raw(int16_t *pData); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 80 | virtual int get_x_sensitivity(float *pfData); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 81 | virtual int get_x_odr(float *odr); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 82 | virtual int set_x_odr(float odr); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 83 | virtual int get_x_fs(float *fullScale); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 84 | virtual int set_x_fs(float fullScale); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 85 | int enable(void); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 86 | int disable(void); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 87 | int read_reg(uint8_t reg, uint8_t *data); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 88 | int write_reg(uint8_t reg, uint8_t data); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 89 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 90 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 91 | * @brief Utility function to read data. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 92 | * @param pBuffer: pointer to data to be read. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 93 | * @param RegisterAddr: specifies internal address register to be read. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 94 | * @param NumByteToRead: number of bytes to be read. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 95 | * @retval 0 if ok, an error code otherwise. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 96 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 97 | uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 98 | { |
Davidroid | 9:038121268b07 | 99 | return (uint8_t) _dev_i2c.i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 100 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 101 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 102 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 103 | * @brief Utility function to write data. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 104 | * @param pBuffer: pointer to data to be written. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 105 | * @param RegisterAddr: specifies internal address register to be written. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 106 | * @param NumByteToWrite: number of bytes to write. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 107 | * @retval 0 if ok, an error code otherwise. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 108 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 109 | uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 110 | { |
Davidroid | 9:038121268b07 | 111 | return (uint8_t) _dev_i2c.i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 112 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 113 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 114 | private: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 115 | int set_x_odr_when_enabled(float odr); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 116 | int set_x_odr_when_disabled(float odr); |
Davidroid | 9:038121268b07 | 117 | int get_x_sensitivity_normal_mode(float *sensitivity ); |
Davidroid | 9:038121268b07 | 118 | int get_x_sensitivity_lp_mode(float *sensitivity ); |
Davidroid | 9:038121268b07 | 119 | int get_x_sensitivity_hr_mode(float *sensitivity ); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 120 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 121 | /* Helper classes. */ |
Davidroid | 9:038121268b07 | 122 | DevI2C &_dev_i2c; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 123 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 124 | /* Configuration */ |
Davidroid | 9:038121268b07 | 125 | uint8_t _address; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 126 | |
Davidroid | 9:038121268b07 | 127 | uint8_t _is_enabled; |
Davidroid | 9:038121268b07 | 128 | float _last_odr; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 129 | }; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 130 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 131 | #ifdef __cplusplus |
davide.aliprandi@st.com | 6:671fd10a51b7 | 132 | extern "C" { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 133 | #endif |
davide.aliprandi@st.com | 6:671fd10a51b7 | 134 | uint8_t LSM303AGR_ACC_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 135 | uint8_t LSM303AGR_ACC_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 136 | #ifdef __cplusplus |
davide.aliprandi@st.com | 6:671fd10a51b7 | 137 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 138 | #endif |
davide.aliprandi@st.com | 6:671fd10a51b7 | 139 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 140 | #endif |