![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Slingshot Controller
Dependencies: ADXL345 DebounceIn USBDevice mbed
ADXL345/ADXL345.cpp@1:2721dc2acc2c, 2012-10-17 (annotated)
- Committer:
- Brandon
- Date:
- Wed Oct 17 16:33:04 2012 +0000
- Revision:
- 1:2721dc2acc2c
- Parent:
- 0:cf17ea89fd09
Updated comments, added names, cleaned old code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Brandon | 0:cf17ea89fd09 | 1 | /** |
Brandon | 0:cf17ea89fd09 | 2 | * @author Aaron Berk |
Brandon | 0:cf17ea89fd09 | 3 | * |
Brandon | 0:cf17ea89fd09 | 4 | * @section LICENSE |
Brandon | 0:cf17ea89fd09 | 5 | * |
Brandon | 0:cf17ea89fd09 | 6 | * Copyright (c) 2010 ARM Limited |
Brandon | 0:cf17ea89fd09 | 7 | * |
Brandon | 0:cf17ea89fd09 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Brandon | 0:cf17ea89fd09 | 9 | * of this software and associated documentation files (the "Software"), to deal |
Brandon | 0:cf17ea89fd09 | 10 | * in the Software without restriction, including without limitation the rights |
Brandon | 0:cf17ea89fd09 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Brandon | 0:cf17ea89fd09 | 12 | * copies of the Software, and to permit persons to whom the Software is |
Brandon | 0:cf17ea89fd09 | 13 | * furnished to do so, subject to the following conditions: |
Brandon | 0:cf17ea89fd09 | 14 | * |
Brandon | 0:cf17ea89fd09 | 15 | * The above copyright notice and this permission notice shall be included in |
Brandon | 0:cf17ea89fd09 | 16 | * all copies or substantial portions of the Software. |
Brandon | 0:cf17ea89fd09 | 17 | * |
Brandon | 0:cf17ea89fd09 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Brandon | 0:cf17ea89fd09 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Brandon | 0:cf17ea89fd09 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Brandon | 0:cf17ea89fd09 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Brandon | 0:cf17ea89fd09 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Brandon | 0:cf17ea89fd09 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Brandon | 0:cf17ea89fd09 | 24 | * THE SOFTWARE. |
Brandon | 0:cf17ea89fd09 | 25 | * |
Brandon | 0:cf17ea89fd09 | 26 | * @section DESCRIPTION |
Brandon | 0:cf17ea89fd09 | 27 | * |
Brandon | 0:cf17ea89fd09 | 28 | * ADXL345, triple axis, digital interface, accelerometer. |
Brandon | 0:cf17ea89fd09 | 29 | * |
Brandon | 0:cf17ea89fd09 | 30 | * Datasheet: |
Brandon | 0:cf17ea89fd09 | 31 | * |
Brandon | 0:cf17ea89fd09 | 32 | * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf |
Brandon | 0:cf17ea89fd09 | 33 | */ |
Brandon | 0:cf17ea89fd09 | 34 | |
Brandon | 0:cf17ea89fd09 | 35 | /** |
Brandon | 0:cf17ea89fd09 | 36 | * Includes |
Brandon | 0:cf17ea89fd09 | 37 | */ |
Brandon | 0:cf17ea89fd09 | 38 | #include "ADXL345.h" |
Brandon | 0:cf17ea89fd09 | 39 | |
Brandon | 0:cf17ea89fd09 | 40 | ADXL345::ADXL345(PinName mosi, |
Brandon | 0:cf17ea89fd09 | 41 | PinName miso, |
Brandon | 0:cf17ea89fd09 | 42 | PinName sck, |
Brandon | 0:cf17ea89fd09 | 43 | PinName cs) : spi_(mosi, miso, sck), nCS_(cs) { |
Brandon | 0:cf17ea89fd09 | 44 | |
Brandon | 0:cf17ea89fd09 | 45 | //2MHz, allowing us to use the fastest data rates. |
Brandon | 0:cf17ea89fd09 | 46 | spi_.frequency(2000000); |
Brandon | 0:cf17ea89fd09 | 47 | spi_.format(8,3); |
Brandon | 0:cf17ea89fd09 | 48 | |
Brandon | 0:cf17ea89fd09 | 49 | nCS_ = 1; |
Brandon | 0:cf17ea89fd09 | 50 | |
Brandon | 0:cf17ea89fd09 | 51 | wait_us(500); |
Brandon | 0:cf17ea89fd09 | 52 | |
Brandon | 0:cf17ea89fd09 | 53 | } |
Brandon | 0:cf17ea89fd09 | 54 | |
Brandon | 0:cf17ea89fd09 | 55 | int ADXL345::getDevId(void) { |
Brandon | 0:cf17ea89fd09 | 56 | |
Brandon | 0:cf17ea89fd09 | 57 | return oneByteRead(ADXL345_DEVID_REG); |
Brandon | 0:cf17ea89fd09 | 58 | |
Brandon | 0:cf17ea89fd09 | 59 | } |
Brandon | 0:cf17ea89fd09 | 60 | |
Brandon | 0:cf17ea89fd09 | 61 | int ADXL345::getTapThreshold(void) { |
Brandon | 0:cf17ea89fd09 | 62 | |
Brandon | 0:cf17ea89fd09 | 63 | return oneByteRead(ADXL345_THRESH_TAP_REG); |
Brandon | 0:cf17ea89fd09 | 64 | |
Brandon | 0:cf17ea89fd09 | 65 | } |
Brandon | 0:cf17ea89fd09 | 66 | |
Brandon | 0:cf17ea89fd09 | 67 | void ADXL345::setTapThreshold(int threshold) { |
Brandon | 0:cf17ea89fd09 | 68 | |
Brandon | 0:cf17ea89fd09 | 69 | oneByteWrite(ADXL345_THRESH_TAP_REG, threshold); |
Brandon | 0:cf17ea89fd09 | 70 | |
Brandon | 0:cf17ea89fd09 | 71 | } |
Brandon | 0:cf17ea89fd09 | 72 | |
Brandon | 0:cf17ea89fd09 | 73 | int ADXL345::getOffset(int axis) { |
Brandon | 0:cf17ea89fd09 | 74 | |
Brandon | 0:cf17ea89fd09 | 75 | int address = 0; |
Brandon | 0:cf17ea89fd09 | 76 | |
Brandon | 0:cf17ea89fd09 | 77 | if (axis == ADXL345_X) { |
Brandon | 0:cf17ea89fd09 | 78 | address = ADXL345_OFSX_REG; |
Brandon | 0:cf17ea89fd09 | 79 | } else if (axis == ADXL345_Y) { |
Brandon | 0:cf17ea89fd09 | 80 | address = ADXL345_OFSY_REG; |
Brandon | 0:cf17ea89fd09 | 81 | } else if (axis == ADXL345_Z) { |
Brandon | 0:cf17ea89fd09 | 82 | address = ADXL345_OFSZ_REG; |
Brandon | 0:cf17ea89fd09 | 83 | } |
Brandon | 0:cf17ea89fd09 | 84 | |
Brandon | 0:cf17ea89fd09 | 85 | return oneByteRead(address); |
Brandon | 0:cf17ea89fd09 | 86 | |
Brandon | 0:cf17ea89fd09 | 87 | } |
Brandon | 0:cf17ea89fd09 | 88 | |
Brandon | 0:cf17ea89fd09 | 89 | void ADXL345::setOffset(int axis, char offset) { |
Brandon | 0:cf17ea89fd09 | 90 | |
Brandon | 0:cf17ea89fd09 | 91 | int address = 0; |
Brandon | 0:cf17ea89fd09 | 92 | |
Brandon | 0:cf17ea89fd09 | 93 | if (axis == ADXL345_X) { |
Brandon | 0:cf17ea89fd09 | 94 | address = ADXL345_OFSX_REG; |
Brandon | 0:cf17ea89fd09 | 95 | } else if (axis == ADXL345_Y) { |
Brandon | 0:cf17ea89fd09 | 96 | address = ADXL345_OFSY_REG; |
Brandon | 0:cf17ea89fd09 | 97 | } else if (axis == ADXL345_Z) { |
Brandon | 0:cf17ea89fd09 | 98 | address = ADXL345_OFSZ_REG; |
Brandon | 0:cf17ea89fd09 | 99 | } |
Brandon | 0:cf17ea89fd09 | 100 | |
Brandon | 0:cf17ea89fd09 | 101 | return oneByteWrite(address, offset); |
Brandon | 0:cf17ea89fd09 | 102 | |
Brandon | 0:cf17ea89fd09 | 103 | } |
Brandon | 0:cf17ea89fd09 | 104 | |
Brandon | 0:cf17ea89fd09 | 105 | int ADXL345::getTapDuration(void) { |
Brandon | 0:cf17ea89fd09 | 106 | |
Brandon | 0:cf17ea89fd09 | 107 | return oneByteRead(ADXL345_DUR_REG)*625; |
Brandon | 0:cf17ea89fd09 | 108 | |
Brandon | 0:cf17ea89fd09 | 109 | } |
Brandon | 0:cf17ea89fd09 | 110 | |
Brandon | 0:cf17ea89fd09 | 111 | void ADXL345::setTapDuration(int duration_us) { |
Brandon | 0:cf17ea89fd09 | 112 | |
Brandon | 0:cf17ea89fd09 | 113 | int tapDuration = duration_us / 625; |
Brandon | 0:cf17ea89fd09 | 114 | |
Brandon | 0:cf17ea89fd09 | 115 | oneByteWrite(ADXL345_DUR_REG, tapDuration); |
Brandon | 0:cf17ea89fd09 | 116 | |
Brandon | 0:cf17ea89fd09 | 117 | } |
Brandon | 0:cf17ea89fd09 | 118 | |
Brandon | 0:cf17ea89fd09 | 119 | float ADXL345::getTapLatency(void) { |
Brandon | 0:cf17ea89fd09 | 120 | |
Brandon | 0:cf17ea89fd09 | 121 | return oneByteRead(ADXL345_LATENT_REG)*1.25; |
Brandon | 0:cf17ea89fd09 | 122 | |
Brandon | 0:cf17ea89fd09 | 123 | } |
Brandon | 0:cf17ea89fd09 | 124 | |
Brandon | 0:cf17ea89fd09 | 125 | void ADXL345::setTapLatency(int latency_ms) { |
Brandon | 0:cf17ea89fd09 | 126 | |
Brandon | 0:cf17ea89fd09 | 127 | int tapLatency = latency_ms / 1.25; |
Brandon | 0:cf17ea89fd09 | 128 | |
Brandon | 0:cf17ea89fd09 | 129 | oneByteWrite(ADXL345_LATENT_REG, tapLatency); |
Brandon | 0:cf17ea89fd09 | 130 | |
Brandon | 0:cf17ea89fd09 | 131 | } |
Brandon | 0:cf17ea89fd09 | 132 | |
Brandon | 0:cf17ea89fd09 | 133 | float ADXL345::getWindowTime(void) { |
Brandon | 0:cf17ea89fd09 | 134 | |
Brandon | 0:cf17ea89fd09 | 135 | return oneByteRead(ADXL345_WINDOW_REG)*1.25; |
Brandon | 0:cf17ea89fd09 | 136 | |
Brandon | 0:cf17ea89fd09 | 137 | } |
Brandon | 0:cf17ea89fd09 | 138 | |
Brandon | 0:cf17ea89fd09 | 139 | void ADXL345::setWindowTime(int window_ms) { |
Brandon | 0:cf17ea89fd09 | 140 | |
Brandon | 0:cf17ea89fd09 | 141 | int windowTime = window_ms / 1.25; |
Brandon | 0:cf17ea89fd09 | 142 | |
Brandon | 0:cf17ea89fd09 | 143 | oneByteWrite(ADXL345_WINDOW_REG, windowTime); |
Brandon | 0:cf17ea89fd09 | 144 | |
Brandon | 0:cf17ea89fd09 | 145 | } |
Brandon | 0:cf17ea89fd09 | 146 | |
Brandon | 0:cf17ea89fd09 | 147 | int ADXL345::getActivityThreshold(void) { |
Brandon | 0:cf17ea89fd09 | 148 | |
Brandon | 0:cf17ea89fd09 | 149 | return oneByteRead(ADXL345_THRESH_ACT_REG); |
Brandon | 0:cf17ea89fd09 | 150 | |
Brandon | 0:cf17ea89fd09 | 151 | } |
Brandon | 0:cf17ea89fd09 | 152 | |
Brandon | 0:cf17ea89fd09 | 153 | void ADXL345::setActivityThreshold(int threshold) { |
Brandon | 0:cf17ea89fd09 | 154 | |
Brandon | 0:cf17ea89fd09 | 155 | oneByteWrite(ADXL345_THRESH_ACT_REG, threshold); |
Brandon | 0:cf17ea89fd09 | 156 | |
Brandon | 0:cf17ea89fd09 | 157 | } |
Brandon | 0:cf17ea89fd09 | 158 | |
Brandon | 0:cf17ea89fd09 | 159 | int ADXL345::getInactivityThreshold(void) { |
Brandon | 0:cf17ea89fd09 | 160 | |
Brandon | 0:cf17ea89fd09 | 161 | return oneByteRead(ADXL345_THRESH_INACT_REG); |
Brandon | 0:cf17ea89fd09 | 162 | |
Brandon | 0:cf17ea89fd09 | 163 | } |
Brandon | 0:cf17ea89fd09 | 164 | |
Brandon | 0:cf17ea89fd09 | 165 | void ADXL345::setInactivityThreshold(int threshold) { |
Brandon | 0:cf17ea89fd09 | 166 | |
Brandon | 0:cf17ea89fd09 | 167 | return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold); |
Brandon | 0:cf17ea89fd09 | 168 | |
Brandon | 0:cf17ea89fd09 | 169 | } |
Brandon | 0:cf17ea89fd09 | 170 | |
Brandon | 0:cf17ea89fd09 | 171 | int ADXL345::getTimeInactivity(void) { |
Brandon | 0:cf17ea89fd09 | 172 | |
Brandon | 0:cf17ea89fd09 | 173 | return oneByteRead(ADXL345_TIME_INACT_REG); |
Brandon | 0:cf17ea89fd09 | 174 | |
Brandon | 0:cf17ea89fd09 | 175 | } |
Brandon | 0:cf17ea89fd09 | 176 | |
Brandon | 0:cf17ea89fd09 | 177 | void ADXL345::setTimeInactivity(int timeInactivity) { |
Brandon | 0:cf17ea89fd09 | 178 | |
Brandon | 0:cf17ea89fd09 | 179 | oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity); |
Brandon | 0:cf17ea89fd09 | 180 | |
Brandon | 0:cf17ea89fd09 | 181 | } |
Brandon | 0:cf17ea89fd09 | 182 | |
Brandon | 0:cf17ea89fd09 | 183 | int ADXL345::getActivityInactivityControl(void) { |
Brandon | 0:cf17ea89fd09 | 184 | |
Brandon | 0:cf17ea89fd09 | 185 | return oneByteRead(ADXL345_ACT_INACT_CTL_REG); |
Brandon | 0:cf17ea89fd09 | 186 | |
Brandon | 0:cf17ea89fd09 | 187 | } |
Brandon | 0:cf17ea89fd09 | 188 | |
Brandon | 0:cf17ea89fd09 | 189 | void ADXL345::setActivityInactivityControl(int settings) { |
Brandon | 0:cf17ea89fd09 | 190 | |
Brandon | 0:cf17ea89fd09 | 191 | oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings); |
Brandon | 0:cf17ea89fd09 | 192 | |
Brandon | 0:cf17ea89fd09 | 193 | } |
Brandon | 0:cf17ea89fd09 | 194 | |
Brandon | 0:cf17ea89fd09 | 195 | int ADXL345::getFreefallThreshold(void) { |
Brandon | 0:cf17ea89fd09 | 196 | |
Brandon | 0:cf17ea89fd09 | 197 | return oneByteRead(ADXL345_THRESH_FF_REG); |
Brandon | 0:cf17ea89fd09 | 198 | |
Brandon | 0:cf17ea89fd09 | 199 | } |
Brandon | 0:cf17ea89fd09 | 200 | |
Brandon | 0:cf17ea89fd09 | 201 | void ADXL345::setFreefallThreshold(int threshold) { |
Brandon | 0:cf17ea89fd09 | 202 | |
Brandon | 0:cf17ea89fd09 | 203 | oneByteWrite(ADXL345_THRESH_FF_REG, threshold); |
Brandon | 0:cf17ea89fd09 | 204 | |
Brandon | 0:cf17ea89fd09 | 205 | } |
Brandon | 0:cf17ea89fd09 | 206 | |
Brandon | 0:cf17ea89fd09 | 207 | int ADXL345::getFreefallTime(void) { |
Brandon | 0:cf17ea89fd09 | 208 | |
Brandon | 0:cf17ea89fd09 | 209 | return oneByteRead(ADXL345_TIME_FF_REG)*5; |
Brandon | 0:cf17ea89fd09 | 210 | |
Brandon | 0:cf17ea89fd09 | 211 | } |
Brandon | 0:cf17ea89fd09 | 212 | |
Brandon | 0:cf17ea89fd09 | 213 | void ADXL345::setFreefallTime(int freefallTime_ms) { |
Brandon | 0:cf17ea89fd09 | 214 | |
Brandon | 0:cf17ea89fd09 | 215 | int freefallTime = freefallTime_ms / 5; |
Brandon | 0:cf17ea89fd09 | 216 | |
Brandon | 0:cf17ea89fd09 | 217 | oneByteWrite(ADXL345_TIME_FF_REG, freefallTime); |
Brandon | 0:cf17ea89fd09 | 218 | |
Brandon | 0:cf17ea89fd09 | 219 | } |
Brandon | 0:cf17ea89fd09 | 220 | |
Brandon | 0:cf17ea89fd09 | 221 | int ADXL345::getTapAxisControl(void) { |
Brandon | 0:cf17ea89fd09 | 222 | |
Brandon | 0:cf17ea89fd09 | 223 | return oneByteRead(ADXL345_TAP_AXES_REG); |
Brandon | 0:cf17ea89fd09 | 224 | |
Brandon | 0:cf17ea89fd09 | 225 | } |
Brandon | 0:cf17ea89fd09 | 226 | |
Brandon | 0:cf17ea89fd09 | 227 | void ADXL345::setTapAxisControl(int settings) { |
Brandon | 0:cf17ea89fd09 | 228 | |
Brandon | 0:cf17ea89fd09 | 229 | oneByteWrite(ADXL345_TAP_AXES_REG, settings); |
Brandon | 0:cf17ea89fd09 | 230 | |
Brandon | 0:cf17ea89fd09 | 231 | } |
Brandon | 0:cf17ea89fd09 | 232 | |
Brandon | 0:cf17ea89fd09 | 233 | int ADXL345::getTapSource(void) { |
Brandon | 0:cf17ea89fd09 | 234 | |
Brandon | 0:cf17ea89fd09 | 235 | return oneByteRead(ADXL345_ACT_TAP_STATUS_REG); |
Brandon | 0:cf17ea89fd09 | 236 | |
Brandon | 0:cf17ea89fd09 | 237 | } |
Brandon | 0:cf17ea89fd09 | 238 | |
Brandon | 0:cf17ea89fd09 | 239 | void ADXL345::setPowerMode(char mode) { |
Brandon | 0:cf17ea89fd09 | 240 | |
Brandon | 0:cf17ea89fd09 | 241 | //Get the current register contents, so we don't clobber the rate value. |
Brandon | 0:cf17ea89fd09 | 242 | char registerContents = oneByteRead(ADXL345_BW_RATE_REG); |
Brandon | 0:cf17ea89fd09 | 243 | |
Brandon | 0:cf17ea89fd09 | 244 | registerContents = (mode << 4) | registerContents; |
Brandon | 0:cf17ea89fd09 | 245 | |
Brandon | 0:cf17ea89fd09 | 246 | oneByteWrite(ADXL345_BW_RATE_REG, registerContents); |
Brandon | 0:cf17ea89fd09 | 247 | |
Brandon | 0:cf17ea89fd09 | 248 | } |
Brandon | 0:cf17ea89fd09 | 249 | |
Brandon | 0:cf17ea89fd09 | 250 | int ADXL345::getPowerControl(void) { |
Brandon | 0:cf17ea89fd09 | 251 | |
Brandon | 0:cf17ea89fd09 | 252 | return oneByteRead(ADXL345_POWER_CTL_REG); |
Brandon | 0:cf17ea89fd09 | 253 | |
Brandon | 0:cf17ea89fd09 | 254 | } |
Brandon | 0:cf17ea89fd09 | 255 | |
Brandon | 0:cf17ea89fd09 | 256 | void ADXL345::setPowerControl(int settings) { |
Brandon | 0:cf17ea89fd09 | 257 | |
Brandon | 0:cf17ea89fd09 | 258 | oneByteWrite(ADXL345_POWER_CTL_REG, settings); |
Brandon | 0:cf17ea89fd09 | 259 | |
Brandon | 0:cf17ea89fd09 | 260 | } |
Brandon | 0:cf17ea89fd09 | 261 | |
Brandon | 0:cf17ea89fd09 | 262 | int ADXL345::getInterruptEnableControl(void) { |
Brandon | 0:cf17ea89fd09 | 263 | |
Brandon | 0:cf17ea89fd09 | 264 | return oneByteRead(ADXL345_INT_ENABLE_REG); |
Brandon | 0:cf17ea89fd09 | 265 | |
Brandon | 0:cf17ea89fd09 | 266 | } |
Brandon | 0:cf17ea89fd09 | 267 | |
Brandon | 0:cf17ea89fd09 | 268 | void ADXL345::setInterruptEnableControl(int settings) { |
Brandon | 0:cf17ea89fd09 | 269 | |
Brandon | 0:cf17ea89fd09 | 270 | oneByteWrite(ADXL345_INT_ENABLE_REG, settings); |
Brandon | 0:cf17ea89fd09 | 271 | |
Brandon | 0:cf17ea89fd09 | 272 | } |
Brandon | 0:cf17ea89fd09 | 273 | |
Brandon | 0:cf17ea89fd09 | 274 | int ADXL345::getInterruptMappingControl(void) { |
Brandon | 0:cf17ea89fd09 | 275 | |
Brandon | 0:cf17ea89fd09 | 276 | return oneByteRead(ADXL345_INT_MAP_REG); |
Brandon | 0:cf17ea89fd09 | 277 | |
Brandon | 0:cf17ea89fd09 | 278 | } |
Brandon | 0:cf17ea89fd09 | 279 | |
Brandon | 0:cf17ea89fd09 | 280 | void ADXL345::setInterruptMappingControl(int settings) { |
Brandon | 0:cf17ea89fd09 | 281 | |
Brandon | 0:cf17ea89fd09 | 282 | oneByteWrite(ADXL345_INT_MAP_REG, settings); |
Brandon | 0:cf17ea89fd09 | 283 | |
Brandon | 0:cf17ea89fd09 | 284 | } |
Brandon | 0:cf17ea89fd09 | 285 | |
Brandon | 0:cf17ea89fd09 | 286 | int ADXL345::getInterruptSource(void){ |
Brandon | 0:cf17ea89fd09 | 287 | |
Brandon | 0:cf17ea89fd09 | 288 | return oneByteRead(ADXL345_INT_SOURCE_REG); |
Brandon | 0:cf17ea89fd09 | 289 | |
Brandon | 0:cf17ea89fd09 | 290 | } |
Brandon | 0:cf17ea89fd09 | 291 | |
Brandon | 0:cf17ea89fd09 | 292 | int ADXL345::getDataFormatControl(void){ |
Brandon | 0:cf17ea89fd09 | 293 | |
Brandon | 0:cf17ea89fd09 | 294 | return oneByteRead(ADXL345_DATA_FORMAT_REG); |
Brandon | 0:cf17ea89fd09 | 295 | |
Brandon | 0:cf17ea89fd09 | 296 | } |
Brandon | 0:cf17ea89fd09 | 297 | |
Brandon | 0:cf17ea89fd09 | 298 | void ADXL345::setDataFormatControl(int settings){ |
Brandon | 0:cf17ea89fd09 | 299 | |
Brandon | 0:cf17ea89fd09 | 300 | oneByteWrite(ADXL345_DATA_FORMAT_REG, settings); |
Brandon | 0:cf17ea89fd09 | 301 | |
Brandon | 0:cf17ea89fd09 | 302 | } |
Brandon | 0:cf17ea89fd09 | 303 | |
Brandon | 0:cf17ea89fd09 | 304 | void ADXL345::setDataRate(int rate) { |
Brandon | 0:cf17ea89fd09 | 305 | |
Brandon | 0:cf17ea89fd09 | 306 | //Get the current register contents, so we don't clobber the power bit. |
Brandon | 0:cf17ea89fd09 | 307 | char registerContents = oneByteRead(ADXL345_BW_RATE_REG); |
Brandon | 0:cf17ea89fd09 | 308 | |
Brandon | 0:cf17ea89fd09 | 309 | registerContents &= 0x10; |
Brandon | 0:cf17ea89fd09 | 310 | registerContents |= rate; |
Brandon | 0:cf17ea89fd09 | 311 | |
Brandon | 0:cf17ea89fd09 | 312 | oneByteWrite(ADXL345_BW_RATE_REG, registerContents); |
Brandon | 0:cf17ea89fd09 | 313 | |
Brandon | 0:cf17ea89fd09 | 314 | } |
Brandon | 0:cf17ea89fd09 | 315 | |
Brandon | 0:cf17ea89fd09 | 316 | void ADXL345::getOutput(int* readings){ |
Brandon | 0:cf17ea89fd09 | 317 | |
Brandon | 0:cf17ea89fd09 | 318 | char buffer[6]; |
Brandon | 0:cf17ea89fd09 | 319 | |
Brandon | 0:cf17ea89fd09 | 320 | multiByteRead(ADXL345_DATAX0_REG, buffer, 6); |
Brandon | 0:cf17ea89fd09 | 321 | |
Brandon | 0:cf17ea89fd09 | 322 | readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; |
Brandon | 0:cf17ea89fd09 | 323 | readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; |
Brandon | 0:cf17ea89fd09 | 324 | readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; |
Brandon | 0:cf17ea89fd09 | 325 | |
Brandon | 0:cf17ea89fd09 | 326 | } |
Brandon | 0:cf17ea89fd09 | 327 | |
Brandon | 0:cf17ea89fd09 | 328 | int ADXL345::getFifoControl(void){ |
Brandon | 0:cf17ea89fd09 | 329 | |
Brandon | 0:cf17ea89fd09 | 330 | return oneByteRead(ADXL345_FIFO_CTL); |
Brandon | 0:cf17ea89fd09 | 331 | |
Brandon | 0:cf17ea89fd09 | 332 | } |
Brandon | 0:cf17ea89fd09 | 333 | |
Brandon | 0:cf17ea89fd09 | 334 | void ADXL345::setFifoControl(int settings){ |
Brandon | 0:cf17ea89fd09 | 335 | |
Brandon | 0:cf17ea89fd09 | 336 | oneByteWrite(ADXL345_FIFO_STATUS, settings); |
Brandon | 0:cf17ea89fd09 | 337 | |
Brandon | 0:cf17ea89fd09 | 338 | } |
Brandon | 0:cf17ea89fd09 | 339 | |
Brandon | 0:cf17ea89fd09 | 340 | int ADXL345::getFifoStatus(void){ |
Brandon | 0:cf17ea89fd09 | 341 | |
Brandon | 0:cf17ea89fd09 | 342 | return oneByteRead(ADXL345_FIFO_STATUS); |
Brandon | 0:cf17ea89fd09 | 343 | |
Brandon | 0:cf17ea89fd09 | 344 | } |
Brandon | 0:cf17ea89fd09 | 345 | |
Brandon | 0:cf17ea89fd09 | 346 | int ADXL345::oneByteRead(int address) { |
Brandon | 0:cf17ea89fd09 | 347 | |
Brandon | 0:cf17ea89fd09 | 348 | int tx = (ADXL345_SPI_READ | (address & 0x3F)); |
Brandon | 0:cf17ea89fd09 | 349 | int rx = 0; |
Brandon | 0:cf17ea89fd09 | 350 | |
Brandon | 0:cf17ea89fd09 | 351 | nCS_ = 0; |
Brandon | 0:cf17ea89fd09 | 352 | //Send address to read from. |
Brandon | 0:cf17ea89fd09 | 353 | spi_.write(tx); |
Brandon | 0:cf17ea89fd09 | 354 | //Read back contents of address. |
Brandon | 0:cf17ea89fd09 | 355 | rx = spi_.write(0x00); |
Brandon | 0:cf17ea89fd09 | 356 | nCS_ = 1; |
Brandon | 0:cf17ea89fd09 | 357 | |
Brandon | 0:cf17ea89fd09 | 358 | return rx; |
Brandon | 0:cf17ea89fd09 | 359 | |
Brandon | 0:cf17ea89fd09 | 360 | } |
Brandon | 0:cf17ea89fd09 | 361 | |
Brandon | 0:cf17ea89fd09 | 362 | void ADXL345::oneByteWrite(int address, char data) { |
Brandon | 0:cf17ea89fd09 | 363 | |
Brandon | 0:cf17ea89fd09 | 364 | int tx = (ADXL345_SPI_WRITE | (address & 0x3F)); |
Brandon | 0:cf17ea89fd09 | 365 | |
Brandon | 0:cf17ea89fd09 | 366 | nCS_ = 0; |
Brandon | 0:cf17ea89fd09 | 367 | //Send address to write to. |
Brandon | 0:cf17ea89fd09 | 368 | spi_.write(tx); |
Brandon | 0:cf17ea89fd09 | 369 | //Send data to be written. |
Brandon | 0:cf17ea89fd09 | 370 | spi_.write(data); |
Brandon | 0:cf17ea89fd09 | 371 | nCS_ = 1; |
Brandon | 0:cf17ea89fd09 | 372 | |
Brandon | 0:cf17ea89fd09 | 373 | } |
Brandon | 0:cf17ea89fd09 | 374 | |
Brandon | 0:cf17ea89fd09 | 375 | void ADXL345::multiByteRead(int startAddress, char* buffer, int size) { |
Brandon | 0:cf17ea89fd09 | 376 | |
Brandon | 0:cf17ea89fd09 | 377 | int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); |
Brandon | 0:cf17ea89fd09 | 378 | |
Brandon | 0:cf17ea89fd09 | 379 | nCS_ = 0; |
Brandon | 0:cf17ea89fd09 | 380 | //Send address to start reading from. |
Brandon | 0:cf17ea89fd09 | 381 | spi_.write(tx); |
Brandon | 0:cf17ea89fd09 | 382 | |
Brandon | 0:cf17ea89fd09 | 383 | for (int i = 0; i < size; i++) { |
Brandon | 0:cf17ea89fd09 | 384 | buffer[i] = spi_.write(0x00); |
Brandon | 0:cf17ea89fd09 | 385 | } |
Brandon | 0:cf17ea89fd09 | 386 | |
Brandon | 0:cf17ea89fd09 | 387 | nCS_ = 1; |
Brandon | 0:cf17ea89fd09 | 388 | |
Brandon | 0:cf17ea89fd09 | 389 | } |
Brandon | 0:cf17ea89fd09 | 390 | |
Brandon | 0:cf17ea89fd09 | 391 | void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) { |
Brandon | 0:cf17ea89fd09 | 392 | |
Brandon | 0:cf17ea89fd09 | 393 | int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); |
Brandon | 0:cf17ea89fd09 | 394 | |
Brandon | 0:cf17ea89fd09 | 395 | nCS_ = 0; |
Brandon | 0:cf17ea89fd09 | 396 | //Send address to start reading from. |
Brandon | 0:cf17ea89fd09 | 397 | spi_.write(tx); |
Brandon | 0:cf17ea89fd09 | 398 | |
Brandon | 0:cf17ea89fd09 | 399 | for (int i = 0; i < size; i++) { |
Brandon | 0:cf17ea89fd09 | 400 | buffer[i] = spi_.write(0x00); |
Brandon | 0:cf17ea89fd09 | 401 | } |
Brandon | 0:cf17ea89fd09 | 402 | |
Brandon | 0:cf17ea89fd09 | 403 | nCS_ = 1; |
Brandon | 0:cf17ea89fd09 | 404 | |
Brandon | 0:cf17ea89fd09 | 405 | } |