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