Accelerometer driver from the MMA7660FC

Files at this revision

API Documentation at this revision

Comitter:
c1728p9
Date:
Tue Jul 23 21:39:37 2019 +0000
Parent:
0:cc40c3196635
Commit message:
Correctly handle negative acceleration values

Changed in this revision

MMA7660.h Show annotated file Show diff for this revision Revisions of this file
MMA7660FC.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MMA7660.h	Fri Sep 05 23:14:35 2014 +0000
+++ b/MMA7660.h	Tue Jul 23 21:39:37 2019 +0000
@@ -65,6 +65,7 @@
     void setMode(uint8_t mode);
     void setSampleRate(uint8_t rate);
     void getXYZ(int8_t *x,int8_t *y,int8_t *z);
+    void getSignedXYZ(int8_t *x,int8_t *y,int8_t *z);
     void getAcceleration(float *ax,float *ay,float *az);
 };
 
--- a/MMA7660FC.cpp	Fri Sep 05 23:14:35 2014 +0000
+++ b/MMA7660FC.cpp	Tue Jul 23 21:39:37 2019 +0000
@@ -119,11 +119,24 @@
     *z = ((char)(val[2]<<2))/4;
 }
 
+void MMA7660::getSignedXYZ(int8_t *x,int8_t *y,int8_t *z)
+{
+    int8_t val[3];
+    getXYZ(val + 0, val + 1, val + 2);
+    
+    for (size_t i = 0; i < sizeof(val); i++) {
+        val[i] = val[i] >= 32 ? val[i] - 64 : val[i];
+    }
+    *x = val[0];
+    *y = val[1];
+    *z = val[2];
+}
+
 void MMA7660::getAcceleration(float *ax,float *ay,float *az)
 {
     int8_t x,y,z;
-    getXYZ(&x,&y,&z);
-    *ax = x/21.00;
-    *ay = y/21.00;
-    *az = z/21.00;
+    getSignedXYZ(&x,&y,&z);
+    *ax = x/21.33;
+    *ay = y/21.33;
+    *az = z/21.33;
 }