ADXL345 test on L476

Dependencies:   mbed

Committer:
tifo
Date:
Wed Feb 07 16:05:59 2018 +0000
Revision:
4:d850da8732c1
Parent:
0:a0f7c6807a3a
buzzer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tifo 0:a0f7c6807a3a 1 #include "adxl345.h"
tifo 0:a0f7c6807a3a 2
tifo 0:a0f7c6807a3a 3 I2C i2c(PB_9, PB_8);
tifo 0:a0f7c6807a3a 4
tifo 0:a0f7c6807a3a 5 #define ADXL345_DEVICE (0xA6) // Device Address for ADXL345
tifo 0:a0f7c6807a3a 6 #define ADXL345_TO_READ (6) // Number of chars Read - Two chars Per Axis
tifo 0:a0f7c6807a3a 7
tifo 0:a0f7c6807a3a 8 ADXL345::ADXL345() {
tifo 0:a0f7c6807a3a 9 status = ADXL345_OK;
tifo 0:a0f7c6807a3a 10 error_code = ADXL345_NO_ERROR;
tifo 0:a0f7c6807a3a 11
tifo 0:a0f7c6807a3a 12 gains[0] = 0.00376390; // Original gain 0.00376390
tifo 0:a0f7c6807a3a 13 gains[1] = 0.00376009; // Original gain 0.00376009
tifo 0:a0f7c6807a3a 14 gains[2] = 0.00349265; // Original gain 0.00349265
tifo 0:a0f7c6807a3a 15 }
tifo 0:a0f7c6807a3a 16
tifo 0:a0f7c6807a3a 17
tifo 0:a0f7c6807a3a 18 void ADXL345::powerOn() {
tifo 0:a0f7c6807a3a 19
tifo 0:a0f7c6807a3a 20 //ADXL345 TURN ON
tifo 0:a0f7c6807a3a 21 writeToI2C(ADXL345_POWER_CTL, 0); // Wakeup
tifo 0:a0f7c6807a3a 22 writeToI2C(ADXL345_POWER_CTL, 16); // Auto_Sleep
tifo 0:a0f7c6807a3a 23 writeToI2C(ADXL345_POWER_CTL, 8); // Measure
tifo 0:a0f7c6807a3a 24
tifo 0:a0f7c6807a3a 25 }
tifo 0:a0f7c6807a3a 26
tifo 0:a0f7c6807a3a 27
tifo 0:a0f7c6807a3a 28 /*********************** READING ACCELERATION ***********************/
tifo 0:a0f7c6807a3a 29 /* Reads Acceleration into Three Variables: x, y and z */
tifo 0:a0f7c6807a3a 30
tifo 0:a0f7c6807a3a 31 void ADXL345::readAccel(int *xyz){
tifo 0:a0f7c6807a3a 32 readAccel(xyz, xyz + 1, xyz + 2);
tifo 0:a0f7c6807a3a 33 }
tifo 0:a0f7c6807a3a 34
tifo 0:a0f7c6807a3a 35 void ADXL345::readAccel(int *x, int *y, int *z) {
tifo 0:a0f7c6807a3a 36 readFromI2C(ADXL345_DATAX0, ADXL345_TO_READ, _buff); // Read Accel Data from ADXL345
tifo 0:a0f7c6807a3a 37
tifo 0:a0f7c6807a3a 38 // Each Axis @ All g Ranges: 10 Bit Resolution (2 chars)
tifo 0:a0f7c6807a3a 39
tifo 0:a0f7c6807a3a 40 *x = (int16_t)((((int)_buff[1]) << 8) | _buff[0]);
tifo 0:a0f7c6807a3a 41 *y = (int16_t)((((int)_buff[3]) << 8) | _buff[2]);
tifo 0:a0f7c6807a3a 42 *z = (int16_t)((((int)_buff[5]) << 8) | _buff[4]);
tifo 0:a0f7c6807a3a 43
tifo 0:a0f7c6807a3a 44 }
tifo 0:a0f7c6807a3a 45
tifo 0:a0f7c6807a3a 46 void ADXL345::get_Gxyz(double *xyz){
tifo 0:a0f7c6807a3a 47 int i;
tifo 0:a0f7c6807a3a 48 int xyz_int[3];
tifo 0:a0f7c6807a3a 49 readAccel(xyz_int);
tifo 0:a0f7c6807a3a 50 for(i=0; i<3; i++){
tifo 0:a0f7c6807a3a 51 xyz[i] = xyz_int[i] * gains[i];
tifo 0:a0f7c6807a3a 52 }
tifo 0:a0f7c6807a3a 53 }
tifo 0:a0f7c6807a3a 54
tifo 0:a0f7c6807a3a 55
tifo 0:a0f7c6807a3a 56 /*************************** WRITE TO I2C ***************************/
tifo 0:a0f7c6807a3a 57 /* Start; Send Register Address; Send Value To Write; End */
tifo 0:a0f7c6807a3a 58 void ADXL345::writeToI2C(char _address, char _val) {
tifo 0:a0f7c6807a3a 59
tifo 0:a0f7c6807a3a 60 char data_write[2];
tifo 0:a0f7c6807a3a 61 data_write[0] = _address;
tifo 0:a0f7c6807a3a 62 data_write[1] = _val;
tifo 0:a0f7c6807a3a 63
tifo 0:a0f7c6807a3a 64 i2c.write(ADXL345_DEVICE, data_write, 2, 0);
tifo 0:a0f7c6807a3a 65 }
tifo 0:a0f7c6807a3a 66
tifo 0:a0f7c6807a3a 67 /*************************** READ FROM I2C **************************/
tifo 0:a0f7c6807a3a 68 /* Start; Send Address To Read; End */
tifo 0:a0f7c6807a3a 69 void ADXL345::readFromI2C(char address, int num, char _buff[]) {
tifo 0:a0f7c6807a3a 70
tifo 0:a0f7c6807a3a 71 char data_write[1];
tifo 0:a0f7c6807a3a 72 data_write[0] = address;
tifo 0:a0f7c6807a3a 73 i2c.write(ADXL345_DEVICE, data_write, 1, 1);
tifo 0:a0f7c6807a3a 74 i2c.read(ADXL345_DEVICE, _buff, num, 0);
tifo 0:a0f7c6807a3a 75 }
tifo 0:a0f7c6807a3a 76
tifo 0:a0f7c6807a3a 77 void ADXL345::setRangeSetting(int val) {
tifo 0:a0f7c6807a3a 78 char _s;
tifo 0:a0f7c6807a3a 79 char _b;
tifo 0:a0f7c6807a3a 80
tifo 0:a0f7c6807a3a 81 switch (val) {
tifo 0:a0f7c6807a3a 82 case 2:
tifo 0:a0f7c6807a3a 83 _s = 00000000;
tifo 0:a0f7c6807a3a 84 break;
tifo 0:a0f7c6807a3a 85 case 4:
tifo 0:a0f7c6807a3a 86 _s = 00000001;
tifo 0:a0f7c6807a3a 87 break;
tifo 0:a0f7c6807a3a 88 case 8:
tifo 0:a0f7c6807a3a 89 _s = 00000010;
tifo 0:a0f7c6807a3a 90 break;
tifo 0:a0f7c6807a3a 91 case 16:
tifo 0:a0f7c6807a3a 92 _s = 00000011;
tifo 0:a0f7c6807a3a 93 break;
tifo 0:a0f7c6807a3a 94 default:
tifo 0:a0f7c6807a3a 95 _s = 00000000;
tifo 0:a0f7c6807a3a 96 }
tifo 0:a0f7c6807a3a 97 readFromI2C(ADXL345_DATA_FORMAT, 1, &_b);
tifo 0:a0f7c6807a3a 98 _s |= (_b & 11101100);
tifo 0:a0f7c6807a3a 99 writeToI2C(ADXL345_DATA_FORMAT, _s);
tifo 0:a0f7c6807a3a 100 }
tifo 0:a0f7c6807a3a 101
tifo 0:a0f7c6807a3a 102 /*************************** SELF_TEST BIT **************************/
tifo 0:a0f7c6807a3a 103 /* ~ GET & SET */
tifo 0:a0f7c6807a3a 104 bool ADXL345::getSelfTestBit() {
tifo 0:a0f7c6807a3a 105 return getRegisterBit(ADXL345_DATA_FORMAT, 7);
tifo 0:a0f7c6807a3a 106 }
tifo 0:a0f7c6807a3a 107
tifo 0:a0f7c6807a3a 108 // If Set (1) Self-Test Applied. Electrostatic Force exerted on the sensor
tifo 0:a0f7c6807a3a 109 // causing a shift in the output data.
tifo 0:a0f7c6807a3a 110 // If Set (0) Self-Test Disabled.
tifo 0:a0f7c6807a3a 111 void ADXL345::setSelfTestBit(bool selfTestBit) {
tifo 0:a0f7c6807a3a 112 setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);
tifo 0:a0f7c6807a3a 113 }
tifo 0:a0f7c6807a3a 114
tifo 0:a0f7c6807a3a 115
tifo 0:a0f7c6807a3a 116 /*********************** INT_INVERT BIT STATE ***********************/
tifo 0:a0f7c6807a3a 117 /* ~ GET & SET */
tifo 0:a0f7c6807a3a 118 bool ADXL345::getInterruptLevelBit() {
tifo 0:a0f7c6807a3a 119 return getRegisterBit(ADXL345_DATA_FORMAT, 5);
tifo 0:a0f7c6807a3a 120 }
tifo 0:a0f7c6807a3a 121
tifo 0:a0f7c6807a3a 122 // If Set (0) Sets the Interrupts to Active HIGH
tifo 0:a0f7c6807a3a 123 // If Set (1) Sets the Interrupts to Active LOW
tifo 0:a0f7c6807a3a 124 void ADXL345::setInterruptLevelBit(bool interruptLevelBit) {
tifo 0:a0f7c6807a3a 125 setRegisterBit(ADXL345_DATA_FORMAT, 5, interruptLevelBit);
tifo 0:a0f7c6807a3a 126 }
tifo 0:a0f7c6807a3a 127
tifo 0:a0f7c6807a3a 128 /************************* FULL_RES BIT STATE ***********************/
tifo 0:a0f7c6807a3a 129 /* ~ GET & SET */
tifo 0:a0f7c6807a3a 130 bool ADXL345::getFullResBit() {
tifo 0:a0f7c6807a3a 131 return getRegisterBit(ADXL345_DATA_FORMAT, 3);
tifo 0:a0f7c6807a3a 132 }
tifo 0:a0f7c6807a3a 133
tifo 0:a0f7c6807a3a 134 // If Set (1) Device is in Full Resolution Mode: Output Resolution Increase with G Range
tifo 0:a0f7c6807a3a 135 // Set by the Range Bits to Maintain a 4mg/LSB Scale Factor
tifo 0:a0f7c6807a3a 136 // If Set (0) Device is in 10-bit Mode: Range Bits Determine Maximum G Range
tifo 0:a0f7c6807a3a 137 // And Scale Factor
tifo 0:a0f7c6807a3a 138 void ADXL345::setFullResBit(bool fullResBit) {
tifo 0:a0f7c6807a3a 139 setRegisterBit(ADXL345_DATA_FORMAT, 3, fullResBit);
tifo 0:a0f7c6807a3a 140 }
tifo 0:a0f7c6807a3a 141
tifo 0:a0f7c6807a3a 142 /*************************** JUSTIFY BIT STATE **************************/
tifo 0:a0f7c6807a3a 143 /* ~ GET & SET */
tifo 0:a0f7c6807a3a 144 bool ADXL345::getJustifyBit() {
tifo 0:a0f7c6807a3a 145 return getRegisterBit(ADXL345_DATA_FORMAT, 2);
tifo 0:a0f7c6807a3a 146 }
tifo 0:a0f7c6807a3a 147
tifo 0:a0f7c6807a3a 148 // If Set (1) Selects the Left Justified Mode
tifo 0:a0f7c6807a3a 149 // If Set (0) Selects Right Justified Mode with Sign Extension
tifo 0:a0f7c6807a3a 150 void ADXL345::setJustifyBit(bool justifyBit) {
tifo 0:a0f7c6807a3a 151 setRegisterBit(ADXL345_DATA_FORMAT, 2, justifyBit);
tifo 0:a0f7c6807a3a 152 }
tifo 0:a0f7c6807a3a 153
tifo 0:a0f7c6807a3a 154 /*********************** THRESH_TAP char VALUE **********************/
tifo 0:a0f7c6807a3a 155 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 156 // Should Set Between 0 and 255
tifo 0:a0f7c6807a3a 157 // Scale Factor is 62.5 mg/LSB
tifo 0:a0f7c6807a3a 158 // A Value of 0 May Result in Undesirable Behavior
tifo 0:a0f7c6807a3a 159 void ADXL345::setTapThreshold(int tapThreshold) {
tifo 0:a0f7c6807a3a 160 if(tapThreshold < 0)
tifo 0:a0f7c6807a3a 161 {
tifo 0:a0f7c6807a3a 162 tapThreshold = 0;
tifo 0:a0f7c6807a3a 163 }
tifo 0:a0f7c6807a3a 164 if(tapThreshold > 255)
tifo 0:a0f7c6807a3a 165 {
tifo 0:a0f7c6807a3a 166 tapThreshold = 255;
tifo 0:a0f7c6807a3a 167 }
tifo 0:a0f7c6807a3a 168
tifo 0:a0f7c6807a3a 169 char _b = char (tapThreshold);
tifo 0:a0f7c6807a3a 170 writeToI2C(ADXL345_THRESH_TAP, _b);
tifo 0:a0f7c6807a3a 171 }
tifo 0:a0f7c6807a3a 172
tifo 0:a0f7c6807a3a 173 // Return Value Between 0 and 255
tifo 0:a0f7c6807a3a 174 // Scale Factor is 62.5 mg/LSB
tifo 0:a0f7c6807a3a 175 int ADXL345::getTapThreshold() {
tifo 0:a0f7c6807a3a 176 char _b;
tifo 0:a0f7c6807a3a 177 readFromI2C(ADXL345_THRESH_TAP, 1, &_b);
tifo 0:a0f7c6807a3a 178 return int (_b);
tifo 0:a0f7c6807a3a 179 }
tifo 0:a0f7c6807a3a 180
tifo 0:a0f7c6807a3a 181 /****************** GAIN FOR EACH AXIS IN Gs / COUNT *****************/
tifo 0:a0f7c6807a3a 182 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 183 void ADXL345::setAxisGains(double *_gains){
tifo 0:a0f7c6807a3a 184 int i;
tifo 0:a0f7c6807a3a 185 for(i = 0; i < 3; i++){
tifo 0:a0f7c6807a3a 186 gains[i] = _gains[i];
tifo 0:a0f7c6807a3a 187 }
tifo 0:a0f7c6807a3a 188 }
tifo 0:a0f7c6807a3a 189 void ADXL345::getAxisGains(double *_gains){
tifo 0:a0f7c6807a3a 190 int i;
tifo 0:a0f7c6807a3a 191 for(i = 0; i < 3; i++){
tifo 0:a0f7c6807a3a 192 _gains[i] = gains[i];
tifo 0:a0f7c6807a3a 193 }
tifo 0:a0f7c6807a3a 194 }
tifo 0:a0f7c6807a3a 195
tifo 0:a0f7c6807a3a 196 /********************* OFSX, OFSY and OFSZ charS ********************/
tifo 0:a0f7c6807a3a 197 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 198 // OFSX, OFSY and OFSZ: User Offset Adjustments in Twos Complement Format
tifo 0:a0f7c6807a3a 199 // Scale Factor of 15.6mg/LSB
tifo 0:a0f7c6807a3a 200 void ADXL345::setAxisOffset(int x, int y, int z) {
tifo 0:a0f7c6807a3a 201 writeToI2C(ADXL345_OFSX, char (x));
tifo 0:a0f7c6807a3a 202 writeToI2C(ADXL345_OFSY, char (y));
tifo 0:a0f7c6807a3a 203 writeToI2C(ADXL345_OFSZ, char (z));
tifo 0:a0f7c6807a3a 204 }
tifo 0:a0f7c6807a3a 205
tifo 0:a0f7c6807a3a 206 void ADXL345::getAxisOffset(int* x, int* y, int*z) {
tifo 0:a0f7c6807a3a 207 char _b;
tifo 0:a0f7c6807a3a 208 readFromI2C(ADXL345_OFSX, 1, &_b);
tifo 0:a0f7c6807a3a 209 *x = int (_b);
tifo 0:a0f7c6807a3a 210 readFromI2C(ADXL345_OFSY, 1, &_b);
tifo 0:a0f7c6807a3a 211 *y = int (_b);
tifo 0:a0f7c6807a3a 212 readFromI2C(ADXL345_OFSZ, 1, &_b);
tifo 0:a0f7c6807a3a 213 *z = int (_b);
tifo 0:a0f7c6807a3a 214 }
tifo 0:a0f7c6807a3a 215
tifo 0:a0f7c6807a3a 216 /****************************** DUR char ****************************/
tifo 0:a0f7c6807a3a 217 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 218 // DUR char: Contains an Unsigned Time Value Representing the Max Time
tifo 0:a0f7c6807a3a 219 // that an Event must be Above the THRESH_TAP Threshold to qualify
tifo 0:a0f7c6807a3a 220 // as a Tap Event
tifo 0:a0f7c6807a3a 221 // The scale factor is 625µs/LSB
tifo 0:a0f7c6807a3a 222 // Value of 0 Disables the Tap/Double Tap Funcitons. Max value is 255.
tifo 0:a0f7c6807a3a 223 void ADXL345::setTapDuration(int tapDuration) {
tifo 0:a0f7c6807a3a 224 if(tapDuration < 0)
tifo 0:a0f7c6807a3a 225 {
tifo 0:a0f7c6807a3a 226 tapDuration = 0;
tifo 0:a0f7c6807a3a 227 }
tifo 0:a0f7c6807a3a 228 if(tapDuration > 255)
tifo 0:a0f7c6807a3a 229 {
tifo 0:a0f7c6807a3a 230 tapDuration = 255;
tifo 0:a0f7c6807a3a 231 }
tifo 0:a0f7c6807a3a 232 char _b = char (tapDuration);
tifo 0:a0f7c6807a3a 233 writeToI2C(ADXL345_DUR, _b);
tifo 0:a0f7c6807a3a 234 }
tifo 0:a0f7c6807a3a 235
tifo 0:a0f7c6807a3a 236 int ADXL345::getTapDuration() {
tifo 0:a0f7c6807a3a 237 char _b;
tifo 0:a0f7c6807a3a 238 readFromI2C(ADXL345_DUR, 1, &_b);
tifo 0:a0f7c6807a3a 239 return int (_b);
tifo 0:a0f7c6807a3a 240 }
tifo 0:a0f7c6807a3a 241
tifo 0:a0f7c6807a3a 242 /************************** LATENT REGISTER *************************/
tifo 0:a0f7c6807a3a 243 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 244 // Contains Unsigned Time Value Representing the Wait Time from the Detection
tifo 0:a0f7c6807a3a 245 // of a Tap Event to the Start of the Time Window (defined by the Window
tifo 0:a0f7c6807a3a 246 // Register) during which a possible Second Tap Even can be Detected.
tifo 0:a0f7c6807a3a 247 // Scale Factor is 1.25ms/LSB.
tifo 0:a0f7c6807a3a 248 // A Value of 0 Disables the Double Tap Function.
tifo 0:a0f7c6807a3a 249 // It Accepts a Maximum Value of 255.
tifo 0:a0f7c6807a3a 250 void ADXL345::setDoubleTapLatency(int doubleTapLatency) {
tifo 0:a0f7c6807a3a 251 char _b = char (doubleTapLatency);
tifo 0:a0f7c6807a3a 252 writeToI2C(ADXL345_LATENT, _b);
tifo 0:a0f7c6807a3a 253 }
tifo 0:a0f7c6807a3a 254
tifo 0:a0f7c6807a3a 255 int ADXL345::getDoubleTapLatency() {
tifo 0:a0f7c6807a3a 256 char _b;
tifo 0:a0f7c6807a3a 257 readFromI2C(ADXL345_LATENT, 1, &_b);
tifo 0:a0f7c6807a3a 258 return int (_b);
tifo 0:a0f7c6807a3a 259 }
tifo 0:a0f7c6807a3a 260
tifo 0:a0f7c6807a3a 261 /************************** WINDOW REGISTER *************************/
tifo 0:a0f7c6807a3a 262 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 263 // Contains an Unsigned Time Value Representing the Amount of Time
tifo 0:a0f7c6807a3a 264 // After the Expiration of the Latency Time (determined by Latent register)
tifo 0:a0f7c6807a3a 265 // During which a Second Valid Tape can Begin.
tifo 0:a0f7c6807a3a 266 // Scale Factor is 1.25ms/LSB.
tifo 0:a0f7c6807a3a 267 // Value of 0 Disables the Double Tap Function.
tifo 0:a0f7c6807a3a 268 // It Accepts a Maximum Value of 255.
tifo 0:a0f7c6807a3a 269 void ADXL345::setDoubleTapWindow(int doubleTapWindow) {
tifo 0:a0f7c6807a3a 270 if(doubleTapWindow < 0)
tifo 0:a0f7c6807a3a 271 {
tifo 0:a0f7c6807a3a 272 doubleTapWindow = 0;
tifo 0:a0f7c6807a3a 273 }
tifo 0:a0f7c6807a3a 274 if(doubleTapWindow > 255)
tifo 0:a0f7c6807a3a 275 {
tifo 0:a0f7c6807a3a 276 doubleTapWindow = 255;
tifo 0:a0f7c6807a3a 277 }
tifo 0:a0f7c6807a3a 278 char _b = char (doubleTapWindow);
tifo 0:a0f7c6807a3a 279 writeToI2C(ADXL345_WINDOW, _b);
tifo 0:a0f7c6807a3a 280 }
tifo 0:a0f7c6807a3a 281
tifo 0:a0f7c6807a3a 282 int ADXL345::getDoubleTapWindow() {
tifo 0:a0f7c6807a3a 283 char _b;
tifo 0:a0f7c6807a3a 284 readFromI2C(ADXL345_WINDOW, 1, &_b);
tifo 0:a0f7c6807a3a 285 return int (_b);
tifo 0:a0f7c6807a3a 286 }
tifo 0:a0f7c6807a3a 287
tifo 0:a0f7c6807a3a 288 /*********************** THRESH_ACT REGISTER ************************/
tifo 0:a0f7c6807a3a 289 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 290 // Holds the Threshold Value for Detecting Activity.
tifo 0:a0f7c6807a3a 291 // Data Format is Unsigned, so the Magnitude of the Activity Event is Compared
tifo 0:a0f7c6807a3a 292 // with the Value is Compared with the Value in the THRESH_ACT Register.
tifo 0:a0f7c6807a3a 293 // The Scale Factor is 62.5mg/LSB.
tifo 0:a0f7c6807a3a 294 // Value of 0 may Result in Undesirable Behavior if the Activity Interrupt Enabled.
tifo 0:a0f7c6807a3a 295 // It Accepts a Maximum Value of 255.
tifo 0:a0f7c6807a3a 296 void ADXL345::setActivityThreshold(int activityThreshold) {
tifo 0:a0f7c6807a3a 297 if(activityThreshold < 0)
tifo 0:a0f7c6807a3a 298 {
tifo 0:a0f7c6807a3a 299 activityThreshold = 0;
tifo 0:a0f7c6807a3a 300 }
tifo 0:a0f7c6807a3a 301 if(activityThreshold > 255)
tifo 0:a0f7c6807a3a 302 {
tifo 0:a0f7c6807a3a 303 activityThreshold = 255;
tifo 0:a0f7c6807a3a 304 }
tifo 0:a0f7c6807a3a 305 char _b = char (activityThreshold);
tifo 0:a0f7c6807a3a 306 writeToI2C(ADXL345_THRESH_ACT, _b);
tifo 0:a0f7c6807a3a 307 }
tifo 0:a0f7c6807a3a 308
tifo 0:a0f7c6807a3a 309 // Gets the THRESH_ACT char
tifo 0:a0f7c6807a3a 310 int ADXL345::getActivityThreshold() {
tifo 0:a0f7c6807a3a 311 char _b;
tifo 0:a0f7c6807a3a 312 readFromI2C(ADXL345_THRESH_ACT, 1, &_b);
tifo 0:a0f7c6807a3a 313 return int (_b);
tifo 0:a0f7c6807a3a 314 }
tifo 0:a0f7c6807a3a 315
tifo 0:a0f7c6807a3a 316 /********************** THRESH_INACT REGISTER ***********************/
tifo 0:a0f7c6807a3a 317 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 318 // Holds the Threshold Value for Detecting Inactivity.
tifo 0:a0f7c6807a3a 319 // The Data Format is Unsigned, so the Magnitude of the INactivity Event is
tifo 0:a0f7c6807a3a 320 // Compared with the value in the THRESH_INACT Register.
tifo 0:a0f7c6807a3a 321 // Scale Factor is 62.5mg/LSB.
tifo 0:a0f7c6807a3a 322 // Value of 0 May Result in Undesirable Behavior if the Inactivity Interrupt Enabled.
tifo 0:a0f7c6807a3a 323 // It Accepts a Maximum Value of 255.
tifo 0:a0f7c6807a3a 324 void ADXL345::setInactivityThreshold(int inactivityThreshold) {
tifo 0:a0f7c6807a3a 325 if(inactivityThreshold < 0)
tifo 0:a0f7c6807a3a 326 {
tifo 0:a0f7c6807a3a 327 inactivityThreshold = 0;
tifo 0:a0f7c6807a3a 328 }
tifo 0:a0f7c6807a3a 329 if(inactivityThreshold > 255)
tifo 0:a0f7c6807a3a 330 {
tifo 0:a0f7c6807a3a 331 inactivityThreshold = 255;
tifo 0:a0f7c6807a3a 332 }
tifo 0:a0f7c6807a3a 333 char _b = char (inactivityThreshold);
tifo 0:a0f7c6807a3a 334 writeToI2C(ADXL345_THRESH_INACT, _b);
tifo 0:a0f7c6807a3a 335 }
tifo 0:a0f7c6807a3a 336
tifo 0:a0f7c6807a3a 337 int ADXL345::getInactivityThreshold() {
tifo 0:a0f7c6807a3a 338 char _b;
tifo 0:a0f7c6807a3a 339 readFromI2C(ADXL345_THRESH_INACT, 1, &_b);
tifo 0:a0f7c6807a3a 340 return int (_b);
tifo 0:a0f7c6807a3a 341 }
tifo 0:a0f7c6807a3a 342
tifo 0:a0f7c6807a3a 343 /*********************** TIME_INACT RESIGER *************************/
tifo 0:a0f7c6807a3a 344 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 345 // Contains an Unsigned Time Value Representing the Amount of Time that
tifo 0:a0f7c6807a3a 346 // Acceleration must be Less Than the Value in the THRESH_INACT Register
tifo 0:a0f7c6807a3a 347 // for Inactivity to be Declared.
tifo 0:a0f7c6807a3a 348 // Uses Filtered Output Data* unlike other Interrupt Functions
tifo 0:a0f7c6807a3a 349 // Scale Factor is 1sec/LSB.
tifo 0:a0f7c6807a3a 350 // Value Must Be Between 0 and 255.
tifo 0:a0f7c6807a3a 351 void ADXL345::setTimeInactivity(int timeInactivity) {
tifo 0:a0f7c6807a3a 352 if(timeInactivity < 0)
tifo 0:a0f7c6807a3a 353 {
tifo 0:a0f7c6807a3a 354 timeInactivity = 0;
tifo 0:a0f7c6807a3a 355 }
tifo 0:a0f7c6807a3a 356 if(timeInactivity > 255)
tifo 0:a0f7c6807a3a 357 {
tifo 0:a0f7c6807a3a 358 timeInactivity = 255;
tifo 0:a0f7c6807a3a 359 }
tifo 0:a0f7c6807a3a 360 char _b = char (timeInactivity);
tifo 0:a0f7c6807a3a 361 writeToI2C(ADXL345_TIME_INACT, _b);
tifo 0:a0f7c6807a3a 362 }
tifo 0:a0f7c6807a3a 363
tifo 0:a0f7c6807a3a 364 int ADXL345::getTimeInactivity() {
tifo 0:a0f7c6807a3a 365 char _b;
tifo 0:a0f7c6807a3a 366 readFromI2C(ADXL345_TIME_INACT, 1, &_b);
tifo 0:a0f7c6807a3a 367 return int (_b);
tifo 0:a0f7c6807a3a 368 }
tifo 0:a0f7c6807a3a 369
tifo 0:a0f7c6807a3a 370 /*********************** THRESH_FF Register *************************/
tifo 0:a0f7c6807a3a 371 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 372 // Holds the Threshold Value, in Unsigned Format, for Free-Fall Detection
tifo 0:a0f7c6807a3a 373 // The Acceleration on all Axes is Compared with the Value in THRES_FF to
tifo 0:a0f7c6807a3a 374 // Determine if a Free-Fall Event Occurred.
tifo 0:a0f7c6807a3a 375 // Scale Factor is 62.5mg/LSB.
tifo 0:a0f7c6807a3a 376 // Value of 0 May Result in Undesirable Behavior if the Free-Fall interrupt Enabled.
tifo 0:a0f7c6807a3a 377 // Accepts a Maximum Value of 255.
tifo 0:a0f7c6807a3a 378 void ADXL345::setFreeFallThreshold(int freeFallThreshold) {
tifo 0:a0f7c6807a3a 379 if(freeFallThreshold < 0)
tifo 0:a0f7c6807a3a 380 {
tifo 0:a0f7c6807a3a 381 freeFallThreshold = 0;
tifo 0:a0f7c6807a3a 382 }
tifo 0:a0f7c6807a3a 383 if(freeFallThreshold > 255)
tifo 0:a0f7c6807a3a 384 {
tifo 0:a0f7c6807a3a 385 freeFallThreshold = 255;
tifo 0:a0f7c6807a3a 386 }
tifo 0:a0f7c6807a3a 387 char _b = char (freeFallThreshold);
tifo 0:a0f7c6807a3a 388 writeToI2C(ADXL345_THRESH_FF, _b);
tifo 0:a0f7c6807a3a 389 }
tifo 0:a0f7c6807a3a 390
tifo 0:a0f7c6807a3a 391 int ADXL345::getFreeFallThreshold() {
tifo 0:a0f7c6807a3a 392 char _b;
tifo 0:a0f7c6807a3a 393 readFromI2C(ADXL345_THRESH_FF, 1, &_b);
tifo 0:a0f7c6807a3a 394 return int (_b);
tifo 0:a0f7c6807a3a 395 }
tifo 0:a0f7c6807a3a 396
tifo 0:a0f7c6807a3a 397 /************************ TIME_FF Register **************************/
tifo 0:a0f7c6807a3a 398 /* ~ SET & GET */
tifo 0:a0f7c6807a3a 399 // Stores an Unsigned Time Value Representing the Minimum Time that the Value
tifo 0:a0f7c6807a3a 400 // of all Axes must be Less Than THRES_FF to Generate a Free-Fall Interrupt.
tifo 0:a0f7c6807a3a 401 // Scale Factor is 5ms/LSB.
tifo 0:a0f7c6807a3a 402 // Value of 0 May Result in Undesirable Behavior if the Free-Fall Interrupt Enabled.
tifo 0:a0f7c6807a3a 403 // Accepts a Maximum Value of 255.
tifo 0:a0f7c6807a3a 404 void ADXL345::setFreeFallDuration(int freeFallDuration) {
tifo 0:a0f7c6807a3a 405 if(freeFallDuration < 0)
tifo 0:a0f7c6807a3a 406 {
tifo 0:a0f7c6807a3a 407 freeFallDuration = 0;
tifo 0:a0f7c6807a3a 408 }
tifo 0:a0f7c6807a3a 409 if(freeFallDuration > 255)
tifo 0:a0f7c6807a3a 410 {
tifo 0:a0f7c6807a3a 411 freeFallDuration = 255;
tifo 0:a0f7c6807a3a 412 }
tifo 0:a0f7c6807a3a 413 char _b = char (freeFallDuration);
tifo 0:a0f7c6807a3a 414 writeToI2C(ADXL345_TIME_FF, _b);
tifo 0:a0f7c6807a3a 415 }
tifo 0:a0f7c6807a3a 416
tifo 0:a0f7c6807a3a 417 int ADXL345::getFreeFallDuration() {
tifo 0:a0f7c6807a3a 418 char _b;
tifo 0:a0f7c6807a3a 419 readFromI2C(ADXL345_TIME_FF, 1, &_b);
tifo 0:a0f7c6807a3a 420 return int (_b);
tifo 0:a0f7c6807a3a 421 }
tifo 0:a0f7c6807a3a 422
tifo 0:a0f7c6807a3a 423 /************************** ACTIVITY BITS ***************************/
tifo 0:a0f7c6807a3a 424 /* */
tifo 0:a0f7c6807a3a 425 bool ADXL345::isActivityXEnabled() {
tifo 0:a0f7c6807a3a 426 return getRegisterBit(ADXL345_ACT_INACT_CTL, 6);
tifo 0:a0f7c6807a3a 427 }
tifo 0:a0f7c6807a3a 428 bool ADXL345::isActivityYEnabled() {
tifo 0:a0f7c6807a3a 429 return getRegisterBit(ADXL345_ACT_INACT_CTL, 5);
tifo 0:a0f7c6807a3a 430 }
tifo 0:a0f7c6807a3a 431 bool ADXL345::isActivityZEnabled() {
tifo 0:a0f7c6807a3a 432 return getRegisterBit(ADXL345_ACT_INACT_CTL, 4);
tifo 0:a0f7c6807a3a 433 }
tifo 0:a0f7c6807a3a 434 bool ADXL345::isInactivityXEnabled() {
tifo 0:a0f7c6807a3a 435 return getRegisterBit(ADXL345_ACT_INACT_CTL, 2);
tifo 0:a0f7c6807a3a 436 }
tifo 0:a0f7c6807a3a 437 bool ADXL345::isInactivityYEnabled() {
tifo 0:a0f7c6807a3a 438 return getRegisterBit(ADXL345_ACT_INACT_CTL, 1);
tifo 0:a0f7c6807a3a 439 }
tifo 0:a0f7c6807a3a 440 bool ADXL345::isInactivityZEnabled() {
tifo 0:a0f7c6807a3a 441 return getRegisterBit(ADXL345_ACT_INACT_CTL, 0);
tifo 0:a0f7c6807a3a 442 }
tifo 0:a0f7c6807a3a 443
tifo 0:a0f7c6807a3a 444 void ADXL345::setActivityX(bool state) {
tifo 0:a0f7c6807a3a 445 setRegisterBit(ADXL345_ACT_INACT_CTL, 6, state);
tifo 0:a0f7c6807a3a 446 }
tifo 0:a0f7c6807a3a 447 void ADXL345::setActivityY(bool state) {
tifo 0:a0f7c6807a3a 448 setRegisterBit(ADXL345_ACT_INACT_CTL, 5, state);
tifo 0:a0f7c6807a3a 449 }
tifo 0:a0f7c6807a3a 450 void ADXL345::setActivityZ(bool state) {
tifo 0:a0f7c6807a3a 451 setRegisterBit(ADXL345_ACT_INACT_CTL, 4, state);
tifo 0:a0f7c6807a3a 452 }
tifo 0:a0f7c6807a3a 453 void ADXL345::setActivityXYZ(bool stateX, bool stateY, bool stateZ) {
tifo 0:a0f7c6807a3a 454 setActivityX(stateX);
tifo 0:a0f7c6807a3a 455 setActivityY(stateY);
tifo 0:a0f7c6807a3a 456 setActivityZ(stateZ);
tifo 0:a0f7c6807a3a 457 }
tifo 0:a0f7c6807a3a 458 void ADXL345::setInactivityX(bool state) {
tifo 0:a0f7c6807a3a 459 setRegisterBit(ADXL345_ACT_INACT_CTL, 2, state);
tifo 0:a0f7c6807a3a 460 }
tifo 0:a0f7c6807a3a 461 void ADXL345::setInactivityY(bool state) {
tifo 0:a0f7c6807a3a 462 setRegisterBit(ADXL345_ACT_INACT_CTL, 1, state);
tifo 0:a0f7c6807a3a 463 }
tifo 0:a0f7c6807a3a 464 void ADXL345::setInactivityZ(bool state) {
tifo 0:a0f7c6807a3a 465 setRegisterBit(ADXL345_ACT_INACT_CTL, 0, state);
tifo 0:a0f7c6807a3a 466 }
tifo 0:a0f7c6807a3a 467 void ADXL345::setInactivityXYZ(bool stateX, bool stateY, bool stateZ) {
tifo 0:a0f7c6807a3a 468 setInactivityX(stateX);
tifo 0:a0f7c6807a3a 469 setInactivityY(stateY);
tifo 0:a0f7c6807a3a 470 setInactivityZ(stateZ);
tifo 0:a0f7c6807a3a 471 }
tifo 0:a0f7c6807a3a 472
tifo 0:a0f7c6807a3a 473 bool ADXL345::isActivityAc() {
tifo 0:a0f7c6807a3a 474 return getRegisterBit(ADXL345_ACT_INACT_CTL, 7);
tifo 0:a0f7c6807a3a 475 }
tifo 0:a0f7c6807a3a 476 bool ADXL345::isInactivityAc(){
tifo 0:a0f7c6807a3a 477 return getRegisterBit(ADXL345_ACT_INACT_CTL, 3);
tifo 0:a0f7c6807a3a 478 }
tifo 0:a0f7c6807a3a 479
tifo 0:a0f7c6807a3a 480 void ADXL345::setActivityAc(bool state) {
tifo 0:a0f7c6807a3a 481 setRegisterBit(ADXL345_ACT_INACT_CTL, 7, state);
tifo 0:a0f7c6807a3a 482 }
tifo 0:a0f7c6807a3a 483 void ADXL345::setInactivityAc(bool state) {
tifo 0:a0f7c6807a3a 484 setRegisterBit(ADXL345_ACT_INACT_CTL, 3, state);
tifo 0:a0f7c6807a3a 485 }
tifo 0:a0f7c6807a3a 486
tifo 0:a0f7c6807a3a 487 /************************* SUPPRESS BITS ****************************/
tifo 0:a0f7c6807a3a 488 /* */
tifo 0:a0f7c6807a3a 489 bool ADXL345::getSuppressBit(){
tifo 0:a0f7c6807a3a 490 return getRegisterBit(ADXL345_TAP_AXES, 3);
tifo 0:a0f7c6807a3a 491 }
tifo 0:a0f7c6807a3a 492 void ADXL345::setSuppressBit(bool state) {
tifo 0:a0f7c6807a3a 493 setRegisterBit(ADXL345_TAP_AXES, 3, state);
tifo 0:a0f7c6807a3a 494 }
tifo 0:a0f7c6807a3a 495
tifo 0:a0f7c6807a3a 496 /**************************** TAP BITS ******************************/
tifo 0:a0f7c6807a3a 497 /* */
tifo 0:a0f7c6807a3a 498 bool ADXL345::isTapDetectionOnX(){
tifo 0:a0f7c6807a3a 499 return getRegisterBit(ADXL345_TAP_AXES, 2);
tifo 0:a0f7c6807a3a 500 }
tifo 0:a0f7c6807a3a 501 void ADXL345::setTapDetectionOnX(bool state) {
tifo 0:a0f7c6807a3a 502 setRegisterBit(ADXL345_TAP_AXES, 2, state);
tifo 0:a0f7c6807a3a 503 }
tifo 0:a0f7c6807a3a 504 bool ADXL345::isTapDetectionOnY(){
tifo 0:a0f7c6807a3a 505 return getRegisterBit(ADXL345_TAP_AXES, 1);
tifo 0:a0f7c6807a3a 506 }
tifo 0:a0f7c6807a3a 507 void ADXL345::setTapDetectionOnY(bool state) {
tifo 0:a0f7c6807a3a 508 setRegisterBit(ADXL345_TAP_AXES, 1, state);
tifo 0:a0f7c6807a3a 509 }
tifo 0:a0f7c6807a3a 510 bool ADXL345::isTapDetectionOnZ(){
tifo 0:a0f7c6807a3a 511 return getRegisterBit(ADXL345_TAP_AXES, 0);
tifo 0:a0f7c6807a3a 512 }
tifo 0:a0f7c6807a3a 513 void ADXL345::setTapDetectionOnZ(bool state) {
tifo 0:a0f7c6807a3a 514 setRegisterBit(ADXL345_TAP_AXES, 0, state);
tifo 0:a0f7c6807a3a 515 }
tifo 0:a0f7c6807a3a 516
tifo 0:a0f7c6807a3a 517 void ADXL345::setTapDetectionOnXYZ(bool stateX, bool stateY, bool stateZ) {
tifo 0:a0f7c6807a3a 518 setTapDetectionOnX(stateX);
tifo 0:a0f7c6807a3a 519 setTapDetectionOnY(stateY);
tifo 0:a0f7c6807a3a 520 setTapDetectionOnZ(stateZ);
tifo 0:a0f7c6807a3a 521 }
tifo 0:a0f7c6807a3a 522
tifo 0:a0f7c6807a3a 523 bool ADXL345::isActivitySourceOnX(){
tifo 0:a0f7c6807a3a 524 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 6);
tifo 0:a0f7c6807a3a 525 }
tifo 0:a0f7c6807a3a 526 bool ADXL345::isActivitySourceOnY(){
tifo 0:a0f7c6807a3a 527 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 5);
tifo 0:a0f7c6807a3a 528 }
tifo 0:a0f7c6807a3a 529 bool ADXL345::isActivitySourceOnZ(){
tifo 0:a0f7c6807a3a 530 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 4);
tifo 0:a0f7c6807a3a 531 }
tifo 0:a0f7c6807a3a 532
tifo 0:a0f7c6807a3a 533 bool ADXL345::isTapSourceOnX(){
tifo 0:a0f7c6807a3a 534 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 2);
tifo 0:a0f7c6807a3a 535 }
tifo 0:a0f7c6807a3a 536 bool ADXL345::isTapSourceOnY(){
tifo 0:a0f7c6807a3a 537 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 1);
tifo 0:a0f7c6807a3a 538 }
tifo 0:a0f7c6807a3a 539 bool ADXL345::isTapSourceOnZ(){
tifo 0:a0f7c6807a3a 540 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 0);
tifo 0:a0f7c6807a3a 541 }
tifo 0:a0f7c6807a3a 542
tifo 0:a0f7c6807a3a 543 /*************************** ASLEEP BIT *****************************/
tifo 0:a0f7c6807a3a 544 /* */
tifo 0:a0f7c6807a3a 545 bool ADXL345::isAsleep(){
tifo 0:a0f7c6807a3a 546 return getRegisterBit(ADXL345_ACT_TAP_STATUS, 3);
tifo 0:a0f7c6807a3a 547 }
tifo 0:a0f7c6807a3a 548
tifo 0:a0f7c6807a3a 549 /************************** LOW POWER BIT ***************************/
tifo 0:a0f7c6807a3a 550 /* */
tifo 0:a0f7c6807a3a 551 bool ADXL345::isLowPower(){
tifo 0:a0f7c6807a3a 552 return getRegisterBit(ADXL345_BW_RATE, 4);
tifo 0:a0f7c6807a3a 553 }
tifo 0:a0f7c6807a3a 554 void ADXL345::setLowPower(bool state) {
tifo 0:a0f7c6807a3a 555 setRegisterBit(ADXL345_BW_RATE, 4, state);
tifo 0:a0f7c6807a3a 556 }
tifo 0:a0f7c6807a3a 557
tifo 0:a0f7c6807a3a 558
tifo 0:a0f7c6807a3a 559 /************************* TRIGGER CHECK ***************************/
tifo 0:a0f7c6807a3a 560 /* */
tifo 0:a0f7c6807a3a 561 // Check if Action was Triggered in Interrupts
tifo 0:a0f7c6807a3a 562 // Example triggered(interrupts, ADXL345_SINGLE_TAP);
tifo 0:a0f7c6807a3a 563 bool ADXL345::triggered(char interrupts, int mask){
tifo 0:a0f7c6807a3a 564 return ((interrupts >> mask) & 1);
tifo 0:a0f7c6807a3a 565 }
tifo 0:a0f7c6807a3a 566
tifo 0:a0f7c6807a3a 567 /*
tifo 0:a0f7c6807a3a 568 ADXL345_DATA_READY
tifo 0:a0f7c6807a3a 569 ADXL345_SINGLE_TAP
tifo 0:a0f7c6807a3a 570 ADXL345_DOUBLE_TAP
tifo 0:a0f7c6807a3a 571 ADXL345_ACTIVITY
tifo 0:a0f7c6807a3a 572 ADXL345_INACTIVITY
tifo 0:a0f7c6807a3a 573 ADXL345_FREE_FALL
tifo 0:a0f7c6807a3a 574 ADXL345_WATERMARK
tifo 0:a0f7c6807a3a 575 ADXL345_OVERRUNY
tifo 0:a0f7c6807a3a 576 */
tifo 0:a0f7c6807a3a 577
tifo 0:a0f7c6807a3a 578
tifo 0:a0f7c6807a3a 579 char ADXL345::getInterruptSource() {
tifo 0:a0f7c6807a3a 580 char _b;
tifo 0:a0f7c6807a3a 581 readFromI2C(ADXL345_INT_SOURCE, 1, &_b);
tifo 0:a0f7c6807a3a 582 return _b;
tifo 0:a0f7c6807a3a 583 }
tifo 0:a0f7c6807a3a 584
tifo 0:a0f7c6807a3a 585 bool ADXL345::getInterruptSource(char interruptBit) {
tifo 0:a0f7c6807a3a 586 return getRegisterBit(ADXL345_INT_SOURCE,interruptBit);
tifo 0:a0f7c6807a3a 587 }
tifo 0:a0f7c6807a3a 588
tifo 0:a0f7c6807a3a 589 bool ADXL345::getInterruptMapping(char interruptBit) {
tifo 0:a0f7c6807a3a 590 return getRegisterBit(ADXL345_INT_MAP,interruptBit);
tifo 0:a0f7c6807a3a 591 }
tifo 0:a0f7c6807a3a 592
tifo 0:a0f7c6807a3a 593 /*********************** INTERRUPT MAPPING **************************/
tifo 0:a0f7c6807a3a 594 /* Set the Mapping of an Interrupt to pin1 or pin2 */
tifo 0:a0f7c6807a3a 595 // eg: setInterruptMapping(ADXL345_INT_DOUBLE_TAP_BIT,ADXL345_INT2_PIN);
tifo 0:a0f7c6807a3a 596 void ADXL345::setInterruptMapping(char interruptBit, bool interruptPin) {
tifo 0:a0f7c6807a3a 597 setRegisterBit(ADXL345_INT_MAP, interruptBit, interruptPin);
tifo 0:a0f7c6807a3a 598 }
tifo 0:a0f7c6807a3a 599
tifo 0:a0f7c6807a3a 600 void ADXL345::setImportantInterruptMapping(int single_tap, int double_tap, int free_fall, int activity, int inactivity) {
tifo 0:a0f7c6807a3a 601 if(single_tap == 1) {
tifo 0:a0f7c6807a3a 602 setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT1_PIN );}
tifo 0:a0f7c6807a3a 603 else if(single_tap == 2) {
tifo 0:a0f7c6807a3a 604 setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT2_PIN );}
tifo 0:a0f7c6807a3a 605
tifo 0:a0f7c6807a3a 606 if(double_tap == 1) {
tifo 0:a0f7c6807a3a 607 setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT1_PIN );}
tifo 0:a0f7c6807a3a 608 else if(double_tap == 2) {
tifo 0:a0f7c6807a3a 609 setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT2_PIN );}
tifo 0:a0f7c6807a3a 610
tifo 0:a0f7c6807a3a 611 if(free_fall == 1) {
tifo 0:a0f7c6807a3a 612 setInterruptMapping( ADXL345_INT_FREE_FALL_BIT, ADXL345_INT1_PIN );}
tifo 0:a0f7c6807a3a 613 else if(free_fall == 2) {
tifo 0:a0f7c6807a3a 614 setInterruptMapping( ADXL345_INT_FREE_FALL_BIT, ADXL345_INT2_PIN );}
tifo 0:a0f7c6807a3a 615
tifo 0:a0f7c6807a3a 616 if(activity == 1) {
tifo 0:a0f7c6807a3a 617 setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN );}
tifo 0:a0f7c6807a3a 618 else if(activity == 2) {
tifo 0:a0f7c6807a3a 619 setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT2_PIN );}
tifo 0:a0f7c6807a3a 620
tifo 0:a0f7c6807a3a 621 if(inactivity == 1) {
tifo 0:a0f7c6807a3a 622 setInterruptMapping( ADXL345_INT_INACTIVITY_BIT, ADXL345_INT1_PIN );}
tifo 0:a0f7c6807a3a 623 else if(inactivity == 2) {
tifo 0:a0f7c6807a3a 624 setInterruptMapping( ADXL345_INT_INACTIVITY_BIT, ADXL345_INT2_PIN );}
tifo 0:a0f7c6807a3a 625 }
tifo 0:a0f7c6807a3a 626
tifo 0:a0f7c6807a3a 627 bool ADXL345::isInterruptEnabled(char interruptBit) {
tifo 0:a0f7c6807a3a 628 return getRegisterBit(ADXL345_INT_ENABLE,interruptBit);
tifo 0:a0f7c6807a3a 629 }
tifo 0:a0f7c6807a3a 630
tifo 0:a0f7c6807a3a 631 void ADXL345::setInterrupt(char interruptBit, bool state) {
tifo 0:a0f7c6807a3a 632 setRegisterBit(ADXL345_INT_ENABLE, interruptBit, state);
tifo 0:a0f7c6807a3a 633 }
tifo 0:a0f7c6807a3a 634
tifo 0:a0f7c6807a3a 635 void ADXL345::singleTapINT(bool status) {
tifo 0:a0f7c6807a3a 636 if(status) {
tifo 0:a0f7c6807a3a 637 setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
tifo 0:a0f7c6807a3a 638 }
tifo 0:a0f7c6807a3a 639 else {
tifo 0:a0f7c6807a3a 640 setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 0);
tifo 0:a0f7c6807a3a 641 }
tifo 0:a0f7c6807a3a 642 }
tifo 0:a0f7c6807a3a 643 void ADXL345::doubleTapINT(bool status) {
tifo 0:a0f7c6807a3a 644 if(status) {
tifo 0:a0f7c6807a3a 645 setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
tifo 0:a0f7c6807a3a 646 }
tifo 0:a0f7c6807a3a 647 else {
tifo 0:a0f7c6807a3a 648 setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 0);
tifo 0:a0f7c6807a3a 649 }
tifo 0:a0f7c6807a3a 650 }
tifo 0:a0f7c6807a3a 651 void ADXL345::FreeFallINT(bool status) {
tifo 0:a0f7c6807a3a 652 if(status) {
tifo 0:a0f7c6807a3a 653 setInterrupt( ADXL345_INT_FREE_FALL_BIT, 1);
tifo 0:a0f7c6807a3a 654 }
tifo 0:a0f7c6807a3a 655 else {
tifo 0:a0f7c6807a3a 656 setInterrupt( ADXL345_INT_FREE_FALL_BIT, 0);
tifo 0:a0f7c6807a3a 657 }
tifo 0:a0f7c6807a3a 658 }
tifo 0:a0f7c6807a3a 659 void ADXL345::ActivityINT(bool status) {
tifo 0:a0f7c6807a3a 660 if(status) {
tifo 0:a0f7c6807a3a 661 setInterrupt( ADXL345_INT_ACTIVITY_BIT, 1);
tifo 0:a0f7c6807a3a 662 }
tifo 0:a0f7c6807a3a 663 else {
tifo 0:a0f7c6807a3a 664 setInterrupt( ADXL345_INT_ACTIVITY_BIT, 0);
tifo 0:a0f7c6807a3a 665 }
tifo 0:a0f7c6807a3a 666 }
tifo 0:a0f7c6807a3a 667 void ADXL345::InactivityINT(bool status) {
tifo 0:a0f7c6807a3a 668 if(status) {
tifo 0:a0f7c6807a3a 669 setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
tifo 0:a0f7c6807a3a 670 }
tifo 0:a0f7c6807a3a 671 else {
tifo 0:a0f7c6807a3a 672 setInterrupt( ADXL345_INT_INACTIVITY_BIT, 0);
tifo 0:a0f7c6807a3a 673 }
tifo 0:a0f7c6807a3a 674 }
tifo 0:a0f7c6807a3a 675
tifo 0:a0f7c6807a3a 676 void ADXL345::setRegisterBit(char regAdress, int bitPos, bool state) {
tifo 0:a0f7c6807a3a 677 char _b;
tifo 0:a0f7c6807a3a 678 readFromI2C(regAdress, 1, &_b);
tifo 0:a0f7c6807a3a 679 if (state) {
tifo 0:a0f7c6807a3a 680 _b |= (1 << bitPos); // Forces nth Bit of _b to 1. Other Bits Unchanged.
tifo 0:a0f7c6807a3a 681 }
tifo 0:a0f7c6807a3a 682 else {
tifo 0:a0f7c6807a3a 683 _b &= ~(1 << bitPos); // Forces nth Bit of _b to 0. Other Bits Unchanged.
tifo 0:a0f7c6807a3a 684 }
tifo 0:a0f7c6807a3a 685 writeToI2C(regAdress, _b);
tifo 0:a0f7c6807a3a 686 }
tifo 0:a0f7c6807a3a 687
tifo 0:a0f7c6807a3a 688 bool ADXL345::getRegisterBit(char regAdress, int bitPos) {
tifo 0:a0f7c6807a3a 689 char _b;
tifo 0:a0f7c6807a3a 690 readFromI2C(regAdress, 1, &_b);
tifo 0:a0f7c6807a3a 691 return ((_b >> bitPos) & 1);
tifo 0:a0f7c6807a3a 692 }