Microbug / MicroBitDAL_SB2_TEST

Fork of MicroBitDALImageRewrite by Joe Finney

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitMagnetometer.h Source File

MicroBitMagnetometer.h

00001 /**
00002   * Class definition for MicroBit Magnetometer.
00003   *
00004   * Represents an implementation of the Freescale MAG3110 I2C Magnetmometer.
00005   * Also includes basic caching, calibration and on demand activation.
00006   */
00007   
00008 #ifndef MICROBIT_MAGNETOMETER_H
00009 #define MICROBIT_MAGNETOMETER_H
00010 
00011 #include "mbed.h"
00012 
00013 /*
00014  * MAG3110 Register map
00015  */
00016 #define MAG_DR_STATUS 0x00
00017 #define MAG_OUT_X_MSB 0x01
00018 #define MAG_OUT_X_LSB 0x02
00019 #define MAG_OUT_Y_MSB 0x03
00020 #define MAG_OUT_Y_LSB 0x04
00021 #define MAG_OUT_Z_MSB 0x05
00022 #define MAG_OUT_Z_LSB 0x06
00023 #define MAG_WHO_AM_I  0x07
00024 #define MAG_SYSMOD    0x08
00025 #define MAG_OFF_X_MSB 0x09
00026 #define MAG_OFF_X_LSB 0x0A
00027 #define MAG_OFF_Y_MSB 0x0B
00028 #define MAG_OFF_Y_LSB 0x0C
00029 #define MAG_OFF_Z_MSB 0x0D
00030 #define MAG_OFF_Z_LSB 0x0E
00031 #define MAG_DIE_TEMP  0x0F
00032 #define MAG_CTRL_REG1 0x10
00033 #define MAG_CTRL_REG2 0x11
00034 
00035 /*
00036  * MAG3110 MAGIC ID value
00037  * Returned from the MAG_WHO_AM_I register for ID purposes.
00038  */
00039 #define MAG_3110_WHO_AM_I_VALUE 0xC4
00040 
00041 
00042 /*
00043  * MAG3110 Sample Rate constants
00044  */
00045 #define MAG_3110_SAMPLE80 0
00046 #define MAG_3110_SAMPLE40 0x20
00047 #define MAG_3110_SAMPLE20 0x40
00048 #define MAG_3110_SAMPLE10 0x60
00049 #define MAG_3110_SAMPLE5 0x80
00050 #define MAG_3110_SAMPLE2_5 0xA0
00051 #define MAG_3110_SAMPLE1_25 0xC0
00052 #define MAG_3110_SAMPLE0_625 0xE0
00053 
00054 #define MAG_3110_OVERSAMPLE1 0
00055 #define MAG_3110_OVERSAMPLE2 0x08
00056 #define MAG_3110_OVERSAMPLE3 0x10
00057 #define MAG_3110_OVERSAMPLE4 0x18
00058 
00059 // read only 1 byte per axis
00060 #define MAG_3110_FASTREAD 0x04
00061 // do one measurement (even if in standby mode)
00062 #define MAG_3110_TRIGGER 0x02
00063 // put in active mode
00064 #define MAG_3110_ACTIVE 0x01
00065 
00066 // CTRL_REG2: AUTO_MRST_EN  _ RAW MAG_RST _ _ _ _ _
00067 // reset sensor after each reading
00068 #define MAG_3110_AUTO_MRST_EN 0x80
00069 // don't subtract user offsets
00070 #define MAG_3110_RAW 0x20
00071 // reset magnetic sensor after too-large field
00072 #define MAG_3110_MAG_RST 0x10
00073 
00074 // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR
00075 #define MAG_3110_ZYXDR  0x08
00076 
00077 
00078 
00079 class MicroBitMagnetometer
00080 {
00081     /**
00082       * Unique, enumerated ID for this component. 
00083       * Used to track asynchronous events in the event bus.
00084       */
00085       
00086     int id;             // Event Bus ID
00087     int address;        // I2C address of the magnetmometer.  
00088     
00089     public:
00090     
00091     /**
00092       * Constructor. 
00093       * Create a magnetometer representation with the given ID.
00094       * @param id the ID of the new object.
00095       */
00096     MicroBitMagnetometer(int id, int address);
00097     
00098     /**
00099       * Gets the current heading of the device, relative to magnetic north.
00100       * @return the current heading, in degrees.
00101       */
00102     int heading();
00103     
00104 };
00105 
00106 #endif