Modified version of default library

Fork of MMA8451Q8 by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
Raiden817
Date:
Wed Mar 08 16:27:12 2017 +0000
Parent:
7:7812354ef684
Commit message:
Modified to allow gSelect and standby mode and active mode are now functions

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 16:27:12 2017 +0000
@@ -33,7 +33,12 @@
 #define MAX_4G            0x01
 #define MAX_8G            0x02
 
+#define NUMDATA           2
 #define GSCALING          1024.0
+#define ADDRESSINDEX      0
+#define DATAINDEX         1
+#define ACTIVEMASK        0x01
+
 
 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
     // activate the peripheral
@@ -48,15 +53,31 @@
     readRegs(REG_WHO_AM_I, &who_am_i, 1);
     return who_am_i;
 }
-void MMA8451Q::setGLimit() {   
-    uint8_t data[2] = {REG_CTRL_REG_1, 0x00};
+void MMA8451Q::setGLimit(int g) {   
+    uint8_t data[2] = {REG_CTRL_REG_1, 0x00}; 
+    SetStandByMode();
+    data[ADDRESSINDEX] = XYZ_DATA_CFG;
+    data[DATAINDEX] = g
+    writeRegs(data, 2);// change g limit  
+    SetActiveMode();
+}
+
+void MMA8451Q::SetStandByMode() {
+    uint8_t registerData[1];
+    uint8_t data[NUMDATA] = {REG_CTRL_REG_1, 0x00};
+    
+    readRegs(REG_CTRL_REG_1, registerData, 1);
+    data[1] = registerData[0] & ~ACTIVEMASK;
     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
+}
+
+void MMA8451Q::SetActiveMode() {
+    uint8_t registerData[1];
+    uint8_t data[2] = {REG_CTRL_REG_1, 0x00};
+    
+    readRegs(REG_CTRL_REG_1, registerData, 1);
+    data[1] = registerData[0] | ACTIVEMASK;
+    writeRegs(data, NUMDATA); // put in standby
 }
 
 float MMA8451Q::getAccX() {
--- a/MMA8451Q8.h	Wed Feb 22 15:35:47 2017 +0000
+++ b/MMA8451Q8.h	Wed Mar 08 16:27:12 2017 +0000
@@ -101,9 +101,11 @@
 
   I2C m_i2c;
   int m_addr;
+  void SetStandByMode();
+  void SetActiveMode();
   void readRegs(int addr, uint8_t * data, int len);
   void writeRegs(uint8_t * data, int len);
-  void setGLimit(); 
+  void setGLimit(int g); 
   
 private: 
   int16_t getAccAxis(uint8_t addr);