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:
2:a8e20db7901e
Parent:
1:8997a1b348dd
Child:
3:89cb08cc663b
--- a/MMA7660.cpp	Tue Oct 16 19:42:19 2012 +0000
+++ b/MMA7660.cpp	Wed Oct 17 16:38:05 2012 +0000
@@ -1,9 +1,8 @@
 #include "MMA7660.h"
 
-MMA7660::MMA7660(PinName sda, PinName scl, PinName interrupt) : _i2c(sda, scl)
+MMA7660::MMA7660(PinName sda, PinName scl, bool active) : _i2c(sda, scl)
 {
-    _interrupt = interrupt;
-    active = false;
+    setActive(active);
     samplerate = 64;
 
 }
@@ -32,7 +31,6 @@
     write(MMA7660_MODE_R, modereg);
 }
 
-//Add timeout!
 void MMA7660::readData(int *data)
 {
     if (!active) {
@@ -55,6 +53,9 @@
             data[i] = (signed char)temp[i];
         }
     } while (alert);
+
+    if (!active)
+        setActive(false);
 }
 
 
@@ -66,17 +67,17 @@
         data[i] = intdata[i]/MMA7660_SENSITIVITY;
 }
 
-float MMA7660::getX( void )
+float MMA7660::x( void )
 {
     return getSingle(0);
 }
 
-float MMA7660::getY( void )
+float MMA7660::y( void )
 {
     return getSingle(1);
 }
 
-float MMA7660::getZ( void )
+float MMA7660::z( void )
 {
     return getSingle(2);
 }
@@ -97,36 +98,13 @@
 
     //Update the samplerate reg
     temp = read(MMA7660_SR_R);
-    temp &= ~0x07;               //Awake sample rate are lowest 3 bit
+    temp &= ~0x07;                                  //Awake sample rate are lowest 3 bit
     temp |= sampleLoc;
     write(MMA7660_SR_R, temp);
     this->samplerate = rates[sampleLoc];
     setActive(active);                              //Restore previous active state
 }
 
-MMA7660::Orientation MMA7660::getGlobalOrientation( void )
-{
-    int retval = MMA7660::Unknown;
-
-    int accelerations[3];
-    readData(accelerations);
-
-    //Check which side is up
-    int max = 0;
-    for (int i = 0; i<3; i++) {
-        if (-accelerations[i] > max) {
-            max = -accelerations[i];
-            retval = 2 * i;
-        }
-        if (accelerations[i] > max) {
-            max = accelerations[i];
-            retval = 2 * i + 1;
-        }
-    }
-    return (MMA7660::Orientation)retval;
-
-
-}
 
 MMA7660::Orientation MMA7660::getSide( void )
 {
@@ -159,17 +137,7 @@
     return MMA7660::Unknown;
 }
 
-bool MMA7660::isTapped( void )
-{
-    char tiltreg = read(MMA7660_TILT_R);
-    
-    //Tap is bit 5
-    tiltreg >>= 5;
-    tiltreg &= 0x01;
-    
-    return tiltreg==1;
-}
-    
+
 
 //////////////////////////////////////////////
 ///////////////PRIVATE////////////////////////
@@ -203,7 +171,6 @@
 {
     if (!active) {
         setActive(true);
-        active = true;
         wait(0.012 + 1/samplerate); //Wait until new sample is ready
     }
 
@@ -217,9 +184,10 @@
             alert = true;
         if (temp > 31)
             temp += 128+64;
-    }
+    } while (alert);
 
-    while (alert);
+    if (!active)
+        setActive(false);
 
     return temp / MMA7660_SENSITIVITY;
 }
\ No newline at end of file