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

Dependents:   9Dof_unit_testing

Fork of ADXL345 by James Watanabe

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Digixx 0:1e4aa22fc1a1 1 /**
gltest26 3:7b83694c7292 2 * @file ADXL345.cpp
tylerjw 8:4cdd4315189f 3 * @author Tyler Weaver
Digixx 0:1e4aa22fc1a1 4 * @author Peter Swanson
Digixx 0:1e4aa22fc1a1 5 * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because
Digixx 0:1e4aa22fc1a1 6 * today, religion is thought of as something that you do or believe and has about as
Digixx 0:1e4aa22fc1a1 7 * little impact on a person as their political stance. But for me, God gives me daily
Digixx 0:1e4aa22fc1a1 8 * strength and has filled my life with the satisfaction that I could never find in any
tylerjw 6:5fb29534a6cf 9 * of the other things that I once looked for it in.
Digixx 0:1e4aa22fc1a1 10 * If your interested, heres verse that changed my life:
Digixx 0:1e4aa22fc1a1 11 * Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus,
Digixx 0:1e4aa22fc1a1 12 * because through Christ Jesus, the law of the Spirit who gives life has set
tylerjw 6:5fb29534a6cf 13 * me free from the law of sin (which brings...) and death. For what the law
Digixx 0:1e4aa22fc1a1 14 * was powerless to do in that it was weakened by the flesh, God did by sending
Digixx 0:1e4aa22fc1a1 15 * His own Son in the likeness of sinful flesh to be a sin offering. And so He
tylerjw 6:5fb29534a6cf 16 * condemned sin in the flesh in order that the righteous requirements of the
Digixx 0:1e4aa22fc1a1 17 * (God's) law might be fully met in us, who live not according to the flesh
Digixx 0:1e4aa22fc1a1 18 * but according to the Spirit."
Digixx 0:1e4aa22fc1a1 19 *
Digixx 0:1e4aa22fc1a1 20 * A special thanks to Ewout van Bekkum for all his patient help in developing this library!
Digixx 0:1e4aa22fc1a1 21 *
Digixx 0:1e4aa22fc1a1 22 * @section LICENSE
Digixx 0:1e4aa22fc1a1 23 *
Digixx 0:1e4aa22fc1a1 24 * Permission is hereby granted, free of charge, to any person obtaining a copy
Digixx 0:1e4aa22fc1a1 25 * of this software and associated documentation files (the "Software"), to deal
Digixx 0:1e4aa22fc1a1 26 * in the Software without restriction, including without limitation the rights
Digixx 0:1e4aa22fc1a1 27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Digixx 0:1e4aa22fc1a1 28 * copies of the Software, and to permit persons to whom the Software is
Digixx 0:1e4aa22fc1a1 29 * furnished to do so, subject to the following conditions:
Digixx 0:1e4aa22fc1a1 30 *
Digixx 0:1e4aa22fc1a1 31 * The above copyright notice and this permission notice shall be included in
Digixx 0:1e4aa22fc1a1 32 * all copies or substantial portions of the Software.
Digixx 0:1e4aa22fc1a1 33 *
Digixx 0:1e4aa22fc1a1 34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Digixx 0:1e4aa22fc1a1 35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Digixx 0:1e4aa22fc1a1 36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Digixx 0:1e4aa22fc1a1 37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Digixx 0:1e4aa22fc1a1 38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Digixx 0:1e4aa22fc1a1 39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Digixx 0:1e4aa22fc1a1 40 * THE SOFTWARE.
Digixx 0:1e4aa22fc1a1 41 *
Digixx 0:1e4aa22fc1a1 42 * @section DESCRIPTION
Digixx 0:1e4aa22fc1a1 43 *
Digixx 0:1e4aa22fc1a1 44 * ADXL345, triple axis, I2C interface, accelerometer.
Digixx 0:1e4aa22fc1a1 45 *
Digixx 0:1e4aa22fc1a1 46 * Datasheet:
Digixx 0:1e4aa22fc1a1 47 *
Digixx 0:1e4aa22fc1a1 48 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
tylerjw 6:5fb29534a6cf 49 */
tylerjw 6:5fb29534a6cf 50
gltest26 3:7b83694c7292 51 /*
Digixx 0:1e4aa22fc1a1 52 * Includes
Digixx 0:1e4aa22fc1a1 53 */
Digixx 0:1e4aa22fc1a1 54 #include "ADXL345.h"
Digixx 0:1e4aa22fc1a1 55
Digixx 0:1e4aa22fc1a1 56 //#include "mbed.h"
Digixx 0:1e4aa22fc1a1 57
tylerjw 6:5fb29534a6cf 58 ADXL345::ADXL345(PinName sda, PinName scl) : i2c_(*(new I2C(sda, scl)))
tylerjw 6:5fb29534a6cf 59 {
gltest26 5:122a504fcfa3 60 myI2c = &i2c_;
tylerjw 9:cc0260a2404b 61 init();
gltest26 5:122a504fcfa3 62 }
gltest26 5:122a504fcfa3 63
tylerjw 6:5fb29534a6cf 64 ADXL345::~ADXL345()
tylerjw 6:5fb29534a6cf 65 {
gltest26 5:122a504fcfa3 66 delete myI2c;
gltest26 5:122a504fcfa3 67 }
gltest26 5:122a504fcfa3 68
gltest26 5:122a504fcfa3 69
tylerjw 6:5fb29534a6cf 70 void ADXL345::init()
tylerjw 6:5fb29534a6cf 71 {
tylerjw 6:5fb29534a6cf 72 setDataRate(ADXL345_6HZ25); // 6.25 Hz
Digixx 0:1e4aa22fc1a1 73
tylerjw 6:5fb29534a6cf 74 setDataFormatControl(ADXL345_FULL_RES | ADXL345_2G); // full resolution, right justified, 2g range
tylerjw 6:5fb29534a6cf 75
tylerjw 9:cc0260a2404b 76 LocalFileSystem local("local");
tylerjw 9:cc0260a2404b 77
tylerjw 9:cc0260a2404b 78 if(FILE *fp = fopen("/local/config.txt", "r")) { // Open "config.txt" for reading - if it doesn't exist, pass over it
tylerjw 9:cc0260a2404b 79 int8_t calibration_offset[3];
tylerjw 9:cc0260a2404b 80 DigitalOut led(LED1);
tylerjw 9:cc0260a2404b 81 led = 1;
tylerjw 9:cc0260a2404b 82 fscanf(fp, "ADXL345 x:%d,y:%d,z:%d", &calibration_offset[0], &calibration_offset[1], &calibration_offset[2]);
tylerjw 9:cc0260a2404b 83 fclose(fp);
tylerjw 9:cc0260a2404b 84 for(char axis = 0x00; axis < 0x03; axis++)
tylerjw 9:cc0260a2404b 85 setOffset(axis,calibration_offset[axis]);
tylerjw 9:cc0260a2404b 86 led = 0;
tylerjw 9:cc0260a2404b 87 }
Digixx 0:1e4aa22fc1a1 88 }
Digixx 0:1e4aa22fc1a1 89
Digixx 0:1e4aa22fc1a1 90
tylerjw 6:5fb29534a6cf 91 char ADXL345::SingleByteRead(char address)
tylerjw 6:5fb29534a6cf 92 {
tylerjw 6:5fb29534a6cf 93 char tx = address;
tylerjw 6:5fb29534a6cf 94 char output;
Digixx 0:1e4aa22fc1a1 95 i2c_.write( ADXL345_WRITE , &tx, 1); //tell it what you want to read
Digixx 0:1e4aa22fc1a1 96 i2c_.read( ADXL345_READ , &output, 1); //tell it where to store the data
Digixx 0:1e4aa22fc1a1 97 return output;
Digixx 0:1e4aa22fc1a1 98 }
Digixx 0:1e4aa22fc1a1 99
Digixx 0:1e4aa22fc1a1 100
Digixx 0:1e4aa22fc1a1 101 /*
Digixx 0:1e4aa22fc1a1 102 ***info on the i2c_.write***
Digixx 0:1e4aa22fc1a1 103 address 8-bit I2C slave address [ addr | 0 ]
Digixx 0:1e4aa22fc1a1 104 data Pointer to the byte-array data to send
Digixx 0:1e4aa22fc1a1 105 length Number of bytes to send
Digixx 0:1e4aa22fc1a1 106 repeated Repeated start, true - do not send stop at end
Digixx 0:1e4aa22fc1a1 107 returns 0 on success (ack), or non-0 on failure (nack)
Digixx 0:1e4aa22fc1a1 108 */
Digixx 0:1e4aa22fc1a1 109
tylerjw 6:5fb29534a6cf 110 int ADXL345::SingleByteWrite(char address, char data)
tylerjw 6:5fb29534a6cf 111 {
tylerjw 6:5fb29534a6cf 112 int ack = 0;
tylerjw 6:5fb29534a6cf 113 char tx[2];
tylerjw 6:5fb29534a6cf 114 tx[0] = address;
tylerjw 6:5fb29534a6cf 115 tx[1] = data;
tylerjw 6:5fb29534a6cf 116 return ack | i2c_.write( ADXL345_WRITE , tx, 2);
Digixx 0:1e4aa22fc1a1 117 }
Digixx 0:1e4aa22fc1a1 118
Digixx 0:1e4aa22fc1a1 119
tylerjw 6:5fb29534a6cf 120 void ADXL345::multiByteRead(char address, char* output, int size)
tylerjw 6:5fb29534a6cf 121 {
Digixx 0:1e4aa22fc1a1 122 i2c_.write( ADXL345_WRITE, &address, 1); //tell it where to read from
Digixx 0:1e4aa22fc1a1 123 i2c_.read( ADXL345_READ , output, size); //tell it where to store the data read
Digixx 0:1e4aa22fc1a1 124 }
Digixx 0:1e4aa22fc1a1 125
Digixx 0:1e4aa22fc1a1 126
tylerjw 6:5fb29534a6cf 127 int ADXL345::multiByteWrite(char address, char* ptr_data, int size)
tylerjw 6:5fb29534a6cf 128 {
tylerjw 6:5fb29534a6cf 129 int ack;
tylerjw 6:5fb29534a6cf 130
tylerjw 6:5fb29534a6cf 131 ack = i2c_.write( ADXL345_WRITE, &address, 1); //tell it where to write to
tylerjw 6:5fb29534a6cf 132 return ack | i2c_.write( ADXL345_READ, ptr_data, size); //tell it what data to write
Digixx 0:1e4aa22fc1a1 133 }
Digixx 0:1e4aa22fc1a1 134
Digixx 0:1e4aa22fc1a1 135
tylerjw 8:4cdd4315189f 136 void ADXL345::getOutput(int16_t* readings)
tylerjw 6:5fb29534a6cf 137 {
tylerjw 6:5fb29534a6cf 138 char buffer[6];
Digixx 0:1e4aa22fc1a1 139 multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
tylerjw 6:5fb29534a6cf 140
gltest26 3:7b83694c7292 141 readings[0] = wordExtend(&buffer[0]);
gltest26 3:7b83694c7292 142 readings[1] = wordExtend(&buffer[2]);
gltest26 3:7b83694c7292 143 readings[2] = wordExtend(&buffer[4]);
Digixx 0:1e4aa22fc1a1 144 }
Digixx 0:1e4aa22fc1a1 145
tylerjw 6:5fb29534a6cf 146 char ADXL345::getDeviceID()
tylerjw 6:5fb29534a6cf 147 {
Digixx 0:1e4aa22fc1a1 148 return SingleByteRead(ADXL345_DEVID_REG);
tylerjw 6:5fb29534a6cf 149 }
Digixx 0:1e4aa22fc1a1 150 //
tylerjw 6:5fb29534a6cf 151 int ADXL345::setPowerMode(char mode)
tylerjw 6:5fb29534a6cf 152 {
Digixx 0:1e4aa22fc1a1 153 //Get the current register contents, so we don't clobber the rate value.
Digixx 0:1e4aa22fc1a1 154 char registerContents = (mode << 4) | SingleByteRead(ADXL345_BW_RATE_REG);
Digixx 0:1e4aa22fc1a1 155
tylerjw 6:5fb29534a6cf 156 return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
Digixx 0:1e4aa22fc1a1 157 }
Digixx 0:1e4aa22fc1a1 158
tylerjw 8:4cdd4315189f 159 char ADXL345::getBwRateReg()
tylerjw 8:4cdd4315189f 160 {
tylerjw 8:4cdd4315189f 161 return SingleByteRead(ADXL345_BW_RATE_REG);
tylerjw 8:4cdd4315189f 162 }
tylerjw 8:4cdd4315189f 163
tylerjw 8:4cdd4315189f 164 int ADXL345::setBwRateReg(char reg)
tylerjw 8:4cdd4315189f 165 {
tylerjw 8:4cdd4315189f 166 return SingleByteWrite(ADXL345_BW_RATE_REG, reg);
tylerjw 8:4cdd4315189f 167 }
tylerjw 8:4cdd4315189f 168
tylerjw 6:5fb29534a6cf 169 char ADXL345::getPowerControl()
tylerjw 6:5fb29534a6cf 170 {
Digixx 0:1e4aa22fc1a1 171 return SingleByteRead(ADXL345_POWER_CTL_REG);
Digixx 0:1e4aa22fc1a1 172 }
Digixx 0:1e4aa22fc1a1 173
tylerjw 6:5fb29534a6cf 174 int ADXL345::setPowerControl(char settings)
tylerjw 6:5fb29534a6cf 175 {
Digixx 0:1e4aa22fc1a1 176 return SingleByteWrite(ADXL345_POWER_CTL_REG, settings);
Digixx 0:1e4aa22fc1a1 177
Digixx 0:1e4aa22fc1a1 178 }
Digixx 0:1e4aa22fc1a1 179
tylerjw 6:5fb29534a6cf 180 char ADXL345::getDataFormatControl(void)
tylerjw 6:5fb29534a6cf 181 {
Digixx 0:1e4aa22fc1a1 182 return SingleByteRead(ADXL345_DATA_FORMAT_REG);
Digixx 0:1e4aa22fc1a1 183 }
Digixx 0:1e4aa22fc1a1 184
tylerjw 6:5fb29534a6cf 185 int ADXL345::setDataFormatControl(char settings)
tylerjw 6:5fb29534a6cf 186 {
tylerjw 6:5fb29534a6cf 187 return SingleByteWrite(ADXL345_DATA_FORMAT_REG, settings);
Digixx 0:1e4aa22fc1a1 188 }
Digixx 0:1e4aa22fc1a1 189
tylerjw 6:5fb29534a6cf 190 int ADXL345::setDataFormatControl(char settings, char mask, char *prev)
tylerjw 6:5fb29534a6cf 191 {
gltest26 4:8046894b947e 192 char old = SingleByteRead(ADXL345_DATA_FORMAT_REG);
gltest26 4:8046894b947e 193 if(prev)
gltest26 4:8046894b947e 194 *prev = old;
gltest26 4:8046894b947e 195 return SingleByteWrite(ADXL345_DATA_FORMAT_REG, (old | (settings & mask)) & (settings | ~mask));
gltest26 4:8046894b947e 196 }
gltest26 4:8046894b947e 197
tylerjw 6:5fb29534a6cf 198 int ADXL345::setDataRate(char rate)
tylerjw 6:5fb29534a6cf 199 {
Digixx 0:1e4aa22fc1a1 200 //Get the current register contents, so we don't clobber the power bit.
Digixx 0:1e4aa22fc1a1 201 char registerContents = SingleByteRead(ADXL345_BW_RATE_REG);
Digixx 0:1e4aa22fc1a1 202
Digixx 0:1e4aa22fc1a1 203 registerContents &= 0x10;
Digixx 0:1e4aa22fc1a1 204 registerContents |= rate;
Digixx 0:1e4aa22fc1a1 205
Digixx 0:1e4aa22fc1a1 206 return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents);
Digixx 0:1e4aa22fc1a1 207 }
Digixx 0:1e4aa22fc1a1 208
Digixx 0:1e4aa22fc1a1 209
tylerjw 6:5fb29534a6cf 210 char ADXL345::getOffset(char axis)
tylerjw 6:5fb29534a6cf 211 {
Digixx 0:1e4aa22fc1a1 212 char address = 0;
Digixx 0:1e4aa22fc1a1 213
Digixx 0:1e4aa22fc1a1 214 if (axis == ADXL345_X) {
Digixx 0:1e4aa22fc1a1 215 address = ADXL345_OFSX_REG;
Digixx 0:1e4aa22fc1a1 216 } else if (axis == ADXL345_Y) {
Digixx 0:1e4aa22fc1a1 217 address = ADXL345_OFSY_REG;
Digixx 0:1e4aa22fc1a1 218 } else if (axis == ADXL345_Z) {
Digixx 0:1e4aa22fc1a1 219 address = ADXL345_OFSZ_REG;
Digixx 0:1e4aa22fc1a1 220 }
Digixx 0:1e4aa22fc1a1 221
tylerjw 6:5fb29534a6cf 222 return SingleByteRead(address);
Digixx 0:1e4aa22fc1a1 223 }
Digixx 0:1e4aa22fc1a1 224
tylerjw 6:5fb29534a6cf 225 int ADXL345::setOffset(char axis, char offset)
tylerjw 6:5fb29534a6cf 226 {
Digixx 0:1e4aa22fc1a1 227 char address = 0;
Digixx 0:1e4aa22fc1a1 228
Digixx 0:1e4aa22fc1a1 229 if (axis == ADXL345_X) {
Digixx 0:1e4aa22fc1a1 230 address = ADXL345_OFSX_REG;
Digixx 0:1e4aa22fc1a1 231 } else if (axis == ADXL345_Y) {
Digixx 0:1e4aa22fc1a1 232 address = ADXL345_OFSY_REG;
Digixx 0:1e4aa22fc1a1 233 } else if (axis == ADXL345_Z) {
Digixx 0:1e4aa22fc1a1 234 address = ADXL345_OFSZ_REG;
Digixx 0:1e4aa22fc1a1 235 }
Digixx 0:1e4aa22fc1a1 236
tylerjw 6:5fb29534a6cf 237 return SingleByteWrite(address, offset);
Digixx 0:1e4aa22fc1a1 238 }
Digixx 0:1e4aa22fc1a1 239
Digixx 0:1e4aa22fc1a1 240
tylerjw 6:5fb29534a6cf 241 char ADXL345::getFifoControl(void)
tylerjw 6:5fb29534a6cf 242 {
tylerjw 6:5fb29534a6cf 243 return SingleByteRead(ADXL345_FIFO_CTL);
tylerjw 6:5fb29534a6cf 244 }
tylerjw 6:5fb29534a6cf 245
tylerjw 6:5fb29534a6cf 246 int ADXL345::setFifoControl(char settings)
tylerjw 6:5fb29534a6cf 247 {
tylerjw 6:5fb29534a6cf 248 return SingleByteWrite(ADXL345_FIFO_STATUS, settings);
tylerjw 6:5fb29534a6cf 249 }
Digixx 0:1e4aa22fc1a1 250
tylerjw 6:5fb29534a6cf 251 char ADXL345::getFifoStatus(void)
tylerjw 6:5fb29534a6cf 252 {
tylerjw 6:5fb29534a6cf 253 return SingleByteRead(ADXL345_FIFO_STATUS);
tylerjw 6:5fb29534a6cf 254 }
tylerjw 6:5fb29534a6cf 255
tylerjw 6:5fb29534a6cf 256 char ADXL345::getTapThreshold(void)
tylerjw 6:5fb29534a6cf 257 {
tylerjw 6:5fb29534a6cf 258 return SingleByteRead(ADXL345_THRESH_TAP_REG);
tylerjw 6:5fb29534a6cf 259 }
tylerjw 6:5fb29534a6cf 260
tylerjw 6:5fb29534a6cf 261 int ADXL345::setTapThreshold(char threshold)
tylerjw 6:5fb29534a6cf 262 {
tylerjw 6:5fb29534a6cf 263 return SingleByteWrite(ADXL345_THRESH_TAP_REG, threshold);
tylerjw 6:5fb29534a6cf 264 }
tylerjw 6:5fb29534a6cf 265
tylerjw 6:5fb29534a6cf 266 float ADXL345::getTapDuration(void)
tylerjw 6:5fb29534a6cf 267 {
Digixx 0:1e4aa22fc1a1 268 return (float)SingleByteRead(ADXL345_DUR_REG)*625;
Digixx 0:1e4aa22fc1a1 269 }
Digixx 0:1e4aa22fc1a1 270
tylerjw 6:5fb29534a6cf 271 int ADXL345::setTapDuration(short int duration_us)
tylerjw 6:5fb29534a6cf 272 {
Digixx 0:1e4aa22fc1a1 273 short int tapDuration = duration_us / 625;
Digixx 0:1e4aa22fc1a1 274 char tapChar[2];
tylerjw 6:5fb29534a6cf 275 tapChar[0] = (tapDuration & 0x00FF);
tylerjw 6:5fb29534a6cf 276 tapChar[1] = (tapDuration >> 8) & 0x00FF;
Digixx 0:1e4aa22fc1a1 277 return multiByteWrite(ADXL345_DUR_REG, tapChar, 2);
Digixx 0:1e4aa22fc1a1 278 }
Digixx 0:1e4aa22fc1a1 279
tylerjw 6:5fb29534a6cf 280 float ADXL345::getTapLatency(void)
tylerjw 6:5fb29534a6cf 281 {
Digixx 0:1e4aa22fc1a1 282 return (float)SingleByteRead(ADXL345_LATENT_REG)*1.25;
Digixx 0:1e4aa22fc1a1 283 }
Digixx 0:1e4aa22fc1a1 284
tylerjw 6:5fb29534a6cf 285 int ADXL345::setTapLatency(short int latency_ms)
tylerjw 6:5fb29534a6cf 286 {
Digixx 0:1e4aa22fc1a1 287 latency_ms = latency_ms / 1.25;
Digixx 0:1e4aa22fc1a1 288 char latChar[2];
tylerjw 6:5fb29534a6cf 289 latChar[0] = (latency_ms & 0x00FF);
tylerjw 6:5fb29534a6cf 290 latChar[1] = (latency_ms << 8) & 0xFF00;
Digixx 0:1e4aa22fc1a1 291 return multiByteWrite(ADXL345_LATENT_REG, latChar, 2);
Digixx 0:1e4aa22fc1a1 292 }
Digixx 0:1e4aa22fc1a1 293
tylerjw 6:5fb29534a6cf 294 float ADXL345::getWindowTime(void)
tylerjw 6:5fb29534a6cf 295 {
Digixx 0:1e4aa22fc1a1 296 return (float)SingleByteRead(ADXL345_WINDOW_REG)*1.25;
Digixx 0:1e4aa22fc1a1 297 }
Digixx 0:1e4aa22fc1a1 298
tylerjw 6:5fb29534a6cf 299 int ADXL345::setWindowTime(short int window_ms)
tylerjw 6:5fb29534a6cf 300 {
Digixx 0:1e4aa22fc1a1 301 window_ms = window_ms / 1.25;
Digixx 0:1e4aa22fc1a1 302 char windowChar[2];
Digixx 0:1e4aa22fc1a1 303 windowChar[0] = (window_ms & 0x00FF);
Digixx 0:1e4aa22fc1a1 304 windowChar[1] = ((window_ms << 8) & 0xFF00);
tylerjw 6:5fb29534a6cf 305 return multiByteWrite(ADXL345_WINDOW_REG, windowChar, 2);
Digixx 0:1e4aa22fc1a1 306 }
Digixx 0:1e4aa22fc1a1 307
tylerjw 6:5fb29534a6cf 308 char ADXL345::getActivityThreshold(void)
tylerjw 6:5fb29534a6cf 309 {
Digixx 0:1e4aa22fc1a1 310 return SingleByteRead(ADXL345_THRESH_ACT_REG);
Digixx 0:1e4aa22fc1a1 311 }
Digixx 0:1e4aa22fc1a1 312
tylerjw 6:5fb29534a6cf 313 int ADXL345::setActivityThreshold(char threshold)
tylerjw 6:5fb29534a6cf 314 {
Digixx 0:1e4aa22fc1a1 315 return SingleByteWrite(ADXL345_THRESH_ACT_REG, threshold);
Digixx 0:1e4aa22fc1a1 316 }
Digixx 0:1e4aa22fc1a1 317
tylerjw 6:5fb29534a6cf 318 char ADXL345::getInactivityThreshold(void)
tylerjw 6:5fb29534a6cf 319 {
Digixx 0:1e4aa22fc1a1 320 return SingleByteRead(ADXL345_THRESH_INACT_REG);
Digixx 0:1e4aa22fc1a1 321 }
Digixx 0:1e4aa22fc1a1 322
Digixx 0:1e4aa22fc1a1 323 //int FUNCTION(short int * ptr_Output)
Digixx 0:1e4aa22fc1a1 324 //short int FUNCTION ()
Digixx 0:1e4aa22fc1a1 325
tylerjw 6:5fb29534a6cf 326 int ADXL345::setInactivityThreshold(char threshold)
tylerjw 6:5fb29534a6cf 327 {
Digixx 0:1e4aa22fc1a1 328 return SingleByteWrite(ADXL345_THRESH_INACT_REG, threshold);
Digixx 0:1e4aa22fc1a1 329 }
Digixx 0:1e4aa22fc1a1 330
tylerjw 6:5fb29534a6cf 331 char ADXL345::getTimeInactivity(void)
tylerjw 6:5fb29534a6cf 332 {
Digixx 0:1e4aa22fc1a1 333 return SingleByteRead(ADXL345_TIME_INACT_REG);
Digixx 0:1e4aa22fc1a1 334 }
Digixx 0:1e4aa22fc1a1 335
tylerjw 6:5fb29534a6cf 336 int ADXL345::setTimeInactivity(char timeInactivity)
tylerjw 6:5fb29534a6cf 337 {
Digixx 0:1e4aa22fc1a1 338 return SingleByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
Digixx 0:1e4aa22fc1a1 339 }
Digixx 0:1e4aa22fc1a1 340
tylerjw 6:5fb29534a6cf 341 char ADXL345::getActivityInactivityControl(void)
tylerjw 6:5fb29534a6cf 342 {
Digixx 0:1e4aa22fc1a1 343 return SingleByteRead(ADXL345_ACT_INACT_CTL_REG);
Digixx 0:1e4aa22fc1a1 344 }
Digixx 0:1e4aa22fc1a1 345
tylerjw 6:5fb29534a6cf 346 int ADXL345::setActivityInactivityControl(char settings)
tylerjw 6:5fb29534a6cf 347 {
Digixx 0:1e4aa22fc1a1 348 return SingleByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
Digixx 0:1e4aa22fc1a1 349 }
Digixx 0:1e4aa22fc1a1 350
tylerjw 6:5fb29534a6cf 351 char ADXL345::getFreefallThreshold(void)
tylerjw 6:5fb29534a6cf 352 {
Digixx 0:1e4aa22fc1a1 353 return SingleByteRead(ADXL345_THRESH_FF_REG);
Digixx 0:1e4aa22fc1a1 354 }
Digixx 0:1e4aa22fc1a1 355
tylerjw 6:5fb29534a6cf 356 int ADXL345::setFreefallThreshold(char threshold)
tylerjw 6:5fb29534a6cf 357 {
tylerjw 6:5fb29534a6cf 358 return SingleByteWrite(ADXL345_THRESH_FF_REG, threshold);
Digixx 0:1e4aa22fc1a1 359 }
Digixx 0:1e4aa22fc1a1 360
tylerjw 6:5fb29534a6cf 361 char ADXL345::getFreefallTime(void)
tylerjw 6:5fb29534a6cf 362 {
Digixx 0:1e4aa22fc1a1 363 return SingleByteRead(ADXL345_TIME_FF_REG)*5;
Digixx 0:1e4aa22fc1a1 364 }
Digixx 0:1e4aa22fc1a1 365
tylerjw 6:5fb29534a6cf 366 int ADXL345::setFreefallTime(short int freefallTime_ms)
tylerjw 6:5fb29534a6cf 367 {
tylerjw 6:5fb29534a6cf 368 freefallTime_ms = freefallTime_ms / 5;
tylerjw 6:5fb29534a6cf 369 char fallChar[2];
tylerjw 6:5fb29534a6cf 370 fallChar[0] = (freefallTime_ms & 0x00FF);
tylerjw 6:5fb29534a6cf 371 fallChar[1] = (freefallTime_ms << 8) & 0xFF00;
tylerjw 6:5fb29534a6cf 372
Digixx 0:1e4aa22fc1a1 373 return multiByteWrite(ADXL345_TIME_FF_REG, fallChar, 2);
Digixx 0:1e4aa22fc1a1 374 }
Digixx 0:1e4aa22fc1a1 375
tylerjw 6:5fb29534a6cf 376 char ADXL345::getTapAxisControl(void)
tylerjw 6:5fb29534a6cf 377 {
Digixx 0:1e4aa22fc1a1 378 return SingleByteRead(ADXL345_TAP_AXES_REG);
Digixx 0:1e4aa22fc1a1 379 }
Digixx 0:1e4aa22fc1a1 380
tylerjw 6:5fb29534a6cf 381 int ADXL345::setTapAxisControl(char settings)
tylerjw 6:5fb29534a6cf 382 {
tylerjw 6:5fb29534a6cf 383 return SingleByteWrite(ADXL345_TAP_AXES_REG, settings);
Digixx 0:1e4aa22fc1a1 384 }
Digixx 0:1e4aa22fc1a1 385
tylerjw 6:5fb29534a6cf 386 char ADXL345::getTapSource(void)
tylerjw 6:5fb29534a6cf 387 {
Digixx 0:1e4aa22fc1a1 388 return SingleByteRead(ADXL345_ACT_TAP_STATUS_REG);
Digixx 0:1e4aa22fc1a1 389 }
Digixx 0:1e4aa22fc1a1 390
tylerjw 6:5fb29534a6cf 391 char ADXL345::getInterruptEnableControl(void)
tylerjw 6:5fb29534a6cf 392 {
Digixx 0:1e4aa22fc1a1 393 return SingleByteRead(ADXL345_INT_ENABLE_REG);
Digixx 0:1e4aa22fc1a1 394 }
Digixx 0:1e4aa22fc1a1 395
tylerjw 6:5fb29534a6cf 396 int ADXL345::setInterruptEnableControl(char settings)
tylerjw 6:5fb29534a6cf 397 {
tylerjw 6:5fb29534a6cf 398 return SingleByteWrite(ADXL345_INT_ENABLE_REG, settings);
Digixx 0:1e4aa22fc1a1 399 }
Digixx 0:1e4aa22fc1a1 400
tylerjw 6:5fb29534a6cf 401 char ADXL345::getInterruptMappingControl(void)
tylerjw 6:5fb29534a6cf 402 {
Digixx 0:1e4aa22fc1a1 403 return SingleByteRead(ADXL345_INT_MAP_REG);
Digixx 0:1e4aa22fc1a1 404 }
Digixx 0:1e4aa22fc1a1 405
tylerjw 6:5fb29534a6cf 406 int ADXL345::setInterruptMappingControl(char settings)
tylerjw 6:5fb29534a6cf 407 {
Digixx 0:1e4aa22fc1a1 408 return SingleByteWrite(ADXL345_INT_MAP_REG, settings);
Digixx 0:1e4aa22fc1a1 409 }
Digixx 0:1e4aa22fc1a1 410
tylerjw 6:5fb29534a6cf 411 char ADXL345::getInterruptSource(void)
tylerjw 6:5fb29534a6cf 412 {
Digixx 0:1e4aa22fc1a1 413 return SingleByteRead(ADXL345_INT_SOURCE_REG);
tylerjw 8:4cdd4315189f 414 }
tylerjw 8:4cdd4315189f 415
tylerjw 8:4cdd4315189f 416 void ADXL345::sample100avg(float period, int16_t buffer[][3], int16_t *avg, Timer* t)
tylerjw 8:4cdd4315189f 417 {
tylerjw 8:4cdd4315189f 418 double start_time;
tylerjw 8:4cdd4315189f 419
tylerjw 8:4cdd4315189f 420 for(int sample = 0; sample < 100; sample++) {
tylerjw 8:4cdd4315189f 421 start_time = t->read();
tylerjw 8:4cdd4315189f 422
tylerjw 8:4cdd4315189f 423 getOutput(buffer[sample]);
tylerjw 8:4cdd4315189f 424
tylerjw 8:4cdd4315189f 425 wait(period - (start_time - t->read()));
tylerjw 8:4cdd4315189f 426 }
tylerjw 8:4cdd4315189f 427
tylerjw 8:4cdd4315189f 428 for(int axis = 0; axis < 3; axis++) {
tylerjw 8:4cdd4315189f 429 double average = 0.0;
tylerjw 8:4cdd4315189f 430 for(int sample = 0; sample < 100; sample++)
tylerjw 8:4cdd4315189f 431 average += buffer[sample][axis];
tylerjw 8:4cdd4315189f 432 average /= 100.0;
tylerjw 8:4cdd4315189f 433 avg[axis] = static_cast<int16_t>(average);
tylerjw 8:4cdd4315189f 434 }
tylerjw 8:4cdd4315189f 435 }
tylerjw 8:4cdd4315189f 436
tylerjw 8:4cdd4315189f 437 void ADXL345::calibrate(Timer* t, bool store_output, Serial *pc)
tylerjw 8:4cdd4315189f 438 {
tylerjw 8:4cdd4315189f 439 int16_t data[100][3]; // {x,y,z}, data
tylerjw 8:4cdd4315189f 440 int16_t data_avg[3];
tylerjw 8:4cdd4315189f 441 int8_t calibration_offset[3];
tylerjw 8:4cdd4315189f 442
tylerjw 8:4cdd4315189f 443 float period = 0.01; // period of sample rate
tylerjw 8:4cdd4315189f 444
tylerjw 8:4cdd4315189f 445 // wait 11.1ms
tylerjw 8:4cdd4315189f 446 wait(0.0111);
tylerjw 9:cc0260a2404b 447
tylerjw 8:4cdd4315189f 448 pc->puts("Reading old register states... ");
tylerjw 8:4cdd4315189f 449 // read current register states
tylerjw 8:4cdd4315189f 450 char bw_rate = getBwRateReg();
tylerjw 8:4cdd4315189f 451 char power_control = getPowerControl();
tylerjw 8:4cdd4315189f 452 char data_format = getDataFormatControl();
tylerjw 8:4cdd4315189f 453
tylerjw 8:4cdd4315189f 454 pc->puts("Done!\r\nSetting new register states... ");
tylerjw 8:4cdd4315189f 455 // initalize command sequence
tylerjw 8:4cdd4315189f 456 setDataFormatControl((ADXL345_16G | ADXL345_FULL_RES));
tylerjw 8:4cdd4315189f 457 setBwRateReg(ADXL345_100HZ); // 100Hz data rate
tylerjw 8:4cdd4315189f 458 setPowerControl(0x08); // start measurement
tylerjw 8:4cdd4315189f 459
tylerjw 8:4cdd4315189f 460 // wait 1.1ms
tylerjw 8:4cdd4315189f 461 wait(0.0111);
tylerjw 8:4cdd4315189f 462 pc->puts("Done!\r\nSampling... ");
tylerjw 8:4cdd4315189f 463 //take 100 data points and average (100Hz)
tylerjw 8:4cdd4315189f 464 sample100avg(period, data, data_avg, t);
tylerjw 8:4cdd4315189f 465 pc->puts("Done!\r\nCalculating offset values... ");
tylerjw 8:4cdd4315189f 466 // calculate calibration value
tylerjw 8:4cdd4315189f 467 calibration_offset[0] = -1 * (data_avg[0] / 4); // x
tylerjw 8:4cdd4315189f 468 calibration_offset[1] = -1 * (data_avg[1] / 4); // y
tylerjw 8:4cdd4315189f 469 calibration_offset[2] = -1 * ((data_avg[2] - 256) / 4); // z
tylerjw 9:cc0260a2404b 470
tylerjw 8:4cdd4315189f 471 if(store_output) {
tylerjw 8:4cdd4315189f 472 pc->puts("Done!\r\nStoring output to file... ");
tylerjw 8:4cdd4315189f 473 LocalFileSystem local("local");
tylerjw 8:4cdd4315189f 474 FILE *fp = fopen("/local/OFF_CAL.csv", "w"); // write
tylerjw 8:4cdd4315189f 475 fprintf(fp, "ADXL345 Calibration offsets\r\nx,%d\r\ny,%d\r\nz,%d\r\n\r\n", calibration_offset[0], calibration_offset[1], calibration_offset[2]);
tylerjw 8:4cdd4315189f 476
tylerjw 8:4cdd4315189f 477 fputs("Raw Data:\r\nX,Y,Z\r\n", fp);
tylerjw 8:4cdd4315189f 478 for(int sample = 0; sample < 100; sample++)
tylerjw 8:4cdd4315189f 479 fprintf(fp, "%d,%d,%d\r\n",data[sample][0],data[sample][1],data[sample][2]);
tylerjw 8:4cdd4315189f 480 fclose(fp);
tylerjw 8:4cdd4315189f 481 }
tylerjw 8:4cdd4315189f 482 pc->puts("Done!\r\nSetting the offset registers... ");
tylerjw 8:4cdd4315189f 483 // update offset registers
tylerjw 8:4cdd4315189f 484 for(char axis = 0x00; axis < 0x03; axis++)
tylerjw 8:4cdd4315189f 485 setOffset(axis,calibration_offset[axis]);
tylerjw 8:4cdd4315189f 486 pc->puts("Done!\r\nReturning registers to original state... ");
tylerjw 8:4cdd4315189f 487 // return control registers to original state
tylerjw 8:4cdd4315189f 488 setDataFormatControl(data_format);
tylerjw 8:4cdd4315189f 489 setBwRateReg(bw_rate);
tylerjw 8:4cdd4315189f 490 setPowerControl(power_control);
tylerjw 8:4cdd4315189f 491 pc->puts("Done!\r\n");
tylerjw 8:4cdd4315189f 492 }
tylerjw 8:4cdd4315189f 493
tylerjw 8:4cdd4315189f 494 void ADXL345::calibrate(Timer* t, bool store_output)
tylerjw 8:4cdd4315189f 495 {
tylerjw 8:4cdd4315189f 496 int16_t data[100][3]; // {x,y,z}, data
tylerjw 8:4cdd4315189f 497 int16_t data_avg[3];
tylerjw 8:4cdd4315189f 498 int8_t calibration_offset[3];
tylerjw 8:4cdd4315189f 499
tylerjw 8:4cdd4315189f 500 float period = 0.01; // period of sample rate
tylerjw 8:4cdd4315189f 501
tylerjw 8:4cdd4315189f 502 // wait 11.1ms
tylerjw 8:4cdd4315189f 503 wait(0.0111);
tylerjw 9:cc0260a2404b 504
tylerjw 8:4cdd4315189f 505 // read current register states
tylerjw 8:4cdd4315189f 506 char bw_rate = getBwRateReg();
tylerjw 8:4cdd4315189f 507 char power_control = getPowerControl();
tylerjw 8:4cdd4315189f 508 char data_format = getDataFormatControl();
tylerjw 8:4cdd4315189f 509
tylerjw 8:4cdd4315189f 510 // initalize command sequence
tylerjw 8:4cdd4315189f 511 setDataFormatControl((ADXL345_16G | ADXL345_FULL_RES));
tylerjw 8:4cdd4315189f 512 setBwRateReg(ADXL345_100HZ); // 100Hz data rate
tylerjw 8:4cdd4315189f 513 setPowerControl(0x08); // start measurement
tylerjw 8:4cdd4315189f 514
tylerjw 8:4cdd4315189f 515 // wait 1.1ms
tylerjw 8:4cdd4315189f 516 wait(0.0111);
tylerjw 8:4cdd4315189f 517 //take 100 data points and average (100Hz)
tylerjw 8:4cdd4315189f 518 sample100avg(period, data, data_avg, t);
tylerjw 8:4cdd4315189f 519 // calculate calibration value
tylerjw 8:4cdd4315189f 520 calibration_offset[0] = -1 * (data_avg[0] / 4); // x
tylerjw 8:4cdd4315189f 521 calibration_offset[1] = -1 * (data_avg[1] / 4); // y
tylerjw 8:4cdd4315189f 522 calibration_offset[2] = -1 * ((data_avg[2] - 256) / 4); // z
tylerjw 9:cc0260a2404b 523
tylerjw 8:4cdd4315189f 524 if(store_output) {
tylerjw 8:4cdd4315189f 525 LocalFileSystem local("local");
tylerjw 8:4cdd4315189f 526 FILE *fp = fopen("/local/OFF_CAL.csv", "w"); // write
tylerjw 8:4cdd4315189f 527 fprintf(fp, "ADXL345 Calibration offsets\r\nx,%d\r\ny,%d\r\nz,%d\r\n\r\n", calibration_offset[0], calibration_offset[1], calibration_offset[2]);
tylerjw 8:4cdd4315189f 528
tylerjw 8:4cdd4315189f 529 fputs("Raw Data:\r\nX,Y,Z\r\n", fp);
tylerjw 8:4cdd4315189f 530 for(int sample = 0; sample < 100; sample++)
tylerjw 8:4cdd4315189f 531 fprintf(fp, "%d,%d,%d\r\n",data[sample][0],data[sample][1],data[sample][2]);
tylerjw 8:4cdd4315189f 532 fclose(fp);
tylerjw 8:4cdd4315189f 533 }
tylerjw 8:4cdd4315189f 534 // update offset registers
tylerjw 8:4cdd4315189f 535 for(char axis = 0x00; axis < 0x03; axis++)
tylerjw 8:4cdd4315189f 536 setOffset(axis,calibration_offset[axis]);
tylerjw 8:4cdd4315189f 537 // return control registers to original state
tylerjw 8:4cdd4315189f 538 setDataFormatControl(data_format);
tylerjw 8:4cdd4315189f 539 setBwRateReg(bw_rate);
tylerjw 8:4cdd4315189f 540 setPowerControl(power_control);
tylerjw 6:5fb29534a6cf 541 }