ADXL345.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. ADXL345.h
  3. Library for accelerometer_ADXL345
  4. Copyright (c) 2013 seeed technology inc.
  5. Author : FrankieChu
  6. Create Time : Jan 2013
  7. Change Log :
  8. The MIT License (MIT)
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. */
  25. #include "Arduino.h"
  26. #ifndef ADXL345_h
  27. #define ADXL345_h
  28. /* ------- Register names ------- */
  29. #define ADXL345_DEVID 0x00
  30. #define ADXL345_RESERVED1 0x01
  31. #define ADXL345_THRESH_TAP 0x1d
  32. #define ADXL345_OFSX 0x1e
  33. #define ADXL345_OFSY 0x1f
  34. #define ADXL345_OFSZ 0x20
  35. #define ADXL345_DUR 0x21
  36. #define ADXL345_LATENT 0x22
  37. #define ADXL345_WINDOW 0x23
  38. #define ADXL345_THRESH_ACT 0x24
  39. #define ADXL345_THRESH_INACT 0x25
  40. #define ADXL345_TIME_INACT 0x26
  41. #define ADXL345_ACT_INACT_CTL 0x27
  42. #define ADXL345_THRESH_FF 0x28
  43. #define ADXL345_TIME_FF 0x29
  44. #define ADXL345_TAP_AXES 0x2a
  45. #define ADXL345_ACT_TAP_STATUS 0x2b
  46. #define ADXL345_BW_RATE 0x2c
  47. #define ADXL345_POWER_CTL 0x2d
  48. #define ADXL345_INT_ENABLE 0x2e
  49. #define ADXL345_INT_MAP 0x2f
  50. #define ADXL345_INT_SOURCE 0x30
  51. #define ADXL345_DATA_FORMAT 0x31
  52. #define ADXL345_DATAX0 0x32
  53. #define ADXL345_DATAX1 0x33
  54. #define ADXL345_DATAY0 0x34
  55. #define ADXL345_DATAY1 0x35
  56. #define ADXL345_DATAZ0 0x36
  57. #define ADXL345_DATAZ1 0x37
  58. #define ADXL345_FIFO_CTL 0x38
  59. #define ADXL345_FIFO_STATUS 0x39
  60. #define ADXL345_BW_1600 0xF // 1111
  61. #define ADXL345_BW_800 0xE // 1110
  62. #define ADXL345_BW_400 0xD // 1101
  63. #define ADXL345_BW_200 0xC // 1100
  64. #define ADXL345_BW_100 0xB // 1011
  65. #define ADXL345_BW_50 0xA // 1010
  66. #define ADXL345_BW_25 0x9 // 1001
  67. #define ADXL345_BW_12 0x8 // 1000
  68. #define ADXL345_BW_6 0x7 // 0111
  69. #define ADXL345_BW_3 0x6 // 0110
  70. /*
  71. Interrupt PINs
  72. INT1: 0
  73. INT2: 1
  74. */
  75. #define ADXL345_INT1_PIN 0x00
  76. #define ADXL345_INT2_PIN 0x01
  77. /*Interrupt bit position*/
  78. #define ADXL345_INT_DATA_READY_BIT 0x07
  79. #define ADXL345_INT_SINGLE_TAP_BIT 0x06
  80. #define ADXL345_INT_DOUBLE_TAP_BIT 0x05
  81. #define ADXL345_INT_ACTIVITY_BIT 0x04
  82. #define ADXL345_INT_INACTIVITY_BIT 0x03
  83. #define ADXL345_INT_FREE_FALL_BIT 0x02
  84. #define ADXL345_INT_WATERMARK_BIT 0x01
  85. #define ADXL345_INT_OVERRUNY_BIT 0x00
  86. #define ADXL345_DATA_READY 0x07
  87. #define ADXL345_SINGLE_TAP 0x06
  88. #define ADXL345_DOUBLE_TAP 0x05
  89. #define ADXL345_ACTIVITY 0x04
  90. #define ADXL345_INACTIVITY 0x03
  91. #define ADXL345_FREE_FALL 0x02
  92. #define ADXL345_WATERMARK 0x01
  93. #define ADXL345_OVERRUNY 0x00
  94. #define ADXL345_OK 1 // no error
  95. #define ADXL345_ERROR 0 // indicates error is predent
  96. #define ADXL345_NO_ERROR 0 // initial state
  97. #define ADXL345_READ_ERROR 1 // problem reading accel
  98. #define ADXL345_BAD_ARG 2 // bad method argument
  99. #define ADXL345_MODE_BYPASS 0x00 //0000
  100. #define ADXL345_MODE_FIFO 0x01 //0001
  101. #define ADXL345_MODE_STREM 0x02 //0010
  102. #define ADXL345_MODE_TRIGGER 0x03 //0011
  103. class ADXL345 {
  104. public:
  105. bool status; // set when error occurs
  106. // see error code for details
  107. byte error_code; // Initial state
  108. double gains[3]; // counts to Gs
  109. ADXL345();
  110. void powerOn();
  111. void readAccel(int* xyx);
  112. void readXYZ(int* x, int* y, int* z);
  113. void getAcceleration(double* xyz);
  114. void setTapThreshold(int tapThreshold);
  115. int getTapThreshold();
  116. void setAxisGains(double* _gains);
  117. void getAxisGains(double* _gains);
  118. void setAxisOffset(int x, int y, int z);
  119. void getAxisOffset(int* x, int* y, int* z);
  120. void setTapDuration(int tapDuration);
  121. int getTapDuration();
  122. void setDoubleTapLatency(int doubleTapLatency);
  123. int getDoubleTapLatency();
  124. void setDoubleTapWindow(int doubleTapWindow);
  125. int getDoubleTapWindow();
  126. void setActivityThreshold(int activityThreshold);
  127. int getActivityThreshold();
  128. void setInactivityThreshold(int inactivityThreshold);
  129. int getInactivityThreshold();
  130. void setTimeInactivity(int timeInactivity);
  131. int getTimeInactivity();
  132. void setFreeFallThreshold(int freeFallthreshold);
  133. int getFreeFallThreshold();
  134. void setFreeFallDuration(int freeFallDuration);
  135. int getFreeFallDuration();
  136. bool isActivityXEnabled();
  137. bool isActivityYEnabled();
  138. bool isActivityZEnabled();
  139. bool isInactivityXEnabled();
  140. bool isInactivityYEnabled();
  141. bool isInactivityZEnabled();
  142. bool isActivityAc();
  143. bool isInactivityAc();
  144. void setActivityAc(bool state);
  145. void setInactivityAc(bool state);
  146. bool getSuppressBit();
  147. void setSuppressBit(bool state);
  148. bool isTapDetectionOnX();
  149. void setTapDetectionOnX(bool state);
  150. bool isTapDetectionOnY();
  151. void setTapDetectionOnY(bool state);
  152. bool isTapDetectionOnZ();
  153. void setTapDetectionOnZ(bool state);
  154. void setActivityX(bool state);
  155. void setActivityY(bool state);
  156. void setActivityZ(bool state);
  157. void setInactivityX(bool state);
  158. void setInactivityY(bool state);
  159. void setInactivityZ(bool state);
  160. bool isActivitySourceOnX();
  161. bool isActivitySourceOnY();
  162. bool isActivitySourceOnZ();
  163. bool isTapSourceOnX();
  164. bool isTapSourceOnY();
  165. bool isTapSourceOnZ();
  166. bool isAsleep();
  167. bool isLowPower();
  168. void setLowPower(bool state);
  169. double getRate();
  170. void setRate(double rate);
  171. void set_bw(byte bw_code);
  172. byte get_bw_code();
  173. bool triggered(byte interrupts, int mask);
  174. byte getInterruptSource();
  175. bool getInterruptSource(byte interruptBit);
  176. bool getInterruptMapping(byte interruptBit);
  177. void setInterruptMapping(byte interruptBit, bool interruptPin);
  178. bool isInterruptEnabled(byte interruptBit);
  179. void setInterrupt(byte interruptBit, bool state);
  180. void setMode(byte modeBit); // setting operation mode
  181. byte getMode();
  182. void setWatermark(byte watermark);
  183. byte getFifoEntries(void); // reading number of samples from fifo
  184. void burstReadXYZ(int* x, int* y, int* z, byte samples); // burst read function for getting all samples from fifo
  185. void getRangeSetting(byte* rangeSetting);
  186. void setRangeSetting(int val);
  187. bool getSelfTestBit();
  188. void setSelfTestBit(bool selfTestBit);
  189. bool getSpiBit();
  190. void setSpiBit(bool spiBit);
  191. bool getInterruptLevelBit();
  192. void setInterruptLevelBit(bool interruptLevelBit);
  193. bool getFullResBit();
  194. void setFullResBit(bool fullResBit);
  195. bool getJustifyBit();
  196. void setJustifyBit(bool justifyBit);
  197. void printAllRegister();
  198. private:
  199. void writeTo(byte address, byte val);
  200. void readFrom(byte address, int num, byte buff[]);
  201. void setRegisterBit(byte regAdress, int bitPos, bool state);
  202. bool getRegisterBit(byte regAdress, int bitPos);
  203. byte _buff[6] ; //6 bytes buffer for saving data read from the device
  204. };
  205. void print_byte(byte val);
  206. #endif