Aleksandr Koptevtsov / Mbed 2 deprecated adxl345

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers adxl345.cpp Source File

adxl345.cpp

00001 #include "adxl345.h"
00002 
00003 I2C i2c(PB_9, PB_8);
00004 
00005 #define ADXL345_DEVICE (0xA6)    // Device Address for ADXL345
00006 #define ADXL345_TO_READ (6)      // Number of chars Read - Two chars Per Axis
00007 
00008 ADXL345::ADXL345() {
00009     status = ADXL345_OK;
00010     error_code = ADXL345_NO_ERROR;
00011     
00012     gains[0] = 0.00376390;      // Original gain 0.00376390 
00013     gains[1] = 0.00376009;      // Original gain 0.00376009
00014     gains[2] = 0.00349265;      // Original gain 0.00349265
00015 }
00016 
00017 
00018 void ADXL345::powerOn() {
00019 
00020     //ADXL345 TURN ON
00021     writeToI2C(ADXL345_POWER_CTL, 0);  // Wakeup     
00022     writeToI2C(ADXL345_POWER_CTL, 16); // Auto_Sleep
00023     writeToI2C(ADXL345_POWER_CTL, 8);  // Measure
00024     
00025 }
00026 
00027 
00028 /*********************** READING ACCELERATION ***********************/
00029 /*    Reads Acceleration into Three Variables:  x, y and z          */
00030 
00031 void ADXL345::readAccel(int *xyz){
00032     readAccel(xyz, xyz + 1, xyz + 2);
00033 }
00034 
00035 void ADXL345::readAccel(int *x, int *y, int *z) {
00036     readFromI2C(ADXL345_DATAX0, ADXL345_TO_READ, _buff);   // Read Accel Data from ADXL345
00037     
00038     // Each Axis @ All g Ranges: 10 Bit Resolution (2 chars)
00039     
00040     *x = (int16_t)((((int)_buff[1]) << 8) | _buff[0]);
00041     *y = (int16_t)((((int)_buff[3]) << 8) | _buff[2]);
00042     *z = (int16_t)((((int)_buff[5]) << 8) | _buff[4]);
00043         
00044 }
00045 
00046 void ADXL345::get_Gxyz(double *xyz){
00047     int i;
00048     int xyz_int[3];
00049     readAccel(xyz_int);
00050     for(i=0; i<3; i++){
00051         xyz[i] = xyz_int[i] * gains[i];
00052     }
00053 }
00054 
00055 
00056 /*************************** WRITE TO I2C ***************************/
00057 /*      Start; Send Register Address; Send Value To Write; End      */
00058 void ADXL345::writeToI2C(char _address, char _val) {
00059     
00060     char data_write[2];
00061     data_write[0] = _address;
00062     data_write[1] = _val;
00063     
00064     i2c.write(ADXL345_DEVICE, data_write, 2, 0);     
00065 }
00066 
00067 /*************************** READ FROM I2C **************************/
00068 /*                Start; Send Address To Read; End                  */
00069 void ADXL345::readFromI2C(char address, int num, char _buff[]) {
00070     
00071     char data_write[1];
00072     data_write[0] = address;
00073     i2c.write(ADXL345_DEVICE, data_write, 1, 1); 
00074     i2c.read(ADXL345_DEVICE, _buff, num, 0);    
00075 }
00076 
00077 void ADXL345::setRangeSetting(int val) {
00078     char _s;
00079     char _b;
00080     
00081     switch (val) {
00082         case 2:  
00083             _s = 00000000; 
00084             break;
00085         case 4:  
00086             _s = 00000001; 
00087             break;
00088         case 8:  
00089             _s = 00000010; 
00090             break;
00091         case 16: 
00092             _s = 00000011; 
00093             break;
00094         default: 
00095             _s = 00000000;
00096     }
00097     readFromI2C(ADXL345_DATA_FORMAT, 1, &_b);
00098     _s |= (_b & 11101100);
00099     writeToI2C(ADXL345_DATA_FORMAT, _s);
00100 }
00101 
00102 /*************************** SELF_TEST BIT **************************/
00103 /*                            ~ GET & SET                           */
00104 bool ADXL345::getSelfTestBit() {
00105     return getRegisterBit(ADXL345_DATA_FORMAT, 7);
00106 }
00107 
00108 // If Set (1) Self-Test Applied. Electrostatic Force exerted on the sensor
00109 //  causing a shift in the output data.
00110 // If Set (0) Self-Test Disabled.
00111 void ADXL345::setSelfTestBit(bool selfTestBit) {
00112     setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);
00113 }
00114 
00115 
00116 /*********************** INT_INVERT BIT STATE ***********************/
00117 /*                           ~ GET & SET                            */
00118 bool ADXL345::getInterruptLevelBit() {
00119     return getRegisterBit(ADXL345_DATA_FORMAT, 5);
00120 }
00121 
00122 // If Set (0) Sets the Interrupts to Active HIGH
00123 // If Set (1) Sets the Interrupts to Active LOW
00124 void ADXL345::setInterruptLevelBit(bool interruptLevelBit) {
00125     setRegisterBit(ADXL345_DATA_FORMAT, 5, interruptLevelBit);
00126 }
00127 
00128 /************************* FULL_RES BIT STATE ***********************/
00129 /*                           ~ GET & SET                            */
00130 bool ADXL345::getFullResBit() {
00131     return getRegisterBit(ADXL345_DATA_FORMAT, 3);
00132 }
00133 
00134 // If Set (1) Device is in Full Resolution Mode: Output Resolution Increase with G Range
00135 //  Set by the Range Bits to Maintain a 4mg/LSB Scale Factor
00136 // If Set (0) Device is in 10-bit Mode: Range Bits Determine Maximum G Range
00137 //  And Scale Factor
00138 void ADXL345::setFullResBit(bool fullResBit) {
00139     setRegisterBit(ADXL345_DATA_FORMAT, 3, fullResBit);
00140 }
00141 
00142 /*************************** JUSTIFY BIT STATE **************************/
00143 /*                           ~ GET & SET                            */
00144 bool ADXL345::getJustifyBit() {
00145     return getRegisterBit(ADXL345_DATA_FORMAT, 2);
00146 }
00147 
00148 // If Set (1) Selects the Left Justified Mode
00149 // If Set (0) Selects Right Justified Mode with Sign Extension
00150 void ADXL345::setJustifyBit(bool justifyBit) {
00151     setRegisterBit(ADXL345_DATA_FORMAT, 2, justifyBit);
00152 }
00153 
00154 /*********************** THRESH_TAP char VALUE **********************/
00155 /*                          ~ SET & GET                             */
00156 // Should Set Between 0 and 255
00157 // Scale Factor is 62.5 mg/LSB
00158 // A Value of 0 May Result in Undesirable Behavior
00159 void ADXL345::setTapThreshold(int tapThreshold) {
00160     if(tapThreshold < 0)
00161     {
00162         tapThreshold = 0;
00163     }
00164     if(tapThreshold > 255)
00165     {
00166         tapThreshold = 255;
00167     }
00168     
00169     char _b = char (tapThreshold);
00170     writeToI2C(ADXL345_THRESH_TAP, _b);  
00171 }
00172 
00173 // Return Value Between 0 and 255
00174 // Scale Factor is 62.5 mg/LSB
00175 int ADXL345::getTapThreshold() {
00176     char _b;
00177     readFromI2C(ADXL345_THRESH_TAP, 1, &_b);  
00178     return int (_b);
00179 }
00180 
00181 /****************** GAIN FOR EACH AXIS IN Gs / COUNT *****************/
00182 /*                           ~ SET & GET                            */
00183 void ADXL345::setAxisGains(double *_gains){
00184     int i;
00185     for(i = 0; i < 3; i++){
00186         gains[i] = _gains[i];
00187     }
00188 }
00189 void ADXL345::getAxisGains(double *_gains){
00190     int i;
00191     for(i = 0; i < 3; i++){
00192         _gains[i] = gains[i];
00193     }
00194 }
00195 
00196 /********************* OFSX, OFSY and OFSZ charS ********************/
00197 /*                           ~ SET & GET                            */
00198 // OFSX, OFSY and OFSZ: User Offset Adjustments in Twos Complement Format
00199 // Scale Factor of 15.6mg/LSB
00200 void ADXL345::setAxisOffset(int x, int y, int z) {
00201     writeToI2C(ADXL345_OFSX, char (x));  
00202     writeToI2C(ADXL345_OFSY, char (y));  
00203     writeToI2C(ADXL345_OFSZ, char (z));  
00204 }
00205 
00206 void ADXL345::getAxisOffset(int* x, int* y, int*z) {
00207     char _b;
00208     readFromI2C(ADXL345_OFSX, 1, &_b);  
00209     *x = int (_b);
00210     readFromI2C(ADXL345_OFSY, 1, &_b);  
00211     *y = int (_b);
00212     readFromI2C(ADXL345_OFSZ, 1, &_b);  
00213     *z = int (_b);
00214 }
00215 
00216 /****************************** DUR char ****************************/
00217 /*                           ~ SET & GET                            */
00218 // DUR char: Contains an Unsigned Time Value Representing the Max Time 
00219 //  that an Event must be Above the THRESH_TAP Threshold to qualify 
00220 //  as a Tap Event
00221 // The scale factor is 625µs/LSB
00222 // Value of 0 Disables the Tap/Double Tap Funcitons. Max value is 255.
00223 void ADXL345::setTapDuration(int tapDuration) {
00224     if(tapDuration < 0)
00225     {
00226         tapDuration = 0;
00227     }
00228     if(tapDuration > 255)
00229     {
00230         tapDuration = 255;
00231     }
00232     char _b = char (tapDuration);
00233     writeToI2C(ADXL345_DUR, _b);  
00234 }
00235 
00236 int ADXL345::getTapDuration() {
00237     char _b;
00238     readFromI2C(ADXL345_DUR, 1, &_b);  
00239     return int (_b);
00240 }
00241 
00242 /************************** LATENT REGISTER *************************/
00243 /*                           ~ SET & GET                            */
00244 // Contains Unsigned Time Value Representing the Wait Time from the Detection
00245 //  of a Tap Event to the Start of the Time Window (defined by the Window 
00246 //  Register) during which a possible Second Tap Even can be Detected.
00247 // Scale Factor is 1.25ms/LSB. 
00248 // A Value of 0 Disables the Double Tap Function.
00249 // It Accepts a Maximum Value of 255.
00250 void ADXL345::setDoubleTapLatency(int doubleTapLatency) {
00251     char _b = char (doubleTapLatency);
00252     writeToI2C(ADXL345_LATENT, _b);  
00253 }
00254 
00255 int ADXL345::getDoubleTapLatency() {
00256     char _b;
00257     readFromI2C(ADXL345_LATENT, 1, &_b);  
00258     return int (_b);
00259 }
00260 
00261 /************************** WINDOW REGISTER *************************/
00262 /*                           ~ SET & GET                            */
00263 // Contains an Unsigned Time Value Representing the Amount of Time 
00264 //  After the Expiration of the Latency Time (determined by Latent register)
00265 //  During which a Second Valid Tape can Begin. 
00266 // Scale Factor is 1.25ms/LSB. 
00267 // Value of 0 Disables the Double Tap Function. 
00268 // It Accepts a Maximum Value of 255.
00269 void ADXL345::setDoubleTapWindow(int doubleTapWindow) {
00270     if(doubleTapWindow < 0)
00271     {
00272         doubleTapWindow = 0;
00273     }
00274     if(doubleTapWindow > 255)
00275     {
00276         doubleTapWindow = 255;
00277     }
00278     char _b = char (doubleTapWindow);
00279     writeToI2C(ADXL345_WINDOW, _b);  
00280 }
00281 
00282 int ADXL345::getDoubleTapWindow() {
00283     char _b;
00284     readFromI2C(ADXL345_WINDOW, 1, &_b);  
00285     return int (_b);
00286 }
00287 
00288 /*********************** THRESH_ACT REGISTER ************************/
00289 /*                          ~ SET & GET                             */
00290 // Holds the Threshold Value for Detecting Activity.
00291 // Data Format is Unsigned, so the Magnitude of the Activity Event is Compared 
00292 //  with the Value is Compared with the Value in the THRESH_ACT Register. 
00293 // The Scale Factor is 62.5mg/LSB. 
00294 // Value of 0 may Result in Undesirable Behavior if the Activity Interrupt Enabled. 
00295 // It Accepts a Maximum Value of 255.
00296 void ADXL345::setActivityThreshold(int activityThreshold) {
00297     if(activityThreshold < 0)
00298     {
00299         activityThreshold = 0;
00300     }
00301     if(activityThreshold > 255)
00302     {
00303         activityThreshold = 255;
00304     }
00305     char _b = char (activityThreshold);
00306     writeToI2C(ADXL345_THRESH_ACT, _b);  
00307 }
00308 
00309 // Gets the THRESH_ACT char
00310 int ADXL345::getActivityThreshold() {
00311     char _b;
00312     readFromI2C(ADXL345_THRESH_ACT, 1, &_b);  
00313     return int (_b);
00314 }
00315 
00316 /********************** THRESH_INACT REGISTER ***********************/
00317 /*                          ~ SET & GET                             */
00318 // Holds the Threshold Value for Detecting Inactivity.
00319 // The Data Format is Unsigned, so the Magnitude of the INactivity Event is 
00320 //  Compared with the value in the THRESH_INACT Register. 
00321 // Scale Factor is 62.5mg/LSB. 
00322 // Value of 0 May Result in Undesirable Behavior if the Inactivity Interrupt Enabled. 
00323 // It Accepts a Maximum Value of 255.
00324 void ADXL345::setInactivityThreshold(int inactivityThreshold) {
00325     if(inactivityThreshold < 0)
00326     {
00327         inactivityThreshold = 0;
00328     }
00329     if(inactivityThreshold > 255)
00330     {
00331         inactivityThreshold = 255;
00332     }
00333     char _b = char (inactivityThreshold);
00334     writeToI2C(ADXL345_THRESH_INACT, _b);  
00335 }
00336 
00337 int ADXL345::getInactivityThreshold() {
00338     char _b;
00339     readFromI2C(ADXL345_THRESH_INACT, 1, &_b);  
00340     return int (_b);
00341 }
00342 
00343 /*********************** TIME_INACT RESIGER *************************/
00344 /*                          ~ SET & GET                             */
00345 // Contains an Unsigned Time Value Representing the Amount of Time that
00346 //  Acceleration must be Less Than the Value in the THRESH_INACT Register
00347 //  for Inactivity to be Declared. 
00348 // Uses Filtered Output Data* unlike other Interrupt Functions
00349 // Scale Factor is 1sec/LSB. 
00350 // Value Must Be Between 0 and 255. 
00351 void ADXL345::setTimeInactivity(int timeInactivity) {
00352     if(timeInactivity < 0)
00353     {
00354         timeInactivity = 0;
00355     }
00356     if(timeInactivity > 255)
00357     {
00358         timeInactivity = 255;
00359     }
00360     char _b = char (timeInactivity);
00361     writeToI2C(ADXL345_TIME_INACT, _b);  
00362 }
00363 
00364 int ADXL345::getTimeInactivity() {
00365     char _b;
00366     readFromI2C(ADXL345_TIME_INACT, 1, &_b);  
00367     return int (_b);
00368 }
00369 
00370 /*********************** THRESH_FF Register *************************/
00371 /*                          ~ SET & GET                             */
00372 // Holds the Threshold Value, in Unsigned Format, for Free-Fall Detection
00373 // The Acceleration on all Axes is Compared with the Value in THRES_FF to
00374 //  Determine if a Free-Fall Event Occurred. 
00375 // Scale Factor is 62.5mg/LSB. 
00376 // Value of 0 May Result in Undesirable Behavior if the Free-Fall interrupt Enabled.
00377 // Accepts a Maximum Value of 255.
00378 void ADXL345::setFreeFallThreshold(int freeFallThreshold) {
00379     if(freeFallThreshold < 0)
00380     {
00381         freeFallThreshold = 0;
00382     }
00383     if(freeFallThreshold > 255)
00384     {
00385         freeFallThreshold = 255;
00386     }
00387     char _b = char (freeFallThreshold);
00388     writeToI2C(ADXL345_THRESH_FF, _b);  
00389 }
00390 
00391 int ADXL345::getFreeFallThreshold() {
00392     char _b;
00393     readFromI2C(ADXL345_THRESH_FF, 1, &_b);  
00394     return int (_b);
00395 }
00396 
00397 /************************ TIME_FF Register **************************/
00398 /*                          ~ SET & GET                             */
00399 // Stores an Unsigned Time Value Representing the Minimum Time that the Value 
00400 //  of all Axes must be Less Than THRES_FF to Generate a Free-Fall Interrupt.
00401 // Scale Factor is 5ms/LSB. 
00402 // Value of 0 May Result in Undesirable Behavior if the Free-Fall Interrupt Enabled.
00403 // Accepts a Maximum Value of 255.
00404 void ADXL345::setFreeFallDuration(int freeFallDuration) {
00405     if(freeFallDuration < 0)
00406     {
00407         freeFallDuration = 0;
00408     }
00409     if(freeFallDuration > 255)
00410     {
00411         freeFallDuration = 255;
00412     }
00413     char _b = char (freeFallDuration);
00414     writeToI2C(ADXL345_TIME_FF, _b);  
00415 }
00416 
00417 int ADXL345::getFreeFallDuration() {
00418     char _b;
00419     readFromI2C(ADXL345_TIME_FF, 1, &_b);  
00420     return int (_b);
00421 }
00422 
00423 /************************** ACTIVITY BITS ***************************/
00424 /*                                                                  */
00425 bool ADXL345::isActivityXEnabled() {  
00426     return getRegisterBit(ADXL345_ACT_INACT_CTL, 6); 
00427 }
00428 bool ADXL345::isActivityYEnabled() {  
00429     return getRegisterBit(ADXL345_ACT_INACT_CTL, 5); 
00430 }
00431 bool ADXL345::isActivityZEnabled() {  
00432     return getRegisterBit(ADXL345_ACT_INACT_CTL, 4); 
00433 }
00434 bool ADXL345::isInactivityXEnabled() {  
00435     return getRegisterBit(ADXL345_ACT_INACT_CTL, 2); 
00436 }
00437 bool ADXL345::isInactivityYEnabled() {  
00438     return getRegisterBit(ADXL345_ACT_INACT_CTL, 1); 
00439 }
00440 bool ADXL345::isInactivityZEnabled() {  
00441     return getRegisterBit(ADXL345_ACT_INACT_CTL, 0); 
00442 }
00443 
00444 void ADXL345::setActivityX(bool state) {  
00445     setRegisterBit(ADXL345_ACT_INACT_CTL, 6, state); 
00446 }
00447 void ADXL345::setActivityY(bool state) {  
00448     setRegisterBit(ADXL345_ACT_INACT_CTL, 5, state); 
00449 }
00450 void ADXL345::setActivityZ(bool state) {  
00451     setRegisterBit(ADXL345_ACT_INACT_CTL, 4, state); 
00452 }
00453 void ADXL345::setActivityXYZ(bool stateX, bool stateY, bool stateZ) {
00454     setActivityX(stateX);
00455     setActivityY(stateY);
00456     setActivityZ(stateZ);
00457 }
00458 void ADXL345::setInactivityX(bool state) {  
00459     setRegisterBit(ADXL345_ACT_INACT_CTL, 2, state); 
00460 }
00461 void ADXL345::setInactivityY(bool state) {  
00462     setRegisterBit(ADXL345_ACT_INACT_CTL, 1, state); 
00463 }
00464 void ADXL345::setInactivityZ(bool state) {  
00465     setRegisterBit(ADXL345_ACT_INACT_CTL, 0, state); 
00466 }
00467 void ADXL345::setInactivityXYZ(bool stateX, bool stateY, bool stateZ) {
00468     setInactivityX(stateX);
00469     setInactivityY(stateY);
00470     setInactivityZ(stateZ);
00471 }
00472 
00473 bool ADXL345::isActivityAc() { 
00474     return getRegisterBit(ADXL345_ACT_INACT_CTL, 7); 
00475 }
00476 bool ADXL345::isInactivityAc(){ 
00477     return getRegisterBit(ADXL345_ACT_INACT_CTL, 3); 
00478 }
00479 
00480 void ADXL345::setActivityAc(bool state) {  
00481     setRegisterBit(ADXL345_ACT_INACT_CTL, 7, state); 
00482 }
00483 void ADXL345::setInactivityAc(bool state) {  
00484     setRegisterBit(ADXL345_ACT_INACT_CTL, 3, state); 
00485 }
00486 
00487 /************************* SUPPRESS BITS ****************************/
00488 /*                                                                  */
00489 bool ADXL345::getSuppressBit(){ 
00490     return getRegisterBit(ADXL345_TAP_AXES, 3); 
00491 }
00492 void ADXL345::setSuppressBit(bool state) {  
00493     setRegisterBit(ADXL345_TAP_AXES, 3, state); 
00494 }
00495 
00496 /**************************** TAP BITS ******************************/
00497 /*                                                                  */
00498 bool ADXL345::isTapDetectionOnX(){ 
00499     return getRegisterBit(ADXL345_TAP_AXES, 2); 
00500 }
00501 void ADXL345::setTapDetectionOnX(bool state) {  
00502     setRegisterBit(ADXL345_TAP_AXES, 2, state); 
00503 }
00504 bool ADXL345::isTapDetectionOnY(){ 
00505     return getRegisterBit(ADXL345_TAP_AXES, 1); 
00506 }
00507 void ADXL345::setTapDetectionOnY(bool state) {  
00508     setRegisterBit(ADXL345_TAP_AXES, 1, state); 
00509 }
00510 bool ADXL345::isTapDetectionOnZ(){ 
00511     return getRegisterBit(ADXL345_TAP_AXES, 0); 
00512 }
00513 void ADXL345::setTapDetectionOnZ(bool state) {  
00514     setRegisterBit(ADXL345_TAP_AXES, 0, state); 
00515 }
00516 
00517 void ADXL345::setTapDetectionOnXYZ(bool stateX, bool stateY, bool stateZ) {
00518     setTapDetectionOnX(stateX);
00519     setTapDetectionOnY(stateY);
00520     setTapDetectionOnZ(stateZ);
00521 }
00522 
00523 bool ADXL345::isActivitySourceOnX(){ 
00524     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 6); 
00525 }
00526 bool ADXL345::isActivitySourceOnY(){ 
00527     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 5); 
00528 }
00529 bool ADXL345::isActivitySourceOnZ(){ 
00530     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 4); 
00531 }
00532 
00533 bool ADXL345::isTapSourceOnX(){ 
00534     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 2); 
00535 }
00536 bool ADXL345::isTapSourceOnY(){ 
00537     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 1); 
00538 }
00539 bool ADXL345::isTapSourceOnZ(){ 
00540     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 0); 
00541 }
00542 
00543 /*************************** ASLEEP BIT *****************************/
00544 /*                                                                  */
00545 bool ADXL345::isAsleep(){ 
00546     return getRegisterBit(ADXL345_ACT_TAP_STATUS, 3); 
00547 }
00548 
00549 /************************** LOW POWER BIT ***************************/
00550 /*                                                                  */
00551 bool ADXL345::isLowPower(){ 
00552     return getRegisterBit(ADXL345_BW_RATE, 4); 
00553 }
00554 void ADXL345::setLowPower(bool state) {  
00555     setRegisterBit(ADXL345_BW_RATE, 4, state); 
00556 }
00557 
00558 
00559 /************************* TRIGGER CHECK  ***************************/
00560 /*                                                                  */
00561 // Check if Action was Triggered in Interrupts
00562 // Example triggered(interrupts, ADXL345_SINGLE_TAP);
00563 bool ADXL345::triggered(char interrupts, int mask){
00564     return ((interrupts >> mask) & 1);
00565 }
00566 
00567 /*
00568  ADXL345_DATA_READY
00569  ADXL345_SINGLE_TAP
00570  ADXL345_DOUBLE_TAP
00571  ADXL345_ACTIVITY
00572  ADXL345_INACTIVITY
00573  ADXL345_FREE_FALL
00574  ADXL345_WATERMARK
00575  ADXL345_OVERRUNY
00576  */
00577 
00578 
00579 char ADXL345::getInterruptSource() {
00580     char _b;
00581     readFromI2C(ADXL345_INT_SOURCE, 1, &_b);
00582     return _b;
00583 }
00584 
00585 bool ADXL345::getInterruptSource(char interruptBit) {
00586     return getRegisterBit(ADXL345_INT_SOURCE,interruptBit);
00587 }
00588 
00589 bool ADXL345::getInterruptMapping(char interruptBit) {
00590     return getRegisterBit(ADXL345_INT_MAP,interruptBit);
00591 }
00592 
00593 /*********************** INTERRUPT MAPPING **************************/
00594 /*         Set the Mapping of an Interrupt to pin1 or pin2          */
00595 // eg: setInterruptMapping(ADXL345_INT_DOUBLE_TAP_BIT,ADXL345_INT2_PIN);
00596 void ADXL345::setInterruptMapping(char interruptBit, bool interruptPin) {
00597     setRegisterBit(ADXL345_INT_MAP, interruptBit, interruptPin);
00598 }
00599 
00600 void ADXL345::setImportantInterruptMapping(int single_tap, int double_tap, int free_fall, int activity, int inactivity) {
00601     if(single_tap == 1) {
00602         setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT,   ADXL345_INT1_PIN );}
00603     else if(single_tap == 2) {
00604         setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT,   ADXL345_INT2_PIN );}
00605 
00606     if(double_tap == 1) {
00607         setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT,   ADXL345_INT1_PIN );}
00608     else if(double_tap == 2) {
00609         setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT,   ADXL345_INT2_PIN );}
00610 
00611     if(free_fall == 1) {
00612         setInterruptMapping( ADXL345_INT_FREE_FALL_BIT,   ADXL345_INT1_PIN );}
00613     else if(free_fall == 2) {
00614         setInterruptMapping( ADXL345_INT_FREE_FALL_BIT,   ADXL345_INT2_PIN );}
00615 
00616     if(activity == 1) {
00617         setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,   ADXL345_INT1_PIN );}
00618     else if(activity == 2) {
00619         setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,   ADXL345_INT2_PIN );}
00620 
00621     if(inactivity == 1) {
00622         setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,   ADXL345_INT1_PIN );}
00623     else if(inactivity == 2) {
00624         setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,   ADXL345_INT2_PIN );}
00625 }
00626 
00627 bool ADXL345::isInterruptEnabled(char interruptBit) {
00628     return getRegisterBit(ADXL345_INT_ENABLE,interruptBit);
00629 }
00630 
00631 void ADXL345::setInterrupt(char interruptBit, bool state) {
00632     setRegisterBit(ADXL345_INT_ENABLE, interruptBit, state);
00633 }
00634 
00635 void ADXL345::singleTapINT(bool status) {
00636     if(status) {
00637         setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
00638     }
00639     else {
00640         setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 0);
00641     }
00642 }
00643 void ADXL345::doubleTapINT(bool status) {
00644     if(status) {
00645         setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
00646     }
00647     else {
00648         setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 0);       
00649     }   
00650 }
00651 void ADXL345::FreeFallINT(bool status) {
00652     if(status) {
00653         setInterrupt( ADXL345_INT_FREE_FALL_BIT,  1);
00654     }
00655     else {
00656         setInterrupt( ADXL345_INT_FREE_FALL_BIT,  0);
00657     }   
00658 }
00659 void ADXL345::ActivityINT(bool status) {
00660     if(status) {
00661         setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
00662     }
00663     else {
00664         setInterrupt( ADXL345_INT_ACTIVITY_BIT,   0);
00665     }
00666 }
00667 void ADXL345::InactivityINT(bool status) {
00668     if(status) {
00669         setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
00670     }
00671     else {
00672         setInterrupt( ADXL345_INT_INACTIVITY_BIT, 0);
00673     }
00674 }
00675 
00676 void ADXL345::setRegisterBit(char regAdress, int bitPos, bool state) {
00677     char _b;
00678     readFromI2C(regAdress, 1, &_b);
00679     if (state) {
00680         _b |= (1 << bitPos);  // Forces nth Bit of _b to 1. Other Bits Unchanged.  
00681     } 
00682     else {
00683         _b &= ~(1 << bitPos); // Forces nth Bit of _b to 0. Other Bits Unchanged.
00684     }
00685     writeToI2C(regAdress, _b);  
00686 }
00687 
00688 bool ADXL345::getRegisterBit(char regAdress, int bitPos) {
00689     char _b;
00690     readFromI2C(regAdress, 1, &_b);
00691     return ((_b >> bitPos) & 1);
00692 }