Republished Library, to be refined for use with the SparkFun 9DOF in HARP project.

Dependents:   9Dof_unit_testing

Fork of ADXL345 by James Watanabe

Files at this revision

API Documentation at this revision

Comitter:
tylerjw
Date:
Tue Nov 06 16:40:36 2012 +0000
Parent:
8:4cdd4315189f
Child:
10:d81793e01ec4
Commit message:
added config.txt functionality to init()

Changed in this revision

ADXL345.cpp Show annotated file Show diff for this revision Revisions of this file
ADXL345.h Show annotated file Show diff for this revision Revisions of this file
--- a/ADXL345.cpp	Mon Nov 05 18:34:49 2012 +0000
+++ b/ADXL345.cpp	Tue Nov 06 16:40:36 2012 +0000
@@ -58,6 +58,7 @@
 ADXL345::ADXL345(PinName sda, PinName scl) : i2c_(*(new I2C(sda, scl)))
 {
     myI2c = &i2c_;
+    init();
 }
 
 ADXL345::~ADXL345()
@@ -68,14 +69,22 @@
 
 void ADXL345::init()
 {
-    // initialize the BW data rate
     setDataRate(ADXL345_6HZ25); // 6.25 Hz
 
-    //Data format (for +-16g) - This is done by setting Bit D3 of the DATA_FORMAT register (Address 0x31)
-    //and writing a value of 0x03 to the range bits (Bit D1 and Bit D0) of the DATA_FORMAT register (Address 0x31).
     setDataFormatControl(ADXL345_FULL_RES | ADXL345_2G); // full resolution, right justified, 2g range
 
-    // Set Offset  - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE.
+    LocalFileSystem local("local");
+    
+    if(FILE *fp = fopen("/local/config.txt", "r")) { // Open "config.txt" for reading - if it doesn't exist, pass over it
+        int8_t calibration_offset[3];
+        DigitalOut led(LED1);
+        led = 1;
+        fscanf(fp, "ADXL345 x:%d,y:%d,z:%d", &calibration_offset[0], &calibration_offset[1], &calibration_offset[2]);
+        fclose(fp);
+        for(char axis = 0x00; axis < 0x03; axis++)
+            setOffset(axis,calibration_offset[axis]);
+        led = 0;
+    }
 }
 
 
@@ -435,7 +444,7 @@
 
     // wait 11.1ms
     wait(0.0111);
-    
+
     pc->puts("Reading old register states... ");
     // read current register states
     char bw_rate = getBwRateReg();
@@ -458,7 +467,7 @@
     calibration_offset[0] = -1 * (data_avg[0] / 4); // x
     calibration_offset[1] = -1 * (data_avg[1] / 4); // y
     calibration_offset[2] = -1 * ((data_avg[2] - 256) / 4); // z
-    
+
     if(store_output) {
         pc->puts("Done!\r\nStoring output to file... ");
         LocalFileSystem local("local");
@@ -492,7 +501,7 @@
 
     // wait 11.1ms
     wait(0.0111);
-    
+
     // read current register states
     char bw_rate = getBwRateReg();
     char power_control = getPowerControl();
@@ -511,7 +520,7 @@
     calibration_offset[0] = -1 * (data_avg[0] / 4); // x
     calibration_offset[1] = -1 * (data_avg[1] / 4); // y
     calibration_offset[2] = -1 * ((data_avg[2] - 256) / 4); // z
-    
+
     if(store_output) {
         LocalFileSystem local("local");
         FILE *fp = fopen("/local/OFF_CAL.csv", "w"); // write
--- a/ADXL345.h	Mon Nov 05 18:34:49 2012 +0000
+++ b/ADXL345.h	Tue Nov 06 16:40:36 2012 +0000
@@ -148,7 +148,7 @@
      *
      * @param i2c The I2C interface object to use.
      */
-    ADXL345(I2C &i2c) : i2c_(i2c), myI2c(NULL) {}
+    ADXL345(I2C &i2c) : i2c_(i2c), myI2c(NULL) {init();}
 
     /**
      * Destructor that frees self-allocated I2C object.
@@ -626,19 +626,7 @@
      * @param size: The number of bytes to write.
      */
     int multiByteWrite(char startAddress, char* ptr_data, int size);
-
-    /**
-    * Sample 100 times and average
-    *
-    * @param period of sample rate
-    * @param array to hold raw data, should be int16_t[100][3] (sample,axis)
-    * @param array to hold averages, should be 3 in length
-    * @param pointer to timer object
-    */
-    void sample100avg(float, int16_t[][3], int16_t*, Timer*);
-
-private:
-
+    
     /**
      * Converts little-endian 2's complement byte pair to native byte order of
      * the CPU and then sign extend it to the CPU's register size.
@@ -655,6 +643,16 @@
         // compilers if we want to keep it portable.
         return int16_t(((unsigned char)rx[1] << 8) | (unsigned char)rx[0]);
     }
+    
+    /**
+    * Sample 100 times and average
+    *
+    * @param period of sample rate
+    * @param array to hold raw data, should be int16_t[100][3] (sample,axis)
+    * @param array to hold averages, should be 3 in length
+    * @param pointer to timer object
+    */
+    void sample100avg(float, int16_t[][3], int16_t*, Timer*);
 };
 
 #endif /* ADXL345_H */