My fork

Dependencies:   BLE_API mbed-dev-bin nRF51822-bluetooth-mdw

Fork of microbit-dal-bluetooth-mdw by Martin Woolley

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:18:22 2016 +0100
Revision:
42:e2869e0fa366
Parent:
41:da05ec75cd5d
Child:
55:6920de8ba10a
Synchronized with git rev ece079c7
Author: Joe Finney
microbit: FREEFALL gesture event is now damped

After further testing, dmaping is required on FREEFALL event (but not impulse
events) to avoid false positives.

Who changed what in which revision?

UserRevisionLine numberNew 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
Jonathan Austin 1:8aa5cdb4ab67 100 #define MICROBIT_ACCELEROMETER_SHAKE_TOLERANCE 1000
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
Jonathan Austin 1:8aa5cdb4ab67 104 #define MICROBIT_ACCELEROMETER_GESTURE_DAMPING 10
Jonathan Austin 1:8aa5cdb4ab67 105 #define MICROBIT_ACCELEROMETER_SHAKE_DAMPING 10
Jonathan Austin 1:8aa5cdb4ab67 106
Jonathan Austin 1:8aa5cdb4ab67 107 #define MICROBIT_ACCELEROMETER_REST_THRESHOLD (MICROBIT_ACCELEROMETER_REST_TOLERANCE * MICROBIT_ACCELEROMETER_REST_TOLERANCE)
Jonathan Austin 1:8aa5cdb4ab67 108 #define MICROBIT_ACCELEROMETER_FREEFALL_THRESHOLD (MICROBIT_ACCELEROMETER_FREEFALL_TOLERANCE * MICROBIT_ACCELEROMETER_FREEFALL_TOLERANCE)
Jonathan Austin 1:8aa5cdb4ab67 109 #define MICROBIT_ACCELEROMETER_3G_THRESHOLD (MICROBIT_ACCELEROMETER_3G_TOLERANCE * MICROBIT_ACCELEROMETER_3G_TOLERANCE)
Jonathan Austin 1:8aa5cdb4ab67 110 #define MICROBIT_ACCELEROMETER_6G_THRESHOLD (MICROBIT_ACCELEROMETER_6G_TOLERANCE * MICROBIT_ACCELEROMETER_6G_TOLERANCE)
Jonathan Austin 1:8aa5cdb4ab67 111 #define MICROBIT_ACCELEROMETER_8G_THRESHOLD (MICROBIT_ACCELEROMETER_8G_TOLERANCE * MICROBIT_ACCELEROMETER_8G_TOLERANCE)
Jonathan Austin 1:8aa5cdb4ab67 112 #define MICROBIT_ACCELEROMETER_SHAKE_COUNT_THRESHOLD 4
Jonathan Austin 1:8aa5cdb4ab67 113
Jonathan Austin 1:8aa5cdb4ab67 114 struct MMA8653Sample
Jonathan Austin 1:8aa5cdb4ab67 115 {
Jonathan Austin 1:8aa5cdb4ab67 116 int16_t x;
Jonathan Austin 1:8aa5cdb4ab67 117 int16_t y;
Jonathan Austin 1:8aa5cdb4ab67 118 int16_t z;
Jonathan Austin 1:8aa5cdb4ab67 119 };
Jonathan Austin 1:8aa5cdb4ab67 120
Jonathan Austin 1:8aa5cdb4ab67 121 struct MMA8653SampleRateConfig
Jonathan Austin 1:8aa5cdb4ab67 122 {
Jonathan Austin 1:8aa5cdb4ab67 123 uint32_t sample_period;
Jonathan Austin 1:8aa5cdb4ab67 124 uint8_t ctrl_reg1;
Jonathan Austin 1:8aa5cdb4ab67 125 };
Jonathan Austin 1:8aa5cdb4ab67 126
Jonathan Austin 1:8aa5cdb4ab67 127 struct MMA8653SampleRangeConfig
Jonathan Austin 1:8aa5cdb4ab67 128 {
Jonathan Austin 1:8aa5cdb4ab67 129 uint8_t sample_range;
Jonathan Austin 1:8aa5cdb4ab67 130 uint8_t xyz_data_cfg;
Jonathan Austin 1:8aa5cdb4ab67 131 };
Jonathan Austin 1:8aa5cdb4ab67 132
Jonathan Austin 1:8aa5cdb4ab67 133
Jonathan Austin 1:8aa5cdb4ab67 134 extern const MMA8653SampleRangeConfig MMA8653SampleRange[];
Jonathan Austin 1:8aa5cdb4ab67 135 extern const MMA8653SampleRateConfig MMA8653SampleRate[];
Jonathan Austin 1:8aa5cdb4ab67 136
Jonathan Austin 1:8aa5cdb4ab67 137 struct ShakeHistory
Jonathan Austin 1:8aa5cdb4ab67 138 {
Jonathan Austin 1:8aa5cdb4ab67 139 uint16_t shaken:1,
Jonathan Austin 1:8aa5cdb4ab67 140 x:1,
Jonathan Austin 1:8aa5cdb4ab67 141 y:1,
Jonathan Austin 1:8aa5cdb4ab67 142 z:1,
LancasterUniversity 42:e2869e0fa366 143 unused,
LancasterUniversity 38:1a9e8e5e23f2 144 impulse_3,
LancasterUniversity 38:1a9e8e5e23f2 145 impulse_6,
LancasterUniversity 38:1a9e8e5e23f2 146 impulse_8,
Jonathan Austin 1:8aa5cdb4ab67 147 count:4,
LancasterUniversity 38:1a9e8e5e23f2 148 timer:4;
Jonathan Austin 1:8aa5cdb4ab67 149 };
Jonathan Austin 1:8aa5cdb4ab67 150
Jonathan Austin 1:8aa5cdb4ab67 151 /**
Jonathan Austin 1:8aa5cdb4ab67 152 * Class definition for MicroBit Accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 153 *
Jonathan Austin 1:8aa5cdb4ab67 154 * Represents an implementation of the Freescale MMA8653 3 axis accelerometer
Jonathan Austin 1:8aa5cdb4ab67 155 * Also includes basic data caching and on demand activation.
Jonathan Austin 1:8aa5cdb4ab67 156 */
Jonathan Austin 1:8aa5cdb4ab67 157 class MicroBitAccelerometer : public MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 158 {
Jonathan Austin 1:8aa5cdb4ab67 159 uint16_t address; // I2C address of this accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 160 uint16_t samplePeriod; // The time between samples, in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 161 uint8_t sampleRange; // The sample range of the accelerometer in g.
Jonathan Austin 1:8aa5cdb4ab67 162 MMA8653Sample sample; // The last sample read.
Jonathan Austin 1:8aa5cdb4ab67 163 DigitalIn int1; // Data ready interrupt.
Jonathan Austin 1:8aa5cdb4ab67 164 float pitch; // Pitch of the device, in radians.
Jonathan Austin 1:8aa5cdb4ab67 165 MicroBitI2C& i2c; // The I2C interface to use.
Jonathan Austin 1:8aa5cdb4ab67 166 float roll; // Roll of the device, in radians.
Jonathan Austin 1:8aa5cdb4ab67 167 uint8_t sigma; // the number of ticks that the instantaneous gesture has been stable.
LancasterUniversity 38:1a9e8e5e23f2 168 uint8_t impulseSigma; // the number of ticks since an impulse event has been generated.
LancasterUniversity 39:112df23f039f 169 uint16_t lastGesture; // the last, stable gesture recorded.
LancasterUniversity 39:112df23f039f 170 uint16_t currentGesture; // the instantaneous, unfiltered gesture detected.
Jonathan Austin 1:8aa5cdb4ab67 171 ShakeHistory shake; // State information needed to detect shake events.
Jonathan Austin 1:8aa5cdb4ab67 172
Jonathan Austin 1:8aa5cdb4ab67 173 public:
Jonathan Austin 1:8aa5cdb4ab67 174
Jonathan Austin 1:8aa5cdb4ab67 175 /**
Jonathan Austin 1:8aa5cdb4ab67 176 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 177 * Create a software abstraction of an accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 178 *
Jonathan Austin 1:8aa5cdb4ab67 179 * @param _i2c an instance of MicroBitI2C used to communicate with the onboard accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 180 *
Jonathan Austin 1:8aa5cdb4ab67 181 * @param address the default I2C address of the accelerometer. Defaults to: MMA8653_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 182 *
Jonathan Austin 1:8aa5cdb4ab67 183 * @param id the unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER
Jonathan Austin 1:8aa5cdb4ab67 184 *
Jonathan Austin 1:8aa5cdb4ab67 185 * @code
Jonathan Austin 1:8aa5cdb4ab67 186 * MicroBitI2C i2c = MicroBitI2C(I2C_SDA0, I2C_SCL0);
Jonathan Austin 1:8aa5cdb4ab67 187 *
Jonathan Austin 1:8aa5cdb4ab67 188 * MicroBitAccelerometer accelerometer = MicroBitAccelerometer(i2c);
Jonathan Austin 1:8aa5cdb4ab67 189 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 190 */
Jonathan Austin 1:8aa5cdb4ab67 191 MicroBitAccelerometer(MicroBitI2C &_i2c, uint16_t address = MMA8653_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_ACCELEROMETER);
Jonathan Austin 1:8aa5cdb4ab67 192
Jonathan Austin 1:8aa5cdb4ab67 193 /**
Jonathan Austin 1:8aa5cdb4ab67 194 * Configures the accelerometer for G range and sample rate defined
Jonathan Austin 1:8aa5cdb4ab67 195 * in this object. The nearest values are chosen to those defined
Jonathan Austin 1:8aa5cdb4ab67 196 * that are supported by the hardware. The instance variables are then
Jonathan Austin 1:8aa5cdb4ab67 197 * updated to reflect reality.
Jonathan Austin 1:8aa5cdb4ab67 198 *
Jonathan Austin 1:8aa5cdb4ab67 199 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured.
Jonathan Austin 1:8aa5cdb4ab67 200 */
Jonathan Austin 1:8aa5cdb4ab67 201 int configure();
Jonathan Austin 1:8aa5cdb4ab67 202
Jonathan Austin 1:8aa5cdb4ab67 203 /**
Jonathan Austin 1:8aa5cdb4ab67 204 * Reads the acceleration data from the accelerometer, and stores it in our buffer.
Jonathan Austin 1:8aa5cdb4ab67 205 * This only happens if the accelerometer indicates that it has new data via int1.
Jonathan Austin 1:8aa5cdb4ab67 206 *
Jonathan Austin 1:8aa5cdb4ab67 207 * On first use, this member function will attempt to add this component to the
Jonathan Austin 1:8aa5cdb4ab67 208 * list of fiber components in order to constantly update the values stored
Jonathan Austin 1:8aa5cdb4ab67 209 * by this object.
Jonathan Austin 1:8aa5cdb4ab67 210 *
Jonathan Austin 1:8aa5cdb4ab67 211 * This technique is called lazy instantiation, and it means that we do not
Jonathan Austin 1:8aa5cdb4ab67 212 * obtain the overhead from non-chalantly adding this component to fiber components.
Jonathan Austin 1:8aa5cdb4ab67 213 *
Jonathan Austin 1:8aa5cdb4ab67 214 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the read request fails.
Jonathan Austin 1:8aa5cdb4ab67 215 */
Jonathan Austin 1:8aa5cdb4ab67 216 int updateSample();
Jonathan Austin 1:8aa5cdb4ab67 217
Jonathan Austin 1:8aa5cdb4ab67 218 /**
Jonathan Austin 1:8aa5cdb4ab67 219 * Attempts to set the sample rate of the accelerometer to the specified value (in ms).
Jonathan Austin 1:8aa5cdb4ab67 220 *
Jonathan Austin 1:8aa5cdb4ab67 221 * @param period the requested time between samples, in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 222 *
Jonathan Austin 1:8aa5cdb4ab67 223 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
Jonathan Austin 1:8aa5cdb4ab67 224 *
Jonathan Austin 1:8aa5cdb4ab67 225 * @code
Jonathan Austin 1:8aa5cdb4ab67 226 * // sample rate is now 20 ms.
Jonathan Austin 1:8aa5cdb4ab67 227 * accelerometer.setPeriod(20);
Jonathan Austin 1:8aa5cdb4ab67 228 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 229 *
Jonathan Austin 1:8aa5cdb4ab67 230 * @note The requested rate may not be possible on the hardware. In this case, the
Jonathan Austin 1:8aa5cdb4ab67 231 * nearest lower rate is chosen.
Jonathan Austin 1:8aa5cdb4ab67 232 */
Jonathan Austin 1:8aa5cdb4ab67 233 int setPeriod(int period);
Jonathan Austin 1:8aa5cdb4ab67 234
Jonathan Austin 1:8aa5cdb4ab67 235 /**
Jonathan Austin 1:8aa5cdb4ab67 236 * Reads the currently configured sample rate of the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 237 *
Jonathan Austin 1:8aa5cdb4ab67 238 * @return The time between samples, in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 239 */
Jonathan Austin 1:8aa5cdb4ab67 240 int getPeriod();
Jonathan Austin 1:8aa5cdb4ab67 241
Jonathan Austin 1:8aa5cdb4ab67 242 /**
Jonathan Austin 1:8aa5cdb4ab67 243 * Attempts to set the sample range of the accelerometer to the specified value (in g).
Jonathan Austin 1:8aa5cdb4ab67 244 *
Jonathan Austin 1:8aa5cdb4ab67 245 * @param range The requested sample range of samples, in g.
Jonathan Austin 1:8aa5cdb4ab67 246 *
Jonathan Austin 1:8aa5cdb4ab67 247 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
Jonathan Austin 1:8aa5cdb4ab67 248 *
Jonathan Austin 1:8aa5cdb4ab67 249 * @code
Jonathan Austin 1:8aa5cdb4ab67 250 * // the sample range of the accelerometer is now 8G.
Jonathan Austin 1:8aa5cdb4ab67 251 * accelerometer.setRange(8);
Jonathan Austin 1:8aa5cdb4ab67 252 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 253 *
Jonathan Austin 1:8aa5cdb4ab67 254 * @note The requested range may not be possible on the hardware. In this case, the
Jonathan Austin 1:8aa5cdb4ab67 255 * nearest lower range is chosen.
Jonathan Austin 1:8aa5cdb4ab67 256 */
Jonathan Austin 1:8aa5cdb4ab67 257 int setRange(int range);
Jonathan Austin 1:8aa5cdb4ab67 258
Jonathan Austin 1:8aa5cdb4ab67 259 /**
Jonathan Austin 1:8aa5cdb4ab67 260 * Reads the currently configured sample range of the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 261 *
Jonathan Austin 1:8aa5cdb4ab67 262 * @return The sample range, in g.
Jonathan Austin 1:8aa5cdb4ab67 263 */
Jonathan Austin 1:8aa5cdb4ab67 264 int getRange();
Jonathan Austin 1:8aa5cdb4ab67 265
Jonathan Austin 1:8aa5cdb4ab67 266 /**
Jonathan Austin 1:8aa5cdb4ab67 267 * Attempts to read the 8 bit ID from the accelerometer, this can be used for
Jonathan Austin 1:8aa5cdb4ab67 268 * validation purposes.
Jonathan Austin 1:8aa5cdb4ab67 269 *
Jonathan Austin 1:8aa5cdb4ab67 270 * @return the 8 bit ID returned by the accelerometer, or MICROBIT_I2C_ERROR if the request fails.
Jonathan Austin 1:8aa5cdb4ab67 271 *
Jonathan Austin 1:8aa5cdb4ab67 272 * @code
Jonathan Austin 1:8aa5cdb4ab67 273 * accelerometer.whoAmI();
Jonathan Austin 1:8aa5cdb4ab67 274 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 275 */
Jonathan Austin 1:8aa5cdb4ab67 276 int whoAmI();
Jonathan Austin 1:8aa5cdb4ab67 277
Jonathan Austin 1:8aa5cdb4ab67 278 /**
Jonathan Austin 1:8aa5cdb4ab67 279 * Reads the value of the X axis from the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 280 *
Jonathan Austin 1:8aa5cdb4ab67 281 * @param system The coordinate system to use. By default, a simple cartesian system is provided.
Jonathan Austin 1:8aa5cdb4ab67 282 *
Jonathan Austin 1:8aa5cdb4ab67 283 * @return The force measured in the X axis, in milli-g.
Jonathan Austin 1:8aa5cdb4ab67 284 *
Jonathan Austin 1:8aa5cdb4ab67 285 * @code
Jonathan Austin 1:8aa5cdb4ab67 286 * accelerometer.getX();
Jonathan Austin 1:8aa5cdb4ab67 287 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 288 */
Jonathan Austin 1:8aa5cdb4ab67 289 int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
Jonathan Austin 1:8aa5cdb4ab67 290
Jonathan Austin 1:8aa5cdb4ab67 291 /**
Jonathan Austin 1:8aa5cdb4ab67 292 * Reads the value of the Y axis from the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 293 *
Jonathan Austin 1:8aa5cdb4ab67 294 * @return The force measured in the Y axis, in milli-g.
Jonathan Austin 1:8aa5cdb4ab67 295 *
Jonathan Austin 1:8aa5cdb4ab67 296 * @code
Jonathan Austin 1:8aa5cdb4ab67 297 * accelerometer.getY();
Jonathan Austin 1:8aa5cdb4ab67 298 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 299 */
Jonathan Austin 1:8aa5cdb4ab67 300 int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
Jonathan Austin 1:8aa5cdb4ab67 301
Jonathan Austin 1:8aa5cdb4ab67 302 /**
Jonathan Austin 1:8aa5cdb4ab67 303 * Reads the value of the Z axis from the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 304 *
Jonathan Austin 1:8aa5cdb4ab67 305 * @return The force measured in the Z axis, in milli-g.
Jonathan Austin 1:8aa5cdb4ab67 306 *
Jonathan Austin 1:8aa5cdb4ab67 307 * @code
Jonathan Austin 1:8aa5cdb4ab67 308 * accelerometer.getZ();
Jonathan Austin 1:8aa5cdb4ab67 309 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 310 */
Jonathan Austin 1:8aa5cdb4ab67 311 int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
Jonathan Austin 1:8aa5cdb4ab67 312
Jonathan Austin 1:8aa5cdb4ab67 313 /**
Jonathan Austin 1:8aa5cdb4ab67 314 * Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 315 *
Jonathan Austin 1:8aa5cdb4ab67 316 * @return The pitch of the device, in degrees.
Jonathan Austin 1:8aa5cdb4ab67 317 *
Jonathan Austin 1:8aa5cdb4ab67 318 * @code
Jonathan Austin 1:8aa5cdb4ab67 319 * accelerometer.getPitch();
Jonathan Austin 1:8aa5cdb4ab67 320 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 321 */
Jonathan Austin 1:8aa5cdb4ab67 322 int getPitch();
Jonathan Austin 1:8aa5cdb4ab67 323
Jonathan Austin 1:8aa5cdb4ab67 324 /**
Jonathan Austin 1:8aa5cdb4ab67 325 * Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 326 *
Jonathan Austin 1:8aa5cdb4ab67 327 * @return The pitch of the device, in radians.
Jonathan Austin 1:8aa5cdb4ab67 328 *
Jonathan Austin 1:8aa5cdb4ab67 329 * @code
Jonathan Austin 1:8aa5cdb4ab67 330 * accelerometer.getPitchRadians();
Jonathan Austin 1:8aa5cdb4ab67 331 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 332 */
Jonathan Austin 1:8aa5cdb4ab67 333 float getPitchRadians();
Jonathan Austin 1:8aa5cdb4ab67 334
Jonathan Austin 1:8aa5cdb4ab67 335 /**
Jonathan Austin 1:8aa5cdb4ab67 336 * Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 337 *
Jonathan Austin 1:8aa5cdb4ab67 338 * @return The roll of the device, in degrees.
Jonathan Austin 1:8aa5cdb4ab67 339 *
Jonathan Austin 1:8aa5cdb4ab67 340 * @code
Jonathan Austin 1:8aa5cdb4ab67 341 * accelerometer.getRoll();
Jonathan Austin 1:8aa5cdb4ab67 342 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 343 */
Jonathan Austin 1:8aa5cdb4ab67 344 int getRoll();
Jonathan Austin 1:8aa5cdb4ab67 345
Jonathan Austin 1:8aa5cdb4ab67 346 /**
Jonathan Austin 1:8aa5cdb4ab67 347 * Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 348 *
Jonathan Austin 1:8aa5cdb4ab67 349 * @return The roll of the device, in radians.
Jonathan Austin 1:8aa5cdb4ab67 350 *
Jonathan Austin 1:8aa5cdb4ab67 351 * @code
Jonathan Austin 1:8aa5cdb4ab67 352 * accelerometer.getRollRadians();
Jonathan Austin 1:8aa5cdb4ab67 353 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 354 */
Jonathan Austin 1:8aa5cdb4ab67 355 float getRollRadians();
Jonathan Austin 1:8aa5cdb4ab67 356
Jonathan Austin 1:8aa5cdb4ab67 357 /**
Jonathan Austin 1:8aa5cdb4ab67 358 * Retrieves the last recorded gesture.
Jonathan Austin 1:8aa5cdb4ab67 359 *
Jonathan Austin 1:8aa5cdb4ab67 360 * @return The last gesture that was detected.
Jonathan Austin 1:8aa5cdb4ab67 361 *
Jonathan Austin 1:8aa5cdb4ab67 362 * Example:
Jonathan Austin 1:8aa5cdb4ab67 363 * @code
Jonathan Austin 1:8aa5cdb4ab67 364 * MicroBitDisplay display;
Jonathan Austin 1:8aa5cdb4ab67 365 *
Jonathan Austin 1:8aa5cdb4ab67 366 * if (accelerometer.getGesture() == SHAKE)
Jonathan Austin 1:8aa5cdb4ab67 367 * display.scroll("SHAKE!");
Jonathan Austin 1:8aa5cdb4ab67 368 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 369 */
LancasterUniversity 39:112df23f039f 370 uint16_t getGesture();
Jonathan Austin 1:8aa5cdb4ab67 371
Jonathan Austin 1:8aa5cdb4ab67 372 /**
Jonathan Austin 1:8aa5cdb4ab67 373 * A periodic callback invoked by the fiber scheduler idle thread.
Jonathan Austin 1:8aa5cdb4ab67 374 *
Jonathan Austin 1:8aa5cdb4ab67 375 * Internally calls updateSample().
Jonathan Austin 1:8aa5cdb4ab67 376 */
Jonathan Austin 1:8aa5cdb4ab67 377 virtual void idleTick();
Jonathan Austin 1:8aa5cdb4ab67 378
Jonathan Austin 1:8aa5cdb4ab67 379 /**
Jonathan Austin 1:8aa5cdb4ab67 380 * Destructor for MicroBitButton, where we deregister this instance from the array of fiber components.
Jonathan Austin 1:8aa5cdb4ab67 381 */
Jonathan Austin 1:8aa5cdb4ab67 382 ~MicroBitAccelerometer();
Jonathan Austin 1:8aa5cdb4ab67 383
Jonathan Austin 1:8aa5cdb4ab67 384 private:
Jonathan Austin 1:8aa5cdb4ab67 385
Jonathan Austin 1:8aa5cdb4ab67 386 /**
Jonathan Austin 1:8aa5cdb4ab67 387 * Issues a standard, 2 byte I2C command write to the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 388 *
Jonathan Austin 1:8aa5cdb4ab67 389 * Blocks the calling thread until complete.
Jonathan Austin 1:8aa5cdb4ab67 390 *
Jonathan Austin 1:8aa5cdb4ab67 391 * @param reg The address of the register to write to.
Jonathan Austin 1:8aa5cdb4ab67 392 *
Jonathan Austin 1:8aa5cdb4ab67 393 * @param value The value to write.
Jonathan Austin 1:8aa5cdb4ab67 394 *
Jonathan Austin 1:8aa5cdb4ab67 395 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed.
Jonathan Austin 1:8aa5cdb4ab67 396 */
Jonathan Austin 1:8aa5cdb4ab67 397 int writeCommand(uint8_t reg, uint8_t value);
Jonathan Austin 1:8aa5cdb4ab67 398
Jonathan Austin 1:8aa5cdb4ab67 399 /**
Jonathan Austin 1:8aa5cdb4ab67 400 * Issues a read command, copying data into the specified buffer.
Jonathan Austin 1:8aa5cdb4ab67 401 *
Jonathan Austin 1:8aa5cdb4ab67 402 * Blocks the calling thread until complete.
Jonathan Austin 1:8aa5cdb4ab67 403 *
Jonathan Austin 1:8aa5cdb4ab67 404 * @param reg The address of the register to access.
Jonathan Austin 1:8aa5cdb4ab67 405 *
Jonathan Austin 1:8aa5cdb4ab67 406 * @param buffer Memory area to read the data into.
Jonathan Austin 1:8aa5cdb4ab67 407 *
Jonathan Austin 1:8aa5cdb4ab67 408 * @param length The number of bytes to read.
Jonathan Austin 1:8aa5cdb4ab67 409 *
Jonathan Austin 1:8aa5cdb4ab67 410 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed.
Jonathan Austin 1:8aa5cdb4ab67 411 */
Jonathan Austin 1:8aa5cdb4ab67 412 int readCommand(uint8_t reg, uint8_t* buffer, int length);
Jonathan Austin 1:8aa5cdb4ab67 413
Jonathan Austin 1:8aa5cdb4ab67 414 /**
Jonathan Austin 1:8aa5cdb4ab67 415 * Recalculate roll and pitch values for the current sample.
Jonathan Austin 1:8aa5cdb4ab67 416 *
Jonathan Austin 1:8aa5cdb4ab67 417 * @note We only do this at most once per sample, as the necessary trigonemteric functions are rather
Jonathan Austin 1:8aa5cdb4ab67 418 * heavyweight for a CPU without a floating point unit.
Jonathan Austin 1:8aa5cdb4ab67 419 */
Jonathan Austin 1:8aa5cdb4ab67 420 void recalculatePitchRoll();
Jonathan Austin 1:8aa5cdb4ab67 421
Jonathan Austin 1:8aa5cdb4ab67 422 /**
Jonathan Austin 1:8aa5cdb4ab67 423 * Updates the basic gesture recognizer. This performs instantaneous pose recognition, and also some low pass filtering to promote
Jonathan Austin 1:8aa5cdb4ab67 424 * stability.
Jonathan Austin 1:8aa5cdb4ab67 425 */
Jonathan Austin 1:8aa5cdb4ab67 426 void updateGesture();
Jonathan Austin 1:8aa5cdb4ab67 427
Jonathan Austin 1:8aa5cdb4ab67 428 /**
Jonathan Austin 1:8aa5cdb4ab67 429 * A service function.
Jonathan Austin 1:8aa5cdb4ab67 430 * It calculates the current scalar acceleration of the device (x^2 + y^2 + z^2).
Jonathan Austin 1:8aa5cdb4ab67 431 * It does not, however, square root the result, as this is a relatively high cost operation.
Jonathan Austin 1:8aa5cdb4ab67 432 *
Jonathan Austin 1:8aa5cdb4ab67 433 * This is left to application code should it be needed.
Jonathan Austin 1:8aa5cdb4ab67 434 *
Jonathan Austin 1:8aa5cdb4ab67 435 * @return the sum of the square of the acceleration of the device across all axes.
Jonathan Austin 1:8aa5cdb4ab67 436 */
Jonathan Austin 1:8aa5cdb4ab67 437 int instantaneousAccelerationSquared();
Jonathan Austin 1:8aa5cdb4ab67 438
Jonathan Austin 1:8aa5cdb4ab67 439 /**
Jonathan Austin 1:8aa5cdb4ab67 440 * Service function.
Jonathan Austin 1:8aa5cdb4ab67 441 * Determines a 'best guess' posture of the device based on instantaneous data.
Jonathan Austin 1:8aa5cdb4ab67 442 *
Jonathan Austin 1:8aa5cdb4ab67 443 * This makes no use of historic data, and forms this input to the filter implemented in updateGesture().
Jonathan Austin 1:8aa5cdb4ab67 444 *
Jonathan Austin 1:8aa5cdb4ab67 445 * @return A 'best guess' of the current posture of the device, based on instanataneous data.
Jonathan Austin 1:8aa5cdb4ab67 446 */
LancasterUniversity 39:112df23f039f 447 uint16_t instantaneousPosture();
Jonathan Austin 1:8aa5cdb4ab67 448 };
Jonathan Austin 1:8aa5cdb4ab67 449
LancasterUniversity 38:1a9e8e5e23f2 450 #endif