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

Dependents:   9Dof_unit_testing

Fork of ADXL345 by James Watanabe

Revision:
8:4cdd4315189f
Parent:
7:839128c6e20a
Child:
9:cc0260a2404b
--- a/ADXL345.h	Thu Nov 01 18:46:24 2012 +0000
+++ b/ADXL345.h	Mon Nov 05 18:34:49 2012 +0000
@@ -1,5 +1,6 @@
 /**
  * @file ADXL345.h
+ * @author Tyler Weaver
  * @author Uwe Gartmann
  * @author Used ITG3200 library developed Peter Swanson as template
  * A special thanks to Ewout van Bekkum for all his patient help in developing this library!
@@ -28,10 +29,12 @@
  *
  * ADXL345, triple axis, I2C interface, accelerometer.
  *
+ * Modified for use with 9DOF sensor stick (no interupt pin access) on HARP project.
+ *
  * Datasheet:
  *
  * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
- */  
+ */
 
 
 
@@ -101,14 +104,14 @@
 #define ADXL345_SELF_TEST   0x80
 
 // read or write bytes
-#define ADXL345_READ    0xA7  
-#define ADXL345_WRITE   0xA6 
+#define ADXL345_READ    0xA7
+#define ADXL345_WRITE   0xA6
 #define ADXL345_ADDRESS 0x53   //the ADXL345 7-bit address is 0x53 when ALT ADDRESS is low as it is on the sparkfun chip: when ALT ADDRESS is high the address is 0x1D
 
 /////////////when ALT ADDRESS pin is high:
-//#define ADXL345_READ    0x3B   
+//#define ADXL345_READ    0x3B
 //#define ADXL345_WRITE   0x3A
-//#define ADXL345_ADDRESS 0x1D 
+//#define ADXL345_ADDRESS 0x1D
 
 #define ADXL345_X           0x00
 #define ADXL345_Y           0x01
@@ -120,7 +123,8 @@
 /**
  * ADXL345 triple axis accelerometer.
  */
