Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MicroBitDALImageRewrite by
inc/MicroBitMagnetometer.h@4:f998ee705a20, 2015-05-15 (annotated)
- Committer:
- finneyj
- Date:
- Fri May 15 22:23:17 2015 +0000
- Revision:
- 4:f998ee705a20
SB2 support and minor bugfixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
finneyj | 4:f998ee705a20 | 1 | /** |
finneyj | 4:f998ee705a20 | 2 | * Class definition for MicroBit Magnetometer. |
finneyj | 4:f998ee705a20 | 3 | * |
finneyj | 4:f998ee705a20 | 4 | * Represents an implementation of the Freescale MAG3110 I2C Magnetmometer. |
finneyj | 4:f998ee705a20 | 5 | * Also includes basic caching, calibration and on demand activation. |
finneyj | 4:f998ee705a20 | 6 | */ |
finneyj | 4:f998ee705a20 | 7 | |
finneyj | 4:f998ee705a20 | 8 | #ifndef MICROBIT_MAGNETOMETER_H |
finneyj | 4:f998ee705a20 | 9 | #define MICROBIT_MAGNETOMETER_H |
finneyj | 4:f998ee705a20 | 10 | |
finneyj | 4:f998ee705a20 | 11 | #include "mbed.h" |
finneyj | 4:f998ee705a20 | 12 | |
finneyj | 4:f998ee705a20 | 13 | /* |
finneyj | 4:f998ee705a20 | 14 | * MAG3110 Register map |
finneyj | 4:f998ee705a20 | 15 | */ |
finneyj | 4:f998ee705a20 | 16 | #define MAG_DR_STATUS 0x00 |
finneyj | 4:f998ee705a20 | 17 | #define MAG_OUT_X_MSB 0x01 |
finneyj | 4:f998ee705a20 | 18 | #define MAG_OUT_X_LSB 0x02 |
finneyj | 4:f998ee705a20 | 19 | #define MAG_OUT_Y_MSB 0x03 |
finneyj | 4:f998ee705a20 | 20 | #define MAG_OUT_Y_LSB 0x04 |
finneyj | 4:f998ee705a20 | 21 | #define MAG_OUT_Z_MSB 0x05 |
finneyj | 4:f998ee705a20 | 22 | #define MAG_OUT_Z_LSB 0x06 |
finneyj | 4:f998ee705a20 | 23 | #define MAG_WHO_AM_I 0x07 |
finneyj | 4:f998ee705a20 | 24 | #define MAG_SYSMOD 0x08 |
finneyj | 4:f998ee705a20 | 25 | #define MAG_OFF_X_MSB 0x09 |
finneyj | 4:f998ee705a20 | 26 | #define MAG_OFF_X_LSB 0x0A |
finneyj | 4:f998ee705a20 | 27 | #define MAG_OFF_Y_MSB 0x0B |
finneyj | 4:f998ee705a20 | 28 | #define MAG_OFF_Y_LSB 0x0C |
finneyj | 4:f998ee705a20 | 29 | #define MAG_OFF_Z_MSB 0x0D |
finneyj | 4:f998ee705a20 | 30 | #define MAG_OFF_Z_LSB 0x0E |
finneyj | 4:f998ee705a20 | 31 | #define MAG_DIE_TEMP 0x0F |
finneyj | 4:f998ee705a20 | 32 | #define MAG_CTRL_REG1 0x10 |
finneyj | 4:f998ee705a20 | 33 | #define MAG_CTRL_REG2 0x11 |
finneyj | 4:f998ee705a20 | 34 | |
finneyj | 4:f998ee705a20 | 35 | /* |
finneyj | 4:f998ee705a20 | 36 | * MAG3110 MAGIC ID value |
finneyj | 4:f998ee705a20 | 37 | * Returned from the MAG_WHO_AM_I register for ID purposes. |
finneyj | 4:f998ee705a20 | 38 | */ |
finneyj | 4:f998ee705a20 | 39 | #define MAG_3110_WHO_AM_I_VALUE 0xC4 |
finneyj | 4:f998ee705a20 | 40 | |
finneyj | 4:f998ee705a20 | 41 | |
finneyj | 4:f998ee705a20 | 42 | /* |
finneyj | 4:f998ee705a20 | 43 | * MAG3110 Sample Rate constants |
finneyj | 4:f998ee705a20 | 44 | */ |
finneyj | 4:f998ee705a20 | 45 | #define MAG_3110_SAMPLE80 0 |
finneyj | 4:f998ee705a20 | 46 | #define MAG_3110_SAMPLE40 0x20 |
finneyj | 4:f998ee705a20 | 47 | #define MAG_3110_SAMPLE20 0x40 |
finneyj | 4:f998ee705a20 | 48 | #define MAG_3110_SAMPLE10 0x60 |
finneyj | 4:f998ee705a20 | 49 | #define MAG_3110_SAMPLE5 0x80 |
finneyj | 4:f998ee705a20 | 50 | #define MAG_3110_SAMPLE2_5 0xA0 |
finneyj | 4:f998ee705a20 | 51 | #define MAG_3110_SAMPLE1_25 0xC0 |
finneyj | 4:f998ee705a20 | 52 | #define MAG_3110_SAMPLE0_625 0xE0 |
finneyj | 4:f998ee705a20 | 53 | |
finneyj | 4:f998ee705a20 | 54 | #define MAG_3110_OVERSAMPLE1 0 |
finneyj | 4:f998ee705a20 | 55 | #define MAG_3110_OVERSAMPLE2 0x08 |
finneyj | 4:f998ee705a20 | 56 | #define MAG_3110_OVERSAMPLE3 0x10 |
finneyj | 4:f998ee705a20 | 57 | #define MAG_3110_OVERSAMPLE4 0x18 |
finneyj | 4:f998ee705a20 | 58 | |
finneyj | 4:f998ee705a20 | 59 | // read only 1 byte per axis |
finneyj | 4:f998ee705a20 | 60 | #define MAG_3110_FASTREAD 0x04 |
finneyj | 4:f998ee705a20 | 61 | // do one measurement (even if in standby mode) |
finneyj | 4:f998ee705a20 | 62 | #define MAG_3110_TRIGGER 0x02 |
finneyj | 4:f998ee705a20 | 63 | // put in active mode |
finneyj | 4:f998ee705a20 | 64 | #define MAG_3110_ACTIVE 0x01 |
finneyj | 4:f998ee705a20 | 65 | |
finneyj | 4:f998ee705a20 | 66 | // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _ |
finneyj | 4:f998ee705a20 | 67 | // reset sensor after each reading |
finneyj | 4:f998ee705a20 | 68 | #define MAG_3110_AUTO_MRST_EN 0x80 |
finneyj | 4:f998ee705a20 | 69 | // don't subtract user offsets |
finneyj | 4:f998ee705a20 | 70 | #define MAG_3110_RAW 0x20 |
finneyj | 4:f998ee705a20 | 71 | // reset magnetic sensor after too-large field |
finneyj | 4:f998ee705a20 | 72 | #define MAG_3110_MAG_RST 0x10 |
finneyj | 4:f998ee705a20 | 73 | |
finneyj | 4:f998ee705a20 | 74 | // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR |
finneyj | 4:f998ee705a20 | 75 | #define MAG_3110_ZYXDR 0x08 |
finneyj | 4:f998ee705a20 | 76 | |
finneyj | 4:f998ee705a20 | 77 | |
finneyj | 4:f998ee705a20 | 78 | |
finneyj | 4:f998ee705a20 | 79 | class MicroBitMagnetometer |
finneyj | 4:f998ee705a20 | 80 | { |
finneyj | 4:f998ee705a20 | 81 | /** |
finneyj | 4:f998ee705a20 | 82 | * Unique, enumerated ID for this component. |
finneyj | 4:f998ee705a20 | 83 | * Used to track asynchronous events in the event bus. |
finneyj | 4:f998ee705a20 | 84 | */ |
finneyj | 4:f998ee705a20 | 85 | |
finneyj | 4:f998ee705a20 | 86 | int id; // Event Bus ID |
finneyj | 4:f998ee705a20 | 87 | int address; // I2C address of the magnetmometer. |
finneyj | 4:f998ee705a20 | 88 | |
finneyj | 4:f998ee705a20 | 89 | public: |
finneyj | 4:f998ee705a20 | 90 | |
finneyj | 4:f998ee705a20 | 91 | /** |
finneyj | 4:f998ee705a20 | 92 | * Constructor. |
finneyj | 4:f998ee705a20 | 93 | * Create a magnetometer representation with the given ID. |
finneyj | 4:f998ee705a20 | 94 | * @param id the ID of the new object. |
finneyj | 4:f998ee705a20 | 95 | */ |
finneyj | 4:f998ee705a20 | 96 | MicroBitMagnetometer(int id, int address); |
finneyj | 4:f998ee705a20 | 97 | |
finneyj | 4:f998ee705a20 | 98 | /** |
finneyj | 4:f998ee705a20 | 99 | * Gets the current heading of the device, relative to magnetic north. |
finneyj | 4:f998ee705a20 | 100 | * @return the current heading, in degrees. |
finneyj | 4:f998ee705a20 | 101 | */ |
finneyj | 4:f998ee705a20 | 102 | int heading(); |
finneyj | 4:f998ee705a20 | 103 | |
finneyj | 4:f998ee705a20 | 104 | }; |
finneyj | 4:f998ee705a20 | 105 | |
finneyj | 4:f998ee705a20 | 106 | #endif |