Simple library for MAG3110 magenetometer as built into Avnet Wi-Go module

Dependencies:   MotionSensor

Dependents:   Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more

Files at this revision

API Documentation at this revision

Comitter:
JimCarver
Date:
Fri May 16 18:19:17 2014 +0000
Parent:
5:f3abe901c33a
Commit message:
Implements new virtual MotionSensor class

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
MotionSensor.lib Show annotated file Show diff for this revision Revisions of this file
diff -r f3abe901c33a -r 1da3fe7b3510 MAG3110.cpp
--- a/MAG3110.cpp	Mon Apr 07 21:02:57 2014 +0000
+++ b/MAG3110.cpp	Fri May 16 18:19:17 2014 +0000
@@ -1,55 +1,133 @@
 
 #include "MAG3110.h"
 #include "mbed.h"
+#include "MotionSensor.h"
 
 /******************************************************************************
  * Constructors
  ******************************************************************************/
-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();
-}
-
-void MAG3110::begin()
+MAG3110::MAG3110(PinName sda, PinName scl): m_i2c(sda, scl), 
+    m_addr(0x1d)
 {
     char cmd[2];
 
     cmd[0] = MAG_CTRL_REG2;
     cmd[1] = 0x80;
-    _i2c.write(_i2c_address, cmd, 2);
+    m_i2c.write(m_addr, cmd, 2);
+
+}
 
-    cmd[0] = MAG_CTRL_REG1;
-    cmd[1] = MAG_3110_ACTIVE;
-    _i2c.write(_i2c_address, cmd, 2);
-    
-    // No adjustment initially
-    _avgX = 0;
-    _avgY = 0;
+void MAG3110::enable(void) {
+    uint8_t data[2];
+    readRegs( MAG_CTRL_REG1, &data[1], 1);
+    data[1] |= 0x01;
+    data[0] = MAG_CTRL_REG1;
+    writeRegs(data, 2);
 }
 
-// Read a single byte form 8 bit register, return as int
-int MAG3110::readReg(char regAddr)
+
+void MAG3110::disable(void) {
+    uint8_t data[2];
+    readRegs( MAG_CTRL_REG1, &data[1], 1);
+    data[1] &= 0xFE;
+    data[0] = MAG_CTRL_REG1;
+    writeRegs(data, 2);
+}
+
+
+void MAG3110::readRegs(int addr, uint8_t * data, int len)
 {
     char cmd[1];
 
-    cmd[0] = regAddr;
-    if(_i2c.write(_i2c_address, cmd, 1)) {
-        printf("MAG3110 write error\r\n");
-        _i2c.stop();
-        _i2c.start();
-        }
-    cmd[0] = 0x00;
-    _i2c.read(_i2c_address, cmd, 1);
-    return (int)( cmd[0]);
+    cmd[0] = addr;
+    m_i2c.write( m_addr, cmd, 1, true);
+    m_i2c.read( m_addr, (char *) data, len);
+    return;
+}
+
+
+void MAG3110::writeRegs(uint8_t * data, int len) {
+    m_i2c.write(m_addr, (char *)data, len);
+}
+
+
+uint32_t MAG3110::whoAmI() {
+    uint8_t who_am_i = 0;
+    readRegs(MAG_WHOAMI, &who_am_i, 1);
+    return (uint32_t) who_am_i;
+}
+
+uint32_t MAG3110::dataReady(void) {
+    uint8_t stat = 0;
+    readRegs(MAG_DR_STATUS, &stat, 1);
+    return (uint32_t) stat;
+}
+
+uint32_t MAG3110::sampleRate(uint32_t f) {
+    return(50); // for now sample rate is fixed at 50Hz
+}
+
+
+void MAG3110::getX(float * x) {
+    *x = (float(getMagAxis(MAG_OUT_X_MSB)) * 0.1f);
+}
+
+void MAG3110::getY(float * y) {
+    *y = (float(getMagAxis(MAG_OUT_Y_MSB)) * 0.1f);
+}
+
+void MAG3110::getZ(float * z) {
+    *z = (float(getMagAxis(MAG_OUT_Z_MSB)) * 0.1f);
+}
+
+void MAG3110::getX(int16_t * d) {
+    *d = getMagAxis(MAG_OUT_X_MSB);
 }
 
