Library for the MMA7660 triple axis accelerometer

Dependents:   Websocket_Ethernet_acc app-board-Sprint-WS-Acc app-board-Ethernet-Websocket app-board-Wifly-Websocket ... more

Revision:
3:89cb08cc663b
Parent:
2:a8e20db7901e
Child:
4:36a163511e34
--- a/MMA7660.cpp	Wed Oct 17 16:38:05 2012 +0000
+++ b/MMA7660.cpp	Tue May 13 18:12:59 2014 +0000
@@ -4,7 +4,6 @@
 {
     setActive(active);
     samplerate = 64;
-
 }
 
 //Since the MMA lacks a WHO_AM_I register, we can only check if there is a device that answers to the I2C address
@@ -18,6 +17,7 @@
 
 void MMA7660::setActive(bool state)
 {
+    active = state;
     char modereg = read(MMA7660_MODE_R);
     modereg &= ~(1<<0);
 
@@ -33,9 +33,9 @@
 
 void MMA7660::readData(int *data)
 {
+    bool active_old = active;
     if (!active) {
         setActive(true);
-        active = true;
         wait(0.012 + 1/samplerate); //Wait until new sample is ready, my experience is that 1/samplerate isnt needed, but datasheet says so
     }
 
@@ -54,7 +54,7 @@
         }
     } while (alert);
 
-    if (!active)
+    if (!active_old)
         setActive(false);
 }
 
@@ -85,6 +85,7 @@
 
 void MMA7660::setSampleRate(int samplerate)
 {
+    bool active_old = active;
     setActive(false);                               //Not allowed to be active to change anything
     int rates[] = {120, 64, 32, 16, 8, 4, 2, 1};    //Alowed samplerates (and their number in array is also number required for MMA)
     int sampleLoc = 0, sampleError = 10000, temp;
@@ -102,14 +103,14 @@
     temp |= sampleLoc;
     write(MMA7660_SR_R, temp);
     this->samplerate = rates[sampleLoc];
-    setActive(active);                              //Restore previous active state
+    setActive(active_old);                              //Restore previous active state
 }
 
 
 MMA7660::Orientation MMA7660::getSide( void )
 {
     char tiltreg = read(MMA7660_TILT_R);
-
+    printf("Reg = %d\r\n", tiltreg);
     //We care about 2 LSBs
     tiltreg &= 0x03;
     if (tiltreg == 0x01)
@@ -169,6 +170,7 @@
 
 float MMA7660::getSingle( int number )
 {
+    bool active_old = active;
     if (!active) {
         setActive(true);
         wait(0.012 + 1/samplerate); //Wait until new sample is ready
@@ -186,7 +188,7 @@
             temp += 128+64;
     } while (alert);
 
-    if (!active)
+    if (!active_old)
         setActive(false);
 
     return temp / MMA7660_SENSITIVITY;