Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wue 0:7b58cdacf811 1 /*
wue 0:7b58cdacf811 2 * MAG3110 Sensor Library for mbed
wue 0:7b58cdacf811 3 * TODO: Add proper header
wue 0:7b58cdacf811 4 */
wue 0:7b58cdacf811 5
wue 0:7b58cdacf811 6 #ifndef MAG3110_H
wue 0:7b58cdacf811 7 #define MAG3110_H
wue 0:7b58cdacf811 8
wue 0:7b58cdacf811 9 #include "mbed.h"
wue 0:7b58cdacf811 10
wue 0:7b58cdacf811 11 #define PI 3.14159265359
wue 0:7b58cdacf811 12
wue 0:7b58cdacf811 13 #define MAG_ADDR 0x1D
wue 0:7b58cdacf811 14
wue 0:7b58cdacf811 15 // define registers
wue 0:7b58cdacf811 16 #define MAG_DR_STATUS 0x00
wue 0:7b58cdacf811 17 #define MAG_OUT_X_MSB 0x01
wue 0:7b58cdacf811 18 #define MAG_OUT_X_LSB 0x02
wue 0:7b58cdacf811 19 #define MAG_OUT_Y_MSB 0x03
wue 0:7b58cdacf811 20 #define MAG_OUT_Y_LSB 0x04
wue 0:7b58cdacf811 21 #define MAG_OUT_Z_MSB 0x05
wue 0:7b58cdacf811 22 #define MAG_OUT_Z_LSB 0x06
wue 0:7b58cdacf811 23 #define MAG_WHO_AM_I 0x07
wue 0:7b58cdacf811 24 #define MAG_SYSMOD 0x08
wue 0:7b58cdacf811 25 #define MAG_OFF_X_MSB 0x09
wue 0:7b58cdacf811 26 #define MAG_OFF_X_LSB 0x0A
wue 0:7b58cdacf811 27 #define MAG_OFF_Y_MSB 0x0B
wue 0:7b58cdacf811 28 #define MAG_OFF_Y_LSB 0x0C
wue 0:7b58cdacf811 29 #define MAG_OFF_Z_MSB 0x0D
wue 0:7b58cdacf811 30 #define MAG_OFF_Z_LSB 0x0E
wue 0:7b58cdacf811 31 #define MAG_DIE_TEMP 0x0F
wue 0:7b58cdacf811 32 #define MAG_CTRL_REG1 0x10
wue 0:7b58cdacf811 33 #define MAG_CTRL_REG2 0x11
wue 0:7b58cdacf811 34
wue 0:7b58cdacf811 35 // what should WHO_AM_I return?
wue 0:7b58cdacf811 36 #define MAG_3110_WHO_AM_I_VALUE 0xC4
wue 0:7b58cdacf811 37
wue 0:7b58cdacf811 38
wue 0:7b58cdacf811 39 // Fields in registers
wue 0:7b58cdacf811 40 // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac
wue 0:7b58cdacf811 41
wue 0:7b58cdacf811 42 // Sampling rate from 80Hz down to 0.625Hz
wue 0:7b58cdacf811 43 #define MAG_3110_SAMPLE80 0
wue 0:7b58cdacf811 44 #define MAG_3110_SAMPLE40 0x20
wue 0:7b58cdacf811 45 #define MAG_3110_SAMPLE20 0x40
wue 0:7b58cdacf811 46 #define MAG_3110_SAMPLE10 0x60
wue 0:7b58cdacf811 47 #define MAG_3110_SAMPLE5 0x80
wue 0:7b58cdacf811 48 #define MAG_3110_SAMPLE2_5 0xA0
wue 0:7b58cdacf811 49 #define MAG_3110_SAMPLE1_25 0xC0
wue 0:7b58cdacf811 50 #define MAG_3110_SAMPLE0_625 0xE0
wue 0:7b58cdacf811 51
wue 0:7b58cdacf811 52 // How many samples to average (lowers data rate)
wue 0:7b58cdacf811 53 #define MAG_3110_OVERSAMPLE1 0
wue 0:7b58cdacf811 54 #define MAG_3110_OVERSAMPLE2 0x08
wue 0:7b58cdacf811 55 #define MAG_3110_OVERSAMPLE3 0x10
wue 0:7b58cdacf811 56 #define MAG_3110_OVERSAMPLE4 0x18
wue 0:7b58cdacf811 57
wue 0:7b58cdacf811 58 // read only 1 byte per axis
wue 0:7b58cdacf811 59 #define MAG_3110_FASTREAD 0x04
wue 0:7b58cdacf811 60 // do one measurement (even if in standby mode)
wue 0:7b58cdacf811 61 #define MAG_3110_TRIGGER 0x02
wue 0:7b58cdacf811 62 // put in active mode
wue 0:7b58cdacf811 63 #define MAG_3110_ACTIVE 0x01
wue 0:7b58cdacf811 64
wue 0:7b58cdacf811 65 // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _
wue 0:7b58cdacf811 66 // reset sensor after each reading
wue 0:7b58cdacf811 67 #define MAG_3110_AUTO_MRST_EN 0x80
wue 0:7b58cdacf811 68 // don't subtract user offsets
wue 0:7b58cdacf811 69 #define MAG_3110_RAW 0x20
wue 0:7b58cdacf811 70 // reset magnetic sensor after too-large field
wue 0:7b58cdacf811 71 #define MAG_3110_MAG_RST 0x10
wue 0:7b58cdacf811 72
wue 0:7b58cdacf811 73 // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR
wue 0:7b58cdacf811 74 #define MAG_3110_ZYXDR 0x08
wue 0:7b58cdacf811 75
wue 0:7b58cdacf811 76 /**
wue 0:7b58cdacf811 77 * MAG3110 Class to read X/Y/Z data from the magentometer
wue 0:7b58cdacf811 78 *
wue 0:7b58cdacf811 79 */
wue 0:7b58cdacf811 80 class MAG3110
wue 0:7b58cdacf811 81 {
wue 0:7b58cdacf811 82 public:
wue 0:7b58cdacf811 83 /**
wue 0:7b58cdacf811 84 * Main constructor
wue 0:7b58cdacf811 85 * @param sda SDA pin
wue 0:7b58cdacf811 86 * @param sdl SCL pin
wue 0:7b58cdacf811 87 * @param addr addr of the I2C peripheral
wue 0:7b58cdacf811 88 */
wue 0:7b58cdacf811 89 MAG3110(PinName sda, PinName scl);
wue 0:7b58cdacf811 90 /**
wue 0:7b58cdacf811 91 * Debug version of constructor
wue 0:7b58cdacf811 92 * @param sda SDA pin
wue 0:7b58cdacf811 93 * @param sdl SCL pin
wue 0:7b58cdacf811 94 * @param addr Address of the I2C peripheral
wue 0:7b58cdacf811 95 * @param pc Serial object to output debug messages
wue 0:7b58cdacf811 96 */
wue 0:7b58cdacf811 97 MAG3110(PinName sda, PinName scl, Serial *pc); //pass serial for debug
wue 0:7b58cdacf811 98 /**
wue 0:7b58cdacf811 99 * Setup the Magnetometer
wue 0:7b58cdacf811 100 *
wue 0:7b58cdacf811 101 */
wue 0:7b58cdacf811 102 void begin();
wue 0:7b58cdacf811 103 /**
wue 0:7b58cdacf811 104 * Read a register, return its value as int
wue 0:7b58cdacf811 105 * @param regAddr The address to read
wue 0:7b58cdacf811 106 * @return value in register
wue 0:7b58cdacf811 107 */
wue 0:7b58cdacf811 108 int readReg(char regAddr);
wue 0:7b58cdacf811 109 /**
wue 0:7b58cdacf811 110 * Read a value from a pair of registers, return as int
wue 0:7b58cdacf811 111 * @param regAddr The address to read
wue 0:7b58cdacf811 112 * @return Value from 2 consecutive registers
wue 0:7b58cdacf811 113 */
wue 0:7b58cdacf811 114 int readVal(char regAddr);
wue 0:7b58cdacf811 115 /**
wue 0:7b58cdacf811 116 * Calculate the heading
wue 0:7b58cdacf811 117 * @return heading in degrees
wue 0:7b58cdacf811 118 */
wue 0:7b58cdacf811 119 float getHeading();
wue 0:7b58cdacf811 120 /**
wue 0:7b58cdacf811 121 * Perform a read on the X, Y and Z values.
wue 0:7b58cdacf811 122 * @param xVal Pointer to X value
wue 0:7b58cdacf811 123 * @param yVal Pointer to Y value
wue 0:7b58cdacf811 124 * @param zVal Pointer to Z value
wue 0:7b58cdacf811 125 */
wue 0:7b58cdacf811 126 void getValues(int *xVal, int *yVal, int *zVal);
wue 0:7b58cdacf811 127 /**
wue 0:7b58cdacf811 128 * Set the calibration parameters if required.
wue 0:7b58cdacf811 129 * @param minX Minimum value for X range
wue 0:7b58cdacf811 130 * @param maxX Maximum value for X range
wue 0:7b58cdacf811 131 * @param minY Minimum value for Y range
wue 0:7b58cdacf811 132 * @param maxY maximum value for Y range
wue 0:7b58cdacf811 133 */
wue 0:7b58cdacf811 134 void setCalibration(int minX, int maxX, int minY, int maxY);
wue 0:7b58cdacf811 135
wue 0:7b58cdacf811 136 private:
wue 0:7b58cdacf811 137 I2C _i2c;
wue 0:7b58cdacf811 138 int _i2c_address;
wue 0:7b58cdacf811 139 Serial *_pc;
wue 0:7b58cdacf811 140 bool _debug;
wue 0:7b58cdacf811 141 int _avgX, _avgY;
wue 0:7b58cdacf811 142
wue 0:7b58cdacf811 143 };
wue 0:7b58cdacf811 144 #endif