library mma8452

Dependents:   APP3_Capteur_V2

Fork of MMA8452 by Ashley Mills

Files at this revision

API Documentation at this revision

Comitter:
ashleymills
Date:
Thu Mar 06 18:07:43 2014 +0000
Parent:
18:27d839e6dc0e
Child:
20:d55e9d7eb17e
Commit message:
Adding independent query methods;

Changed in this revision

MMA8452.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8452.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA8452.cpp	Wed Mar 05 17:04:04 2014 +0000
+++ b/MMA8452.cpp	Thu Mar 06 18:07:43 2014 +0000
@@ -21,6 +21,7 @@
 #include "mbed.h"
 
 #ifdef MMA8452_DEBUG
+// you need to define Serial pc(USBTX,USBRX) somewhere for the below line to make sense
 extern Serial pc;
 #define MMA8452_DBG(...) pc.printf(__VA_ARGS__); pc.printf("\r\n");
 #else
@@ -192,6 +193,39 @@
    return readRegister(MMA8452_OUT_X_MSB,dst,readLen);
 }
 
+int MMA8452::readXRaw(char *dst) {
+   if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+      return 1;
+   }
+   int readLen = 1;
+   if(_bitDepth==BIT_DEPTH_12) {
+      readLen = 2;
+   }
+   return readRegister(MMA8452_OUT_X_MSB,dst,readLen);
+}
+
+int MMA8452::readYRaw(char *dst) {
+   if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+      return 1;
+   }
+   int readLen = 1;
+   if(_bitDepth==BIT_DEPTH_12) {
+      readLen = 2;
+   }
+   return readRegister(MMA8452_OUT_Y_MSB,dst,readLen);
+}
+
+int MMA8452::readZRaw(char *dst) {
+   if(_bitDepth==BIT_DEPTH_UNKNOWN) {
+      return 1;
+   }
+   int readLen = 1;
+   if(_bitDepth==BIT_DEPTH_12) {
+      readLen = 2;
+   }
+   return readRegister(MMA8452_OUT_Z_MSB,dst,readLen);
+}
+
 int MMA8452::readXYZCounts(int *x, int *y, int *z) {
    char buf[6];
    if(readXYZRaw((char*)&buf)) {
@@ -210,6 +244,23 @@
    return 0;
 }
 
+int readXCount(int *x) {
+   char buf[2];
+   if(readXRaw((char*)&buf)) {
+       return 1;
+   }
+   if(_bitDepth==BIT_DEPTH_12) {
+     *x = twelveBitToSigned(&buf[0]);
+     *y = twelveBitToSigned(&buf[2]);
+     *z = twelveBitToSigned(&buf[4]);
+   } else {
+     *x = eightBitToSigned(&buf[0]);
+     *y = eightBitToSigned(&buf[1]);
+     *z = eightBitToSigned(&buf[2]);
+   }
+   
+}
+
 double MMA8452::convertCountToGravity(int count, int countsPerG) {
    return (double)count/(double)countsPerG;
 }
@@ -356,6 +407,10 @@
     return readRegister(addr,dst,1);
 }
 
+MMA8452::BitDepth MMA8452::getBitDepth() {
+   return _bitDepth;
+}
+
 #ifdef MMA8452_DEBUG
 void MMA8452::debugRegister(char reg) {
    // get register value
--- a/MMA8452.h	Wed Mar 05 17:04:04 2014 +0000
+++ b/MMA8452.h	Thu Mar 06 18:07:43 2014 +0000
@@ -186,15 +186,41 @@
        */ 
       int readXYZRaw(char *dst);
       
+      int readXRaw(char *dst);
+      int readYRaw(char *dst);
+      int readZRaw(char *dst);
+      
       /**
-       * Read the x,y, and z counts of the MMA7452.
+       * Read the x, y, and z counts of the MMA7452 axes.
+       * 
+       * Count resolution is either 8 bits or 12 bits, and the range is either +-2G, +-4G, or +-8G
+       * depending on settings.
+       * 
+       * This function queries the MMA8452 and returns the signed counts for each axes.
+       *
+       * @param x Pointer to integer to store x count
+       * @param y Pointer to integer to store y count
+       * @param z Pointer to integer to store z count
+       * @return 0 on success, 1 on failure
        */
       int readXYZCounts(int *x, int *y, int *z);
+      int readXCount(int *x);
+      int readYCount(int *y);
+      int readZCount(int *z);
+      
       int readXYZGravity(double *x, double *y, double *z);
       
+      
+      /// Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.
       int isXReady();
+      
+      /// Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.
       int isYReady();
+      
+      /// Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.
       int isZReady();
+      
+      /// Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.
       int isXYZReady();
             
       /**