Controller for smartbot and drawbot

Dependencies:   mbed

Committer:
theschrade54
Date:
Fri Dec 14 05:03:08 2012 +0000
Revision:
0:43ba2b4b613b
Etch Sketch Controller

Who changed what in which revision?

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