BrickUp API Service for Docker version.

ADXL345.cpp 22KB

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