Frederick Huang
/
ADXL345Test
ADXL345 Evaluation
ADXL345_I2C.cpp@3:e4783c57bcc0, 2017-08-21 (annotated)
- Committer:
- Frederick_H
- Date:
- Mon Aug 21 02:40:56 2017 +0000
- Revision:
- 3:e4783c57bcc0
Test purpose
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Frederick_H | 3:e4783c57bcc0 | 1 | /** |
Frederick_H | 3:e4783c57bcc0 | 2 | * @author Peter Swanson |
Frederick_H | 3:e4783c57bcc0 | 3 | * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because |
Frederick_H | 3:e4783c57bcc0 | 4 | * today, religion is thought of as something that you do or believe and has about as |
Frederick_H | 3:e4783c57bcc0 | 5 | * little impact on a person as their political stance. But for me, God gives me daily |
Frederick_H | 3:e4783c57bcc0 | 6 | * strength and has filled my life with the satisfaction that I could never find in any |
Frederick_H | 3:e4783c57bcc0 | 7 | * of the other things that I once looked for it in. |
Frederick_H | 3:e4783c57bcc0 | 8 | * If your interested, heres verse that changed my life: |
Frederick_H | 3:e4783c57bcc0 | 9 | * Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus, |
Frederick_H | 3:e4783c57bcc0 | 10 | * because through Christ Jesus, the law of the Spirit who gives life has set |
Frederick_H | 3:e4783c57bcc0 | 11 | * me free from the law of sin (which brings...) and death. For what the law |
Frederick_H | 3:e4783c57bcc0 | 12 | * was powerless to do in that it was weakened by the flesh, God did by sending |
Frederick_H | 3:e4783c57bcc0 | 13 | * His own Son in the likeness of sinful flesh to be a sin offering. And so He |
Frederick_H | 3:e4783c57bcc0 | 14 | * condemned sin in the flesh in order that the righteous requirements of the |
Frederick_H | 3:e4783c57bcc0 | 15 | * (God's) law might be fully met in us, who live not according to the flesh |
Frederick_H | 3:e4783c57bcc0 | 16 | * but according to the Spirit." |
Frederick_H | 3:e4783c57bcc0 | 17 | * |
Frederick_H | 3:e4783c57bcc0 | 18 | * A special thanks to Ewout van Bekkum for all his patient help in developing this library! |
Frederick_H | 3:e4783c57bcc0 | 19 | * |
Frederick_H | 3:e4783c57bcc0 | 20 | * @section LICENSE |
Frederick_H | 3:e4783c57bcc0 | 21 | * |
Frederick_H | 3:e4783c57bcc0 | 22 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Frederick_H | 3:e4783c57bcc0 | 23 | * of this software and associated documentation files (the "Software"), to deal |
Frederick_H | 3:e4783c57bcc0 | 24 | * in the Software without restriction, including without limitation the rights |
Frederick_H | 3:e4783c57bcc0 | 25 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Frederick_H | 3:e4783c57bcc0 | 26 | * copies of the Software, and to permit persons to whom the Software is |
Frederick_H | 3:e4783c57bcc0 | 27 | * furnished to do so, subject to the following conditions: |
Frederick_H | 3:e4783c57bcc0 | 28 | * |
Frederick_H | 3:e4783c57bcc0 | 29 | * The above copyright notice and this permission notice shall be included in |
Frederick_H | 3:e4783c57bcc0 | 30 | * all copies or substantial portions of the Software. |
Frederick_H | 3:e4783c57bcc0 | 31 | * |
Frederick_H | 3:e4783c57bcc0 | 32 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Frederick_H | 3:e4783c57bcc0 | 33 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Frederick_H | 3:e4783c57bcc0 | 34 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Frederick_H | 3:e4783c57bcc0 | 35 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Frederick_H | 3:e4783c57bcc0 | 36 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Frederick_H | 3:e4783c57bcc0 | 37 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Frederick_H | 3:e4783c57bcc0 | 38 | * THE SOFTWARE. |
Frederick_H | 3:e4783c57bcc0 | 39 | * |
Frederick_H | 3:e4783c57bcc0 | 40 | * @section DESCRIPTION |
Frederick_H | 3:e4783c57bcc0 | 41 | * |
Frederick_H | 3:e4783c57bcc0 | 42 | * ADXL345, triple axis, I2C interface, accelerometer. |
Frederick_H | 3:e4783c57bcc0 | 43 | * |
Frederick_H | 3:e4783c57bcc0 | 44 | * Datasheet: |
Frederick_H | 3:e4783c57bcc0 | 45 | * |
Frederick_H | 3:e4783c57bcc0 | 46 | * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf |
Frederick_H | 3:e4783c57bcc0 | 47 | */ |
Frederick_H | 3:e4783c57bcc0 | 48 | |
Frederick_H | 3:e4783c57bcc0 | 49 | /** |
Frederick_H | 3:e4783c57bcc0 | 50 | * Includes |
Frederick_H | 3:e4783c57bcc0 | 51 | */ |
Frederick_H | 3:e4783c57bcc0 | 52 | #include "ADXL345_I2C.h" |
Frederick_H | 3:e4783c57bcc0 | 53 | |
Frederick_H | 3:e4783c57bcc0 | 54 | //#include "mbed.h" |
Frederick_H | 3:e4783c57bcc0 | 55 | |
Frederick_H | 3:e4783c57bcc0 | 56 | ADXL345_I2C::ADXL345_I2C(PinName sda, PinName scl) : i2c_(sda, scl) { |
Frederick_H | 3:e4783c57bcc0 | 57 | |
Frederick_H | 3:e4783c57bcc0 | 58 | //400kHz, allowing us to use the fastest data rates. |
Frederick_H | 3:e4783c57bcc0 | 59 | i2c_.frequency(400000); |
Frederick_H | 3:e4783c57bcc0 | 60 | // initialize the BW data rate |
Frederick_H | 3:e4783c57bcc0 | 61 | char tx[2]; |
Frederick_H | 3:e4783c57bcc0 | 62 | tx[0] = ADXL345_BW_RATE_REG; |
Frederick_H | 3:e4783c57bcc0 | 63 | tx[1] = ADXL345_1600HZ; //value greater than or equal to 0x0A is written into the rate bits (Bit D3 through Bit D0) in the BW_RATE register |
Frederick_H | 3:e4783c57bcc0 | 64 | i2c_.write( ADXL345_I2C_WRITE , tx, 2); |
Frederick_H | 3:e4783c57bcc0 | 65 | |
Frederick_H | 3:e4783c57bcc0 | 66 | //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). |
Frederick_H | 3:e4783c57bcc0 | 67 | |
Frederick_H | 3:e4783c57bcc0 | 68 | char rx[2]; |
Frederick_H | 3:e4783c57bcc0 | 69 | rx[0] = ADXL345_DATA_FORMAT_REG; |
Frederick_H | 3:e4783c57bcc0 | 70 | rx[1] = 0x0B; |
Frederick_H | 3:e4783c57bcc0 | 71 | // full res and +_16g |
Frederick_H | 3:e4783c57bcc0 | 72 | i2c_.write( ADXL345_I2C_WRITE , rx, 2); |
Frederick_H | 3:e4783c57bcc0 | 73 | |
Frederick_H | 3:e4783c57bcc0 | 74 | // Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE. |
Frederick_H | 3:e4783c57bcc0 | 75 | char x[2]; |
Frederick_H | 3:e4783c57bcc0 | 76 | x[0] = ADXL345_OFSX_REG ; |
Frederick_H | 3:e4783c57bcc0 | 77 | x[1] = 0xFD; |
Frederick_H | 3:e4783c57bcc0 | 78 | i2c_.write( ADXL345_I2C_WRITE , x, 2); |
Frederick_H | 3:e4783c57bcc0 | 79 | char y[2]; |
Frederick_H | 3:e4783c57bcc0 | 80 | y[0] = ADXL345_OFSY_REG ; |
Frederick_H | 3:e4783c57bcc0 | 81 | y[1] = 0x03; |
Frederick_H | 3:e4783c57bcc0 | 82 | i2c_.write( ADXL345_I2C_WRITE , y, 2); |
Frederick_H | 3:e4783c57bcc0 | 83 | char z[2]; |
Frederick_H | 3:e4783c57bcc0 | 84 | z[0] = ADXL345_OFSZ_REG ; |
Frederick_H | 3:e4783c57bcc0 | 85 | z[1] = 0xFE; |
Frederick_H | 3:e4783c57bcc0 | 86 | i2c_.write( ADXL345_I2C_WRITE , z, 2); |
Frederick_H | 3:e4783c57bcc0 | 87 | } |
Frederick_H | 3:e4783c57bcc0 | 88 | |
Frederick_H | 3:e4783c57bcc0 | 89 | |
Frederick_H | 3:e4783c57bcc0 | 90 | char ADXL345_I2C::SingleByteRead(char address){ |
Frederick_H | 3:e4783c57bcc0 | 91 | char tx = address; |
Frederick_H | 3:e4783c57bcc0 | 92 | char output; |
Frederick_H | 3:e4783c57bcc0 | 93 | i2c_.write( ADXL345_I2C_WRITE , &tx, 1); //tell it what you want to read |
Frederick_H | 3:e4783c57bcc0 | 94 | i2c_.read( ADXL345_I2C_READ , &output, 1); //tell it where to store the data |
Frederick_H | 3:e4783c57bcc0 | 95 | return output; |
Frederick_H | 3:e4783c57bcc0 | 96 | |
Frederick_H | 3:e4783c57bcc0 | 97 | } |
Frederick_H | 3:e4783c57bcc0 | 98 | |
Frederick_H | 3:e4783c57bcc0 | 99 | |
Frederick_H | 3:e4783c57bcc0 | 100 | /* |
Frederick_H | 3:e4783c57bcc0 | 101 | ***info on the i2c_.write*** |
Frederick_H | 3:e4783c57bcc0 | 102 | address 8-bit I2C slave address [ addr | 0 ] |
Frederick_H | 3:e4783c57bcc0 | 103 | data Pointer to the byte-array data to send |
Frederick_H | 3:e4783c57bcc0 | 104 | length Number of bytes to send |
Frederick_H | 3:e4783c57bcc0 | 105 | repeated Repeated start, true - do not send stop at end |
Frederick_H | 3:e4783c57bcc0 | 106 | returns 0 on success (ack), or non-0 on failure (nack) |
Frederick_H | 3:e4783c57bcc0 | 107 | */ |
Frederick_H | 3:e4783c57bcc0 | 108 | |
Frederick_H | 3:e4783c57bcc0 | 109 | int ADXL345_I2C::SingleByteWrite(char address, char data){ |
Frederick_H | 3:e4783c57bcc0 | 110 | int ack = 0; |
Frederick_H | 3:e4783c57bcc0 | 111 | char tx[2]; |
Frederick_H | 3:e4783c57bcc0 | 112 | tx[0] = address; |
Frederick_H | 3:e4783c57bcc0 | 113 | tx[1] = data; |
Frederick_H | 3:e4783c57bcc0 | 114 | return ack | i2c_.write( ADXL345_I2C_WRITE , tx, 2); |
Frederick_H | 3:e4783c57bcc0 | 115 | } |
Frederick_H | 3:e4783c57bcc0 | 116 | |
Frederick_H | 3:e4783c57bcc0 | 117 | |
Frederick_H | 3:e4783c57bcc0 | 118 | |
Frederick_H | 3:e4783c57bcc0 | 119 | void ADXL345_I2C::multiByteRead(char address, char* output, int size) { |
Frederick_H | 3:e4783c57bcc0 | 120 | i2c_.write( ADXL345_I2C_WRITE, &address, 1); //tell it where to read from |
Frederick_H | 3:e4783c57bcc0 | 121 | i2c_.read( ADXL345_I2C_READ , output, size); //tell it where to store the data read |
Frederick_H | 3:e4783c57bcc0 | 122 | } |
Frederick_H | 3:e4783c57bcc0 | 123 | |
Frederick_H | 3:e4783c57bcc0 | 124 | |
Frederick_H | 3:e4783c57bcc0 | 125 | int ADXL345_I2C::multiByteWrite(char address, char* ptr_data, int size) { |
Frederick_H | 3:e4783c57bcc0 | 126 | int ack; |
Frederick_H | 3:e4783c57bcc0 | 127 | |
Frederick_H | 3:e4783c57bcc0 | 128 | ack = i2c_.write( ADXL345_I2C_WRITE, &address, 1); //tell it where to write to |
Frederick_H | 3:e4783c57bcc0 | 129 | return ack | i2c_.write( ADXL345_I2C_READ, ptr_data, size); //tell it what data to write |
Frederick_H | 3:e4783c57bcc0 | 130 | |
Frederick_H | 3:e4783c57bcc0 | 131 | } |
Frederick_H | 3:e4783c57bcc0 | 132 | |
Frederick_H | 3:e4783c57bcc0 | 133 | |
Frederick_H | 3:e4783c57bcc0 | 134 | void ADXL345_I2C::getOutput(int* readings){ |
Frederick_H | 3:e4783c57bcc0 | 135 | char buffer[6]; |
Frederick_H | 3:e4783c57bcc0 | 136 | multiByteRead(ADXL345_DATAX0_REG, buffer, 6); |
Frederick_H | 3:e4783c57bcc0 | 137 | |
Frederick_H | 3:e4783c57bcc0 | 138 | readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; |
Frederick_H | 3:e4783c57bcc0 | 139 | readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; |
Frederick_H | 3:e4783c57bcc0 | 140 | readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; |
Frederick_H | 3:e4783c57bcc0 | 141 | |
Frederick_H | 3:e4783c57bcc0 | 142 | } |
Frederick_H | 3:e4783c57bcc0 | 143 | |
Frederick_H | 3:e4783c57bcc0 | 144 | |
Frederick_H | 3:e4783c57bcc0 | 145 | |
Frederick_H | 3:e4783c57bcc0 | 146 | char ADXL345_I2C::getDeviceID() { |
Frederick_H | 3:e4783c57bcc0 | 147 | return SingleByteRead(ADXL345_DEVID_REG); |
Frederick_H | 3:e4783c57bcc0 | 148 | } |
Frederick_H | 3:e4783c57bcc0 | 149 | // |
Frederick_H | 3:e4783c57bcc0 | 150 | int ADXL345_I2C::setPowerMode(char mode) { |
Frederick_H | 3:e4783c57bcc0 | 151 | |
Frederick_H | 3:e4783c57bcc0 | 152 | //Get the current register contents, so we don't clobber the rate value. |
Frederick_H | 3:e4783c57bcc0 | 153 | char registerContents = (mode << 4) | SingleByteRead(ADXL345_BW_RATE_REG); |
Frederick_H | 3:e4783c57bcc0 | 154 | |
Frederick_H | 3:e4783c57bcc0 | 155 | return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents); |
Frederick_H | 3:e4783c57bcc0 | 156 | |
Frederick_H | 3:e4783c57bcc0 | 157 | } |
Frederick_H | 3:e4783c57bcc0 | 158 | |
Frederick_H | 3:e4783c57bcc0 | 159 | char ADXL345_I2C::getPowerControl() { |
Frederick_H | 3:e4783c57bcc0 | 160 | return SingleByteRead(ADXL345_POWER_CTL_REG); |
Frederick_H | 3:e4783c57bcc0 | 161 | } |
Frederick_H | 3:e4783c57bcc0 | 162 | |
Frederick_H | 3:e4783c57bcc0 | 163 | int ADXL345_I2C::setPowerControl(char settings) { |
Frederick_H | 3:e4783c57bcc0 | 164 | return SingleByteWrite(ADXL345_POWER_CTL_REG, settings); |
Frederick_H | 3:e4783c57bcc0 | 165 | |
Frederick_H | 3:e4783c57bcc0 | 166 | } |
Frederick_H | 3:e4783c57bcc0 | 167 | |
Frederick_H | 3:e4783c57bcc0 | 168 | |
Frederick_H | 3:e4783c57bcc0 | 169 | |
Frederick_H | 3:e4783c57bcc0 | 170 | char ADXL345_I2C::getDataFormatControl(void){ |
Frederick_H | 3:e4783c57bcc0 | 171 | |
Frederick_H | 3:e4783c57bcc0 | 172 | return SingleByteRead(ADXL345_DATA_FORMAT_REG); |
Frederick_H | 3:e4783c57bcc0 | 173 | } |
Frederick_H | 3:e4783c57bcc0 | 174 | |
Frederick_H | 3:e4783c57bcc0 | 175 | int ADXL345_I2C::setDataFormatControl(char settings){ |
Frederick_H | 3:e4783c57bcc0 | 176 | |
Frederick_H | 3:e4783c57bcc0 | 177 | return SingleByteWrite(ADXL345_DATA_FORMAT_REG, settings); |
Frederick_H | 3:e4783c57bcc0 | 178 | |
Frederick_H | 3:e4783c57bcc0 | 179 | } |
Frederick_H | 3:e4783c57bcc0 | 180 | |
Frederick_H | 3:e4783c57bcc0 | 181 | int ADXL345_I2C::setDataRate(char rate) { |
Frederick_H | 3:e4783c57bcc0 | 182 | |
Frederick_H | 3:e4783c57bcc0 | 183 | //Get the current register contents, so we don't clobber the power bit. |
Frederick_H | 3:e4783c57bcc0 | 184 | char registerContents = SingleByteRead(ADXL345_BW_RATE_REG); |
Frederick_H | 3:e4783c57bcc0 | 185 | |
Frederick_H | 3:e4783c57bcc0 | 186 | registerContents &= 0x10; |
Frederick_H | 3:e4783c57bcc0 | 187 | registerContents |= rate; |
Frederick_H | 3:e4783c57bcc0 | 188 | |
Frederick_H | 3:e4783c57bcc0 | 189 | return SingleByteWrite(ADXL345_BW_RATE_REG, registerContents); |
Frederick_H | 3:e4783c57bcc0 | 190 | |
Frederick_H | 3:e4783c57bcc0 | 191 | } |
Frederick_H | 3:e4783c57bcc0 | 192 | |
Frederick_H | 3:e4783c57bcc0 | 193 | |
Frederick_H | 3:e4783c57bcc0 | 194 | char ADXL345_I2C::getOffset(char axis) { |
Frederick_H | 3:e4783c57bcc0 | 195 | |
Frederick_H | 3:e4783c57bcc0 | 196 | char address = 0; |
Frederick_H | 3:e4783c57bcc0 | 197 | |
Frederick_H | 3:e4783c57bcc0 | 198 | if (axis == ADXL345_X) { |
Frederick_H | 3:e4783c57bcc0 | 199 | address = ADXL345_OFSX_REG; |
Frederick_H | 3:e4783c57bcc0 | 200 | } else if (axis == ADXL345_Y) { |
Frederick_H | 3:e4783c57bcc0 | 201 | address = ADXL345_OFSY_REG; |
Frederick_H | 3:e4783c57bcc0 | 202 | } else if (axis == ADXL345_Z) { |
Frederick_H | 3:e4783c57bcc0 | 203 | address = ADXL345_OFSZ_REG; |
Frederick_H | 3:e4783c57bcc0 | 204 | } |
Frederick_H | 3:e4783c57bcc0 | 205 | |
Frederick_H | 3:e4783c57bcc0 | 206 | return SingleByteRead(address); |
Frederick_H | 3:e4783c57bcc0 | 207 | } |
Frederick_H | 3:e4783c57bcc0 | 208 | |
Frederick_H | 3:e4783c57bcc0 | 209 | int ADXL345_I2C::setOffset(char axis, char offset) { |
Frederick_H | 3:e4783c57bcc0 | 210 | |
Frederick_H | 3:e4783c57bcc0 | 211 | char address = 0; |
Frederick_H | 3:e4783c57bcc0 | 212 | |
Frederick_H | 3:e4783c57bcc0 | 213 | if (axis == ADXL345_X) { |
Frederick_H | 3:e4783c57bcc0 | 214 | address = ADXL345_OFSX_REG; |
Frederick_H | 3:e4783c57bcc0 | 215 | } else if (axis == ADXL345_Y) { |
Frederick_H | 3:e4783c57bcc0 | 216 | address = ADXL345_OFSY_REG; |
Frederick_H | 3:e4783c57bcc0 | 217 | } else if (axis == ADXL345_Z) { |
Frederick_H | 3:e4783c57bcc0 | 218 | address = ADXL345_OFSZ_REG; |
Frederick_H | 3:e4783c57bcc0 | 219 | } |
Frederick_H | 3:e4783c57bcc0 | 220 | |
Frederick_H | 3:e4783c57bcc0 | 221 | return SingleByteWrite(address, offset); |
Frederick_H | 3:e4783c57bcc0 | 222 | |
Frederick_H | 3:e4783c57bcc0 | 223 | } |
Frederick_H | 3:e4783c57bcc0 | 224 | |
Frederick_H | 3:e4783c57bcc0 | 225 | |
Frederick_H | 3:e4783c57bcc0 | 226 | char ADXL345_I2C::getFifoControl(void){ |
Frederick_H | 3:e4783c57bcc0 | 227 | |
Frederick_H | 3:e4783c57bcc0 | 228 | return SingleByteRead(ADXL345_FIFO_CTL); |
Frederick_H | 3:e4783c57bcc0 | 229 | |
Frederick_H | 3:e4783c57bcc0 | 230 | } |
Frederick_H | 3:e4783c57bcc0 | 231 | |
Frederick_H | 3:e4783c57bcc0 | 232 | int ADXL345_I2C::setFifoControl(char settings){ |
Frederick_H | 3:e4783c57bcc0 | 233 | return SingleByteWrite(ADXL345_FIFO_STATUS, settings); |
Frederick_H | 3:e4783c57bcc0 | 234 | |
Frederick_H | 3:e4783c57bcc0 | 235 | } |
Frederick_H | 3:e4783c57bcc0 | 236 | |
Frederick_H | 3:e4783c57bcc0 | 237 | char ADXL345_I2C::getFifoStatus(void){ |
Frederick_H | 3:e4783c57bcc0 | 238 | |
Frederick_H | 3:e4783c57bcc0 | 239 | return SingleByteRead(ADXL345_FIFO_STATUS); |
Frederick_H | 3:e4783c57bcc0 | 240 | |
Frederick_H | 3:e4783c57bcc0 | 241 | } |
Frederick_H | 3:e4783c57bcc0 | 242 | |
Frederick_H | 3:e4783c57bcc0 | 243 | |
Frederick_H | 3:e4783c57bcc0 | 244 | |
Frederick_H | 3:e4783c57bcc0 | 245 | char ADXL345_I2C::getTapThreshold(void) { |
Frederick_H | 3:e4783c57bcc0 | 246 | |
Frederick_H | 3:e4783c57bcc0 | 247 | return SingleByteRead(ADXL345_THRESH_TAP_REG); |
Frederick_H | 3:e4783c57bcc0 | 248 | } |
Frederick_H | 3:e4783c57bcc0 | 249 | |
Frederick_H | 3:e4783c57bcc0 | 250 | int ADXL345_I2C::setTapThreshold(char threshold) { |
Frederick_H | 3:e4783c57bcc0 | 251 | |
Frederick_H | 3:e4783c57bcc0 | 252 | return SingleByteWrite(ADXL345_THRESH_TAP_REG, threshold); |
Frederick_H | 3:e4783c57bcc0 | 253 | |
Frederick_H | 3:e4783c57bcc0 | 254 | } |
Frederick_H | 3:e4783c57bcc0 | 255 | |
Frederick_H | 3:e4783c57bcc0 | 256 | |
Frederick_H | 3:e4783c57bcc0 | 257 | float ADXL345_I2C::getTapDuration(void) { |
Frederick_H | 3:e4783c57bcc0 | 258 | |
Frederick_H | 3:e4783c57bcc0 | 259 | return (float)SingleByteRead(ADXL345_DUR_REG)*625; |
Frederick_H | 3:e4783c57bcc0 | 260 | } |
Frederick_H | 3:e4783c57bcc0 | 261 | |
Frederick_H | 3:e4783c57bcc0 | 262 | int ADXL345_I2C::setTapDuration(short int duration_us) { |
Frederick_H | 3:e4783c57bcc0 | 263 | |
Frederick_H | 3:e4783c57bcc0 | 264 | short int tapDuration = duration_us / 625; |
Frederick_H | 3:e4783c57bcc0 | 265 | char tapChar[2]; |
Frederick_H | 3:e4783c57bcc0 | 266 | tapChar[0] = (tapDuration & 0x00FF); |
Frederick_H | 3:e4783c57bcc0 | 267 | tapChar[1] = (tapDuration >> 8) & 0x00FF; |
Frederick_H | 3:e4783c57bcc0 | 268 | return multiByteWrite(ADXL345_DUR_REG, tapChar, 2); |
Frederick_H | 3:e4783c57bcc0 | 269 | |
Frederick_H | 3:e4783c57bcc0 | 270 | } |
Frederick_H | 3:e4783c57bcc0 | 271 | |
Frederick_H | 3:e4783c57bcc0 | 272 | float ADXL345_I2C::getTapLatency(void) { |
Frederick_H | 3:e4783c57bcc0 | 273 | |
Frederick_H | 3:e4783c57bcc0 | 274 | return (float)SingleByteRead(ADXL345_LATENT_REG)*1.25; |
Frederick_H | 3:e4783c57bcc0 | 275 | } |
Frederick_H | 3:e4783c57bcc0 | 276 | |
Frederick_H | 3:e4783c57bcc0 | 277 | int ADXL345_I2C::setTapLatency(short int latency_ms) { |
Frederick_H | 3:e4783c57bcc0 | 278 | |
Frederick_H | 3:e4783c57bcc0 | 279 | latency_ms = latency_ms / 1.25; |
Frederick_H | 3:e4783c57bcc0 | 280 | char latChar[2]; |
Frederick_H | 3:e4783c57bcc0 | 281 | latChar[0] = (latency_ms & 0x00FF); |
Frederick_H | 3:e4783c57bcc0 | 282 | latChar[1] = (latency_ms << 8) & 0xFF00; |
Frederick_H | 3:e4783c57bcc0 | 283 | return multiByteWrite(ADXL345_LATENT_REG, latChar, 2); |
Frederick_H | 3:e4783c57bcc0 | 284 | |
Frederick_H | 3:e4783c57bcc0 | 285 | } |
Frederick_H | 3:e4783c57bcc0 | 286 | |
Frederick_H | 3:e4783c57bcc0 | 287 | float ADXL345_I2C::getWindowTime(void) { |
Frederick_H | 3:e4783c57bcc0 | 288 | |
Frederick_H | 3:e4783c57bcc0 | 289 | return (float)SingleByteRead(ADXL345_WINDOW_REG)*1.25; |
Frederick_H | 3:e4783c57bcc0 | 290 | } |
Frederick_H | 3:e4783c57bcc0 | 291 | |
Frederick_H | 3:e4783c57bcc0 | 292 | int ADXL345_I2C::setWindowTime(short int window_ms) { |
Frederick_H | 3:e4783c57bcc0 | 293 | |
Frederick_H | 3:e4783c57bcc0 | 294 | window_ms = window_ms / 1.25; |
Frederick_H | 3:e4783c57bcc0 | 295 | char windowChar[2]; |
Frederick_H | 3:e4783c57bcc0 | 296 | windowChar[0] = (window_ms & 0x00FF); |
Frederick_H | 3:e4783c57bcc0 | 297 | windowChar[1] = ((window_ms << 8) & 0xFF00); |
Frederick_H | 3:e4783c57bcc0 | 298 | return multiByteWrite(ADXL345_WINDOW_REG, windowChar, 2); |
Frederick_H | 3:e4783c57bcc0 | 299 | |
Frederick_H | 3:e4783c57bcc0 | 300 | } |
Frederick_H | 3:e4783c57bcc0 | 301 | |
Frederick_H | 3:e4783c57bcc0 | 302 | char ADXL345_I2C::getActivityThreshold(void) { |
Frederick_H | 3:e4783c57bcc0 | 303 | |
Frederick_H | 3:e4783c57bcc0 | 304 | return SingleByteRead(ADXL345_THRESH_ACT_REG); |
Frederick_H | 3:e4783c57bcc0 | 305 | } |
Frederick_H | 3:e4783c57bcc0 | 306 | |
Frederick_H | 3:e4783c57bcc0 | 307 | int ADXL345_I2C::setActivityThreshold(char threshold) { |
Frederick_H | 3:e4783c57bcc0 | 308 | return SingleByteWrite(ADXL345_THRESH_ACT_REG, threshold); |
Frederick_H | 3:e4783c57bcc0 | 309 | |
Frederick_H | 3:e4783c57bcc0 | 310 | } |
Frederick_H | 3:e4783c57bcc0 | 311 | |
Frederick_H | 3:e4783c57bcc0 | 312 | char ADXL345_I2C::getInactivityThreshold(void) { |
Frederick_H | 3:e4783c57bcc0 | 313 | return SingleByteRead(ADXL345_THRESH_INACT_REG); |
Frederick_H | 3:e4783c57bcc0 | 314 | |
Frederick_H | 3:e4783c57bcc0 | 315 | } |
Frederick_H | 3:e4783c57bcc0 | 316 | |
Frederick_H | 3:e4783c57bcc0 | 317 | //int FUNCTION(short int * ptr_Output) |
Frederick_H | 3:e4783c57bcc0 | 318 | //short int FUNCTION () |
Frederick_H | 3:e4783c57bcc0 | 319 | |
Frederick_H | 3:e4783c57bcc0 | 320 | int ADXL345_I2C::setInactivityThreshold(char threshold) { |
Frederick_H | 3:e4783c57bcc0 | 321 | return SingleByteWrite(ADXL345_THRESH_INACT_REG, threshold); |
Frederick_H | 3:e4783c57bcc0 | 322 | |
Frederick_H | 3:e4783c57bcc0 | 323 | } |
Frederick_H | 3:e4783c57bcc0 | 324 | |
Frederick_H | 3:e4783c57bcc0 | 325 | char ADXL345_I2C::getTimeInactivity(void) { |
Frederick_H | 3:e4783c57bcc0 | 326 | |
Frederick_H | 3:e4783c57bcc0 | 327 | return SingleByteRead(ADXL345_TIME_INACT_REG); |
Frederick_H | 3:e4783c57bcc0 | 328 | |
Frederick_H | 3:e4783c57bcc0 | 329 | } |
Frederick_H | 3:e4783c57bcc0 | 330 | |
Frederick_H | 3:e4783c57bcc0 | 331 | int ADXL345_I2C::setTimeInactivity(char timeInactivity) { |
Frederick_H | 3:e4783c57bcc0 | 332 | return SingleByteWrite(ADXL345_TIME_INACT_REG, timeInactivity); |
Frederick_H | 3:e4783c57bcc0 | 333 | |
Frederick_H | 3:e4783c57bcc0 | 334 | } |
Frederick_H | 3:e4783c57bcc0 | 335 | |
Frederick_H | 3:e4783c57bcc0 | 336 | char ADXL345_I2C::getActivityInactivityControl(void) { |
Frederick_H | 3:e4783c57bcc0 | 337 | |
Frederick_H | 3:e4783c57bcc0 | 338 | return SingleByteRead(ADXL345_ACT_INACT_CTL_REG); |
Frederick_H | 3:e4783c57bcc0 | 339 | |
Frederick_H | 3:e4783c57bcc0 | 340 | } |
Frederick_H | 3:e4783c57bcc0 | 341 | |
Frederick_H | 3:e4783c57bcc0 | 342 | int ADXL345_I2C::setActivityInactivityControl(char settings) { |
Frederick_H | 3:e4783c57bcc0 | 343 | return SingleByteWrite(ADXL345_ACT_INACT_CTL_REG, settings); |
Frederick_H | 3:e4783c57bcc0 | 344 | |
Frederick_H | 3:e4783c57bcc0 | 345 | } |
Frederick_H | 3:e4783c57bcc0 | 346 | |
Frederick_H | 3:e4783c57bcc0 | 347 | char ADXL345_I2C::getFreefallThreshold(void) { |
Frederick_H | 3:e4783c57bcc0 | 348 | |
Frederick_H | 3:e4783c57bcc0 | 349 | return SingleByteRead(ADXL345_THRESH_FF_REG); |
Frederick_H | 3:e4783c57bcc0 | 350 | |
Frederick_H | 3:e4783c57bcc0 | 351 | } |
Frederick_H | 3:e4783c57bcc0 | 352 | |
Frederick_H | 3:e4783c57bcc0 | 353 | int ADXL345_I2C::setFreefallThreshold(char threshold) { |
Frederick_H | 3:e4783c57bcc0 | 354 | return SingleByteWrite(ADXL345_THRESH_FF_REG, threshold); |
Frederick_H | 3:e4783c57bcc0 | 355 | |
Frederick_H | 3:e4783c57bcc0 | 356 | } |
Frederick_H | 3:e4783c57bcc0 | 357 | |
Frederick_H | 3:e4783c57bcc0 | 358 | char ADXL345_I2C::getFreefallTime(void) { |
Frederick_H | 3:e4783c57bcc0 | 359 | |
Frederick_H | 3:e4783c57bcc0 | 360 | return SingleByteRead(ADXL345_TIME_FF_REG)*5; |
Frederick_H | 3:e4783c57bcc0 | 361 | |
Frederick_H | 3:e4783c57bcc0 | 362 | } |
Frederick_H | 3:e4783c57bcc0 | 363 | |
Frederick_H | 3:e4783c57bcc0 | 364 | int ADXL345_I2C::setFreefallTime(short int freefallTime_ms) { |
Frederick_H | 3:e4783c57bcc0 | 365 | freefallTime_ms = freefallTime_ms / 5; |
Frederick_H | 3:e4783c57bcc0 | 366 | char fallChar[2]; |
Frederick_H | 3:e4783c57bcc0 | 367 | fallChar[0] = (freefallTime_ms & 0x00FF); |
Frederick_H | 3:e4783c57bcc0 | 368 | fallChar[1] = (freefallTime_ms << 8) & 0xFF00; |
Frederick_H | 3:e4783c57bcc0 | 369 | |
Frederick_H | 3:e4783c57bcc0 | 370 | return multiByteWrite(ADXL345_TIME_FF_REG, fallChar, 2); |
Frederick_H | 3:e4783c57bcc0 | 371 | |
Frederick_H | 3:e4783c57bcc0 | 372 | } |
Frederick_H | 3:e4783c57bcc0 | 373 | |
Frederick_H | 3:e4783c57bcc0 | 374 | char ADXL345_I2C::getTapAxisControl(void) { |
Frederick_H | 3:e4783c57bcc0 | 375 | |
Frederick_H | 3:e4783c57bcc0 | 376 | return SingleByteRead(ADXL345_TAP_AXES_REG); |
Frederick_H | 3:e4783c57bcc0 | 377 | |
Frederick_H | 3:e4783c57bcc0 | 378 | } |
Frederick_H | 3:e4783c57bcc0 | 379 | |
Frederick_H | 3:e4783c57bcc0 | 380 | int ADXL345_I2C::setTapAxisControl(char settings) { |
Frederick_H | 3:e4783c57bcc0 | 381 | return SingleByteWrite(ADXL345_TAP_AXES_REG, settings); |
Frederick_H | 3:e4783c57bcc0 | 382 | |
Frederick_H | 3:e4783c57bcc0 | 383 | } |
Frederick_H | 3:e4783c57bcc0 | 384 | |
Frederick_H | 3:e4783c57bcc0 | 385 | char ADXL345_I2C::getTapSource(void) { |
Frederick_H | 3:e4783c57bcc0 | 386 | |
Frederick_H | 3:e4783c57bcc0 | 387 | return SingleByteRead(ADXL345_ACT_TAP_STATUS_REG); |
Frederick_H | 3:e4783c57bcc0 | 388 | |
Frederick_H | 3:e4783c57bcc0 | 389 | } |
Frederick_H | 3:e4783c57bcc0 | 390 | |
Frederick_H | 3:e4783c57bcc0 | 391 | |
Frederick_H | 3:e4783c57bcc0 | 392 | |
Frederick_H | 3:e4783c57bcc0 | 393 | char ADXL345_I2C::getInterruptEnableControl(void) { |
Frederick_H | 3:e4783c57bcc0 | 394 | |
Frederick_H | 3:e4783c57bcc0 | 395 | return SingleByteRead(ADXL345_INT_ENABLE_REG); |
Frederick_H | 3:e4783c57bcc0 | 396 | |
Frederick_H | 3:e4783c57bcc0 | 397 | } |
Frederick_H | 3:e4783c57bcc0 | 398 | |
Frederick_H | 3:e4783c57bcc0 | 399 | int ADXL345_I2C::setInterruptEnableControl(char settings) { |
Frederick_H | 3:e4783c57bcc0 | 400 | return SingleByteWrite(ADXL345_INT_ENABLE_REG, settings); |
Frederick_H | 3:e4783c57bcc0 | 401 | |
Frederick_H | 3:e4783c57bcc0 | 402 | } |
Frederick_H | 3:e4783c57bcc0 | 403 | |
Frederick_H | 3:e4783c57bcc0 | 404 | char ADXL345_I2C::getInterruptMappingControl(void) { |
Frederick_H | 3:e4783c57bcc0 | 405 | |
Frederick_H | 3:e4783c57bcc0 | 406 | return SingleByteRead(ADXL345_INT_MAP_REG); |
Frederick_H | 3:e4783c57bcc0 | 407 | |
Frederick_H | 3:e4783c57bcc0 | 408 | } |
Frederick_H | 3:e4783c57bcc0 | 409 | |
Frederick_H | 3:e4783c57bcc0 | 410 | int ADXL345_I2C::setInterruptMappingControl(char settings) { |
Frederick_H | 3:e4783c57bcc0 | 411 | return SingleByteWrite(ADXL345_INT_MAP_REG, settings); |
Frederick_H | 3:e4783c57bcc0 | 412 | |
Frederick_H | 3:e4783c57bcc0 | 413 | } |
Frederick_H | 3:e4783c57bcc0 | 414 | |
Frederick_H | 3:e4783c57bcc0 | 415 | char ADXL345_I2C::getInterruptSource(void){ |
Frederick_H | 3:e4783c57bcc0 | 416 | |
Frederick_H | 3:e4783c57bcc0 | 417 | return SingleByteRead(ADXL345_INT_SOURCE_REG); |
Frederick_H | 3:e4783c57bcc0 | 418 | |
Frederick_H | 3:e4783c57bcc0 | 419 | } |
Frederick_H | 3:e4783c57bcc0 | 420 | |
Frederick_H | 3:e4783c57bcc0 | 421 | |
Frederick_H | 3:e4783c57bcc0 | 422 | |
Frederick_H | 3:e4783c57bcc0 | 423 |