ADXL345.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. #include "ADXL345.h"
  27. #include <Wire.h>
  28. #define ADXL345_DEVICE (0x53) // ADXL345 device address
  29. #define ADXL345_TO_READ (6) // num of bytes we are going to read each time (two bytes for each axis)
  30. ADXL345::ADXL345() {
  31. status = ADXL345_OK;
  32. error_code = ADXL345_NO_ERROR;
  33. gains[0] = 0.00376390;
  34. gains[1] = 0.00376009;
  35. gains[2] = 0.00349265;
  36. }
  37. void ADXL345::powerOn() {
  38. Wire.begin(); // join i2c bus (address optional for master)
  39. //Turning on the ADXL345
  40. writeTo(ADXL345_POWER_CTL, 0);
  41. writeTo(ADXL345_POWER_CTL, 16);
  42. writeTo(ADXL345_POWER_CTL, 8);
  43. }
  44. // Reads the acceleration into three variable x, y and z
  45. void ADXL345::readAccel(int* xyz) {
  46. readXYZ(xyz, xyz + 1, xyz + 2);
  47. }
  48. void ADXL345::readXYZ(int* x, int* y, int* z) {
  49. readFrom(ADXL345_DATAX0, ADXL345_TO_READ, _buff); //read the acceleration data from the ADXL345
  50. *x = (short)((((unsigned short)_buff[1]) << 8) | _buff[0]);
  51. *y = (short)((((unsigned short)_buff[3]) << 8) | _buff[2]);
  52. *z = (short)((((unsigned short)_buff[5]) << 8) | _buff[4]);
  53. }
  54. void ADXL345::getAcceleration(double* xyz) {
  55. int i;
  56. int xyz_int[3];
  57. readAccel(xyz_int);
  58. for (i = 0; i < 3; i++) {
  59. xyz[i] = xyz_int[i] * gains[i];
  60. }
  61. }
  62. // Writes val to address register on device
  63. void ADXL345::writeTo(byte address, byte val) {
  64. Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device
  65. Wire.write(address); // send register address
  66. Wire.write(val); // send value to write
  67. Wire.endTransmission(); // end transmission
  68. }
  69. // Reads num bytes starting from address register on device in to _buff array
  70. void ADXL345::readFrom(byte address, int num, byte _buff[]) {
  71. Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device
  72. Wire.write(address); // sends address to read from
  73. Wire.endTransmission(); // end transmission
  74. Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device
  75. Wire.requestFrom(ADXL345_DEVICE, num); // request 6 bytes from device
  76. int i = 0;
  77. while (Wire.available()) { // device may send less than requested (abnormal)
  78. _buff[i] = Wire.read(); // receive a byte
  79. i++;
  80. }
  81. if (i != num) {
  82. status = ADXL345_ERROR;
  83. error_code = ADXL345_READ_ERROR;
  84. }
  85. Wire.endTransmission(); // end transmission
  86. }
  87. // Gets the range setting and return it into rangeSetting
  88. // it can be 2, 4, 8 or 16
  89. void ADXL345::getRangeSetting(byte* rangeSetting) {
  90. byte _b;
  91. readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  92. *rangeSetting = _b & B00000011;
  93. }
  94. // Sets the range setting, possible values are: 2, 4, 8, 16
  95. void ADXL345::setRangeSetting(int val) {
  96. byte _s;
  97. byte _b;
  98. switch (val) {
  99. case 2:
  100. _s = B00000000;
  101. break;
  102. case 4:
  103. _s = B00000001;
  104. break;
  105. case 8:
  106. _s = B00000010;
  107. break;
  108. case 16:
  109. _s = B00000011;
  110. break;
  111. default:
  112. _s = B00000000;
  113. }
  114. readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  115. _s |= (_b & B11101100);
  116. writeTo(ADXL345_DATA_FORMAT, _s);
  117. }
  118. // gets the state of the SELF_TEST bit
  119. bool ADXL345::getSelfTestBit() {
  120. return getRegisterBit(ADXL345_DATA_FORMAT, 7);
  121. }
  122. // Sets the SELF-TEST bit
  123. // if set to 1 it applies a self-test force to the sensor causing a shift in the output data
  124. // if set to 0 it disables the self-test force
  125. void ADXL345::setSelfTestBit(bool selfTestBit) {
  126. setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);
  127. }
  128. // Gets the state of the SPI bit
  129. bool ADXL345::getSpiBit() {
  130. return getRegisterBit(ADXL345_DATA_FORMAT, 6);
  131. }
  132. // Sets the SPI bit
  133. // if set to 1 it sets the device to 3-wire mode
  134. // if set to 0 it sets the device to 4-wire SPI mode
  135. void ADXL345::setSpiBit(bool spiBit) {
  136. setRegisterBit(ADXL345_DATA_FORMAT, 6, spiBit);
  137. }
  138. // Gets the state of the INT_INVERT bit
  139. bool ADXL345::getInterruptLevelBit() {
  140. return getRegisterBit(ADXL345_DATA_FORMAT, 5);
  141. }
  142. // Sets the INT_INVERT bit
  143. // if set to 0 sets the interrupts to active high
  144. // if set to 1 sets the interrupts to active low
  145. void ADXL345::setInterruptLevelBit(bool interruptLevelBit) {
  146. setRegisterBit(ADXL345_DATA_FORMAT, 5, interruptLevelBit);
  147. }
  148. // Gets the state of the FULL_RES bit
  149. bool ADXL345::getFullResBit() {
  150. return getRegisterBit(ADXL345_DATA_FORMAT, 3);
  151. }
  152. // Sets the FULL_RES bit
  153. // if set to 1, the device is in full resolution mode, where the output resolution increases with the
  154. // g range set by the range bits to maintain a 4mg/LSB scal factor
  155. // if set to 0, the device is in 10-bit mode, and the range buts determine the maximum g range
  156. // and scale factor
  157. void ADXL345::setFullResBit(bool fullResBit) {
  158. setRegisterBit(ADXL345_DATA_FORMAT, 3, fullResBit);
  159. }
  160. // Gets the state of the justify bit
  161. bool ADXL345::getJustifyBit() {
  162. return getRegisterBit(ADXL345_DATA_FORMAT, 2);
  163. }
  164. // Sets the JUSTIFY bit
  165. // if sets to 1 selects the left justified mode
  166. // if sets to 0 selects right justified mode with sign extension
  167. void ADXL345::setJustifyBit(bool justifyBit) {
  168. setRegisterBit(ADXL345_DATA_FORMAT, 2, justifyBit);
  169. }
  170. // Sets the THRESH_TAP byte value
  171. // it should be between 0 and 255
  172. // the scale factor is 62.5 mg/LSB
  173. // A value of 0 may result in undesirable behavior
  174. void ADXL345::setTapThreshold(int tapThreshold) {
  175. tapThreshold = constrain(tapThreshold, 0, 255);
  176. byte _b = byte(tapThreshold);
  177. writeTo(ADXL345_THRESH_TAP, _b);
  178. }
  179. // Gets the THRESH_TAP byte value
  180. // return value is comprised between 0 and 255
  181. // the scale factor is 62.5 mg/LSB
  182. int ADXL345::getTapThreshold() {
  183. byte _b;
  184. readFrom(ADXL345_THRESH_TAP, 1, &_b);
  185. return int (_b);
  186. }
  187. // set/get the gain for each axis in Gs / count
  188. void ADXL345::setAxisGains(double* _gains) {
  189. int i;
  190. for (i = 0; i < 3; i++) {
  191. gains[i] = _gains[i];
  192. }
  193. }
  194. void ADXL345::getAxisGains(double* _gains) {
  195. int i;
  196. for (i = 0; i < 3; i++) {
  197. _gains[i] = gains[i];
  198. }
  199. }
  200. // Sets the OFSX, OFSY and OFSZ bytes
  201. // OFSX, OFSY and OFSZ are user offset adjustments in twos complement format with
  202. // a scale factor of 15,6mg/LSB
  203. // OFSX, OFSY and OFSZ should be comprised between
  204. void ADXL345::setAxisOffset(int x, int y, int z) {
  205. writeTo(ADXL345_OFSX, byte(x));
  206. writeTo(ADXL345_OFSY, byte(y));
  207. writeTo(ADXL345_OFSZ, byte(z));
  208. }
  209. // Gets the OFSX, OFSY and OFSZ bytes
  210. void ADXL345::getAxisOffset(int* x, int* y, int* z) {
  211. byte _b;
  212. readFrom(ADXL345_OFSX, 1, &_b);
  213. *x = int (_b);
  214. readFrom(ADXL345_OFSY, 1, &_b);
  215. *y = int (_b);
  216. readFrom(ADXL345_OFSZ, 1, &_b);
  217. *z = int (_b);
  218. }
  219. // Sets the DUR byte
  220. // The DUR byte contains an unsigned time value representing the maximum time
  221. // that an event must be above THRESH_TAP threshold to qualify as a tap event
  222. // The scale factor is 625µs/LSB
  223. // A value of 0 disables the tap/double tap funcitons. Max value is 255.
  224. void ADXL345::setTapDuration(int tapDuration) {
  225. tapDuration = constrain(tapDuration, 0, 255);
  226. byte _b = byte(tapDuration);
  227. writeTo(ADXL345_DUR, _b);
  228. }
  229. // Gets the DUR byte
  230. int ADXL345::getTapDuration() {
  231. byte _b;
  232. readFrom(ADXL345_DUR, 1, &_b);
  233. return int (_b);
  234. }
  235. // Sets the latency (latent register) which contains an unsigned time value
  236. // representing the wait time from the detection of a tap event to the start
  237. // of the time window, during which a possible second tap can be detected.
  238. // The scale factor is 1.25ms/LSB. A value of 0 disables the double tap function.
  239. // It accepts a maximum value of 255.
  240. void ADXL345::setDoubleTapLatency(int doubleTapLatency) {
  241. byte _b = byte(doubleTapLatency);
  242. writeTo(ADXL345_LATENT, _b);
  243. }
  244. // Gets the Latent value
  245. int ADXL345::getDoubleTapLatency() {
  246. byte _b;
  247. readFrom(ADXL345_LATENT, 1, &_b);
  248. return int (_b);
  249. }
  250. // Sets the Window register, which contains an unsigned time value representing
  251. // the amount of time after the expiration of the latency time (Latent register)
  252. // during which a second valud tap can begin. The scale factor is 1.25ms/LSB. A
  253. // value of 0 disables the double tap function. The maximum value is 255.
  254. void ADXL345::setDoubleTapWindow(int doubleTapWindow) {
  255. doubleTapWindow = constrain(doubleTapWindow, 0, 255);
  256. byte _b = byte(doubleTapWindow);
  257. writeTo(ADXL345_WINDOW, _b);
  258. }
  259. // Gets the Window register
  260. int ADXL345::getDoubleTapWindow() {
  261. byte _b;
  262. readFrom(ADXL345_WINDOW, 1, &_b);
  263. return int (_b);
  264. }
  265. // Sets the THRESH_ACT byte which holds the threshold value for detecting activity.
  266. // The data format is unsigned, so the magnitude of the activity event is compared
  267. // with the value is compared with the value in the THRESH_ACT register. The scale
  268. // factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the
  269. // activity interrupt is enabled. The maximum value is 255.
  270. void ADXL345::setActivityThreshold(int activityThreshold) {
  271. activityThreshold = constrain(activityThreshold, 0, 255);
  272. byte _b = byte(activityThreshold);
  273. writeTo(ADXL345_THRESH_ACT, _b);
  274. }
  275. // Gets the THRESH_ACT byte
  276. int ADXL345::getActivityThreshold() {
  277. byte _b;
  278. readFrom(ADXL345_THRESH_ACT, 1, &_b);
  279. return int (_b);
  280. }
  281. // Sets the THRESH_INACT byte which holds the threshold value for detecting inactivity.
  282. // The data format is unsigned, so the magnitude of the inactivity event is compared
  283. // with the value is compared with the value in the THRESH_INACT register. The scale
  284. // factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the
  285. // inactivity interrupt is enabled. The maximum value is 255.
  286. void ADXL345::setInactivityThreshold(int inactivityThreshold) {
  287. inactivityThreshold = constrain(inactivityThreshold, 0, 255);
  288. byte _b = byte(inactivityThreshold);
  289. writeTo(ADXL345_THRESH_INACT, _b);
  290. }
  291. // Gets the THRESH_INACT byte
  292. int ADXL345::getInactivityThreshold() {
  293. byte _b;
  294. readFrom(ADXL345_THRESH_INACT, 1, &_b);
  295. return int (_b);
  296. }
  297. // Sets the TIME_INACT register, which contains an unsigned time value representing the
  298. // amount of time that acceleration must be less thant the value in the THRESH_INACT
  299. // register for inactivity to be declared. The scale factor is 1sec/LSB. The value must
  300. // be between 0 and 255.
  301. void ADXL345::setTimeInactivity(int timeInactivity) {
  302. timeInactivity = constrain(timeInactivity, 0, 255);
  303. byte _b = byte(timeInactivity);
  304. writeTo(ADXL345_TIME_INACT, _b);
  305. }
  306. // Gets the TIME_INACT register
  307. int ADXL345::getTimeInactivity() {
  308. byte _b;
  309. readFrom(ADXL345_TIME_INACT, 1, &_b);
  310. return int (_b);
  311. }
  312. // Sets the THRESH_FF register which holds the threshold value, in an unsigned format, for
  313. // free-fall detection. The root-sum-square (RSS) value of all axes is calculated and
  314. // compared whith the value in THRESH_FF to determine if a free-fall event occured. The
  315. // scale factor is 62.5mg/LSB. A value of 0 may result in undesirable behavior if the free-fall
  316. // interrupt is enabled. The maximum value is 255.
  317. void ADXL345::setFreeFallThreshold(int freeFallThreshold) {
  318. freeFallThreshold = constrain(freeFallThreshold, 0, 255);
  319. byte _b = byte(freeFallThreshold);
  320. writeTo(ADXL345_THRESH_FF, _b);
  321. }
  322. // Gets the THRESH_FF register.
  323. int ADXL345::getFreeFallThreshold() {
  324. byte _b;
  325. readFrom(ADXL345_THRESH_FF, 1, &_b);
  326. return int (_b);
  327. }
  328. // Sets the TIME_FF register, which holds an unsigned time value representing the minimum
  329. // time that the RSS value of all axes must be less than THRESH_FF to generate a free-fall
  330. // interrupt. The scale factor is 5ms/LSB. A value of 0 may result in undesirable behavior if
  331. // the free-fall interrupt is enabled. The maximum value is 255.
  332. void ADXL345::setFreeFallDuration(int freeFallDuration) {
  333. freeFallDuration = constrain(freeFallDuration, 0, 255);
  334. byte _b = byte(freeFallDuration);
  335. writeTo(ADXL345_TIME_FF, _b);
  336. }
  337. // Gets the TIME_FF register.
  338. int ADXL345::getFreeFallDuration() {
  339. byte _b;
  340. readFrom(ADXL345_TIME_FF, 1, &_b);
  341. return int (_b);
  342. }
  343. bool ADXL345::isActivityXEnabled() {
  344. return getRegisterBit(ADXL345_ACT_INACT_CTL, 6);
  345. }
  346. bool ADXL345::isActivityYEnabled() {
  347. return getRegisterBit(ADXL345_ACT_INACT_CTL, 5);
  348. }
  349. bool ADXL345::isActivityZEnabled() {
  350. return getRegisterBit(ADXL345_ACT_INACT_CTL, 4);
  351. }
  352. bool ADXL345::isInactivityXEnabled() {
  353. return getRegisterBit(ADXL345_ACT_INACT_CTL, 2);
  354. }
  355. bool ADXL345::isInactivityYEnabled() {
  356. return getRegisterBit(ADXL345_ACT_INACT_CTL, 1);
  357. }
  358. bool ADXL345::isInactivityZEnabled() {
  359. return getRegisterBit(ADXL345_ACT_INACT_CTL, 0);
  360. }
  361. void ADXL345::setActivityX(bool state) {
  362. setRegisterBit(ADXL345_ACT_INACT_CTL, 6, state);
  363. }
  364. void ADXL345::setActivityY(bool state) {
  365. setRegisterBit(ADXL345_ACT_INACT_CTL, 5, state);
  366. }
  367. void ADXL345::setActivityZ(bool state) {
  368. setRegisterBit(ADXL345_ACT_INACT_CTL, 4, state);
  369. }
  370. void ADXL345::setInactivityX(bool state) {
  371. setRegisterBit(ADXL345_ACT_INACT_CTL, 2, state);
  372. }
  373. void ADXL345::setInactivityY(bool state) {
  374. setRegisterBit(ADXL345_ACT_INACT_CTL, 1, state);
  375. }
  376. void ADXL345::setInactivityZ(bool state) {
  377. setRegisterBit(ADXL345_ACT_INACT_CTL, 0, state);
  378. }
  379. bool ADXL345::isActivityAc() {
  380. return getRegisterBit(ADXL345_ACT_INACT_CTL, 7);
  381. }
  382. bool ADXL345::isInactivityAc() {
  383. return getRegisterBit(ADXL345_ACT_INACT_CTL, 3);
  384. }
  385. void ADXL345::setActivityAc(bool state) {
  386. setRegisterBit(ADXL345_ACT_INACT_CTL, 7, state);
  387. }
  388. void ADXL345::setInactivityAc(bool state) {
  389. setRegisterBit(ADXL345_ACT_INACT_CTL, 3, state);
  390. }
  391. bool ADXL345::getSuppressBit() {
  392. return getRegisterBit(ADXL345_TAP_AXES, 3);
  393. }
  394. void ADXL345::setSuppressBit(bool state) {
  395. setRegisterBit(ADXL345_TAP_AXES, 3, state);
  396. }
  397. bool ADXL345::isTapDetectionOnX() {
  398. return getRegisterBit(ADXL345_TAP_AXES, 2);
  399. }
  400. void ADXL345::setTapDetectionOnX(bool state) {
  401. setRegisterBit(ADXL345_TAP_AXES, 2, state);
  402. }
  403. bool ADXL345::isTapDetectionOnY() {
  404. return getRegisterBit(ADXL345_TAP_AXES, 1);
  405. }
  406. void ADXL345::setTapDetectionOnY(bool state) {
  407. setRegisterBit(ADXL345_TAP_AXES, 1, state);
  408. }
  409. bool ADXL345::isTapDetectionOnZ() {
  410. return getRegisterBit(ADXL345_TAP_AXES, 0);
  411. }
  412. void ADXL345::setTapDetectionOnZ(bool state) {
  413. setRegisterBit(ADXL345_TAP_AXES, 0, state);
  414. }
  415. bool ADXL345::isActivitySourceOnX() {
  416. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 6);
  417. }
  418. bool ADXL345::isActivitySourceOnY() {
  419. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 5);
  420. }
  421. bool ADXL345::isActivitySourceOnZ() {
  422. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 4);
  423. }
  424. bool ADXL345::isTapSourceOnX() {
  425. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 2);
  426. }
  427. bool ADXL345::isTapSourceOnY() {
  428. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 1);
  429. }
  430. bool ADXL345::isTapSourceOnZ() {
  431. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 0);
  432. }
  433. bool ADXL345::isAsleep() {
  434. return getRegisterBit(ADXL345_ACT_TAP_STATUS, 3);
  435. }
  436. bool ADXL345::isLowPower() {
  437. return getRegisterBit(ADXL345_BW_RATE, 4);
  438. }
  439. void ADXL345::setLowPower(bool state) {
  440. setRegisterBit(ADXL345_BW_RATE, 4, state);
  441. }
  442. double ADXL345::getRate() {
  443. byte _b;
  444. readFrom(ADXL345_BW_RATE, 1, &_b);
  445. _b &= B00001111;
  446. return (pow(2, ((int) _b) - 6)) * 6.25;
  447. }
  448. void ADXL345::setRate(double rate) {
  449. byte _b, _s;
  450. int v = (int)(rate / 6.25);
  451. int r = 0;
  452. while (v >>= 1) {
  453. r++;
  454. }
  455. if (r <= 9) {
  456. readFrom(ADXL345_BW_RATE, 1, &_b);
  457. _s = (byte)(r + 6) | (_b & B11110000);
  458. writeTo(ADXL345_BW_RATE, _s);
  459. }
  460. }
  461. void ADXL345::set_bw(byte bw_code) {
  462. if ((bw_code < ADXL345_BW_3) || (bw_code > ADXL345_BW_1600)) {
  463. status = false;
  464. error_code = ADXL345_BAD_ARG;
  465. } else {
  466. writeTo(ADXL345_BW_RATE, bw_code);
  467. }
  468. }
  469. byte ADXL345::get_bw_code() {
  470. byte bw_code;
  471. readFrom(ADXL345_BW_RATE, 1, &bw_code);
  472. return bw_code;
  473. }
  474. //Used to check if action was triggered in interrupts
  475. //Example triggered(interrupts, ADXL345_SINGLE_TAP);
  476. bool ADXL345::triggered(byte interrupts, int mask) {
  477. return ((interrupts >> mask) & 1);
  478. }
  479. /*
  480. ADXL345_DATA_READY
  481. ADXL345_SINGLE_TAP
  482. ADXL345_DOUBLE_TAP
  483. ADXL345_ACTIVITY
  484. ADXL345_INACTIVITY
  485. ADXL345_FREE_FALL
  486. ADXL345_WATERMARK
  487. ADXL345_OVERRUNY
  488. */
  489. byte ADXL345::getInterruptSource() {
  490. byte _b;
  491. readFrom(ADXL345_INT_SOURCE, 1, &_b);
  492. return _b;
  493. }
  494. bool ADXL345::getInterruptSource(byte interruptBit) {
  495. return getRegisterBit(ADXL345_INT_SOURCE, interruptBit);
  496. }
  497. bool ADXL345::getInterruptMapping(byte interruptBit) {
  498. return getRegisterBit(ADXL345_INT_MAP, interruptBit);
  499. }
  500. // Set the mapping of an interrupt to pin1 or pin2
  501. // eg: setInterruptMapping(ADXL345_INT_DOUBLE_TAP_BIT,ADXL345_INT2_PIN);
  502. void ADXL345::setInterruptMapping(byte interruptBit, bool interruptPin) {
  503. setRegisterBit(ADXL345_INT_MAP, interruptBit, interruptPin);
  504. }
  505. bool ADXL345::isInterruptEnabled(byte interruptBit) {
  506. return getRegisterBit(ADXL345_INT_ENABLE, interruptBit);
  507. }
  508. void ADXL345::setInterrupt(byte interruptBit, bool state) {
  509. setRegisterBit(ADXL345_INT_ENABLE, interruptBit, state);
  510. }
  511. void ADXL345::setRegisterBit(byte regAdress, int bitPos, bool state) {
  512. byte _b;
  513. readFrom(regAdress, 1, &_b);
  514. if (state) {
  515. _b |= (1 << bitPos); // forces nth bit of _b to be 1. all other bits left alone.
  516. } else {
  517. _b &= ~(1 << bitPos); // forces nth bit of _b to be 0. all other bits left alone.
  518. }
  519. writeTo(regAdress, _b);
  520. }
  521. bool ADXL345::getRegisterBit(byte regAdress, int bitPos) {
  522. byte _b;
  523. readFrom(regAdress, 1, &_b);
  524. return ((_b >> bitPos) & 1);
  525. }
  526. // print all register value to the serial ouptut, which requires it to be setup
  527. // this can be used to manually to check the current configuration of the device
  528. void ADXL345::printAllRegister() {
  529. byte _b;
  530. Serial.print("0x00: ");
  531. readFrom(0x00, 1, &_b);
  532. print_byte(_b);
  533. Serial.println("");
  534. int i;
  535. for (i = 29; i <= 57; i++) {
  536. Serial.print("0x");
  537. Serial.print(i, HEX);
  538. Serial.print(": ");
  539. readFrom(i, 1, &_b);
  540. print_byte(_b);
  541. Serial.println("");
  542. }
  543. }
  544. // set the operation mode
  545. void ADXL345::setMode(byte operationMode) {
  546. byte _b;
  547. readFrom(ADXL345_FIFO_CTL, 1, &_b);
  548. _b &= ~(0b11000000); //clearing bit 6 and 7
  549. _b |= (operationMode << 6); //setting op mode
  550. //setRegisterBit(ADXL345_FIFO_CTL, 6, operationMode);
  551. writeTo(ADXL345_FIFO_CTL, _b);
  552. }
  553. // readback mode
  554. byte ADXL345::getMode(void) {
  555. byte _b;
  556. readFrom(ADXL345_FIFO_CTL, 1, &_b);
  557. _b &= 0b11000000; //masking bit 6 and 7
  558. _b = (_b >> 6); //setting op mode
  559. return _b;
  560. }
  561. // set watermark
  562. void ADXL345::setWatermark(byte watermark) {
  563. byte _b, _w;
  564. readFrom(ADXL345_FIFO_CTL, 1, &_b);
  565. _b &= (0b11100000); //clearing bit 0 to 4
  566. _w = watermark & (0b00011111); //clearing highest 3 bits in waterlevel
  567. _b |= _w; //setting waterlevel in operationmode register
  568. //setRegisterBit(ADXL345_FIFO_CTL, 6, operationMode);
  569. writeTo(ADXL345_FIFO_CTL, _b);
  570. }
  571. // read how many samples in Fifi
  572. byte ADXL345::getFifoEntries(void) {
  573. byte _b;
  574. readFrom(ADXL345_FIFO_STATUS, 1, &_b);
  575. _b &= 0b00111111;
  576. return _b;
  577. }
  578. void ADXL345::burstReadXYZ(int* x, int* y, int* z, byte samples) {
  579. for (int i = 0; i < samples; i++) {
  580. readFrom(ADXL345_DATAX0, ADXL345_TO_READ, _buff); //read the acceleration data from the ADXL345
  581. x[i] = (short)((((unsigned short)_buff[1]) << 8) | _buff[0]);
  582. y[i] = (short)((((unsigned short)_buff[3]) << 8) | _buff[2]);
  583. z[i] = (short)((((unsigned short)_buff[5]) << 8) | _buff[4]);
  584. }
  585. }
  586. void print_byte(byte val) {
  587. int i;
  588. Serial.print("B");
  589. for (i = 7; i >= 0; i--) {
  590. Serial.print(val >> i & 1, BIN);
  591. }
  592. }