No Changes

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:18:21 2016 +0100
Revision:
41:da05ec75cd5d
Parent:
1:8aa5cdb4ab67
Synchronized with git rev e0369cab
Author: Joe Finney
microbit: Removed legacy isIdleCallbackNeeded() method. [issue #138]

This function has been deprecated, so now removed for v2.0.0 release.

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_COMPASS_H
Jonathan Austin 1:8aa5cdb4ab67 27 #define MICROBIT_COMPASS_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 "MicroBitAccelerometer.h"
Jonathan Austin 1:8aa5cdb4ab67 34 #include "MicroBitStorage.h"
Jonathan Austin 1:8aa5cdb4ab67 35
Jonathan Austin 1:8aa5cdb4ab67 36 /**
Jonathan Austin 1:8aa5cdb4ab67 37 * Relevant pin assignments
Jonathan Austin 1:8aa5cdb4ab67 38 */
Jonathan Austin 1:8aa5cdb4ab67 39 #define MICROBIT_PIN_COMPASS_DATA_READY P0_29
Jonathan Austin 1:8aa5cdb4ab67 40
Jonathan Austin 1:8aa5cdb4ab67 41 /**
Jonathan Austin 1:8aa5cdb4ab67 42 * I2C constants
Jonathan Austin 1:8aa5cdb4ab67 43 */
Jonathan Austin 1:8aa5cdb4ab67 44 #define MAG3110_DEFAULT_ADDR 0x1D
Jonathan Austin 1:8aa5cdb4ab67 45
Jonathan Austin 1:8aa5cdb4ab67 46 /**
Jonathan Austin 1:8aa5cdb4ab67 47 * MAG3110 Register map
Jonathan Austin 1:8aa5cdb4ab67 48 */
Jonathan Austin 1:8aa5cdb4ab67 49 #define MAG_DR_STATUS 0x00
Jonathan Austin 1:8aa5cdb4ab67 50 #define MAG_OUT_X_MSB 0x01
Jonathan Austin 1:8aa5cdb4ab67 51 #define MAG_OUT_X_LSB 0x02
Jonathan Austin 1:8aa5cdb4ab67 52 #define MAG_OUT_Y_MSB 0x03
Jonathan Austin 1:8aa5cdb4ab67 53 #define MAG_OUT_Y_LSB 0x04
Jonathan Austin 1:8aa5cdb4ab67 54 #define MAG_OUT_Z_MSB 0x05
Jonathan Austin 1:8aa5cdb4ab67 55 #define MAG_OUT_Z_LSB 0x06
Jonathan Austin 1:8aa5cdb4ab67 56 #define MAG_WHOAMI 0x07
Jonathan Austin 1:8aa5cdb4ab67 57 #define MAG_SYSMOD 0x08
Jonathan Austin 1:8aa5cdb4ab67 58 #define MAG_OFF_X_MSB 0x09
Jonathan Austin 1:8aa5cdb4ab67 59 #define MAG_OFF_X_LSB 0x0A
Jonathan Austin 1:8aa5cdb4ab67 60 #define MAG_OFF_Y_MSB 0x0B
Jonathan Austin 1:8aa5cdb4ab67 61 #define MAG_OFF_Y_LSB 0x0C
Jonathan Austin 1:8aa5cdb4ab67 62 #define MAG_OFF_Z_MSB 0x0D
Jonathan Austin 1:8aa5cdb4ab67 63 #define MAG_OFF_Z_LSB 0x0E
Jonathan Austin 1:8aa5cdb4ab67 64 #define MAG_DIE_TEMP 0x0F
Jonathan Austin 1:8aa5cdb4ab67 65 #define MAG_CTRL_REG1 0x10
Jonathan Austin 1:8aa5cdb4ab67 66 #define MAG_CTRL_REG2 0x11
Jonathan Austin 1:8aa5cdb4ab67 67
Jonathan Austin 1:8aa5cdb4ab67 68 /**
Jonathan Austin 1:8aa5cdb4ab67 69 * Configuration options
Jonathan Austin 1:8aa5cdb4ab67 70 */
Jonathan Austin 1:8aa5cdb4ab67 71 struct MAG3110SampleRateConfig
Jonathan Austin 1:8aa5cdb4ab67 72 {
Jonathan Austin 1:8aa5cdb4ab67 73 uint32_t sample_period;
Jonathan Austin 1:8aa5cdb4ab67 74 uint8_t ctrl_reg1;
Jonathan Austin 1:8aa5cdb4ab67 75 };
Jonathan Austin 1:8aa5cdb4ab67 76
Jonathan Austin 1:8aa5cdb4ab67 77 extern const MAG3110SampleRateConfig MAG3110SampleRate[];
Jonathan Austin 1:8aa5cdb4ab67 78
Jonathan Austin 1:8aa5cdb4ab67 79 #define MAG3110_SAMPLE_RATES 11
Jonathan Austin 1:8aa5cdb4ab67 80
Jonathan Austin 1:8aa5cdb4ab67 81 /**
Jonathan Austin 1:8aa5cdb4ab67 82 * Compass events
Jonathan Austin 1:8aa5cdb4ab67 83 */
Jonathan Austin 1:8aa5cdb4ab67 84 #define MICROBIT_COMPASS_EVT_CAL_REQUIRED 1 // DEPRECATED
Jonathan Austin 1:8aa5cdb4ab67 85 #define MICROBIT_COMPASS_EVT_CAL_START 2 // DEPRECATED
Jonathan Austin 1:8aa5cdb4ab67 86 #define MICROBIT_COMPASS_EVT_CAL_END 3 // DEPRECATED
Jonathan Austin 1:8aa5cdb4ab67 87
Jonathan Austin 1:8aa5cdb4ab67 88 #define MICROBIT_COMPASS_EVT_DATA_UPDATE 4
Jonathan Austin 1:8aa5cdb4ab67 89 #define MICROBIT_COMPASS_EVT_CONFIG_NEEDED 5
Jonathan Austin 1:8aa5cdb4ab67 90 #define MICROBIT_COMPASS_EVT_CALIBRATE 6
Jonathan Austin 1:8aa5cdb4ab67 91
Jonathan Austin 1:8aa5cdb4ab67 92 /**
Jonathan Austin 1:8aa5cdb4ab67 93 * Status Bits
Jonathan Austin 1:8aa5cdb4ab67 94 */
Jonathan Austin 1:8aa5cdb4ab67 95 #define MICROBIT_COMPASS_STATUS_CALIBRATED 2
Jonathan Austin 1:8aa5cdb4ab67 96 #define MICROBIT_COMPASS_STATUS_CALIBRATING 4
Jonathan Austin 1:8aa5cdb4ab67 97 #define MICROBIT_COMPASS_STATUS_ADDED_TO_IDLE 8
Jonathan Austin 1:8aa5cdb4ab67 98
Jonathan Austin 1:8aa5cdb4ab67 99 /**
Jonathan Austin 1:8aa5cdb4ab67 100 * Term to convert sample data into SI units
Jonathan Austin 1:8aa5cdb4ab67 101 */
Jonathan Austin 1:8aa5cdb4ab67 102 #define MAG3110_NORMALIZE_SAMPLE(x) (100*x)
Jonathan Austin 1:8aa5cdb4ab67 103
Jonathan Austin 1:8aa5cdb4ab67 104 /**
Jonathan Austin 1:8aa5cdb4ab67 105 * MAG3110 MAGIC ID value
Jonathan Austin 1:8aa5cdb4ab67 106 * Returned from the MAG_WHO_AM_I register for ID purposes.
Jonathan Austin 1:8aa5cdb4ab67 107 */
Jonathan Austin 1:8aa5cdb4ab67 108 #define MAG3110_WHOAMI_VAL 0xC4
Jonathan Austin 1:8aa5cdb4ab67 109
Jonathan Austin 1:8aa5cdb4ab67 110 struct CompassSample
Jonathan Austin 1:8aa5cdb4ab67 111 {
Jonathan Austin 1:8aa5cdb4ab67 112 int x;
Jonathan Austin 1:8aa5cdb4ab67 113 int y;
Jonathan Austin 1:8aa5cdb4ab67 114 int z;
Jonathan Austin 1:8aa5cdb4ab67 115
Jonathan Austin 1:8aa5cdb4ab67 116 CompassSample()
Jonathan Austin 1:8aa5cdb4ab67 117 {
Jonathan Austin 1:8aa5cdb4ab67 118 this->x = 0;
Jonathan Austin 1:8aa5cdb4ab67 119 this->y = 0;
Jonathan Austin 1:8aa5cdb4ab67 120 this->z = 0;
Jonathan Austin 1:8aa5cdb4ab67 121 }
Jonathan Austin 1:8aa5cdb4ab67 122
Jonathan Austin 1:8aa5cdb4ab67 123 CompassSample(int x, int y, int z)
Jonathan Austin 1:8aa5cdb4ab67 124 {
Jonathan Austin 1:8aa5cdb4ab67 125 this->x = x;
Jonathan Austin 1:8aa5cdb4ab67 126 this->y = y;
Jonathan Austin 1:8aa5cdb4ab67 127 this->z = z;
Jonathan Austin 1:8aa5cdb4ab67 128 }
Jonathan Austin 1:8aa5cdb4ab67 129
Jonathan Austin 1:8aa5cdb4ab67 130 bool operator==(const CompassSample& other) const
Jonathan Austin 1:8aa5cdb4ab67 131 {
Jonathan Austin 1:8aa5cdb4ab67 132 return x == other.x && y == other.y && z == other.z;
Jonathan Austin 1:8aa5cdb4ab67 133 }
Jonathan Austin 1:8aa5cdb4ab67 134
Jonathan Austin 1:8aa5cdb4ab67 135 bool operator!=(const CompassSample& other) const
Jonathan Austin 1:8aa5cdb4ab67 136 {
Jonathan Austin 1:8aa5cdb4ab67 137 return !(x == other.x && y == other.y && z == other.z);
Jonathan Austin 1:8aa5cdb4ab67 138 }
Jonathan Austin 1:8aa5cdb4ab67 139 };
Jonathan Austin 1:8aa5cdb4ab67 140
Jonathan Austin 1:8aa5cdb4ab67 141 /**
Jonathan Austin 1:8aa5cdb4ab67 142 * Class definition for MicroBit Compass.
Jonathan Austin 1:8aa5cdb4ab67 143 *
Jonathan Austin 1:8aa5cdb4ab67 144 * Represents an implementation of the Freescale MAG3110 I2C Magnetmometer.
Jonathan Austin 1:8aa5cdb4ab67 145 * Also includes basic caching, calibration and on demand activation.
Jonathan Austin 1:8aa5cdb4ab67 146 */
Jonathan Austin 1:8aa5cdb4ab67 147 class MicroBitCompass : public MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 148 {
Jonathan Austin 1:8aa5cdb4ab67 149 uint16_t address; // I2C address of the magnetmometer.
Jonathan Austin 1:8aa5cdb4ab67 150 uint16_t samplePeriod; // The time between samples, in millseconds.
Jonathan Austin 1:8aa5cdb4ab67 151
Jonathan Austin 1:8aa5cdb4ab67 152 CompassSample average; // Centre point of sample data.
Jonathan Austin 1:8aa5cdb4ab67 153 CompassSample sample; // The latest sample data recorded.
Jonathan Austin 1:8aa5cdb4ab67 154 DigitalIn int1; // Data ready interrupt.
Jonathan Austin 1:8aa5cdb4ab67 155 MicroBitI2C& i2c; // The I2C interface the sensor is connected to.
Jonathan Austin 1:8aa5cdb4ab67 156 MicroBitAccelerometer* accelerometer; // The accelerometer to use for tilt compensation.
Jonathan Austin 1:8aa5cdb4ab67 157 MicroBitStorage* storage; // An instance of MicroBitStorage used for persistence.
Jonathan Austin 1:8aa5cdb4ab67 158
Jonathan Austin 1:8aa5cdb4ab67 159 public:
Jonathan Austin 1:8aa5cdb4ab67 160
Jonathan Austin 1:8aa5cdb4ab67 161 /**
Jonathan Austin 1:8aa5cdb4ab67 162 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 163 * Create a software representation of an e-compass.
Jonathan Austin 1:8aa5cdb4ab67 164 *
Jonathan Austin 1:8aa5cdb4ab67 165 * @param _i2c an instance of i2c, which the compass is accessible from.
Jonathan Austin 1:8aa5cdb4ab67 166 *
Jonathan Austin 1:8aa5cdb4ab67 167 * @param _accelerometer an instance of the accelerometer, used for tilt compensation.
Jonathan Austin 1:8aa5cdb4ab67 168 *
Jonathan Austin 1:8aa5cdb4ab67 169 * @param _storage an instance of MicroBitStorage, used to persist calibration data across resets.
Jonathan Austin 1:8aa5cdb4ab67 170 *
Jonathan Austin 1:8aa5cdb4ab67 171 * @param address the default address for the compass register on the i2c bus. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 172 *
Jonathan Austin 1:8aa5cdb4ab67 173 * @param id the ID of the new MicroBitCompass object. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 174 *
Jonathan Austin 1:8aa5cdb4ab67 175 * @code
Jonathan Austin 1:8aa5cdb4ab67 176 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
Jonathan Austin 1:8aa5cdb4ab67 177 *
Jonathan Austin 1:8aa5cdb4ab67 178 * MicroBitAccelerometer accelerometer(i2c);
Jonathan Austin 1:8aa5cdb4ab67 179 *
Jonathan Austin 1:8aa5cdb4ab67 180 * MicroBitStorage storage;
Jonathan Austin 1:8aa5cdb4ab67 181 *
Jonathan Austin 1:8aa5cdb4ab67 182 * MicroBitCompass compass(i2c, accelerometer, storage);
Jonathan Austin 1:8aa5cdb4ab67 183 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 184 */
Jonathan Austin 1:8aa5cdb4ab67 185 MicroBitCompass(MicroBitI2C& _i2c, MicroBitAccelerometer& _accelerometer, MicroBitStorage& _storage, uint16_t address = MAG3110_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_COMPASS);
Jonathan Austin 1:8aa5cdb4ab67 186
Jonathan Austin 1:8aa5cdb4ab67 187 /**
Jonathan Austin 1:8aa5cdb4ab67 188 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 189 * Create a software representation of an e-compass.
Jonathan Austin 1:8aa5cdb4ab67 190 *
Jonathan Austin 1:8aa5cdb4ab67 191 * @param _i2c an instance of i2c, which the compass is accessible from.
Jonathan Austin 1:8aa5cdb4ab67 192 *
Jonathan Austin 1:8aa5cdb4ab67 193 * @param _accelerometer an instance of the accelerometer, used for tilt compensation.
Jonathan Austin 1:8aa5cdb4ab67 194 *
Jonathan Austin 1:8aa5cdb4ab67 195 * @param address the default address for the compass register on the i2c bus. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 196 *
Jonathan Austin 1:8aa5cdb4ab67 197 * @param id the ID of the new MicroBitCompass object. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 198 *
Jonathan Austin 1:8aa5cdb4ab67 199 * @code
Jonathan Austin 1:8aa5cdb4ab67 200 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
Jonathan Austin 1:8aa5cdb4ab67 201 *
Jonathan Austin 1:8aa5cdb4ab67 202 * MicroBitAccelerometer accelerometer(i2c);
Jonathan Austin 1:8aa5cdb4ab67 203 *
Jonathan Austin 1:8aa5cdb4ab67 204 * MicroBitCompass compass(i2c, accelerometer, storage);
Jonathan Austin 1:8aa5cdb4ab67 205 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 206 */
Jonathan Austin 1:8aa5cdb4ab67 207 MicroBitCompass(MicroBitI2C& _i2c, MicroBitAccelerometer& _accelerometer, uint16_t address = MAG3110_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_COMPASS);
Jonathan Austin 1:8aa5cdb4ab67 208
Jonathan Austin 1:8aa5cdb4ab67 209 /**
Jonathan Austin 1:8aa5cdb4ab67 210 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 211 * Create a software representation of an e-compass.
Jonathan Austin 1:8aa5cdb4ab67 212 *
Jonathan Austin 1:8aa5cdb4ab67 213 * @param _i2c an instance of i2c, which the compass is accessible from.
Jonathan Austin 1:8aa5cdb4ab67 214 *
Jonathan Austin 1:8aa5cdb4ab67 215 * @param _storage an instance of MicroBitStorage, used to persist calibration data across resets.
Jonathan Austin 1:8aa5cdb4ab67 216 *
Jonathan Austin 1:8aa5cdb4ab67 217 * @param address the default address for the compass register on the i2c bus. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 218 *
Jonathan Austin 1:8aa5cdb4ab67 219 * @param id the ID of the new MicroBitCompass object. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 220 *
Jonathan Austin 1:8aa5cdb4ab67 221 * @code
Jonathan Austin 1:8aa5cdb4ab67 222 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
Jonathan Austin 1:8aa5cdb4ab67 223 *
Jonathan Austin 1:8aa5cdb4ab67 224 * MicroBitStorage storage;
Jonathan Austin 1:8aa5cdb4ab67 225 *
Jonathan Austin 1:8aa5cdb4ab67 226 * MicroBitCompass compass(i2c, storage);
Jonathan Austin 1:8aa5cdb4ab67 227 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 228 */
Jonathan Austin 1:8aa5cdb4ab67 229 MicroBitCompass(MicroBitI2C& _i2c, MicroBitStorage& _storage, uint16_t address = MAG3110_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_COMPASS);
Jonathan Austin 1:8aa5cdb4ab67 230
Jonathan Austin 1:8aa5cdb4ab67 231 /**
Jonathan Austin 1:8aa5cdb4ab67 232 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 233 * Create a software representation of an e-compass.
Jonathan Austin 1:8aa5cdb4ab67 234 *
Jonathan Austin 1:8aa5cdb4ab67 235 * @param _i2c an instance of i2c, which the compass is accessible from.
Jonathan Austin 1:8aa5cdb4ab67 236 *
Jonathan Austin 1:8aa5cdb4ab67 237 * @param address the default address for the compass register on the i2c bus. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 238 *
Jonathan Austin 1:8aa5cdb4ab67 239 * @param id the ID of the new MicroBitCompass object. Defaults to MAG3110_DEFAULT_ADDR.
Jonathan Austin 1:8aa5cdb4ab67 240 *
Jonathan Austin 1:8aa5cdb4ab67 241 * @code
Jonathan Austin 1:8aa5cdb4ab67 242 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
Jonathan Austin 1:8aa5cdb4ab67 243 *
Jonathan Austin 1:8aa5cdb4ab67 244 * MicroBitCompass compass(i2c);
Jonathan Austin 1:8aa5cdb4ab67 245 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 246 */
Jonathan Austin 1:8aa5cdb4ab67 247 MicroBitCompass(MicroBitI2C& _i2c, uint16_t address = MAG3110_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_COMPASS);
Jonathan Austin 1:8aa5cdb4ab67 248
Jonathan Austin 1:8aa5cdb4ab67 249 /**
Jonathan Austin 1:8aa5cdb4ab67 250 * Configures the compass for the sample rate defined in this object.
Jonathan Austin 1:8aa5cdb4ab67 251 * The nearest values are chosen to those defined that are supported by the hardware.
Jonathan Austin 1:8aa5cdb4ab67 252 * The instance variables are then updated to reflect reality.
Jonathan Austin 1:8aa5cdb4ab67 253 *
Jonathan Austin 1:8aa5cdb4ab67 254 * @return MICROBIT_OK or MICROBIT_I2C_ERROR if the magnetometer could not be configured.
Jonathan Austin 1:8aa5cdb4ab67 255 */
Jonathan Austin 1:8aa5cdb4ab67 256 int configure();
Jonathan Austin 1:8aa5cdb4ab67 257
Jonathan Austin 1:8aa5cdb4ab67 258 /**
Jonathan Austin 1:8aa5cdb4ab67 259 * Attempts to set the sample rate of the compass to the specified value (in ms).
Jonathan Austin 1:8aa5cdb4ab67 260 *
Jonathan Austin 1:8aa5cdb4ab67 261 * @param period the requested time between samples, in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 262 *
Jonathan Austin 1:8aa5cdb4ab67 263 * @return MICROBIT_OK or MICROBIT_I2C_ERROR if the magnetometer could not be updated.
Jonathan Austin 1:8aa5cdb4ab67 264 *
Jonathan Austin 1:8aa5cdb4ab67 265 * @code
Jonathan Austin 1:8aa5cdb4ab67 266 * // sample rate is now 20 ms.
Jonathan Austin 1:8aa5cdb4ab67 267 * compass.setPeriod(20);
Jonathan Austin 1:8aa5cdb4ab67 268 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 269 *
Jonathan Austin 1:8aa5cdb4ab67 270 * @note The requested rate may not be possible on the hardware. In this case, the
Jonathan Austin 1:8aa5cdb4ab67 271 * nearest lower rate is chosen.
Jonathan Austin 1:8aa5cdb4ab67 272 */
Jonathan Austin 1:8aa5cdb4ab67 273 int setPeriod(int period);
Jonathan Austin 1:8aa5cdb4ab67 274
Jonathan Austin 1:8aa5cdb4ab67 275 /**
Jonathan Austin 1:8aa5cdb4ab67 276 * Reads the currently configured sample rate of the compass.
Jonathan Austin 1:8aa5cdb4ab67 277 *
Jonathan Austin 1:8aa5cdb4ab67 278 * @return The time between samples, in milliseconds.
Jonathan Austin 1:8aa5cdb4ab67 279 */
Jonathan Austin 1:8aa5cdb4ab67 280 int getPeriod();
Jonathan Austin 1:8aa5cdb4ab67 281
Jonathan Austin 1:8aa5cdb4ab67 282 /**
Jonathan Austin 1:8aa5cdb4ab67 283 * Gets the current heading of the device, relative to magnetic north.
Jonathan Austin 1:8aa5cdb4ab67 284 *
Jonathan Austin 1:8aa5cdb4ab67 285 * If the compass is not calibrated, it will raise the MICROBIT_COMPASS_EVT_CALIBRATE event.
Jonathan Austin 1:8aa5cdb4ab67 286 *
Jonathan Austin 1:8aa5cdb4ab67 287 * Users wishing to implement their own calibration algorithms should listen for this event,
Jonathan Austin 1:8aa5cdb4ab67 288 * using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before
Jonathan Austin 1:8aa5cdb4ab67 289 * the user program continues.
Jonathan Austin 1:8aa5cdb4ab67 290 *
Jonathan Austin 1:8aa5cdb4ab67 291 * @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating.
Jonathan Austin 1:8aa5cdb4ab67 292 *
Jonathan Austin 1:8aa5cdb4ab67 293 * @code
Jonathan Austin 1:8aa5cdb4ab67 294 * compass.heading();
Jonathan Austin 1:8aa5cdb4ab67 295 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 296 */
Jonathan Austin 1:8aa5cdb4ab67 297 int heading();
Jonathan Austin 1:8aa5cdb4ab67 298
Jonathan Austin 1:8aa5cdb4ab67 299 /**
Jonathan Austin 1:8aa5cdb4ab67 300 * Attempts to read the 8 bit ID from the magnetometer, this can be used for
Jonathan Austin 1:8aa5cdb4ab67 301 * validation purposes.
Jonathan Austin 1:8aa5cdb4ab67 302 *
Jonathan Austin 1:8aa5cdb4ab67 303 * @return the 8 bit ID returned by the magnetometer, or MICROBIT_I2C_ERROR if the request fails.
Jonathan Austin 1:8aa5cdb4ab67 304 *
Jonathan Austin 1:8aa5cdb4ab67 305 * @code
Jonathan Austin 1:8aa5cdb4ab67 306 * compass.whoAmI();
Jonathan Austin 1:8aa5cdb4ab67 307 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 308 */
Jonathan Austin 1:8aa5cdb4ab67 309 int whoAmI();
Jonathan Austin 1:8aa5cdb4ab67 310
Jonathan Austin 1:8aa5cdb4ab67 311 /**
Jonathan Austin 1:8aa5cdb4ab67 312 * Reads the value of the X axis from the latest update retrieved from the magnetometer.
Jonathan Austin 1:8aa5cdb4ab67 313 *
Jonathan Austin 1:8aa5cdb4ab67 314 * @param system The coordinate system to use. By default, a simple cartesian system is provided.
Jonathan Austin 1:8aa5cdb4ab67 315 *
Jonathan Austin 1:8aa5cdb4ab67 316 * @return The magnetic force measured in the X axis, in nano teslas.
Jonathan Austin 1:8aa5cdb4ab67 317 *
Jonathan Austin 1:8aa5cdb4ab67 318 * @code
Jonathan Austin 1:8aa5cdb4ab67 319 * compass.getX();
Jonathan Austin 1:8aa5cdb4ab67 320 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 321 */
Jonathan Austin 1:8aa5cdb4ab67 322 int getX(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
Jonathan Austin 1:8aa5cdb4ab67 323
Jonathan Austin 1:8aa5cdb4ab67 324 /**
Jonathan Austin 1:8aa5cdb4ab67 325 * Reads the value of the Y axis from the latest update retrieved from the magnetometer.
Jonathan Austin 1:8aa5cdb4ab67 326 *
Jonathan Austin 1:8aa5cdb4ab67 327 * @param system The coordinate system to use. By default, a simple cartesian system is provided.
Jonathan Austin 1:8aa5cdb4ab67 328 *
Jonathan Austin 1:8aa5cdb4ab67 329 * @return The magnetic force measured in the Y axis, in nano teslas.
Jonathan Austin 1:8aa5cdb4ab67 330 *
Jonathan Austin 1:8aa5cdb4ab67 331 * @code
Jonathan Austin 1:8aa5cdb4ab67 332 * compass.getY();
Jonathan Austin 1:8aa5cdb4ab67 333 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 334 */
Jonathan Austin 1:8aa5cdb4ab67 335 int getY(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
Jonathan Austin 1:8aa5cdb4ab67 336
Jonathan Austin 1:8aa5cdb4ab67 337 /**
Jonathan Austin 1:8aa5cdb4ab67 338 * Reads the value of the Z axis from the latest update retrieved from the magnetometer.
Jonathan Austin 1:8aa5cdb4ab67 339 *
Jonathan Austin 1:8aa5cdb4ab67 340 * @param system The coordinate system to use. By default, a simple cartesian system is provided.
Jonathan Austin 1:8aa5cdb4ab67 341 *
Jonathan Austin 1:8aa5cdb4ab67 342 * @return The magnetic force measured in the Z axis, in nano teslas.
Jonathan Austin 1:8aa5cdb4ab67 343 *
Jonathan Austin 1:8aa5cdb4ab67 344 * @code
Jonathan Austin 1:8aa5cdb4ab67 345 * compass.getZ();
Jonathan Austin 1:8aa5cdb4ab67 346 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 347 */
Jonathan Austin 1:8aa5cdb4ab67 348 int getZ(MicroBitCoordinateSystem system = SIMPLE_CARTESIAN);
Jonathan Austin 1:8aa5cdb4ab67 349
Jonathan Austin 1:8aa5cdb4ab67 350 /**
Jonathan Austin 1:8aa5cdb4ab67 351 * Determines the overall magnetic field strength based on the latest update from the magnetometer.
Jonathan Austin 1:8aa5cdb4ab67 352 *
Jonathan Austin 1:8aa5cdb4ab67 353 * @return The magnetic force measured across all axis, in nano teslas.
Jonathan Austin 1:8aa5cdb4ab67 354 *
Jonathan Austin 1:8aa5cdb4ab67 355 * @code
Jonathan Austin 1:8aa5cdb4ab67 356 * compass.getFieldStrength();
Jonathan Austin 1:8aa5cdb4ab67 357 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 358 */
Jonathan Austin 1:8aa5cdb4ab67 359 int getFieldStrength();
Jonathan Austin 1:8aa5cdb4ab67 360
Jonathan Austin 1:8aa5cdb4ab67 361 /**
Jonathan Austin 1:8aa5cdb4ab67 362 * Reads the current die temperature of the compass.
Jonathan Austin 1:8aa5cdb4ab67 363 *
Jonathan Austin 1:8aa5cdb4ab67 364 * @return the temperature in degrees celsius, or MICROBIT_I2C_ERROR if the temperature reading could not be retreived
Jonathan Austin 1:8aa5cdb4ab67 365 * from the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 366 */
Jonathan Austin 1:8aa5cdb4ab67 367 int readTemperature();
Jonathan Austin 1:8aa5cdb4ab67 368
Jonathan Austin 1:8aa5cdb4ab67 369 /**
Jonathan Austin 1:8aa5cdb4ab67 370 * Perform a calibration of the compass.
Jonathan Austin 1:8aa5cdb4ab67 371 *
Jonathan Austin 1:8aa5cdb4ab67 372 * This method will be called automatically if a user attempts to read a compass value when
Jonathan Austin 1:8aa5cdb4ab67 373 * the compass is uncalibrated. It can also be called at any time by the user.
Jonathan Austin 1:8aa5cdb4ab67 374 *
Jonathan Austin 1:8aa5cdb4ab67 375 * The method will only return once the compass has been calibrated.
Jonathan Austin 1:8aa5cdb4ab67 376 *
Jonathan Austin 1:8aa5cdb4ab67 377 * @return MICROBIT_OK, MICROBIT_I2C_ERROR if the magnetometer could not be accessed,
Jonathan Austin 1:8aa5cdb4ab67 378 * or MICROBIT_CALIBRATION_REQUIRED if the calibration algorithm failed to complete successfully.
Jonathan Austin 1:8aa5cdb4ab67 379 *
Jonathan Austin 1:8aa5cdb4ab67 380 * @note THIS MUST BE CALLED TO GAIN RELIABLE VALUES FROM THE COMPASS
Jonathan Austin 1:8aa5cdb4ab67 381 */
Jonathan Austin 1:8aa5cdb4ab67 382 int calibrate();
Jonathan Austin 1:8aa5cdb4ab67 383
Jonathan Austin 1:8aa5cdb4ab67 384 /**
Jonathan Austin 1:8aa5cdb4ab67 385 * Configure the compass to use the calibration data that is supplied to this call.
Jonathan Austin 1:8aa5cdb4ab67 386 *
Jonathan Austin 1:8aa5cdb4ab67 387 * Calibration data is comprised of the perceived zero offset of each axis of the compass.
Jonathan Austin 1:8aa5cdb4ab67 388 *
Jonathan Austin 1:8aa5cdb4ab67 389 * After calibration this should now take into account trimming errors in the magnetometer,
Jonathan Austin 1:8aa5cdb4ab67 390 * and any "hard iron" offsets on the device.
Jonathan Austin 1:8aa5cdb4ab67 391 *
Jonathan Austin 1:8aa5cdb4ab67 392 * @param calibration A CompassSample containing the offsets for the x, y and z axis.
Jonathan Austin 1:8aa5cdb4ab67 393 */
Jonathan Austin 1:8aa5cdb4ab67 394 void setCalibration(CompassSample calibration);
Jonathan Austin 1:8aa5cdb4ab67 395
Jonathan Austin 1:8aa5cdb4ab67 396 /**
Jonathan Austin 1:8aa5cdb4ab67 397 * Provides the calibration data currently in use by the compass.
Jonathan Austin 1:8aa5cdb4ab67 398 *
Jonathan Austin 1:8aa5cdb4ab67 399 * More specifically, the x, y and z zero offsets of the compass.
Jonathan Austin 1:8aa5cdb4ab67 400 *
Jonathan Austin 1:8aa5cdb4ab67 401 * @return calibration A CompassSample containing the offsets for the x, y and z axis.
Jonathan Austin 1:8aa5cdb4ab67 402 */
Jonathan Austin 1:8aa5cdb4ab67 403 CompassSample getCalibration();
Jonathan Austin 1:8aa5cdb4ab67 404
Jonathan Austin 1:8aa5cdb4ab67 405 /**
Jonathan Austin 1:8aa5cdb4ab67 406 * Updates the local sample, only if the compass indicates that
Jonathan Austin 1:8aa5cdb4ab67 407 * data is stale.
Jonathan Austin 1:8aa5cdb4ab67 408 *
Jonathan Austin 1:8aa5cdb4ab67 409 * @note Can be used to trigger manual updates, if the device is running without a scheduler.
Jonathan Austin 1:8aa5cdb4ab67 410 * Also called internally by all get[X,Y,Z]() member functions.
Jonathan Austin 1:8aa5cdb4ab67 411 */
Jonathan Austin 1:8aa5cdb4ab67 412 int updateSample();
Jonathan Austin 1:8aa5cdb4ab67 413
Jonathan Austin 1:8aa5cdb4ab67 414 /**
Jonathan Austin 1:8aa5cdb4ab67 415 * Periodic callback from MicroBit idle thread.
Jonathan Austin 1:8aa5cdb4ab67 416 *
Jonathan Austin 1:8aa5cdb4ab67 417 * Calls updateSample().
Jonathan Austin 1:8aa5cdb4ab67 418 */
Jonathan Austin 1:8aa5cdb4ab67 419 virtual void idleTick();
Jonathan Austin 1:8aa5cdb4ab67 420
Jonathan Austin 1:8aa5cdb4ab67 421 /**
Jonathan Austin 1:8aa5cdb4ab67 422 * Returns 0 or 1. 1 indicates that the compass is calibrated, zero means the compass requires calibration.
Jonathan Austin 1:8aa5cdb4ab67 423 */
Jonathan Austin 1:8aa5cdb4ab67 424 int isCalibrated();
Jonathan Austin 1:8aa5cdb4ab67 425
Jonathan Austin 1:8aa5cdb4ab67 426 /**
Jonathan Austin 1:8aa5cdb4ab67 427 * Returns 0 or 1. 1 indicates that the compass is calibrating, zero means the compass is not currently calibrating.
Jonathan Austin 1:8aa5cdb4ab67 428 */
Jonathan Austin 1:8aa5cdb4ab67 429 int isCalibrating();
Jonathan Austin 1:8aa5cdb4ab67 430
Jonathan Austin 1:8aa5cdb4ab67 431 /**
Jonathan Austin 1:8aa5cdb4ab67 432 * Clears the calibration held in persistent storage, and sets the calibrated flag to zero.
Jonathan Austin 1:8aa5cdb4ab67 433 */
Jonathan Austin 1:8aa5cdb4ab67 434 void clearCalibration();
Jonathan Austin 1:8aa5cdb4ab67 435
Jonathan Austin 1:8aa5cdb4ab67 436 /**
Jonathan Austin 1:8aa5cdb4ab67 437 * Destructor for MicroBitCompass, where we deregister this instance from the array of fiber components.
Jonathan Austin 1:8aa5cdb4ab67 438 */
Jonathan Austin 1:8aa5cdb4ab67 439 ~MicroBitCompass();
Jonathan Austin 1:8aa5cdb4ab67 440
Jonathan Austin 1:8aa5cdb4ab67 441 private:
Jonathan Austin 1:8aa5cdb4ab67 442
Jonathan Austin 1:8aa5cdb4ab67 443 /**
Jonathan Austin 1:8aa5cdb4ab67 444 * Issues a standard, 2 byte I2C command write to the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 445 *
Jonathan Austin 1:8aa5cdb4ab67 446 * Blocks the calling thread until complete.
Jonathan Austin 1:8aa5cdb4ab67 447 *
Jonathan Austin 1:8aa5cdb4ab67 448 * @param reg The address of the register to write to.
Jonathan Austin 1:8aa5cdb4ab67 449 *
Jonathan Austin 1:8aa5cdb4ab67 450 * @param value The value to write.
Jonathan Austin 1:8aa5cdb4ab67 451 *
Jonathan Austin 1:8aa5cdb4ab67 452 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed.
Jonathan Austin 1:8aa5cdb4ab67 453 */
Jonathan Austin 1:8aa5cdb4ab67 454 int writeCommand(uint8_t reg, uint8_t value);
Jonathan Austin 1:8aa5cdb4ab67 455
Jonathan Austin 1:8aa5cdb4ab67 456 /**
Jonathan Austin 1:8aa5cdb4ab67 457 * Issues a read command, copying data into the specified buffer.
Jonathan Austin 1:8aa5cdb4ab67 458 *
Jonathan Austin 1:8aa5cdb4ab67 459 * Blocks the calling thread until complete.
Jonathan Austin 1:8aa5cdb4ab67 460 *
Jonathan Austin 1:8aa5cdb4ab67 461 * @param reg The address of the register to access.
Jonathan Austin 1:8aa5cdb4ab67 462 *
Jonathan Austin 1:8aa5cdb4ab67 463 * @param buffer Memory area to read the data into.
Jonathan Austin 1:8aa5cdb4ab67 464 *
Jonathan Austin 1:8aa5cdb4ab67 465 * @param length The number of bytes to read.
Jonathan Austin 1:8aa5cdb4ab67 466 *
Jonathan Austin 1:8aa5cdb4ab67 467 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed.
Jonathan Austin 1:8aa5cdb4ab67 468 */
Jonathan Austin 1:8aa5cdb4ab67 469 int readCommand(uint8_t reg, uint8_t* buffer, int length);
Jonathan Austin 1:8aa5cdb4ab67 470
Jonathan Austin 1:8aa5cdb4ab67 471 /**
Jonathan Austin 1:8aa5cdb4ab67 472 * Issues a read of a given address, and returns the value.
Jonathan Austin 1:8aa5cdb4ab67 473 *
Jonathan Austin 1:8aa5cdb4ab67 474 * Blocks the calling thread until complete.
Jonathan Austin 1:8aa5cdb4ab67 475 *
Jonathan Austin 1:8aa5cdb4ab67 476 * @param reg The address of the 16 bit register to access.
Jonathan Austin 1:8aa5cdb4ab67 477 *
Jonathan Austin 1:8aa5cdb4ab67 478 * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
Jonathan Austin 1:8aa5cdb4ab67 479 */
Jonathan Austin 1:8aa5cdb4ab67 480 int read16(uint8_t reg);
Jonathan Austin 1:8aa5cdb4ab67 481
Jonathan Austin 1:8aa5cdb4ab67 482 /**
Jonathan Austin 1:8aa5cdb4ab67 483 * Issues a read of a given address, and returns the value.
Jonathan Austin 1:8aa5cdb4ab67 484 *
Jonathan Austin 1:8aa5cdb4ab67 485 * Blocks the calling thread until complete.
Jonathan Austin 1:8aa5cdb4ab67 486 *
Jonathan Austin 1:8aa5cdb4ab67 487 * @param reg The address of the 16 bit register to access.
Jonathan Austin 1:8aa5cdb4ab67 488 *
Jonathan Austin 1:8aa5cdb4ab67 489 * @return The register value, interpreted as a 8 bit unsigned value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
Jonathan Austin 1:8aa5cdb4ab67 490 */
Jonathan Austin 1:8aa5cdb4ab67 491 int read8(uint8_t reg);
Jonathan Austin 1:8aa5cdb4ab67 492
Jonathan Austin 1:8aa5cdb4ab67 493 /**
Jonathan Austin 1:8aa5cdb4ab67 494 * Calculates a tilt compensated bearing of the device, using the accelerometer.
Jonathan Austin 1:8aa5cdb4ab67 495 */
Jonathan Austin 1:8aa5cdb4ab67 496 int tiltCompensatedBearing();
Jonathan Austin 1:8aa5cdb4ab67 497
Jonathan Austin 1:8aa5cdb4ab67 498 /**
Jonathan Austin 1:8aa5cdb4ab67 499 * Calculates a non-tilt compensated bearing of the device.
Jonathan Austin 1:8aa5cdb4ab67 500 */
Jonathan Austin 1:8aa5cdb4ab67 501 int basicBearing();
Jonathan Austin 1:8aa5cdb4ab67 502
Jonathan Austin 1:8aa5cdb4ab67 503 /**
Jonathan Austin 1:8aa5cdb4ab67 504 * An initialisation member function used by the many constructors of MicroBitCompass.
Jonathan Austin 1:8aa5cdb4ab67 505 *
Jonathan Austin 1:8aa5cdb4ab67 506 * @param id the unique identifier for this compass instance.
Jonathan Austin 1:8aa5cdb4ab67 507 *
Jonathan Austin 1:8aa5cdb4ab67 508 * @param address the base address of the magnetometer on the i2c bus.
Jonathan Austin 1:8aa5cdb4ab67 509 */
Jonathan Austin 1:8aa5cdb4ab67 510 void init(uint16_t id, uint16_t address);
Jonathan Austin 1:8aa5cdb4ab67 511 };
Jonathan Austin 1:8aa5cdb4ab67 512
LancasterUniversity 41:da05ec75cd5d 513 #endif