mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal
Dependencies: BLE_API nRF51822 mbed-dev-bin
Dependents: microbit Microbit IoTChallenge1 microbit ... more
inc/drivers/MicroBitAccelerometer.h@73:eb91bba49623, 2016-07-13 (annotated)
- Committer:
- JonnyA
- Date:
- Wed Jul 13 14:32:54 2016 +0000
- Revision:
- 73:eb91bba49623
- Parent:
- 65:f7ebabf23e15
Link to latest mbed-dev-bin (mbed library)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | #ifndef MICROBIT_ACCELEROMETER_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_ACCELEROMETER_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | #include "mbed.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "MicroBitComponent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitCoordinateSystem.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | #include "MicroBitI2C.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | * Relevant pin assignments |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | #define MICROBIT_PIN_ACCEL_DATA_READY P0_28 |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | * Status flags |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | #define MICROBIT_ACCEL_PITCH_ROLL_VALID 0x02 |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | #define MICROBIT_ACCEL_ADDED_TO_IDLE 0x04 |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | * I2C constants |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | #define MMA8653_DEFAULT_ADDR 0x3A |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | * MMA8653 Register map (partial) |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | #define MMA8653_STATUS 0x00 |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | #define MMA8653_OUT_X_MSB 0x01 |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | #define MMA8653_WHOAMI 0x0D |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | #define MMA8653_XYZ_DATA_CFG 0x0E |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | #define MMA8653_CTRL_REG1 0x2A |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | #define MMA8653_CTRL_REG2 0x2B |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | #define MMA8653_CTRL_REG3 0x2C |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | #define MMA8653_CTRL_REG4 0x2D |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | #define MMA8653_CTRL_REG5 0x2E |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | * MMA8653 constants |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | #define MMA8653_WHOAMI_VAL 0x5A |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | #define MMA8653_SAMPLE_RANGES 3 |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | #define MMA8653_SAMPLE_RATES 8 |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | * Accelerometer events |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | #define MICROBIT_ACCELEROMETER_EVT_DATA_UPDATE 1 |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * Gesture events |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | */ |
LancasterUniversity | 39:112df23f039f | 81 | #define MICROBIT_ACCELEROMETER_EVT_NONE 0 |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | #define MICROBIT_ACCELEROMETER_EVT_TILT_UP 1 |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | #define MICROBIT_ACCELEROMETER_EVT_TILT_DOWN 2 |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | #define MICROBIT_ACCELEROMETER_EVT_TILT_LEFT 3 |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | #define MICROBIT_ACCELEROMETER_EVT_TILT_RIGHT 4 |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | #define MICROBIT_ACCELEROMETER_EVT_FACE_UP 5 |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | #define MICROBIT_ACCELEROMETER_EVT_FACE_DOWN 6 |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | #define MICROBIT_ACCELEROMETER_EVT_FREEFALL 7 |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | #define MICROBIT_ACCELEROMETER_EVT_3G 8 |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | #define MICROBIT_ACCELEROMETER_EVT_6G 9 |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | #define MICROBIT_ACCELEROMETER_EVT_8G 10 |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | #define MICROBIT_ACCELEROMETER_EVT_SHAKE 11 |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * Gesture recogniser constants |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | #define MICROBIT_ACCELEROMETER_REST_TOLERANCE 200 |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | #define MICROBIT_ACCELEROMETER_TILT_TOLERANCE 200 |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | #define MICROBIT_ACCELEROMETER_FREEFALL_TOLERANCE 400 |
LancasterUniversity | 65:f7ebabf23e15 | 100 | #define MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE 400 |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | #define MICROBIT_ACCELEROMETER_3G_TOLERANCE 3072 |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | #define MICROBIT_ACCELEROMETER_6G_TOLERANCE 6144 |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | #define MICROBIT_ACCELEROMETER_8G_TOLERANCE 8192 |
LancasterUniversity | 65:f7ebabf23e15 | 104 | #define MICROBIT_ACCELEROMETER_GESTURE_DAMPING 5 |
LancasterUniversity | 65:f7ebabf23e15 | 105 | #define MICROBIT_ACCELEROMETER_SHAKE_DAMPING 10 |
LancasterUniversity | 65:f7ebabf23e15 | 106 | #define MICROBIT_ACCELEROMETER_SHAKE_RTX 30 |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | #define MICROBIT_ACCELEROMETER_REST_THRESHOLD (MICROBIT_ACCELEROMETER_REST_TOLERANCE * MICROBIT_ACCELEROMETER_REST_TOLERANCE) |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | #define MICROBIT_ACCELEROMETER_FREEFALL_THRESHOLD (MICROBIT_ACCELEROMETER_FREEFALL_TOLERANCE * MICROBIT_ACCELEROMETER_FREEFALL_TOLERANCE) |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | #define MICROBIT_ACCELEROMETER_3G_THRESHOLD (MICROBIT_ACCELEROMETER_3G_TOLERANCE * MICROBIT_ACCELEROMETER_3G_TOLERANCE) |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | #define MICROBIT_ACCELEROMETER_6G_THRESHOLD (MICROBIT_ACCELEROMETER_6G_TOLERANCE * MICROBIT_ACCELEROMETER_6G_TOLERANCE) |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | #define MICROBIT_ACCELEROMETER_8G_THRESHOLD (MICROBIT_ACCELEROMETER_8G_TOLERANCE * MICROBIT_ACCELEROMETER_8G_TOLERANCE) |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | #define MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD 4 |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | struct MMA8653Sample |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | int16_t x; |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | int16_t y; |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | int16_t z; |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | struct MMA8653SampleRateConfig |
Jonathan Austin |
1:8aa5cdb4ab67 | 123 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 124 | uint32_t sample_period; |
Jonathan Austin |
1:8aa5cdb4ab67 | 125 | uint8_t ctrl_reg1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 126 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 127 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 128 | struct MMA8653SampleRangeConfig |
Jonathan Austin |
1:8aa5cdb4ab67 | 129 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 130 | uint8_t sample_range; |
Jonathan Austin |
1:8aa5cdb4ab67 | 131 | uint8_t xyz_data_cfg; |
Jonathan Austin |
1:8aa5cdb4ab67 | 132 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 133 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 134 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 135 | extern const MMA8653SampleRangeConfig MMA8653SampleRange[]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | extern const MMA8653SampleRateConfig MMA8653SampleRate[]; |
Jonathan Austin |
1:8aa5cdb4ab67 | 137 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | struct ShakeHistory |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | uint16_t shaken:1, |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | x:1, |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | y:1, |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | z:1, |
LancasterUniversity | 42:e2869e0fa366 | 144 | unused, |
LancasterUniversity | 38:1a9e8e5e23f2 | 145 | impulse_3, |
LancasterUniversity | 38:1a9e8e5e23f2 | 146 | impulse_6, |
LancasterUniversity | 38:1a9e8e5e23f2 | 147 | impulse_8, |
LancasterUniversity | 65:f7ebabf23e15 | 148 | count:8; |
LancasterUniversity | 65:f7ebabf23e15 | 149 | |
LancasterUniversity | 65:f7ebabf23e15 | 150 | uint16_t timer; |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 152 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 153 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 154 | * Class definition for MicroBit Accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 155 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 156 | * Represents an implementation of the Freescale MMA8653 3 axis accelerometer |
Jonathan Austin |
1:8aa5cdb4ab67 | 157 | * Also includes basic data caching and on demand activation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 159 | class MicroBitAccelerometer : public MicroBitComponent |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | uint16_t address; // I2C address of this accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | uint16_t samplePeriod; // The time between samples, in milliseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | uint8_t sampleRange; // The sample range of the accelerometer in g. |
Jonathan Austin |
1:8aa5cdb4ab67 | 164 | MMA8653Sample sample; // The last sample read. |
Jonathan Austin |
1:8aa5cdb4ab67 | 165 | DigitalIn int1; // Data ready interrupt. |
Jonathan Austin |
1:8aa5cdb4ab67 | 166 | float pitch; // Pitch of the device, in radians. |
Jonathan Austin |
1:8aa5cdb4ab67 | 167 | MicroBitI2C& i2c; // The I2C interface to use. |
Jonathan Austin |
1:8aa5cdb4ab67 | 168 | float roll; // Roll of the device, in radians. |
Jonathan Austin |
1:8aa5cdb4ab67 | 169 | uint8_t sigma; // the number of ticks that the instantaneous gesture has been stable. |
LancasterUniversity | 38:1a9e8e5e23f2 | 170 | uint8_t impulseSigma; // the number of ticks since an impulse event has been generated. |
LancasterUniversity | 39:112df23f039f | 171 | uint16_t lastGesture; // the last, stable gesture recorded. |
LancasterUniversity | 39:112df23f039f | 172 | uint16_t currentGesture; // the instantaneous, unfiltered gesture detected. |
Jonathan Austin |
1:8aa5cdb4ab67 | 173 | ShakeHistory shake; // State information needed to detect shake events. |
Jonathan Austin |
1:8aa5cdb4ab67 | 174 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 175 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 176 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 177 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 178 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 179 | * Create a software abstraction of an accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 180 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 181 | * @param _i2c an instance of MicroBitI2C used to communicate with the onboard accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 182 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 183 | * @param address the default I2C address of the accelerometer. Defaults to: MMA8653_DEFAULT_ADDR. |
Jonathan Austin |
1:8aa5cdb4ab67 | 184 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 185 | * @param id the unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER |
Jonathan Austin |
1:8aa5cdb4ab67 | 186 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 187 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 188 | * MicroBitI2C i2c = MicroBitI2C(I2C_SDA0, I2C_SCL0); |
Jonathan Austin |
1:8aa5cdb4ab67 | 189 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 190 | * MicroBitAccelerometer accelerometer = MicroBitAccelerometer(i2c); |
Jonathan Austin |
1:8aa5cdb4ab67 | 191 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 192 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 193 | MicroBitAccelerometer(MicroBitI2C &_i2c, uint16_t address = MMA8653_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_ACCELEROMETER); |
Jonathan Austin |
1:8aa5cdb4ab67 | 194 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 195 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 196 | * Configures the accelerometer for G range and sample rate defined |
Jonathan Austin |
1:8aa5cdb4ab67 | 197 | * in this object. The nearest values are chosen to those defined |
Jonathan Austin |
1:8aa5cdb4ab67 | 198 | * that are supported by the hardware. The instance variables are then |
Jonathan Austin |
1:8aa5cdb4ab67 | 199 | * updated to reflect reality. |
Jonathan Austin |
1:8aa5cdb4ab67 | 200 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 201 | * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured. |
Jonathan Austin |
1:8aa5cdb4ab67 | 202 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 203 | int configure(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 204 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 205 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 206 | * Reads the acceleration data from the accelerometer, and stores it in our buffer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 207 | * This only happens if the accelerometer indicates that it has new data via int1. |
Jonathan Austin |
1:8aa5cdb4ab67 | 208 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 209 | * On first use, this member function will attempt to add this component to the |
Jonathan Austin |
1:8aa5cdb4ab67 | 210 | * list of fiber components in order to constantly update the values stored |
Jonathan Austin |
1:8aa5cdb4ab67 | 211 | * by this object. |
Jonathan Austin |
1:8aa5cdb4ab67 | 212 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 213 | * This technique is called lazy instantiation, and it means that we do not |
Jonathan Austin |
1:8aa5cdb4ab67 | 214 | * obtain the overhead from non-chalantly adding this component to fiber components. |
Jonathan Austin |
1:8aa5cdb4ab67 | 215 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 216 | * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the read request fails. |
Jonathan Austin |
1:8aa5cdb4ab67 | 217 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 218 | int updateSample(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 219 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 220 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 221 | * Attempts to set the sample rate of the accelerometer to the specified value (in ms). |
Jonathan Austin |
1:8aa5cdb4ab67 | 222 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 223 | * @param period the requested time between samples, in milliseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 224 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 225 | * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails. |
Jonathan Austin |
1:8aa5cdb4ab67 | 226 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 227 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 228 | * // sample rate is now 20 ms. |
Jonathan Austin |
1:8aa5cdb4ab67 | 229 | * accelerometer.setPeriod(20); |
Jonathan Austin |
1:8aa5cdb4ab67 | 230 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 231 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 232 | * @note The requested rate may not be possible on the hardware. In this case, the |
Jonathan Austin |
1:8aa5cdb4ab67 | 233 | * nearest lower rate is chosen. |
Jonathan Austin |
1:8aa5cdb4ab67 | 234 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 235 | int setPeriod(int period); |
Jonathan Austin |
1:8aa5cdb4ab67 | 236 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 237 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 238 | * Reads the currently configured sample rate of the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 239 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 240 | * @return The time between samples, in milliseconds. |
Jonathan Austin |
1:8aa5cdb4ab67 | 241 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 242 | int getPeriod(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 243 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 244 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 245 | * Attempts to set the sample range of the accelerometer to the specified value (in g). |
Jonathan Austin |
1:8aa5cdb4ab67 | 246 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 247 | * @param range The requested sample range of samples, in g. |
Jonathan Austin |
1:8aa5cdb4ab67 | 248 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 249 | * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails. |
Jonathan Austin |
1:8aa5cdb4ab67 | 250 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 251 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 252 | * // the sample range of the accelerometer is now 8G. |
Jonathan Austin |
1:8aa5cdb4ab67 | 253 | * accelerometer.setRange(8); |
Jonathan Austin |
1:8aa5cdb4ab67 | 254 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 255 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 256 | * @note The requested range may not be possible on the hardware. In this case, the |
Jonathan Austin |
1:8aa5cdb4ab67 | 257 | * nearest lower range is chosen. |
Jonathan Austin |
1:8aa5cdb4ab67 | 258 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 259 | int setRange(int range); |
Jonathan Austin |
1:8aa5cdb4ab67 | 260 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 261 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 262 | * Reads the currently configured sample range of the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 263 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 264 | * @return The sample range, in g. |
Jonathan Austin |
1:8aa5cdb4ab67 | 265 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 266 | int getRange(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 267 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 268 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 269 | * Attempts to read the 8 bit ID from the accelerometer, this can be used for |
Jonathan Austin |
1:8aa5cdb4ab67 | 270 | * validation purposes. |
Jonathan Austin |
1:8aa5cdb4ab67 | 271 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 272 | * @return the 8 bit ID returned by the accelerometer, or MICROBIT_I2C_ERROR if the request fails. |
Jonathan Austin |
1:8aa5cdb4ab67 | 273 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 274 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 275 | * accelerometer.whoAmI(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 276 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 277 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 278 | int whoAmI(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 279 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 280 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 281 | * Reads the value of the X axis from the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 282 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 283 | * @param system The coordinate system to use. By default, a simple cartesian system is provided. |
Jonathan Austin |
1:8aa5cdb4ab67 | 284 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 285 | * @return The force measured in the X axis, in milli-g. |
Jonathan Austin |
1:8aa5cdb4ab67 | 286 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 287 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 288 | * accelerometer.getX(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 289 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 290 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 291 | int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); |
Jonathan Austin |
1:8aa5cdb4ab67 | 292 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 293 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 294 | * Reads the value of the Y axis from the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 295 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 296 | * @return The force measured in the Y axis, in milli-g. |
Jonathan Austin |
1:8aa5cdb4ab67 | 297 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 298 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 299 | * accelerometer.getY(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 300 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 301 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 302 | int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); |
Jonathan Austin |
1:8aa5cdb4ab67 | 303 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 304 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 305 | * Reads the value of the Z axis from the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 306 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 307 | * @return The force measured in the Z axis, in milli-g. |
Jonathan Austin |
1:8aa5cdb4ab67 | 308 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 309 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 310 | * accelerometer.getZ(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 311 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 312 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 313 | int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN); |
Jonathan Austin |
1:8aa5cdb4ab67 | 314 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 315 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 316 | * Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 317 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 318 | * @return The pitch of the device, in degrees. |
Jonathan Austin |
1:8aa5cdb4ab67 | 319 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 320 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 321 | * accelerometer.getPitch(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 322 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 323 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 324 | int getPitch(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 325 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 326 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 327 | * Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 328 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 329 | * @return The pitch of the device, in radians. |
Jonathan Austin |
1:8aa5cdb4ab67 | 330 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 331 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 332 | * accelerometer.getPitchRadians(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 333 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 334 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 335 | float getPitchRadians(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 336 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 337 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 338 | * Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 339 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 340 | * @return The roll of the device, in degrees. |
Jonathan Austin |
1:8aa5cdb4ab67 | 341 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 342 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 343 | * accelerometer.getRoll(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 344 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 345 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 346 | int getRoll(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 347 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 348 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 349 | * Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 350 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 351 | * @return The roll of the device, in radians. |
Jonathan Austin |
1:8aa5cdb4ab67 | 352 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 353 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 354 | * accelerometer.getRollRadians(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 355 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 356 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 357 | float getRollRadians(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 358 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 359 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 360 | * Retrieves the last recorded gesture. |
Jonathan Austin |
1:8aa5cdb4ab67 | 361 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 362 | * @return The last gesture that was detected. |
Jonathan Austin |
1:8aa5cdb4ab67 | 363 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 364 | * Example: |
Jonathan Austin |
1:8aa5cdb4ab67 | 365 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 366 | * MicroBitDisplay display; |
Jonathan Austin |
1:8aa5cdb4ab67 | 367 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 368 | * if (accelerometer.getGesture() == SHAKE) |
Jonathan Austin |
1:8aa5cdb4ab67 | 369 | * display.scroll("SHAKE!"); |
Jonathan Austin |
1:8aa5cdb4ab67 | 370 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 371 | */ |
LancasterUniversity | 39:112df23f039f | 372 | uint16_t getGesture(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 373 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 374 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 375 | * A periodic callback invoked by the fiber scheduler idle thread. |
Jonathan Austin |
1:8aa5cdb4ab67 | 376 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 377 | * Internally calls updateSample(). |
Jonathan Austin |
1:8aa5cdb4ab67 | 378 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 379 | virtual void idleTick(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 380 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 381 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 382 | * Destructor for MicroBitButton, where we deregister this instance from the array of fiber components. |
Jonathan Austin |
1:8aa5cdb4ab67 | 383 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 384 | ~MicroBitAccelerometer(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 385 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 386 | private: |
Jonathan Austin |
1:8aa5cdb4ab67 | 387 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 388 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 389 | * Issues a standard, 2 byte I2C command write to the accelerometer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 390 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 391 | * Blocks the calling thread until complete. |
Jonathan Austin |
1:8aa5cdb4ab67 | 392 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 393 | * @param reg The address of the register to write to. |
Jonathan Austin |
1:8aa5cdb4ab67 | 394 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 395 | * @param value The value to write. |
Jonathan Austin |
1:8aa5cdb4ab67 | 396 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 397 | * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 398 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 399 | int writeCommand(uint8_t reg, uint8_t value); |
Jonathan Austin |
1:8aa5cdb4ab67 | 400 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 401 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 402 | * Issues a read command, copying data into the specified buffer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 403 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 404 | * Blocks the calling thread until complete. |
Jonathan Austin |
1:8aa5cdb4ab67 | 405 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 406 | * @param reg The address of the register to access. |
Jonathan Austin |
1:8aa5cdb4ab67 | 407 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 408 | * @param buffer Memory area to read the data into. |
Jonathan Austin |
1:8aa5cdb4ab67 | 409 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 410 | * @param length The number of bytes to read. |
Jonathan Austin |
1:8aa5cdb4ab67 | 411 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 412 | * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 413 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 414 | int readCommand(uint8_t reg, uint8_t* buffer, int length); |
Jonathan Austin |
1:8aa5cdb4ab67 | 415 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 416 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 417 | * Recalculate roll and pitch values for the current sample. |
Jonathan Austin |
1:8aa5cdb4ab67 | 418 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 419 | * @note We only do this at most once per sample, as the necessary trigonemteric functions are rather |
Jonathan Austin |
1:8aa5cdb4ab67 | 420 | * heavyweight for a CPU without a floating point unit. |
Jonathan Austin |
1:8aa5cdb4ab67 | 421 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 422 | void recalculatePitchRoll(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 423 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 424 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 425 | * Updates the basic gesture recognizer. This performs instantaneous pose recognition, and also some low pass filtering to promote |
Jonathan Austin |
1:8aa5cdb4ab67 | 426 | * stability. |
Jonathan Austin |
1:8aa5cdb4ab67 | 427 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 428 | void updateGesture(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 429 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 430 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 431 | * A service function. |
Jonathan Austin |
1:8aa5cdb4ab67 | 432 | * It calculates the current scalar acceleration of the device (x^2 + y^2 + z^2). |
Jonathan Austin |
1:8aa5cdb4ab67 | 433 | * It does not, however, square root the result, as this is a relatively high cost operation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 434 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 435 | * This is left to application code should it be needed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 436 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 437 | * @return the sum of the square of the acceleration of the device across all axes. |
Jonathan Austin |
1:8aa5cdb4ab67 | 438 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 439 | int instantaneousAccelerationSquared(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 440 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 441 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 442 | * Service function. |
Jonathan Austin |
1:8aa5cdb4ab67 | 443 | * Determines a 'best guess' posture of the device based on instantaneous data. |
Jonathan Austin |
1:8aa5cdb4ab67 | 444 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 445 | * This makes no use of historic data, and forms this input to the filter implemented in updateGesture(). |
Jonathan Austin |
1:8aa5cdb4ab67 | 446 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 447 | * @return A 'best guess' of the current posture of the device, based on instanataneous data. |
Jonathan Austin |
1:8aa5cdb4ab67 | 448 | */ |
LancasterUniversity | 39:112df23f039f | 449 | uint16_t instantaneousPosture(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 450 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 451 | |
LancasterUniversity | 38:1a9e8e5e23f2 | 452 | #endif |