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

Files at this revision

API Documentation at this revision

Comitter:
Sissors
Date:
Tue Oct 16 19:42:19 2012 +0000
Parent:
0:7bc29a9ea016
Child:
2:a8e20db7901e
Commit message:
Tapping doesnt work yet

Changed in this revision

MMA7660.cpp Show annotated file Show diff for this revision Revisions of this file
MMA7660.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA7660.cpp	Sun Oct 14 08:02:53 2012 +0000
+++ b/MMA7660.cpp	Tue Oct 16 19:42:19 2012 +0000
@@ -4,7 +4,8 @@
 {
     _interrupt = interrupt;
     active = false;
-    samplerate = 120;
+    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
@@ -37,6 +38,7 @@
     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
     }
 
     char temp[3];
@@ -55,6 +57,7 @@
     } while (alert);
 }
 
+
 void MMA7660::readData(float *data)
 {
     int intdata[3];
@@ -63,17 +66,115 @@
         data[i] = intdata[i]/MMA7660_SENSITIVITY;
 }
 
-float MMA7660::getX( void ) {
+float MMA7660::getX( void )
+{
     return getSingle(0);
+}
+
+float MMA7660::getY( void )
+{
+    return getSingle(1);
+}
+
+float MMA7660::getZ( void )
+{
+    return getSingle(2);
+}
+
+
+void MMA7660::setSampleRate(int samplerate)
+{
+    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;
+    for (int i = 0; i<8; i++) {
+        temp = abs( rates[i] - samplerate );
+        if (temp<sampleError) {
+            sampleLoc = i;
+            sampleError=temp;
+        }
     }
 
-float MMA7660::getY( void ) {
-    return getSingle(1);
+    //Update the samplerate reg
+    temp = read(MMA7660_SR_R);
+    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 )
+{
+    char tiltreg = read(MMA7660_TILT_R);
+
+    //We care about 2 LSBs
+    tiltreg &= 0x03;
+    if (tiltreg == 0x01)
+        return MMA7660::Front;
+    if (tiltreg == 0x02)
+        return MMA7660::Back;
+    return MMA7660::Unknown;
+}
+
+MMA7660::Orientation MMA7660::getOrientation( void )
+{
+    char tiltreg = read(MMA7660_TILT_R);
+
+    //We care about bit 2, 3 and 4 (counting from zero)
+    tiltreg &= 0x07<<2;
+    tiltreg >>= 2;
+    if (tiltreg == 0x01)
+        return MMA7660::Left;
+    if (tiltreg == 0x02)
+        return MMA7660::Right;
+    if (tiltreg == 0x05)
+        return MMA7660::Down;
+    if (tiltreg == 0x06)
+        return MMA7660::Up;
+    return MMA7660::Unknown;
+}
+
+bool MMA7660::isTapped( void )
+{
+    char tiltreg = read(MMA7660_TILT_R);
     
-float MMA7660::getZ( void ) {
-    return getSingle(2);
-    }        
+    //Tap is bit 5
+    tiltreg >>= 5;
+    tiltreg &= 0x01;
+    
+    return tiltreg==1;
+}
+    
+
+//////////////////////////////////////////////
+///////////////PRIVATE////////////////////////
+//////////////////////////////////////////////
+
 
 void MMA7660::write(char address, char data)
 {
@@ -103,6 +204,7 @@
     if (!active) {
         setActive(true);
         active = true;
+        wait(0.012 + 1/samplerate); //Wait until new sample is ready
     }
 
     signed char temp;
--- a/MMA7660.h	Sun Oct 14 08:02:53 2012 +0000
+++ b/MMA7660.h	Tue Oct 16 19:42:19 2012 +0000
@@ -10,11 +10,28 @@
 #define MMA7660_XOUT_R      0x00
 #define MMA7660_YOUT_R      0x01
 #define MMA7660_ZOUT_R      0x02
+#define MMA7660_TILT_R      0x03
+#define MMA7660_INT_R       0x06
 #define MMA7660_MODE_R      0x07
+#define MMA7660_SR_R        0x08
+
 
 class MMA7660
 {
 public:
+    /** 
+    * The 6 different orientations and unknown
+    *
+    * Up & Down = X-axis
+    * Right & Left = Y-axis
+    * Back & Front = Z-axis
+    * 
+    */
+    enum Orientation{Up, Down,
+                    Right, Left,
+                    Back, Front,
+                    Unknown};
+    
     /**
     * Creates a new MMA7660 object
     *
@@ -54,29 +71,75 @@
     void readData( int *data);
     void readData( float *data);
     
-    /** Get X-data
+    /** 
+    * Get X-data
     *
     * @param return - X-acceleration in g's
     */
     float getX( void );
     
-    /** Get Y-data
+    /** 
+    * Get Y-data
     *
     * @param return - Y-acceleration in g's
     */
     float getY( void );
     
-    /** Get Z-data
+    /** 
+    * Get Z-data
     *
     * @param return - Z-acceleration in g's
     */
-    float getZ( void );        
+    float getZ( void );
+    
+    /** 
+    * Sets the active samplerate
+    *
+    * The entered samplerate will be rounded to nearest supported samplerate.
+    * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second.
+    *
+    * @param samplerate - the samplerate that will be set
+    */
+    void setSampleRate(int samplerate);        
+
+    /**
+    * Returns which side is pointing down
+    *
+    * @param return - Orientation which is closest to down
+    */    
+    Orientation getGlobalOrientation( void );
+    
+    /**
+    * Returns if it is on its front, back, or unknown side
+    *
+    * This is read from MMA7760s registers, page 12 of datasheet
+    *
+    * @param return - Front, Back or Unknown orientation
+    */
+    Orientation getSide( void );
+    
+    /**
+    * Returns if it is on it left, right, down or up side
+    *
+    * This is read from MMA7760s registers, page 12 of datasheet
+    *
+    * @param return - Left, Right, Down, Up or Unknown orientation
+    */
+    Orientation getOrientation ( void );
+    
+    /**
+    * Returns if since last check there has been a tap
+    *
+    * @param return - bool that is true when a tap has been detected
+    */    
+    bool isTapped( void );
+
 
 
 private:
 
     /**
-    * Writes data to the device, could be private, but public is handy so you can transmit directly to the MPU.
+    * Writes data to the device
     *
     * @param adress - register address to write to
     * @param data - data to write
@@ -84,7 +147,7 @@
     void write( char address, char data);
 
     /**
-    * Read data from the device, could be private, but public is handy so you can transmit directly to the MPU.
+    * Read data from the device
     *
     * @param adress - register address to write to
     * @return - data from the register specified by RA