Glimit

Fork of MMA8451Q8 by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
mireyarod23
Date:
Wed Mar 08 05:15:45 2017 +0000
Parent:
7:7812354ef684
Commit message:
Attempt;

Changed in this revision

MMA8451Q8.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8451Q8.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA8451Q8.cpp	Wed Feb 22 15:35:47 2017 +0000
+++ b/MMA8451Q8.cpp	Wed Mar 08 05:15:45 2017 +0000
@@ -33,7 +33,12 @@
 #define MAX_4G            0x01
 #define MAX_8G            0x02
 
+#define NUM_DATA            2
 #define GSCALING          1024.0
+#define ADDRESS_INDEX       0
+#define DATA_INDEX          1
+
+float gScaling[3] = {4095.0,2048.0,1024.0}; //scaling for acceleration of gravity values
 
 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
     // activate the peripheral
@@ -48,27 +53,51 @@
     readRegs(REG_WHO_AM_I, &who_am_i, 1);
     return who_am_i;
 }
-void MMA8451Q::setGLimit() {   
+
+void MMA8451Q::setStandbyMode()
+{
+    #define ACTIVEMASK 0x01
+    uint8_t registerData[1];
+    uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
+    
+    readRegs(REG_CTRL_REG_1, registerData, 1);
+    data[1] = registerData[0] & ~ACTIVEMASK;
+    writeRegs(data, NUM_DATA); //put in standby
+}
+
+void MMA8451Q::setActiveMode()
+{
+    #define ACTIVEMASK 0x01
+    uint8_t registerData[1];
+    uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
+    
+    readRegs(REG_CTRL_REG_1, registerData, 1);
+    data[1] = registerData[0] | ACTIVEMASK;
+    writeRegs(data, NUM_DATA);    
+}
+
+//seGLimit obtains index of the bit that needs to be changed
+void MMA8451Q::setGLimit(int gSelect) {   
     uint8_t data[2] = {REG_CTRL_REG_1, 0x00};
-    writeRegs(data, 2); // put in standby
-    data[0] = XYZ_DATA_CFG;
-    data[1] = MAX_8G;
-    writeRegs(data, 2);// change g limit
-    data[0] = REG_CTRL_REG_1;
-    data[1] = 0x01;
-    writeRegs(data, 2); // make active
+    gChosen = gSelect;
+    setStandbyMode();
+    
+    data[ADDRESS_INDEX ] = XYZ_DATA_CFG;
+    data[DATA_INDEX] = gChosen;
+    writeRegs(data, 2);
+    setActiveMode();
 }
 
 float MMA8451Q::getAccX() {
-    return (float(getAccAxis(REG_OUT_X_MSB))/GSCALING);
+    return (float(getAccAxis(REG_OUT_X_MSB))/gScaling[gChosen]);
 }
 
 float MMA8451Q::getAccY() {
-    return (float(getAccAxis(REG_OUT_Y_MSB))/GSCALING);
+    return (float(getAccAxis(REG_OUT_Y_MSB))/gScaling[gChosen]);
 }
 
 float MMA8451Q::getAccZ() {
-    return (float(getAccAxis(REG_OUT_Z_MSB))/GSCALING);
+    return (float(getAccAxis(REG_OUT_Z_MSB))/gScaling[gChosen]);
 }
 
 void MMA8451Q::getAccAllAxis(float * res) {
--- a/MMA8451Q8.h	Wed Feb 22 15:35:47 2017 +0000
+++ b/MMA8451Q8.h	Wed Mar 08 05:15:45 2017 +0000
@@ -97,16 +97,22 @@
    * @param res array where acceleration data will be stored
    */
   void getAccAllAxis(float * res);
-
-
+  
+  int16_t getAccAxis(uint8_t addr);
+  
   I2C m_i2c;
   int m_addr;
+  int gChosen;
+  
+  /** Allow user to get max g's **/
   void readRegs(int addr, uint8_t * data, int len);
   void writeRegs(uint8_t * data, int len);
-  void setGLimit(); 
+  void setGLimit(int gSelect);
+  void setStandbyMode();
+  void setActiveMode(); 
   
 private: 
-  int16_t getAccAxis(uint8_t addr);
+ 
 
 };