-class ADXL345 {
+class ADXL345
+{
 
 public:
 
@@ -137,20 +141,20 @@
      * @param sck mbed pin to use for SCL line of I2C interface.
      */
     ADXL345(PinName sda, PinName scl);
-    
+
 
     /**
      * Constructor that accepts external i2c interface object.
      *
      * @param i2c The I2C interface object to use.
      */
-    ADXL345(I2C &i2c) : i2c_(i2c), myI2c(NULL){}
-    
+    ADXL345(I2C &i2c) : i2c_(i2c), myI2c(NULL) {}
+
     /**
      * Destructor that frees self-allocated I2C object.
      */
     ~ADXL345();
-    
+
     void init();
 
     /**
@@ -159,7 +163,7 @@
      * @param Pointer to a buffer to hold the accelerometer value for the
      *        x-axis, y-axis and z-axis [in that order].
      */
-    void getOutput(int* readings);
+    void getOutput(int16_t* readings);
 
     /**
      * Read the device ID register on the device.
@@ -167,41 +171,56 @@
      * @return The device ID code [0xE5]
      */
     char getDeviceID(void);
-    
-     /**
-     * Set the power mode.
-     *
-     * @param mode 0 -> Normal operation.
-     *             1 -> Reduced power operation.
-     */     
+
+    /**
+    * Set the power mode.
+    *
+    * @param mode 0 -> Normal operation.
+    *             1 -> Reduced power operation.
+    */
     int setPowerMode(char mode);
-  
-     /**
-     * Set the power control settings.
-     *
-     * See datasheet for details.
-     *
-     * @param The control byte to write to the POWER_CTL register.
-     */
-    int setPowerControl(char settings);     
-      /**
-     * Get the power control settings.
-     *
-     * See datasheet for details.
-     *
-     * @return The contents of the POWER_CTL register.
-     */
+
+    /**
+    * Get the BW_RATE register contents
+    *
+    * @returns The contents of BW_RATE register
+    */
+    char getBwRateReg();
+    
+    /**
+    * Set the BW_RATE register contents
+    *
+    * @param Byte to write to BW_RATE register
+    */
+    int setBwRateReg(char);
+
+    /**
+    * Set the power control settings.
+    *
+    * See datasheet for details.
+    *
+    * @param The control byte to write to the POWER_CTL register.
+    */
+    int setPowerControl(char settings);
+
+    /**
+    * Get the power control settings.
+    *
+    * See datasheet for details.
+    *
+    * @return The contents of the POWER_CTL register.
+    */
     char getPowerControl(void);
 
-       
+
     /**
      * Get the data format settings.
      *
      * @return The contents of the DATA_FORMAT register.
      */
-     
+
     char getDataFormatControl(void);
-    
+
     /**
      * Set the data format settings.
      *
@@ -213,17 +232,17 @@
      * Set the data format settings with only specified bits.
      *
      * @param settings The control byte to write to the DATA_FORMAT register.
-     * @param mask The mask bits that 
+     * @param mask The mask bits that
      */
     int setDataFormatControl(char settings, char mask, char *prev = NULL);
-  
+
     /**
      * Set the data rate.
      *
      * @param rate The rate code (see #defines or datasheet).
      */
     int setDataRate(char rate);
-    
+
 
     /**
      * Get the current offset for a particular axis.
@@ -234,7 +253,6 @@
      * @return The current offset as an 8-bit 2's complement number with scale
      *         factor 15.6mg/LSB.
      */
-     
     char getOffset(char axis);
 
     /**
@@ -247,28 +265,28 @@
      *               factor 15.6mg/LSB.
      */
     int setOffset(char axis, char offset);
-    
+
     /**
      * Get the FIFO control settings.
      *
      * @return The contents of the FIFO_CTL register.
      */
     char getFifoControl(void);
-    
+
     /**
      * Set the FIFO control settings.
      *
      * @param The control byte to write to the FIFO_CTL register.
      */
     int setFifoControl(char settings);
-    
+
     /**
      * Get FIFO status.
      *
      * @return The contents of the FIFO_STATUS register.
      */
     char getFifoStatus(void);
-    
+
     /**
      * Read the tap threshold on the device.
      *
@@ -346,7 +364,7 @@
      * @return The threshold value for detecting activity as an 8-bit number.
      *         Scale factor is 62.5mg/LSB.
      */
-     char getActivityThreshold(void);
+    char getActivityThreshold(void);
 
     /**
      * Set the threshold value for detecting activity.
@@ -364,7 +382,7 @@
      * @return The threshold value for detecting inactivity as an 8-bit number.
      *         Scale factor is 62.5mg/LSB.
      */
-     char getInactivityThreshold(void);
+    char getInactivityThreshold(void);
 
     /**
      * Set the threshold value for detecting inactivity.
@@ -381,8 +399,8 @@
      *         inactivity threshold for inactivity to be declared, in
      *         seconds.
      */
-     char getTimeInactivity(void);
-    
+    char getTimeInactivity(void);
+
     /**
      * Set the time required for inactivity to be declared.
      *
@@ -393,7 +411,7 @@
      *                   threshold inactivity.
      */
     int setTimeInactivity(char timeInactivity);
-    
+
     /**
      * Get the activity/inactivity control settings.
      *
@@ -411,8 +429,8 @@
      *
      * @return The contents of the ACT_INACT_CTL register.
      */
-     char getActivityInactivityControl(void);
-    
+    char getActivityInactivityControl(void);
+
     /**
      * Set the activity/inactivity control settings.
      *
@@ -431,25 +449,25 @@
      * @param settings The control byte to write to the ACT_INACT_CTL register.
      */
     int setActivityInactivityControl(char settings);
-    
+
     /**
      * Get the threshold for free fall detection.
      *
      * @return The threshold value for free-fall detection, as an 8-bit number,
      *         with scale factor 62.5mg/LSB.
      */
-     char getFreefallThreshold(void);
-    
+    char getFreefallThreshold(void);
+
     /**
      * Set the threshold for free fall detection.
      *
      * @return The threshold value for free-fall detection, as an 8-bit number,
-     *         with scale factor 62.5mg/LSB. A value of 0 may result in 
+     *         with scale factor 62.5mg/LSB. A value of 0 may result in
      *         undesirable behavior if the free-fall interrupt is enabled.
      *         Values between 300 mg and 600 mg (0x05 to 0x09) are recommended.
      */
     int setFreefallThreshold(char threshold);
-    
+
     /**
      * Get the time required to generate a free fall interrupt.
      *
@@ -457,19 +475,19 @@
      *         the freefall threshold to generate a free-fall interrupt, in
      *         milliseconds.
      */
-     char getFreefallTime(void);
-    
+    char getFreefallTime(void);
+
     /**
      * Set the time required to generate a free fall interrupt.
      *
      * @return The minimum time that the value of all axes must be less than
      *         the freefall threshold to generate a free-fall interrupt, in
      *         milliseconds. A value of 0 may result in undesirable behavior
-     *         if the free-fall interrupt is enabled. Values between 100 ms 
+     *         if the free-fall interrupt is enabled. Values between 100 ms
      *         and 350 ms (0x14 to 0x46) are recommended.
      */
     int setFreefallTime(short int freefallTime_ms);
-    
+
     /**
      * Get the axis tap settings.
      *
@@ -483,9 +501,9 @@
      * See datasheet for more details.
      *
      * @return The contents of the TAP_AXES register.
-     */ 
-     char getTapAxisControl(void);
-    
+     */
+    char getTapAxisControl(void);
+
     /**
      * Set the axis tap settings.
      *
@@ -501,57 +519,77 @@
      * @param The control byte to write to the TAP_AXES register.
      */
     int setTapAxisControl(char settings);
-    
+
     /**
      * Get the source of a tap.
      *
      * @return The contents of the ACT_TAP_STATUS register.
      */
-     char getTapSource(void);
-    
-     /**
-     * Get the interrupt enable settings.
-     *
-     * @return The contents of the INT_ENABLE register.
-     */
+    char getTapSource(void);
 
-     char getInterruptEnableControl(void);
-    
+    /**
+    * Get the interrupt enable settings.
+    *
+    * @return The contents of the INT_ENABLE register.
+    */
+
+    char getInterruptEnableControl(void);
+
     /**
      * Set the interrupt enable settings.
      *
      * @param settings The control byte to write to the INT_ENABLE register.
      */
     int setInterruptEnableControl(char settings);
-    
+
     /**
      * Get the interrupt mapping settings.
      *
      * @return The contents of the INT_MAP register.
      */
-     char getInterruptMappingControl(void);
-    
+    char getInterruptMappingControl(void);
+
     /**
      * Set the interrupt mapping settings.
      *
      * @param settings The control byte to write to the INT_MAP register.
      */
     int setInterruptMappingControl(char settings);
-    
+
     /**
      * Get the interrupt source.
      *
      * @return The contents of the INT_SOURCE register.
      */
-     char getInterruptSource(void);
-    
-   
+    char getInterruptSource(void);
+
+    /**
+    * Calibrate using the calibrate routine, sets the offset registers
+    *
+    * 100 samples at 100Hz (1 second)
+    * Sensor should be in x=0g,y=0g,z=1g orientation and held still
+    *
+    * @param pointer to timer object (should already be started)
+    * @param store_output true -> stores offsets in local file system, false -> only offset registers are set
+    * @param serial output (pc) for debugging  
+    */
+    void calibrate(Timer*, bool, Serial*);
+    /**
+    * Calibrate using the calibrate routine, sets the offset registers
+    *
+    * 100 samples at 100Hz (1 second)
+    * Sensor should be in x=0g,y=0g,z=1g orientation and held still
+    *
+    * @param pointer to timer object (should already be started)
+    * @param store_output true -> stores offsets in local file system, false -> only offset registers are set
+    */
+    void calibrate(Timer*, bool);
+
 private:
 
     I2C &i2c_;
-    
+
     I2C *myI2c;
-    
 
     /**
      * Read one byte from a register on the device.
@@ -589,6 +627,16 @@
      */
     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:
 
     /**
@@ -597,7 +645,7 @@
      *
      * Implemented here to make the compiler inline expand it.
      */
-    static int wordExtend(const char rx[2]){
+    static int wordExtend(const char rx[2]) {
         // Readings are expressed in 16bit 2's complement, so we must first
         // concatenate two bytes to make a word and sign extend it to obtain
         // correct negative values.
@@ -607,7 +655,6 @@
         // compilers if we want to keep it portable.
         return int16_t(((unsigned char)rx[1] << 8) | (unsigned char)rx[0]);
     }
-
 };
 
 #endif /* ADXL345_H */