Republished Library, to be refined for use with the SparkFun 9DOF in HARP project.
Fork of ADXL345 by
Diff: ADXL345.cpp
- Revision:
- 9:cc0260a2404b
- Parent:
- 8:4cdd4315189f
- Child:
- 10:d81793e01ec4
--- 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