used with BBC microbit

Dependencies:   MotionSensor

Fork of MAG3110 by Jim Carver

Files at this revision

API Documentation at this revision

Comitter:
suntopbd
Date:
Mon Oct 29 19:00:45 2018 +0000
Parent:
6:1da3fe7b3510
Commit message:
none

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
diff -r 1da3fe7b3510 -r f246f14f8bd2 MAG3110.cpp
--- a/MAG3110.cpp	Fri May 16 18:19:17 2014 +0000
+++ b/MAG3110.cpp	Mon Oct 29 19:00:45 2018 +0000
@@ -1,151 +1,66 @@
 
 #include "MAG3110.h"
 #include "mbed.h"
-#include "MotionSensor.h"
 
 /******************************************************************************
  * Constructors
  ******************************************************************************/
-MAG3110::MAG3110(PinName sda, PinName scl): m_i2c(sda, scl), 
-    m_addr(0x1d)
+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()
 {
     char cmd[2];
 
     cmd[0] = MAG_CTRL_REG2;
     cmd[1] = 0x80;
-    m_i2c.write(m_addr, cmd, 2);
-
-}
+    _i2c.write(_i2c_address, cmd, 2);
 
-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);
+    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;
 }
 
-
-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)
+// Read a single byte form 8 bit register, return as int
+int MAG3110::readReg(char regAddr)
 {
     char cmd[1];
 
-    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;
-}
+    cmd[0] = regAddr;
+    _i2c.write(_i2c_address, cmd, 1);
 
-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);
+    cmd[0] = 0x00;
+    _i2c.read(_i2c_address, cmd, 1);
+    return (int)( cmd[0]);
 }
 
-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)
 {
     char cmd[2];
-    int16_t t;
+
     cmd[0] = regAddr;
-    if(_i2c.write(m_addr, cmd, 1)) {
-        printf("MAG3110 write error\r\n");
-        _i2c.stop();
-        _i2c.start();
-        }
+    _i2c.write(_i2c_address, cmd, 1);
 
     cmd[0] = 0x00;
     cmd[1] = 0x00;
-    _i2c.read(m_addr, cmd, 2);
-    t = (cmd[0] * 256) + (unsigned short) cmd[1];
-    return ((int) t); //concatenate the MSB and LSB
+    _i2c.read(_i2c_address, cmd, 2);
+    return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
 }
 
 
@@ -163,32 +78,14 @@
     *zVal = readVal(MAG_OUT_Z_MSB);
 }
 
-void MAG3110::ReadXYZ(float * mag)
-{
-    int x, y, z;
-    x = readVal(MAG_OUT_X_MSB);
-    y = readVal(MAG_OUT_Y_MSB);
-    z = readVal(MAG_OUT_Z_MSB);
-    mag[0] = (float) x / 10.0;
-    mag[1] = (float) y / 10.0;
-    mag[2] = (float) z / 10.0;
-    
-}
-
-void MAG3110::ReadXYZraw(int16_t * mag_raw)
-{
-    mag_raw[0] = readVal(MAG_OUT_X_MSB);
-    mag_raw[1] = readVal(MAG_OUT_Y_MSB);
-    mag_raw[2] = readVal(MAG_OUT_Z_MSB);
-}
 
 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
 {
     _avgX=(maxX+minX)/2;
     _avgY=(maxY+minY)/2;
 }
-*/
 
 
 
 
+
diff -r 1da3fe7b3510 -r f246f14f8bd2 MAG3110.h
--- a/MAG3110.h	Fri May 16 18:19:17 2014 +0000
+++ b/MAG3110.h	Mon Oct 29 19:00:45 2018 +0000
@@ -7,7 +7,6 @@
 #define MAG3110_H
 
 #include "mbed.h"
-#include "MotionSensor.h"
 
 #define PI 3.14159265359
 
@@ -21,7 +20,7 @@
 #define MAG_OUT_Y_LSB 0x04
 #define MAG_OUT_Z_MSB 0x05
 #define MAG_OUT_Z_LSB 0x06
-#define MAG_WHOAMI  0x07
+#define MAG_WHO_AM_I  0x07
 #define MAG_SYSMOD    0x08
 #define MAG_OFF_X_MSB 0x09
 #define MAG_OFF_X_LSB 0x0A
@@ -78,7 +77,7 @@
  * MAG3110 Class to read X/Y/Z data from the magentometer
  *
  */
-class MAG3110 : public MotionSensor
+class MAG3110
 {
 public:
     /**
@@ -88,27 +87,58 @@
      * @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.
+     * @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 m_i2c;
-  char m_addr;
-  int16_t getMagAxis(uint8_t addr);
-  void writeRegs(uint8_t * data, int len);
+    I2C _i2c;
+    int _i2c_address;
+    Serial *_pc;
+    bool _debug;
+    int _avgX, _avgY;
 
 };
 #endif