BrickUp API Service for Docker version.

ADXL345.h 7.3KB

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