-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathADXL343.cs
1905 lines (1647 loc) · 81.1 KB
/
ADXL343.cs
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
using System;
using System.Buffers.Binary;
using System.Device.I2c;
using System.Numerics;
using System.Threading;
using Iot.Device.Adxl343Lib;
using UnitsNet;
namespace Iot.Device.Adxl343Lib
{
/// <summary>
/// Library for the ADXL343 sensor using I2C.
/// </summary>
public class Adxl343
{
private const int Resolution = 1024;
private int _range = 4;
private GravityRange _gravityRangeByte = 0;
private I2cDevice _i2c;
/// <summary>
/// Initializes a new instance of the <see cref="Adxl343" /> class.
/// </summary>
/// <param name="i2cDevice">I2C Device.</param>
/// <param name="gravityRange">Gravity Range.</param>
public Adxl343(I2cDevice i2cDevice, GravityRange gravityRange)
{
_range = ConvertGravityRangeToInt(gravityRange);
_gravityRangeByte = gravityRange;
_i2c = i2cDevice;
Initialize();
}
private static int ConvertGravityRangeToInt(GravityRange gravityRange)
{
switch (gravityRange)
{
case GravityRange.Range04:
return 1;
case GravityRange.Range08:
return 2;
case GravityRange.Range16:
return 3;
case GravityRange.Range02:
default:
return 0;
}
}
private void Initialize()
{
TrySetDataFormat(false, false, false, true, false, _gravityRangeByte);
TrySetPowerControl(false, false, true, false, 0);
}
#region Device Id
/// <summary>
/// The DEVID register holds a fixed device ID code of 0xE5.
/// </summary>
/// <param name="deviceId">Device id returned by ADXL 343.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetDeviceId(ref int deviceId)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.DevId);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
deviceId = readBuf[0];
return true;
}
return false;
}
#endregion
#region Thresh Tap
/// <summary>
/// The THRESH_TAP register is eight bits and holds the threshold
/// value for tap interrupts.The data format is unsigned, therefore,
/// the magnitude of the tap event is compared with the value
/// in THRESH_TAP for normal tap detection. The scale factor is
/// 62.5 mg/LSB (that is, 0xFF = 16 g). A value of 0 may result in
/// undesirable behavior if single tap/double tap interrupts are enabled.
/// </summary>
/// <param name="threshTap">Referenced scale factor value 0 to 255.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetThresholdTap(ref Mass threshTap)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.ThreshTap);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
double tt = readBuf[0];
tt *= 62.5;
threshTap = Mass.FromMilligrams(tt * 62.5);
return true;
}
return false;
}
/// <summary>
/// The THRESH_TAP register is eight bits and holds the threshold
/// value for tap interrupts.The data format is unsigned, therefore,
/// the magnitude of the tap event is compared with the value
/// in THRESH_TAP for normal tap detection. The scale factor is
/// 62.5 mg/LSB (that is, 0xFF = 16 g). A value of 0 may result in
/// undesirable behavior if single tap/double tap interrupts are enabled.
/// </summary>
/// <param name="threshTap">Scale factor value 0 to 255.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">ThreshTap needs to be between 0mg and 15,937.</exception>
public bool TrySetThresholdTap(Mass threshTap)
{
int tt = (int)(threshTap.Milligrams / 62.5);
if (tt < 0 || tt > 255)
{
throw new ArgumentOutOfRangeException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.ThreshTap;
writeBuf[1] = (byte)tt;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Offset Adjustments
/// <summary>
/// The OFSX, OFSY, and OFSZ registers are each eight bits and offer
/// user-set offset adjustments in twos complement format with a scale
/// factor of 15.6 mg/LSB(that is, 0x7F = 2 g). The value stored in
/// the offset registers is automatically added to the acceleration data,
/// and the resulting value is stored in the output data registers.
/// undesirable behavior if single tap/double tap interrupts are enabled.
/// </summary>
/// <param name="point">Referenced point containing offset values.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetOffsetAdjustments(ref Vector3 point)
{
SpanByte readBuf = new byte[3];
var res = _i2c.WriteByte((byte)Register.OfsX);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
point.X = readBuf[0];
point.Y = readBuf[1];
point.Z = readBuf[2];
return true;
}
return false;
}
/// <summary>
/// The OFSX, OFSY, and OFSZ registers are each eight bits and offer
/// user-set offset adjustments in twos complement format with a scale
/// factor of 15.6 mg/LSB(that is, 0x7F = 2 g). The value stored in
/// the offset registers is automatically added to the acceleration data,
/// and the resulting value is stored in the output data registers.
/// undesirable behavior if single tap/double tap interrupts are enabled.
/// </summary>
/// <param name="point">Point containing X, Y, and Z offset values.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">Point.X needs to be between 0 and 255.</exception>
/// <exception cref="ArgumentException">Point.Y needs to be between 0 and 255.</exception>
/// <exception cref="ArgumentException">Point.Z needs to be between 0 and 255.</exception>
public bool TrySetOffsetAdjustments(Vector3 point)
{
if (point.X < 0 || point.X > 255)
{
throw new ArgumentOutOfRangeException();
}
if (point.Y < 0 || point.Y > 255)
{
throw new ArgumentOutOfRangeException();
}
if (point.Z < 0 || point.Z > 255)
{
throw new ArgumentOutOfRangeException();
}
SpanByte writeBuf = new byte[4];
writeBuf[0] = (byte)Register.OfsX;
writeBuf[1] = (byte)point.X;
writeBuf[2] = (byte)point.Y;
writeBuf[3] = (byte)point.Z;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region DUR
/// <summary>
/// The DUR register is eight bits and contains an unsigned time value
/// representing the maximum time that an event must be above the
/// THRESH_TAP threshold to qualify as a tap event. The scale factor
/// is 625 µs/LSB. A value of 0 disables the single tap/ double tap
/// functions.
/// </summary>
/// <param name="duration">Reference to maximum duration above thresh tap value.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetDuration(ref Duration duration)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.Dur);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
duration = Duration.FromMicroseconds(readBuf[0]);
return true;
}
return false;
}
/// <summary>
/// The DUR register is eight bits and contains an unsigned time value
/// representing the maximum time that an event must be above the
/// THRESH_TAP threshold to qualify as a tap event. The scale factor
/// is 625 µs/LSB. A value of 0 disables the single tap/ double tap
/// functions.
/// </summary>
/// <param name="duration">Maximum duration above thresh tap value.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">Duration must be between 0ms and 159.</exception>
public bool TrySetDuration(Duration duration)
{
double d = Duration.FromMilliseconds(duration.Milliseconds).Microseconds / 625;
if (d > 255 || d < 0)
{
throw new ArgumentOutOfRangeException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.Dur;
writeBuf[1] = (byte)d;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Latent
/// <summary>
/// The latent register is eight bits and contains an unsigned time value
/// representing the wait time from the detection of a tap event to the
/// start of the time window(defined by the window register) during
/// which a possible second tap event can be detected.The scale
/// factor is 1.25 ms/LSB.A value of 0 disables the double tap function.
/// </summary>
/// <param name="latent">Reference to Wait time between a tap even and a double-tap.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetLatent(ref TimeSpan latent)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.Latent);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
latent = TimeSpan.FromMilliseconds((long)(readBuf[0] * 1.25));
return true;
}
return false;
}
/// <summary>
/// The latent register is eight bits and contains an unsigned time value
/// representing the wait time from the detection of a tap event to the
/// start of the time window(defined by the window register) during
/// which a possible second tap event can be detected.The scale
/// factor is 1.25 ms/LSB.A value of 0 disables the double tap function.
/// </summary>
/// <param name="latent">Wait time between a tap even and a double-tap.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">Latent must be between 0ms and 318ms.</exception>
public bool TrySetLatent(TimeSpan latent)
{
double l = latent.TotalMilliseconds / 1.25;
if (l < 0 || l > 255)
{
throw new ArgumentOutOfRangeException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.Latent;
writeBuf[1] = (byte)l;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Window
/// <summary>
/// The window register is eight bits and contains an unsigned time
/// value representing the amount of time after the expiration of the
/// latency time(determined by the latent register) during which a
/// second valid tap can begin.The scale factor is 1.25 ms/LSB.A
/// value of 0 disables the double tap function.
/// </summary>
/// <param name="window">Reference to amount of time after expiration of latency time.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetWindow(ref TimeSpan window)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.Window);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
window = TimeSpan.FromMilliseconds((long)(readBuf[0] * 1.25));
return true;
}
return false;
}
/// <summary>
/// The window register is eight bits and contains an unsigned time
/// value representing the amount of time after the expiration of the
/// latency time(determined by the latent register) during which a
/// second valid tap can begin.The scale factor is 1.25 ms/LSB.A
/// value of 0 disables the double tap function.
/// </summary>
/// <param name="window">Amount of time after expiration of latency time.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">Window must be between 0ms and 318ms.</exception>
public bool TrySetWindow(TimeSpan window)
{
double w = window.Milliseconds / 1.25;
if (w < 0 || w > 255)
{
throw new ArgumentOutOfRangeException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.Window;
writeBuf[1] = (byte)w;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Threshold Activity
/// <summary>
/// The THRESH_ACT register is eight bits and holds the threshold
/// value for detecting activity.The data format is unsigned, therefore,
/// the magnitude of the activity event is compared with the value in the
/// THRESH_ACT register. The scale factor is 62.5 mg/LSB. A value
/// of 0 may result in undesirable behavior if the activity interrupt is
/// enabled.
/// </summary>
/// <param name="thresholdDetect">Reference to threshold value for detecting activity.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetThresholdActivity(ref Mass thresholdDetect)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.ThreshAct);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
thresholdDetect = Mass.FromMilligrams(readBuf[0] * 62.5);
return true;
}
return false;
}
/// <summary>
/// The THRESH_ACT register is eight bits and holds the threshold
/// value for detecting activity.The data format is unsigned, therefore,
/// the magnitude of the activity event is compared with the value in the
/// THRESH_ACT register. The scale factor is 62.5 mg/LSB. A value
/// of 0 may result in undesirable behavior if the activity interrupt is
/// enabled.
/// </summary>
/// <param name="thresholdDetect">Threshold value for detecting activity.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">ThresholdDetect must be between 0mg and 15,937mg.</exception>
public bool TrySetThresholdActivity(Mass thresholdDetect)
{
double td = thresholdDetect.Milligrams / 62.5;
if (td < 0 || td > 255)
{
throw new ArgumentException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.ThreshAct;
writeBuf[1] = (byte)td;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Threshold Inactivity
/// <summary>
/// The THRESH_INACT register is eight bits and holds the threshold
/// value for detecting inactivity.The data format is unsigned, therefore,
/// the magnitude of the inactivity event is compared with the value
/// in the THRESH_INACT register. The scale factor is 62.5 mg/LSB.
/// A value of 0 may result in undesirable behavior if the inactivity
/// interrupt is enabled.
/// </summary>
/// <param name="thresholdDetect">Reference to threshold value for detecting inactivity.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetThresholdInactivity(ref Mass thresholdDetect)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.ThreshInact);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
thresholdDetect = Mass.FromMilligrams(readBuf[0] * 62.5);
return true;
}
return false;
}
/// <summary>
/// The THRESH_INACT register is eight bits and holds the threshold
/// value for detecting inactivity.The data format is unsigned, therefore,
/// the magnitude of the inactivity event is compared with the value
/// in the THRESH_INACT register. The scale factor is 62.5 mg/LSB.
/// A value of 0 may result in undesirable behavior if the inactivity
/// interrupt is enabled.
/// </summary>
/// <param name="thresholdDetect">Reference to threshold value for detecting inactivity.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">ThresholdDetect must be between 0mg and 15,937mg.</exception>
public bool TrySetThresholdInactivity(Mass thresholdDetect)
{
double td = thresholdDetect.Milligrams / 62.5;
if (td < 0 || td > 255)
{
throw new ArgumentException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.ThreshInact;
writeBuf[1] = (byte)td;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Time Inactivity
/// <summary>
/// The TIME_INACT register is eight bits and contains an unsigned
/// time value representing the amount of time that acceleration must
/// be less than the value in the THRESH_INACT register for inactivity
/// to be declared.The scale factor is 1 sec/LSB.Unlike the other
/// interrupt functions, which use unfiltered data,
/// the inactivity function uses filtered output data.At least
/// one output sample must be generated for the inactivity interrupt to
/// be triggered.This results in the function appearing unresponsive
/// if the TIME_INACT register is set to a value less than the time
/// constant of the output data rate. A value of 0 results in an interrupt
/// when the output data is less than the value in the THRESH_INACT
/// register.
/// </summary>
/// <param name="time">Reference to the amount of time that acceleration must be less than Threshhold Inactivity to register for inactivity.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetTimeInactivity(ref int time)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.TimeInact);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
time = readBuf[0];
return true;
}
return false;
}
/// <summary>
/// The TIME_INACT register is eight bits and contains an unsigned
/// time value representing the amount of time that acceleration must
/// be less than the value in the THRESH_INACT register for inactivity
/// to be declared.The scale factor is 1 sec/LSB.Unlike the other
/// interrupt functions, which use unfiltered data,
/// the inactivity function uses filtered output data.At least
/// one output sample must be generated for the inactivity interrupt to
/// be triggered.This results in the function appearing unresponsive
/// if the TIME_INACT register is set to a value less than the time
/// constant of the output data rate. A value of 0 results in an interrupt
/// when the output data is less than the value in the THRESH_INACT
/// register.
/// </summary>
/// <param name="time">Amount of time that acceleration must be less than Threshhold Inactivity to register for inactivity.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TrySetTimeInactactivity(byte time)
{
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.TimeInact;
writeBuf[1] = time;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Act/Inact Control
/// <summary>
/// Act and Inact of AC-DC Bits
/// A setting of 0 selects dc-coupled operation, and a setting of 1
/// enables ac-coupled operation.In dc-coupled operation, the current
/// acceleration magnitude is compared directly with THRESH_ACT
/// and THRESH_INACT to determine whether activity or inactivity is
/// detected.
/// In ac-coupled operation for activity detection, the acceleration value
/// at the start of activity detection is taken as a reference value.New
/// samples of acceleration are then compared to this reference value,
/// and if the magnitude of the difference exceeds the THRESH_ACT
/// value, the device triggers an activity interrupt.
/// Similarly, in ac-coupled operation for inactivity detection, a reference value is used for comparison and is updated whenever
/// the device exceeds the inactivity threshold.After the reference
/// value is selected, the device compares the magnitude of the difference between the reference value and the current acceleration
/// with THRESH_INACT.If the difference is less than the value in
/// THRESH_INACT for the time in TIME_INACT, the device is considered inactive and the inactivity interrupt is triggered.
/// Act and Inact of X, Y, and Z
/// A setting of 1 enables x-, y-, or z-axis participation in detecting
/// activity or inactivity.A setting of 0 excludes the selected axis from
/// participation.If all axes are excluded, the function is disabled.For
/// activity detection, all participating axes are logically OR’ed, causing
/// the activity function to trigger when any of the participating axes
/// exceeds the threshold.For inactivity detection, all participating axes
/// are logically AND’ed, causing the inactivity function to trigger only if
/// all participating axes are below the threshold for the specified time.
/// </summary>
/// <param name="actAcDc">Active AC/DC.</param>
/// <param name="actXEnable">Active X Enable.</param>
/// <param name="actYEnable">Active Y Enable.</param>
/// <param name="actZEnable">Active Z Enable.</param>
/// <param name="inactAcDc">Inactive AC/DC.</param>
/// <param name="inactXEnable">Inactive X Enable.</param>
/// <param name="inactYEnable">Inactive Y Enable.</param>
/// <param name="inactZEnable">Inactive Z Enable.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetActiveInactiveControl(ref bool actAcDc, ref bool actXEnable, ref bool actYEnable, ref bool actZEnable, ref bool inactAcDc, ref bool inactXEnable, ref bool inactYEnable, ref bool inactZEnable)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.ActInactCtl);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
byte map = readBuf[0];
actAcDc = (map & (byte)ActInactCtlMap.ActAcDc) == (byte)ActInactCtlMap.ActAcDc;
actXEnable = (map & (byte)ActInactCtlMap.ActXEnable) == (byte)ActInactCtlMap.ActXEnable;
actYEnable = (map & (byte)ActInactCtlMap.ActYEnable) == (byte)ActInactCtlMap.ActYEnable;
actZEnable = (map & (byte)ActInactCtlMap.ActZEnable) == (byte)ActInactCtlMap.ActZEnable;
inactAcDc = (map & (byte)ActInactCtlMap.InactAcDc) == (byte)ActInactCtlMap.InactAcDc;
inactXEnable = (map & (byte)ActInactCtlMap.InactXEnable) == (byte)ActInactCtlMap.InactXEnable;
inactYEnable = (map & (byte)ActInactCtlMap.InactYEnable) == (byte)ActInactCtlMap.InactYEnable;
inactZEnable = (map & (byte)ActInactCtlMap.InactZEnable) == (byte)ActInactCtlMap.InactZEnable;
return true;
}
return false;
}
/// <summary>
/// Act and Inact of AC-DC Bits
/// A setting of 0 selects dc-coupled operation, and a setting of 1
/// enables ac-coupled operation.In dc-coupled operation, the current
/// acceleration magnitude is compared directly with THRESH_ACT
/// and THRESH_INACT to determine whether activity or inactivity is
/// detected.
/// In ac-coupled operation for activity detection, the acceleration value
/// at the start of activity detection is taken as a reference value.New
/// samples of acceleration are then compared to this reference value,
/// and if the magnitude of the difference exceeds the THRESH_ACT
/// value, the device triggers an activity interrupt.
/// Similarly, in ac-coupled operation for inactivity detection, a reference value is used for comparison and is updated whenever
/// the device exceeds the inactivity threshold.After the reference
/// value is selected, the device compares the magnitude of the difference between the reference value and the current acceleration
/// with THRESH_INACT.If the difference is less than the value in
/// THRESH_INACT for the time in TIME_INACT, the device is considered inactive and the inactivity interrupt is triggered.
/// Act and Inact of X, Y, and Z
/// A setting of 1 enables x-, y-, or z-axis participation in detecting
/// activity or inactivity.A setting of 0 excludes the selected axis from
/// participation.If all axes are excluded, the function is disabled.For
/// activity detection, all participating axes are logically OR’ed, causing
/// the activity function to trigger when any of the participating axes
/// exceeds the threshold.For inactivity detection, all participating axes
/// are logically AND’ed, causing the inactivity function to trigger only if
/// all participating axes are below the threshold for the specified time.
/// </summary>
/// <param name="actAcDc">Active AC/DC.</param>
/// <param name="actXEnable">Active X Enable.</param>
/// <param name="actYEnable">Active Y Enable.</param>
/// <param name="actZEnable">Active Z Enable.</param>
/// <param name="inactAcDc">Inactive AC/DC.</param>
/// <param name="inactXEnable">Inactive X Enable.</param>
/// <param name="inactYEnable">Inactive Y Enable.</param>
/// <param name="inactZEnable">Inactive Z Enable.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TrySetActiveInactiveControl(bool actAcDc, bool actXEnable, bool actYEnable, bool actZEnable, bool inactAcDc, bool inactXEnable, bool inactYEnable, bool inactZEnable)
{
byte map = 0;
if (actAcDc)
{
map |= (byte)ActInactCtlMap.ActAcDc;
}
if (actXEnable)
{
map |= (byte)ActInactCtlMap.ActXEnable;
}
if (actYEnable)
{
map |= (byte)ActInactCtlMap.ActYEnable;
}
if (actZEnable)
{
map |= (byte)ActInactCtlMap.ActZEnable;
}
if (inactAcDc)
{
map |= (byte)ActInactCtlMap.InactAcDc;
}
if (inactXEnable)
{
map |= (byte)ActInactCtlMap.InactXEnable;
}
if (inactYEnable)
{
map |= (byte)ActInactCtlMap.InactYEnable;
}
if (inactZEnable)
{
map |= (byte)ActInactCtlMap.InactZEnable;
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.TapAxes;
writeBuf[1] = map;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Thresh FF
/// <summary>
/// The THRESH_FF register is eight bits and holds the threshold
/// value, in unsigned format, for free-fall detection.The acceleration
/// on all axes is compared with the value in THRESH_FF to determine
/// if a free-fall event occurred.The scale factor is 62.5 mg/LSB.Note
/// that a value of 0 mg may result in undesirable behavior if the
/// free-fall interrupt is enabled.Values between 300 mg and 600 mg
/// (0x05 to 0x09) are recommended.
/// </summary>
/// <param name="threshFF">Free-fall detection value.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetThresholdFreeFall(ref Mass threshFF)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.ThreshFF);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
threshFF = Mass.FromMilligrams(readBuf[0] * 62.5);
return true;
}
return false;
}
/// <summary>
/// The THRESH_FF register is eight bits and holds the threshold
/// value, in unsigned format, for free-fall detection.The acceleration
/// on all axes is compared with the value in THRESH_FF to determine
/// if a free-fall event occurred.The scale factor is 62.5 mg/LSB.Note
/// that a value of 0 mg may result in undesirable behavior if the
/// free-fall interrupt is enabled.Values between 300 mg and 600 mg
/// (0x05 to 0x09) are recommended.
/// </summary>
/// <param name="threshFF">Free-fall detection value.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">ThreshFF must be between 0mg and 15,937mg.</exception>
public bool TrySetThresholdFreeFall(Mass threshFF)
{
double td = threshFF.Milligrams / 62.5;
if (td < 0 || td > 255)
{
throw new ArgumentException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.ThreshFF;
writeBuf[1] = (byte)td;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Time FF
/// <summary>
/// The TIME_FF register is eight bits and stores an unsigned time
/// value representing the minimum time that the value of all axes
/// must be less than THRESH_FF to generate a free-fall interrupt.The
/// scale factor is 5 ms/LSB.A value of 0 may result in undesirable
/// behavior if the free-fall interrupt is enabled.Values between 100 ms
/// and 350 ms (0x14 to 0x46) are recommended.
/// </summary>
/// <param name="timeFF">Minimum time all axes less than Thresh FF.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetTimeFreeFall(ref TimeSpan timeFF)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.TimeFF);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
timeFF = TimeSpan.FromMilliseconds(readBuf[0] * 5);
return true;
}
return false;
}
/// <summary>
/// The TIME_FF register is eight bits and stores an unsigned time
/// value representing the minimum time that the value of all axes
/// must be less than THRESH_FF to generate a free-fall interrupt.The
/// scale factor is 5 ms/LSB.A value of 0 may result in undesirable
/// behavior if the free-fall interrupt is enabled.Values between 100 ms
/// and 350 ms (0x14 to 0x46) are recommended.
/// </summary>
/// <param name="timeFF">Minimum time all axes less than Thresh FF.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
/// <exception cref="ArgumentException">TimFF can only be between 0 and 1.275 seconds.</exception>
public bool TrySetTimeFreeFall(TimeSpan timeFF)
{
double ms = timeFF.TotalMilliseconds / 5;
if (ms < 0 || ms > 255)
{
throw new ArgumentException();
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.TimeFF;
writeBuf[1] = (byte)ms;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Act/Inact Control
/// <summary>
/// Suppress Bit
/// Setting the suppress bit suppresses double tap detection if acceleration greater than the value in THRESH_TAP is present between
/// taps.
/// TAP_x Enable Bits
/// A setting of 1 in the TAP_X enable, TAP_Y enable, or TAP_Z
/// enable bit enables x-, y-, or z-axis participation in tap detection.
/// A setting of 0 excludes the selected axis from participation in tap
/// detection.
/// </summary>
/// <param name="suppress">Suppress.</param>
/// <param name="tapXEnable">Tap X Enable.</param>
/// <param name="tapYEnable">Tap Y Enable.</param>
/// <param name="tapZEnable">Tap Z Enable.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetTapAxes(ref bool suppress, ref bool tapXEnable, ref bool tapYEnable, ref bool tapZEnable)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.TapAxes);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
byte map = readBuf[0];
suppress = (map & (byte)TapAxesMap.Suppress) == (byte)TapAxesMap.Suppress;
tapXEnable = (map & (byte)TapAxesMap.TapXEnable) == (byte)TapAxesMap.TapXEnable;
tapYEnable = (map & (byte)TapAxesMap.TapYEnable) == (byte)TapAxesMap.TapYEnable;
tapZEnable = (map & (byte)TapAxesMap.TapZEnable) == (byte)TapAxesMap.TapZEnable;
return true;
}
return false;
}
/// <summary>
/// Suppress Bit
/// Setting the suppress bit suppresses double tap detection if acceleration greater than the value in THRESH_TAP is present between
/// taps.
/// TAP_x Enable Bits
/// A setting of 1 in the TAP_X enable, TAP_Y enable, or TAP_Z
/// enable bit enables x-, y-, or z-axis participation in tap detection.
/// A setting of 0 excludes the selected axis from participation in tap
/// detection.
/// </summary>
/// <param name="suppress">Suppress.</param>
/// <param name="tapXEnable">Tap X Enable.</param>
/// <param name="tapYEnable">Tap Y Enable.</param>
/// <param name="tapZEnable">Tap Z Enable.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TrySetTapAxes(bool suppress, bool tapXEnable, bool tapYEnable, bool tapZEnable)
{
byte map = 0;
if (suppress)
{
map |= (byte)TapAxesMap.Suppress;
}
if (tapXEnable)
{
map |= (byte)TapAxesMap.TapXEnable;
}
if (tapYEnable)
{
map |= (byte)TapAxesMap.TapYEnable;
}
if (tapZEnable)
{
map |= (byte)TapAxesMap.TapZEnable;
}
SpanByte writeBuf = new byte[2];
writeBuf[0] = (byte)Register.ActInactCtl;
writeBuf[1] = map;
var res = _i2c.Write(writeBuf);
if (res.Status == I2cTransferStatus.FullTransfer)
{
return true;
}
return false;
}
#endregion
#region Act Tap Status
/// <summary>
/// <para>
/// ACT_x Source and TAP_x Source Bits
/// These bits indicate the first axis involved in a tap or activity event.
/// A setting of 1 corresponds to involvement in the event, and a
/// setting of 0 corresponds to no involvement. When new data is
/// available, these bits are not cleared but are overwritten by the
/// new data.The ACT_TAP_STATUS register should be read before
/// clearing the interrupt.Disabling an axis from participation clears the
/// corresponding source bit when the next activity or single tap/double
/// tap event occurs.
/// </para>
/// <para>
/// Asleep Bit
/// A setting of 1 in the asleep bit indicates that the part is asleep,
/// and a setting of 0 indicates that the part is not asleep.This bit
/// toggles only if the device is configured for auto sleep. See the
/// AUTO_SLEEP Bit section for more information on autosleep mode.
/// </para>
/// </summary>
/// <param name="actXSource">Active X Source.</param>
/// <param name="actYSource">Active Y Source.</param>
/// <param name="actZSource">Active Z Source.</param>
/// <param name="asleep">Asleep.</param>
/// <param name="tapXSource">Tap X Source.</param>
/// <param name="tapYSource">Tap Y Source.</param>
/// <param name="tapZSource">Tap Z Source.</param>
/// <returns>True if Full Transfer result, false if any other result.</returns>
public bool TryGetActTapStatus(ref bool actXSource, ref bool actYSource, ref bool actZSource, ref bool asleep, ref bool tapXSource, ref bool tapYSource, ref bool tapZSource)
{
SpanByte readBuf = new byte[1];
var res = _i2c.WriteByte((byte)Register.ActTapStatus);
res = _i2c.Read(readBuf);
if (res.Status == I2cTransferStatus.FullTransfer)