This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspACC.cpp
919 lines (775 loc) · 23.7 KB
/
WaspACC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.0
* Design: David Gascón
* Implementation: David Cuartielles, Alberto Bielsa, Marcos Yarza
*/
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
// Variables ///////////////////////////////////////////////////////////////////
// Constructors ////////////////////////////////////////////////////////////////
WaspACC::WaspACC()
{
// no interruption set at the beginning
accInt=NO_INT;
// init attribute
isON = 0;
// init default Full Scale mode to +/- 2G
fsSelection = FS_2G;
// init threshold level
_int1_ths = INT1_THS_val;
_threshold = 448; // 448 mg as default threshold
}
// Public Methods //////////////////////////////////////////////////////////////
/******************************************************************************
* QUICK BOOTING AND CONFIGURATION OF THE SENSOR
******************************************************************************/
/*
* ON (void) - It opens I2C bus and powers the accelerometer
*
* It opens I2C bus and powers the accelerometer
*/
void WaspACC::ON(void)
{
ON(fsSelection);
}
/*
* ON (void) - It opens I2C bus and powers the accelerometer
*
* It opens I2C bus and powers the accelerometer
*/
void WaspACC::ON(uint8_t FS_OPTION)
{
// join i2c bus (address optional for master)
if (!Wire.isON)
{
Wire.begin();
}
accMode = ACC_ON;
setMode(ACC_ON);
// store Full-scale selection
fsSelection = FS_OPTION;
switch (FS_OPTION)
{
case FS_2G:
{
writeRegister(CTRL_REG4, B00000000);
break;
}
case FS_4G:
{
writeRegister(CTRL_REG4, B00010000);
break;
}
case FS_8G:
{
writeRegister(CTRL_REG4, B00110000);
break;
}
}
// set the interruption line down
pinMode(MUX_RX, OUTPUT);
digitalWrite(MUX_RX, LOW);
}
/*
* boot (void) - reboot the accelerometer
*
* takes for granted the accelerometer is on and forces the sensor to reboot
* by writing to the CTRL_REG2 into the right pin it makes no tests and checks
* nothing from the communication, therefore returns nothing and modifies no flags
*/
void WaspACC::boot(void)
{
// send reboot command
writeRegister(CTRL_REG2, B10000000);
// make a short pause after rebooting
delay(1);
// set CTRL_REG2 to zero: no reboot, no filter
writeRegister(CTRL_REG2, B00000000);
}
/*
* OFF (void) - closes I2C bus
*
* It closes I2C bus. Accelerometer uses I2C to communicate with microcontroller
* Set the accelerometer into POWER DOWN mode
* It is used to keep the battery to the maximum
*/
void WaspACC::OFF(void)
{
setMode(ACC_POWER_DOWN);
// close I2C
if (Wire.isON && RTC.isON!=1)
{
PWR.closeI2C();
RTC.setMode(RTC_OFF, RTC_I2C_MODE);
}
}
/*******************************************************************************
* CHECK ACCELEROMETER'S STATUS
******************************************************************************/
/*
* getStatus (void) - gets the accelerometer status
*
* returns a byte containing the status of the accelerometer as read from
* the proper register. It won't activate any flags by itself, but activates
* ACC_COMM_ERROR in case there was an error writing to the register
*/
uint8_t WaspACC::getStatus(void)
{
return (uint8_t) readRegister(STATUS_REG);
}
/*
* check (void) - checks if there is an accelerometer on board
*
* sends the accelerometer dummy register on 0x0F that should always answer 0x32
*
* This function can be used to determine if the accelerometer is on the board
* but also to check if it is still working properly. Any answer other than
* 0x32 means there is an error
*
* since it calls readRegister, check won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::check(void)
{
return (uint8_t) readRegister(WHO_AM_I);
}
/*******************************************************************************
* HANDLE THE CONFIGURATION REGISTERS, BOTH READ AND WRITE
*******************************************************************************/
/*
* getCTRL1 (void) - checks accelerometer's control register 1: CTRL_REG1
*
* returns the contents of CTRL_REG1. Since it calls readRegister, getCTRL1
* won't activate any flags by itself, but the other functions will activate
* ACC_COMM_ERROR in case there was an error communicating to the register
*/
uint8_t WaspACC::getCTRL1(void)
{
return (uint8_t) readRegister(CTRL_REG1);
}
/*
* setCTRL1 (void) - sets accelerometer's control register 1: CTRL_REG1
*
* sets the contents of CTRL_REG1, returns 1 if error
* since it calls writeRegister, setCTRL1 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setCTRL1(uint8_t val)
{
return (uint8_t) writeRegister(CTRL_REG1, val);
}
/*
* getCTRL2 (void) - checks accelerometer's control register 2: CTRL_REG2
*
* returns the contents of control register 2
* since it calls readRegister, getCTRL2 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::getCTRL2(void)
{
return (uint8_t) readRegister(CTRL_REG2);
}
/*
* setCTRL2 (void) - sets accelerometer's control register 2: CTRL_REG2
*
* sets the contents of control register 2, returns 1 if error
* since it calls writeRegister, setCTRL2 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setCTRL2(uint8_t val)
{
return (uint8_t) writeRegister(CTRL_REG2, val);
}
/*
* getCTRL3 (void) - checks accelerometer's control register 3: CTRL_REG3
*
* returns the contents of control register 3
* since it calls readRegister, getCTRL3 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::getCTRL3(void)
{
return (uint8_t) readRegister(CTRL_REG3);
}
/*
* setCTRL3 (void) - sets accelerometer's control register 3: CTRL_REG3
*
* sets the contents of control register 3, returns 1 if error
* since it calls writeRegister, setCTRL3 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setCTRL3(uint8_t val)
{
return (uint8_t) writeRegister(CTRL_REG3, val);
}
/*
* getCTRL4 (void) - checks accelerometer's control register 4: CTRL_REG4
*
* returns the contents of control register 4
* since it calls readRegister, getCTRL4 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::getCTRL4(void)
{
return (uint8_t) readRegister(CTRL_REG4);
}
/*
* setCTRL4 (void) - sets accelerometer's control register 4: CTRL_REG4
*
* sets the contents of control register 4, returns 1 if error
* since it calls writeRegister, setCTRL4 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setCTRL4(uint8_t val)
{
return (uint8_t) writeRegister(CTRL_REG4, val);
}
/*
* getCTRL5 (void) - checks accelerometer's control register 5: CTRL_REG5
*
* returns the contents of control register 5
* since it calls readRegister, getCTRL5 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::getCTRL5(void)
{
return (uint8_t) readRegister(CTRL_REG5);
}
/*
* setCTRL4 (void) - sets accelerometer's control register 5: CTRL_REG5
*
* sets the contents of control register 5, returns 1 if error
* since it calls writeRegister, setCTRL5 won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setCTRL5(uint8_t val)
{
return (uint8_t) writeRegister(CTRL_REG5, val);
}
/*
* setSamplingRate (rate) - sets the accelerometer's sampling rate
*
* writes, inside the CTRL1 the sampling rate desired
* since it calls writeRegister, setADCmode won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setSamplingRate(uint8_t rate)
{
// get actual CTRL_REG1
uint8_t aux = readRegister(CTRL_REG1);
aux &= B11100111;
// Depending on the rate selected change CTRL_REG1
switch( rate )
{
case ACC_RATE_50: aux |= rate;
break;
case ACC_RATE_100: aux |= B00001000;
break;
case ACC_RATE_400: aux |= B00010000;
break;
case ACC_RATE_1000: aux |= B00011000;
break;
}
return writeRegister(CTRL_REG1, aux);
}
/*******************************************************************************
* INTERRUPTS REGISTERS
*******************************************************************************/
/*
* getINT1CFG (void)
*
* returns the contents of interrupt 1 control reg
*/
uint8_t WaspACC::getINT1CFG(void)
{
return (uint8_t) readRegister(INT1_CFG);
}
/*
* getINT1SRC (void)
*
* returns the contents of interrupt 1 source reg
*/
uint8_t WaspACC::getINT1SRC(void)
{
return (uint8_t) readRegister(INT1_SRC);
}
/*
* getINT1THS (void)
*
* returns the contents of interrupt 1 threshold reg
*/
uint8_t WaspACC::getINT1THS(void)
{
return (uint8_t) readRegister(INT1_THS);
}
/*
* getINT1DURATION (void)
*
* returns the contents of interrupt 1 duration reg
*/
uint8_t WaspACC::getINT1DURATION(void)
{
return (uint8_t) readRegister(INT1_DURATION);
}
/*******************************************************************************
* GET ACCELERATION ON THE THREE AXIS
******************************************************************************/
/*
* getX (void) - checks accelerometer's acceleration on OX
*
* returns the combined contents of data registers OUT_X_H and OUT_X_L as an
* integer according to ADC's configuration
* A conversion is made depending on the full-scale selection (fsSelection)
*
* since it calls readRegister, getX won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
int16_t WaspACC::getX(void)
{
int32_t aux = ((int8_t)readRegister(OUT_X_H)*256) +
(uint8_t)readRegister(OUT_X_L);
return fsSelection * 1000 * aux/32768;
}
/*
* getY (void) - checks accelerometer's acceleration on OY
*
* returns the combined contents of data registers OUT_Y_H and OUT_Y_L as an
* integer according to ADC's configuration
* A conversion is made depending on the full-scale selection (fsSelection)
*
* since it calls readRegister, getY won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
int16_t WaspACC::getY(void)
{
int32_t aux = ((int8_t)readRegister(OUT_Y_H)*256) +
(uint8_t)readRegister(OUT_Y_L);
return fsSelection * 1000 * aux/32768;
}
/*
* getZ (void) - checks accelerometer's acceleration on OZ
*
* returns the combined contents of data registers OUT_Z_H and OUT_Z_L as an
* integer according to ADC's configuration
* A conversion is made depending on the full-scale selection (fsSelection)
*
* since it calls readRegister, getZ won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
int16_t WaspACC::getZ(void)
{
int32_t aux = ((int8_t)readRegister(OUT_Z_H)*256) +
(uint8_t)readRegister(OUT_Z_L);
return fsSelection * 1000 * aux/32768;
}
/*******************************************************************************
* HANDLE ACCELEROMETER'S WORK MODES *
******************************************************************************/
/*
* getMode (void) - checks accelerometer's work mode
*
* returns the value for the accelerometer's work mode, the possibilites are:
* - ACC_ON: normal mode
* - ACC_POWER_DOWN: turn ACC off
* - ACC_LOW_POWER_1
* - ACC_LOW_POWER_2
* - ACC_LOW_POWER_3
* - ACC_LOW_POWER_4
* - ACC_LOW_POWER_5
*
* It doesn't call any other functions and therefore it won't activate any flags
*/
uint8_t WaspACC::getMode()
{
return accMode;
}
/*
* setMode (uint8_t) - sets accelerometer's work mode
*
* configures the accelerometer to a new work mode, the possibilites are:
* - ACC_HIBERNATE: go on the lowest power mode possible. This mode has no
* interrupt calls and therefore won't disturb the processor if anything
* happened
* - ACC_ON: read/send at a constant pace upon request
* - ACC_CUSTOM: user defined work mode, should be programmed in here
*
* since it calls writeRegister, setAccMode won't activate any flags by itself,
* but the other functions will activate ACC_COMM_ERROR in case there
* was an error communicating to the register
*/
uint8_t WaspACC::setMode(uint8_t mode)
{
uint8_t result = 0;
accMode = mode;
switch (mode)
{
case ACC_ON:
// turn power ON
// Normal mode is selected (PM2=0; PM1=0; PM0=1)
// ODR is set to 50 Hz (DR1=0; DR0=0)
// X-Y-Z axis are enabled
result = writeRegister(CTRL_REG1, B00100111);
isON = 1;
// Ton (Turn-on time)= 1/ODR+1ms
delay(21);
break;
case ACC_POWER_DOWN:
// turn power off
result = writeRegister(CTRL_REG1,B00000000);
isON = 0;
break;
case ACC_LOW_POWER_1:
// turn lo power mode. ODR=0.5 Hz
result = writeRegister(CTRL_REG1, B01000111);
isON = 1;
break;
case ACC_LOW_POWER_2:
// turn lo power mode. ODR=1 Hz
result = writeRegister(CTRL_REG1, B01100111);
isON = 1;
break;
case ACC_LOW_POWER_3:
// turn lo power mode. ODR=2 Hz
result = writeRegister(CTRL_REG1, B10000111);
isON = 1;
break;
case ACC_LOW_POWER_4:
// turn lo power mode. ODR=5 Hz
result = writeRegister(CTRL_REG1, B10100111);
isON = 1;
break;
case ACC_LOW_POWER_5:
// turn lo power mode. ODR=10 Hz
result = writeRegister(CTRL_REG1, B11000111);
isON = 1;
break;
default:
break;
}
return result;
}
/*
* setSleepToWake (void) - sets accelerometer's Sleep to Wake mode
*
* The sleep to wake function, in conjunction with Low Power mode, allows
* further reduction of system power consumption
* When the sleep to wake function is activated, the LIS331DLH is able to
* automatically wake up as soon as the interrupt event has been detected,
* increasing the output data rate and bandwidth. With this feature the system
* can be efficiently switched from Low Power mode to full performance,
* depending on user-selectable
*
*/
uint8_t WaspACC::setSleepToWake(void)
{
return writeRegister(CTRL_REG5, B00000011);
}
/*
* unSetSleepToWake (void) - unsets accelerometer's Sleep to Wake mode
*
* The sleep to wake function, in conjunction with Low Power mode, allows
* further reduction of system power consumption
* When the sleep to wake function is activated, the LIS331DLH is able to
* automatically wake up as soon as the interrupt event has been detected,
* increasing the output data rate and bandwidth. With this feature the system
* can be efficiently switched from Low Power mode to full performance,
* depending on user-selectable
*
*/
uint8_t WaspACC::unSetSleepToWake(void)
{
return writeRegister(CTRL_REG5, B00000000);
}
/*******************************************************************************
* SPECIFIC WASP FUNCTIONS TO HANDLE EVENTS
******************************************************************************/
/*
* setFF (void) - sets the Free Fall interrupt
*/
uint8_t WaspACC::setFF(void)
{
// set Free-Fall interruption with the defined threshold margin
return setFF( _threshold );
}
/*
* setFF (void) - sets the Free Fall interrupt
*/
uint8_t WaspACC::setFF( uint16_t ths )
{
// reboot ACC in order to clean the ACC Interruption
// pin in the it has been locked to '1'
boot();
delay(100);
// clear the FF interrupt if active
unsetFF();
// configure the different registers to
// handle acceleration detection on the X, Y, or Z axis
// set latch interrupt request: when an interrupt condition is applied,
// the interrupt signal remains high even if the condition returns to a
// non-interrupt status, until a reading of the INTx_SRC register is performed.
writeRegister(CTRL_REG2,0x00);
writeRegister(CTRL_REG3,0x04);
// set Free-Fall threshold
uint8_t threshold = calculateThreshold(ths);
writeRegister(INT1_THS, threshold);
// set minimum event duration
writeRegister(INT1_DURATION, INT1_DURATION_val);
// set interrupt configuration
writeRegister(INT1_CFG, INT1_CFG_val);
// Free-Fall interruption set
accInt=FF_INT;
// attach the hardware interrupt to the pin
enableInterrupts(ACC_INT);
return flag;
}
/*
* unsetFF (void) - clears and unsets the Free Fall interrupt
*/
uint8_t WaspACC::unsetFF(void)
{
// set CTRL_REG3 to zero
writeRegister(CTRL_REG3, 0x00);
// read INT1_SRC register in order to clear the interrupt flag
readRegister(INT1_SRC);
// detach the event
detachInt();
// no interruption set
accInt=NO_INT;
return flag;
}
/*
* setIWU (void) - sets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::setIWU(void)
{
// set Inertial-Wake-Up interruption with a defined threshold margin
return setIWU( _threshold );
}
/*
* setIWU (void) - sets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::setIWU( uint16_t ths )
{
// clear the FF interrupt if active
unsetIWU();
// configure the different registers to
// handle acceleration detection on the X, Y, or Z axis
writeRegister(CTRL_REG3,0x00);
// setup register for IWU
uint8_t threshold = calculateThreshold(ths);
writeRegister(INT1_THS, threshold); // set threshold value
writeRegister(INT1_DURATION,0x00); // set duration value
writeRegister(CTRL_REG2, 0x04); // enable highpass filter
writeRegister(INT1_CFG,0x2A); // X-Y-Z enabled
// attach the hardware interrupt to the pin
enableInterrupts(ACC_INT);
// IWU interruption set
accInt=IWU_INT;
return flag;
}
/*
* unsetIWU (void) - clears and unsets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::unsetIWU(void)
{
// set CTRL_REG3 to zero
writeRegister(CTRL_REG3, 0x00);
// read INT1_SRC register in order to clear the interrupt flag
readRegister(INT1_SRC);
// detach the event
detachInt();
// no interruption set
accInt=NO_INT;
return flag;
}
/*
* set6DMovement (void) - sets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::set6DMovement(void)
{
// clear the FF interrupt if active
unset6DMovement();
// configure the different registers to
// handle acceleration detection on the X, Y, or Z axis
writeRegister(CTRL_REG3,0x04);
writeRegister(INT1_THS,0x30); // threshold
writeRegister(INT1_DURATION,0x00); // event duration
writeRegister(INT1_CFG,0x7F); // 6D movement recognition in 3-axes
// attach the hardware interrupt to the pin
enableInterrupts(ACC_INT);
// 6D Movement interruption set
accInt=_6DMOV_INT;
return flag;
}
/*
* unset6DPosition (void) - clears and unsets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::unset6DMovement(void)
{
// set CTRL_REG3 to zero
writeRegister(CTRL_REG3, 0x00);
// read INT1_SRC register in order to clear the interrupt flag
readRegister(INT1_SRC);
// detach the event
detachInt();
// no interruption set
accInt=NO_INT;
return flag;
}
/*
* set6DPosition (void) - sets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::set6DPosition(void)
{
// clear the FF interrupt if active
unset6DPosition();
// configure the different registers to
// handle acceleration detection on the X, Y, or Z axis
writeRegister(CTRL_REG3,0x04);
writeRegister(INT1_THS,0x21); // threshold
writeRegister(INT1_DURATION,0x00); // event duration
writeRegister(INT1_CFG,0xFF); // 6D position recognition in 3-axes
// attach the hardware interrupt to the pin
enableInterrupts(ACC_INT);
// 6D Position interruption set
accInt=_6DPOS_INT;
return flag;
}
/*
* unset6DPosition (void) - clears and unsets the Inertial Wake-UP interrupt
*/
uint8_t WaspACC::unset6DPosition(void)
{
// set CTRL_REG3 to zero
writeRegister(CTRL_REG3, 0x00);
// read INT1_SRC register in order to clear the interrupt flag
readRegister(INT1_SRC);
// detach the event
detachInt();
// no interruption set
accInt=NO_INT;
return flag;
}
/*
* calculateThreshold () - sets the threshold 'ths' specified in mg units
* Depending on the full scale mode, different conversions are needed
*/
uint8_t WaspACC::calculateThreshold( uint16_t ths )
{
uint16_t aux = ths;
switch( fsSelection )
{
case FS_2G:
{
aux = aux/16; // 16 mg for each LSB
break;
}
case FS_4G:
{
aux = aux/31; // 31 mg for each LSB
break;
}
case FS_8G:
{
aux = aux/63; // 63 mg for each LSB
break;
}
default: return _int1_ths;
}
_int1_ths = (uint8_t) (aux&0x00FF);
_threshold = ths;
return _int1_ths;
}
/*******************************************************************************
* HANDLING HARDWARE INTERRUPTS
*******************************************************************************/
/*
* attachInt(void) - configure the specific hardware interrupt for the acc
*
* the default interrupt functions are defined inside WInterrupts.c
*/
void WaspACC::attachInt(void)
{
enableInterrupts(ACC_INT);
}
/*
* detachInt(void) - unset the specific hardware interrupt for the acc
*/
void WaspACC::detachInt(void)
{
disableInterrupts(ACC_INT);
}
/*******************************************************************************
* LOW LEVEL READ AND WRITE REGISTERS
*******************************************************************************/
// reads a register from the accelerometer
// returns its value or -1 if error
int16_t WaspACC::readRegister(uint8_t regNum)
{
// reset the flag
flag &= ~(ACC_ERROR_READING);
uint8_t aux = 0;
Wire.beginTransmission(I2C_ADDRESS_WASP_ACC);
Wire.send(regNum);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS_WASP_ACC, 1);
if(Wire.available())
{
aux = Wire.receive();
return aux;
}
// error, activate the reading flag
flag |= ACC_ERROR_READING;
return -1;
}
// writes a byte to a register in the accelerometer
// returns 1 on success; 0 if error
uint8_t WaspACC::writeRegister(uint8_t address, uint8_t val)
{
// reset the flag
flag &= ~(ACC_ERROR_WRITING);
Wire.beginTransmission(I2C_ADDRESS_WASP_ACC);
Wire.send(address);
Wire.send(val);
Wire.endTransmission();
if (readRegister(address) != val)
{
// error, activate the reading flag
flag |= ACC_ERROR_WRITING;
return 0;
}
// exit with no error
return 1;
}
// Private Methods /////////////////////////////////////////////////////////////
// Preinstantiate Objects //////////////////////////////////////////////////////
WaspACC ACC = WaspACC();