+void MAG3110::getY(int16_t * d) {
+    *d = getMagAxis(MAG_OUT_Y_MSB);
+}
+
+void MAG3110::getZ(int16_t * d) {
+    *d = getMagAxis(MAG_OUT_Z_MSB);
+}
+
+int16_t MAG3110::getMagAxis(uint8_t addr) {
+    int16_t acc;
+    uint8_t res[2];
+    readRegs(addr, res, 2);
+
+    acc = (res[0] << 8) | res[1];
+
+    return acc;
+}
+
+
+void MAG3110::getAxis(MotionSensorDataUnits &data) {
+    int16_t t[3];
+    uint8_t res[6];
+
+    readRegs(MAG_OUT_X_MSB, res, 6);
+    t[0] = (res[0] << 8) | res[1];
+    t[1] = (res[2] << 8) | res[3];
+    t[2] = (res[4] << 8) | res[5];
+    data.x = ((float) t[0]) * 0.1f;
+    data.y = ((float) t[1]) * 0.1f;
+    data.z = ((float) t[2]) * 0.1f;
+}
+
+
+void MAG3110::getAxis(MotionSensorDataCounts &data) {
+
+    uint8_t res[6];
+    readRegs(MAG_OUT_X_MSB, res, 6);
+
+    data.x = (res[0] << 8) | res[1];
+    data.y = (res[2] << 8) | res[3];
+    data.z = (res[4] << 8) | res[5];
+}
+/*
+
 // read a register per, pass first reg value, reading 2 bytes increments register
 // Reads MSB first then LSB
 int MAG3110::readVal(char regAddr)
@@ -57,7 +135,7 @@
     char cmd[2];
     int16_t t;
     cmd[0] = regAddr;
-    if(_i2c.write(_i2c_address, cmd, 1)) {
+    if(_i2c.write(m_addr, cmd, 1)) {
         printf("MAG3110 write error\r\n");
         _i2c.stop();
         _i2c.start();
@@ -65,7 +143,7 @@
 
     cmd[0] = 0x00;
     cmd[1] = 0x00;
-    _i2c.read(_i2c_address, cmd, 2);
+    _i2c.read(m_addr, cmd, 2);
     t = (cmd[0] * 256) + (unsigned short) cmd[1];
     return ((int) t); //concatenate the MSB and LSB
 }
@@ -109,8 +187,8 @@
     _avgX=(maxX+minX)/2;
     _avgY=(maxY+minY)/2;
 }
+*/
 
 
 
 
-
diff -r f3abe901c33a -r 1da3fe7b3510 MAG3110.h
--- a/MAG3110.h	Mon Apr 07 21:02:57 2014 +0000
+++ b/MAG3110.h	Fri May 16 18:19:17 2014 +0000
@@ -7,6 +7,7 @@
 #define MAG3110_H
 
 #include "mbed.h"
+#include "MotionSensor.h"
 
 #define PI 3.14159265359
 
@@ -20,7 +21,7 @@
 #define MAG_OUT_Y_LSB 0x04
 #define MAG_OUT_Z_MSB 0x05
 #define MAG_OUT_Z_LSB 0x06
-#define MAG_WHO_AM_I  0x07
+#define MAG_WHOAMI  0x07
 #define MAG_SYSMOD    0x08
 #define MAG_OFF_X_MSB 0x09
 #define MAG_OFF_X_LSB 0x0A
@@ -77,7 +78,7 @@
  * MAG3110 Class to read X/Y/Z data from the magentometer
  *
  */
-class MAG3110
+class MAG3110 : public MotionSensor
 {
 public:
     /**
@@ -87,72 +88,27 @@
      * @param addr addr of the I2C peripheral
      */
     MAG3110(PinName sda, PinName scl);
-    /**
-     * Debug version of constructor
-     * @param sda SDA pin
-     * @param sdl SCL pin
-     * @param addr Address of the I2C peripheral
-     * @param pc Serial 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, converted to microteslas.
-     * @paran mag Pointer to the 3 element array whare the results will be placed
-     */
-    void ReadXYZ(float * mag);
-    
-    /**
-     * Perform a read on the raw X, Y and Z values.
-     * @paran mag Pointer to the 3 element array whare the results will be placed
-     */
-    void ReadXYZraw(int16_t * mag_raw);
-    
-    /**
-     * 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);
 
+    void enable(void);
+    void disable(void);
+    uint32_t sampleRate(uint32_t fequency);
+    uint32_t whoAmI(void);
+    uint32_t dataReady(void);
+    void getX(int16_t * x);
+    void getY(int16_t * y);
+    void getZ(int16_t * z);
+    void getX(float * x);
+    void getY(float * y);
+    void getZ(float * z);
+    void getAxis(MotionSensorDataCounts &data);
+    void getAxis(MotionSensorDataUnits &data);
+    void readRegs(int addr, uint8_t * data, int len);
+  
 private:
-    I2C _i2c;
-    int _i2c_address;
-    Serial *_pc;
-    bool _debug;
-    int _avgX, _avgY;
-    int x, y, z;
+  I2C m_i2c;
+  char m_addr;
+  int16_t getMagAxis(uint8_t addr);
+  void writeRegs(uint8_t * data, int len);
 
 };
 #endif
diff -r f3abe901c33a -r 1da3fe7b3510 MotionSensor.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotionSensor.lib	Fri May 16 18:19:17 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/components/code/MotionSensor/#4d6e28d4a18a