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