from bbc microbit library

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
Jonathan Austin
Date:
Thu Apr 07 01:33:22 2016 +0100
Revision:
1:8aa5cdb4ab67
Child:
38:1a9e8e5e23f2
Synchronized with git rev 55cb9199

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