|
| 1 | +/* |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2019 ubirch GmbH. |
| 5 | +Adapted from LSM303Accelerometer.h by (c) Lancaster University. |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | +copy of this software and associated documentation files (the "Software"), |
| 9 | +to deal in the Software without restriction, including without limitation |
| 10 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | +and/or sell copies of the Software, and to permit persons to whom the |
| 12 | +Software is furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in |
| 15 | +all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | +DEALINGS IN THE SOFTWARE. |
| 24 | +*/ |
| 25 | + |
| 26 | +#ifndef BMX055_ACCELEROMETER_H |
| 27 | +#define BMX055_ACCELEROMETER_H |
| 28 | + |
| 29 | +#include "MicroBitConfig.h" |
| 30 | +#include "MicroBitComponent.h" |
| 31 | +#include "CoordinateSystem.h" |
| 32 | +#include "MicroBitAccelerometer.h" |
| 33 | +#include "MicroBitI2C.h" |
| 34 | +#include "MicroBitUtil.h" |
| 35 | + |
| 36 | +#define BMX055_A_DEFAULT_ADDR 0x30 //!< address of the BMX055 accelerometer |
| 37 | + |
| 38 | +#define BMX055_A_WHOAMI 0x00 // should return 0xFA |
| 39 | +//#define BMX055_A_Reserved 0x01 |
| 40 | +#define BMX055_A_D_X_LSB 0x02 |
| 41 | +#define BMX055_A_D_X_MSB 0x03 |
| 42 | +#define BMX055_A_D_Y_LSB 0x04 |
| 43 | +#define BMX055_A_D_Y_MSB 0x05 |
| 44 | +#define BMX055_A_D_Z_LSB 0x06 |
| 45 | +#define BMX055_A_D_Z_MSB 0x07 |
| 46 | +#define BMX055_A_D_TEMP 0x08 |
| 47 | +#define BMX055_A_INT_STATUS_0 0x09 |
| 48 | +#define BMX055_A_INT_STATUS_1 0x0A |
| 49 | +#define BMX055_A_INT_STATUS_2 0x0B |
| 50 | +#define BMX055_A_INT_STATUS_3 0x0C |
| 51 | +//#define BMX055_A_Reserved 0x0D |
| 52 | +#define BMX055_A_FIFO_STATUS 0x0E |
| 53 | +#define BMX055_A_PMU_RANGE 0x0F |
| 54 | +#define BMX055_A_PMU_BW 0x10 |
| 55 | +#define BMX055_A_PMU_LPW 0x11 |
| 56 | +#define BMX055_A_PMU_LOW_POWER 0x12 |
| 57 | +#define BMX055_A_D_HBW 0x13 |
| 58 | +#define BMX055_A_BGW_SOFTRESET 0x14 |
| 59 | +//#define BMX055_A_Reserved 0x15 |
| 60 | +#define BMX055_A_INT_EN_0 0x16 |
| 61 | +#define BMX055_A_INT_EN_1 0x17 |
| 62 | +#define BMX055_A_INT_EN_2 0x18 |
| 63 | +#define BMX055_A_INT_MAP_0 0x19 |
| 64 | +#define BMX055_A_INT_MAP_1 0x1A |
| 65 | +#define BMX055_A_INT_MAP_2 0x1B |
| 66 | +//#define BMX055_A_Reserved 0x1C |
| 67 | +//#define BMX055_A_Reserved 0x1D |
| 68 | +#define BMX055_A_INT_SRC 0x1E |
| 69 | +//#define BMX055_A_Reserved 0x1F |
| 70 | +#define BMX055_A_INT_OUT_CTRL 0x20 |
| 71 | +#define BMX055_A_INT_RST_LATCH 0x21 |
| 72 | +#define BMX055_A_INT_0 0x22 |
| 73 | +#define BMX055_A_INT_1 0x23 |
| 74 | +#define BMX055_A_INT_2 0x24 |
| 75 | +#define BMX055_A_INT_3 0x25 |
| 76 | +#define BMX055_A_INT_4 0x26 |
| 77 | +#define BMX055_A_INT_5 0x27 |
| 78 | +#define BMX055_A_INT_6 0x28 |
| 79 | +#define BMX055_A_INT_7 0x29 |
| 80 | +#define BMX055_A_INT_8 0x2A |
| 81 | +#define BMX055_A_INT_9 0x2B |
| 82 | +#define BMX055_A_INT_A 0x2C |
| 83 | +#define BMX055_A_INT_B 0x2D |
| 84 | +#define BMX055_A_INT_C 0x2E |
| 85 | +#define BMX055_A_INT_D 0x2F |
| 86 | +#define BMX055_A_FIFO_CONFIG_0 0x30 |
| 87 | +//#define BMX055_A_Reserved 0x31 |
| 88 | +#define BMX055_A_PMU_SELF_TEST 0x32 |
| 89 | +#define BMX055_A_TRIM_NVM_CTRL 0x33 |
| 90 | +#define BMX055_A_BGW_SPI3_WDT 0x34 |
| 91 | +//#define BMX055_A_Reserved 0x35 |
| 92 | +#define BMX055_A_OFC_CTRL 0x36 |
| 93 | +#define BMX055_A_OFC_SETTING 0x37 |
| 94 | +#define BMX055_A_OFC_OFFSET_X 0x38 |
| 95 | +#define BMX055_A_OFC_OFFSET_Y 0x39 |
| 96 | +#define BMX055_A_OFC_OFFSET_Z 0x3A |
| 97 | +#define BMX055_A_TRIM_GPO 0x3B |
| 98 | +#define BMX055_A_TRIM_GP1 0x3C |
| 99 | +//#define BMX055_A_Reserved 0x3D |
| 100 | +#define BMX055_A_FIFO_CONFIG_1 0x3E |
| 101 | +#define BMX055_A_FIFO_DATA 0x3F |
| 102 | + |
| 103 | +#define BMX055_A_WHOAMI_VAL 0xFA |
| 104 | + |
| 105 | +/** |
| 106 | + * Class definition for BMX055Accelerometer. |
| 107 | + * |
| 108 | + * This class defines the interface for the BMX055 accelerometer. |
| 109 | + */ |
| 110 | +class BMX055Accelerometer : public MicroBitAccelerometer |
| 111 | +{ |
| 112 | + MicroBitI2C& i2c; // The I2C interface to use. |
| 113 | + MicroBitPin int1; // Data ready interrupt. |
| 114 | + uint16_t address; // I2C address of this accelerometer. |
| 115 | + |
| 116 | + public: |
| 117 | + |
| 118 | + /** |
| 119 | + * Constructor. |
| 120 | + * Create a software abstraction of an accelerometer. |
| 121 | + * |
| 122 | + * @param coordinateSpace The orientation of the sensor. Defaults to: SIMPLE_CARTESIAN |
| 123 | + * @param id The unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER |
| 124 | + * |
| 125 | + */ |
| 126 | + BMX055Accelerometer(MicroBitI2C& _i2c, MicroBitPin _int1, CoordinateSpace &coordinateSpace, uint16_t address = BMX055_A_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_ACCELEROMETER); |
| 127 | + |
| 128 | + /** |
| 129 | + * Configures the accelerometer for G range and sample rate defined |
| 130 | + * in this object. The nearest values are chosen to those defined |
| 131 | + * that are supported by the hardware. The instance variables are then |
| 132 | + * updated to reflect reality. |
| 133 | + * |
| 134 | + * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured. |
| 135 | + * |
| 136 | + * @note This method should be overidden by the hardware driver to implement the requested |
| 137 | + * changes in hardware. |
| 138 | + */ |
| 139 | + virtual int configure(); |
| 140 | + |
| 141 | + /** |
| 142 | + * Poll to see if new data is available from the hardware. If so, update it. |
| 143 | + * n.b. it is not necessary to explicitly call this funciton to update data |
| 144 | + * (it normally happens in the background when the scheduler is idle), but a check is performed |
| 145 | + * if the user explicitly requests up to date data. |
| 146 | + * |
| 147 | + * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the update fails. |
| 148 | + * |
| 149 | + * @note This method should be overidden by the hardware driver to implement the requested |
| 150 | + * changes in hardware. |
| 151 | + */ |
| 152 | + virtual int requestUpdate(); |
| 153 | + |
| 154 | + /** |
| 155 | + * A periodic callback invoked by the fiber scheduler idle thread. |
| 156 | + * |
| 157 | + * Internally calls updateSample(). |
| 158 | + */ |
| 159 | + virtual void idleTick(); |
| 160 | + |
| 161 | + /** |
| 162 | + * Attempts to read the 8 bit WHO_AM_I value from the accelerometer |
| 163 | + * |
| 164 | + * @return true if the WHO_AM_I value is succesfully read. false otherwise. |
| 165 | + */ |
| 166 | + static int isDetected(MicroBitI2C &i2c, uint16_t address = BMX055_A_DEFAULT_ADDR); |
| 167 | + |
| 168 | + /** |
| 169 | + * Destructor. |
| 170 | + */ |
| 171 | + ~BMX055Accelerometer(); |
| 172 | + |
| 173 | +}; |
| 174 | + |
| 175 | +#endif |
| 176 | + |
0 commit comments