Homework 8.1, which changes a library to set the accelerometer to standy mode temporarily via bit masking in order to change g limits.

Fork of MMA8451Q8 by Stanley Cohen

Revision:
8:ebf150f92734
Parent:
7:7812354ef684
--- a/MMA8451Q8.cpp	Wed Feb 22 15:35:47 2017 +0000
+++ b/MMA8451Q8.cpp	Mon Mar 06 21:17:38 2017 +0000
@@ -34,6 +34,10 @@
 #define MAX_8G            0x02
 
 #define GSCALING          1024.0
+#define NUM_DATA            2
+#define ADDRESS_INDEX       0
+#define DATA_INDEX          1
+#define ACTIVEMASK          0x01
 
 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
     // activate the peripheral
@@ -43,20 +47,67 @@
 
 MMA8451Q::~MMA8451Q() { }
 
+void MMA8451Q::setStandbyMode(){
+    //create unsigned integer to hold bits
+    uint8_t registerData[1];
+    //create unsigned int array with 2 spots
+    //that will hold the registry control bits and 8 zeroes
+    uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
+    
+    //read the registry control data into the registerData array
+    readRegs(REG_CTRL_REG_1, registerData, 1);
+    //bitwise AND operation done between the register data being read,
+    //and the flipped (~) ACTIVEMASK (00000001 becomes 11111110 before
+    //being compared). The data is then set into the data array
+    data[1] = registerData[0] & ~ACTIVEMASK;
+    //write the new value back into the registry. The only thing that
+    //has changed here is that the last bit has been switched to a 0
+    //to put the device in standby mode with the bitmask.
+    writeRegs(data,NUM_DATA);
+}
+
+void MMA8451Q::setActiveMode(){
+    //create unsigned integer to hold bits
+    uint8_t registerData[1];
+    //create unsigned int array with 2 spots
+    //that will hold the registry control bits and 8 zeroes
+    uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
+    
+    //read the registry control data into the registerData array
+    readRegs(REG_CTRL_REG_1, registerData, 1);
+    //bitwise OR operation done between the register data being read,
+    //and ACTIVEMASK (00000001). The data is then set into the data array
+    data[1] = registerData[0] | ACTIVEMASK;
+    //write the new value back into the registry. The only thing that
+    //has changed here is that the last bit has been switched to a 1
+    //to put the device in active mode with the bitmask.
+    writeRegs(data,NUM_DATA);
+}
+
 uint8_t MMA8451Q::getWhoAmI() {
     uint8_t who_am_i = 0;
     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};
-    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::setGLimit(int gSelect) {   
+    uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
+    
+    //incoming gSelect integer used to select g level
+    gChosen = gSelect;
+    
+    setStandbyMode(); //put in standby to select g mode
+    
+    //writeRegs(data, 2);
+    
+    data[ADDRESS_INDEX] = XYZ_DATA_CFG;
+    data[DATA_INDEX] = gChosen;
+    
+    //writeRegs(data, 2);// change g limit
+    //data[0] = REG_CTRL_REG_1;
+    //data[1] = 0x01;
+    
+    writeRegs(data, 2); //write new g level to register
+    setActiveMode(); //make active again
 }
 
 float MMA8451Q::getAccX() {