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.
Dependents: Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more
Revision 1:5a0e7a58d980, committed 2013-05-24
- Comitter:
- SomeRandomBloke
- Date:
- Fri May 24 20:04:48 2013 +0000
- Parent:
- 0:63a8594a3866
- Child:
- 2:fb8024297377
- Commit message:
- updates to constructors, document functions and cleanup
Changed in this revision
| MAG3110.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MAG3110.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MAG3110.cpp Sun May 19 20:34:27 2013 +0000
+++ b/MAG3110.cpp Fri May 24 20:04:48 2013 +0000
@@ -5,16 +5,33 @@
/******************************************************************************
* Constructors
******************************************************************************/
-MAG3110::MAG3110(I2C *i2c):
- _i2c_address(0x1D), _i2c(i2c),_pc(NULL), _debug(false)
+MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
+ _i2c_address(0x1D), _pc(NULL), _debug(false)
{
+ begin();
+}
+MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
+ _i2c_address(0x1D), _pc(pc), _debug(true)
+{
+ begin();
}
-MAG3110::MAG3110(I2C *i2c, Serial *pc):
- _i2c_address(0x1D), _i2c(i2c), _pc(pc), _debug(true)
+void MAG3110::begin()
{
+ char cmd[2];
+ cmd[0] = MAG_CTRL_REG2;
+ cmd[1] = 0x80;
+ _i2c.write(_i2c_address, cmd, 2);
+
+ cmd[0] = MAG_CTRL_REG1;
+ cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
+ _i2c.write(_i2c_address, cmd, 2);
+
+ // No adjustment initially
+ _avgX = 0;
+ _avgY = 0;
}
// Read a single byte form 8 bit register, return as int
@@ -23,10 +40,10 @@
char cmd[1];
cmd[0] = regAddr;
- _i2c->write(_i2c_address, cmd, 1);
+ _i2c.write(_i2c_address, cmd, 1);
cmd[0] = 0x00;
- _i2c->read(_i2c_address, cmd, 1);
+ _i2c.read(_i2c_address, cmd, 1);
return (int)( cmd[0]);
}
@@ -38,28 +55,15 @@
char cmd[2];
cmd[0] = regAddr;
- _i2c->write(_i2c_address, cmd, 1);
+ _i2c.write(_i2c_address, cmd, 1);
cmd[0] = 0x00;
cmd[1] = 0x00;
- _i2c->read(_i2c_address, cmd, 2);
+ _i2c.read(_i2c_address, cmd, 2);
return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
}
-void MAG3110::begin()
-{
- char cmd[2];
-
- cmd[0] = MAG_CTRL_REG2;
- cmd[1] = 0x80;
- _i2c->write(_i2c_address, cmd, 2);
-
- cmd[0] = MAG_CTRL_REG1;
- cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
- _i2c->write(_i2c_address, cmd, 2);
-}
-
float MAG3110::getHeading()
{
int xVal = readVal(MAG_OUT_X_MSB);
--- a/MAG3110.h Sun May 19 20:34:27 2013 +0000
+++ b/MAG3110.h Fri May 24 20:04:48 2013 +0000
@@ -1,5 +1,5 @@
-/*
- * MAG3110 Sensor Library for mbed
+/*
+ * MAG3110 Sensor Library for mbed
* TODO: Add proper header
*/
@@ -75,22 +75,66 @@
class MAG3110
{
- private:
- short _i2c_address;
- I2C *_i2c;
- Serial *_pc;
- bool _debug;
- int _avgX, _avgY; //, _newX, _tempXmin, _tempXmax, _newY, _tempYmin, _tempYmax;
+private:
+ int _i2c_address;
+ I2C _i2c;
+ Serial *_pc;
+ bool _debug;
+ int _avgX, _avgY;
- public:
- MAG3110(I2C *i2c);
- MAG3110(I2C *i2c, Serial *pc); //pass serial for debug
- void begin();
- int readReg(char regAddr);
- int readVal(char regAddr);
- float getHeading();
- void getValues(int *xVal, int *yVal, int *zVal);
- void setCalibration(int minX, int maxX, int minY, int maxY);
+public:
+ /**
+ * MAG3110 constructor
+ * @param sda SDA pin
+ * @param sdl SCL pin
+ * @param addr addr of the I2C peripheral
+ */
+ MAG3110(PinName sda, PinName scl);
+ /**
+ * MAG3110 constructor
+ * @param sda SDA pin
+ * @param sdl SCL pin
+ * @param addr addr of the I2C peripheral
+ * @param pc serial port object to output debug messages
+ */
+ MAG3110(PinName sda, PinName scl, Serial *pc); //pass serial for debug
+ /**
+ * Setup the Magnetometer
+ *
+ */
+ void begin();
+ /**
+ * Read a register, return its value as int
+ * @param regAddr The address to read
+ * @return value in register
+ */
+ int readReg(char regAddr);
+ /**
+ * Read a value from a pair of registers, return as int
+ * @param regAddr The address to read
+ * @return Value from 2 consecutive registers
+ */
+ int readVal(char regAddr);
+ /**
+ * Calculate the heading
+ * @return heading in degrees
+ */
+ float getHeading();
+ /**
+ * Perform a read on the X, Y and Z values.
+ * @param xVal Pointer to X value
+ * @param yVal Pointer to Y value
+ * @param zVal Pointer to Z value
+ */
+ void getValues(int *xVal, int *yVal, int *zVal);
+ /**
+ * Set the calibration parameters if required.
+ * @param minX Minimum value for X range
+ * @param maxX Maximum value for X range
+ * @param minY Minimum value for Y range
+ * @param maxY maximum value for Y range
+ */
+ void setCalibration(int minX, int maxX, int minY, int maxY);
};
-#endif
\ No newline at end of file
+#endif
NXP MAG3110 Magnetometer