-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
1043 lines (941 loc) · 30.3 KB
/
index.js
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
// javascript/node rewrite of the Adafruit ads1x15 python library...
var i2c = require('i2c');
var async = require('async');
// chip
IC_ADS1015 = 0x00
IC_ADS1115 = 0x01
// Pointer Register
ADS1015_REG_POINTER_MASK = 0x03
ADS1015_REG_POINTER_CONVERT = 0x00
ADS1015_REG_POINTER_CONFIG = 0x01
ADS1015_REG_POINTER_LOWTHRESH = 0x02
ADS1015_REG_POINTER_HITHRESH = 0x03
// Config Register
ADS1015_REG_CONFIG_OS_MASK = 0x8000
ADS1015_REG_CONFIG_OS_SINGLE = 0x8000 // Write: Set to start a single-conversion
ADS1015_REG_CONFIG_OS_BUSY = 0x0000 // Read: Bit = 0 when conversion is in progress
ADS1015_REG_CONFIG_OS_NOTBUSY = 0x8000 // Read: Bit = 1 when device is not performing a conversion
ADS1015_REG_CONFIG_MUX_MASK = 0x7000
ADS1015_REG_CONFIG_MUX_DIFF_0_1 = 0x0000 // Differential P = AIN0, N = AIN1 (default)
ADS1015_REG_CONFIG_MUX_DIFF_0_3 = 0x1000 // Differential P = AIN0, N = AIN3
ADS1015_REG_CONFIG_MUX_DIFF_1_3 = 0x2000 // Differential P = AIN1, N = AIN3
ADS1015_REG_CONFIG_MUX_DIFF_2_3 = 0x3000 // Differential P = AIN2, N = AIN3
ADS1015_REG_CONFIG_MUX_SINGLE_0 = 0x4000 // Single-ended AIN0
ADS1015_REG_CONFIG_MUX_SINGLE_1 = 0x5000 // Single-ended AIN1
ADS1015_REG_CONFIG_MUX_SINGLE_2 = 0x6000 // Single-ended AIN2
ADS1015_REG_CONFIG_MUX_SINGLE_3 = 0x7000 // Single-ended AIN3
ADS1015_REG_CONFIG_PGA_MASK = 0x0E00
ADS1015_REG_CONFIG_PGA_6_144V = 0x0000 // +/-6.144V range
ADS1015_REG_CONFIG_PGA_4_096V = 0x0200 // +/-4.096V range
ADS1015_REG_CONFIG_PGA_2_048V = 0x0400 // +/-2.048V range (default)
ADS1015_REG_CONFIG_PGA_1_024V = 0x0600 // +/-1.024V range
ADS1015_REG_CONFIG_PGA_0_512V = 0x0800 // +/-0.512V range
ADS1015_REG_CONFIG_PGA_0_256V = 0x0A00 // +/-0.256V range
ADS1015_REG_CONFIG_MODE_MASK = 0x0100
ADS1015_REG_CONFIG_MODE_CONTIN = 0x0000 // Continuous conversion mode
ADS1015_REG_CONFIG_MODE_SINGLE = 0x0100 // Power-down single-shot mode (default)
ADS1015_REG_CONFIG_DR_MASK = 0x00E0
ADS1015_REG_CONFIG_DR_128SPS = 0x0000 // 128 samples per second
ADS1015_REG_CONFIG_DR_250SPS = 0x0020 // 250 samples per second
ADS1015_REG_CONFIG_DR_490SPS = 0x0040 // 490 samples per second
ADS1015_REG_CONFIG_DR_920SPS = 0x0060 // 920 samples per second
ADS1015_REG_CONFIG_DR_1600SPS = 0x0080 // 1600 samples per second (default)
ADS1015_REG_CONFIG_DR_2400SPS = 0x00A0 // 2400 samples per second
ADS1015_REG_CONFIG_DR_3300SPS = 0x00C0 // 3300 samples per second (also 0x00E0)
ADS1115_REG_CONFIG_DR_8SPS = 0x0000 // 8 samples per second
ADS1115_REG_CONFIG_DR_16SPS = 0x0020 // 16 samples per second
ADS1115_REG_CONFIG_DR_32SPS = 0x0040 // 32 samples per second
ADS1115_REG_CONFIG_DR_64SPS = 0x0060 // 64 samples per second
ADS1115_REG_CONFIG_DR_128SPS = 0x0080 // 128 samples per second
ADS1115_REG_CONFIG_DR_250SPS = 0x00A0 // 250 samples per second (default)
ADS1115_REG_CONFIG_DR_475SPS = 0x00C0 // 475 samples per second
ADS1115_REG_CONFIG_DR_860SPS = 0x00E0 // 860 samples per second
ADS1015_REG_CONFIG_CMODE_MASK = 0x0010
ADS1015_REG_CONFIG_CMODE_TRAD = 0x0000 // Traditional comparator with hysteresis (default)
ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010 // Window comparator
ADS1015_REG_CONFIG_CPOL_MASK = 0x0008
ADS1015_REG_CONFIG_CPOL_ACTVLOW = 0x0000 // ALERT/RDY pin is low when active (default)
ADS1015_REG_CONFIG_CPOL_ACTVHI = 0x0008 // ALERT/RDY pin is high when active
ADS1015_REG_CONFIG_CLAT_MASK = 0x0004 // Determines if ALERT/RDY pin latches once asserted
ADS1015_REG_CONFIG_CLAT_NONLAT = 0x0000 // Non-latching comparator (default)
ADS1015_REG_CONFIG_CLAT_LATCH = 0x0004 // Latching comparator
ADS1015_REG_CONFIG_CQUE_MASK = 0x0003
ADS1015_REG_CONFIG_CQUE_1CONV = 0x0000 // Assert ALERT/RDY after one conversions
ADS1015_REG_CONFIG_CQUE_2CONV = 0x0001 // Assert ALERT/RDY after two conversions
ADS1015_REG_CONFIG_CQUE_4CONV = 0x0002 // Assert ALERT/RDY after four conversions
ADS1015_REG_CONFIG_CQUE_NONE = 0x0003 // Disable the comparator and put ALERT/RDY in high state (default)
// This is a javascript port of python, so use objects instead of dictionaries here
// These simplify and clean the code (avoid the abuse of if/elif/else clauses)
var spsADS1115 = {
8 : ADS1115_REG_CONFIG_DR_8SPS,
16 : ADS1115_REG_CONFIG_DR_16SPS,
32 : ADS1115_REG_CONFIG_DR_32SPS,
64 : ADS1115_REG_CONFIG_DR_64SPS,
128 : ADS1115_REG_CONFIG_DR_128SPS,
250 : ADS1115_REG_CONFIG_DR_250SPS,
475 : ADS1115_REG_CONFIG_DR_475SPS,
860 : ADS1115_REG_CONFIG_DR_860SPS
};
var spsADS1015 = {
128 : ADS1015_REG_CONFIG_DR_128SPS,
250 : ADS1015_REG_CONFIG_DR_250SPS,
490 : ADS1015_REG_CONFIG_DR_490SPS,
920 : ADS1015_REG_CONFIG_DR_920SPS,
1600 : ADS1015_REG_CONFIG_DR_1600SPS,
2400 : ADS1015_REG_CONFIG_DR_2400SPS,
3300 : ADS1015_REG_CONFIG_DR_3300SPS
};
// Dictionary with the programable gains
var pgaADS1x15 = {
6144 : ADS1015_REG_CONFIG_PGA_6_144V,
4096 : ADS1015_REG_CONFIG_PGA_4_096V,
2048 : ADS1015_REG_CONFIG_PGA_2_048V,
1024 : ADS1015_REG_CONFIG_PGA_1_024V,
512 : ADS1015_REG_CONFIG_PGA_0_512V,
256 : ADS1015_REG_CONFIG_PGA_0_256V
};
// set up I2C for ADS1015/ADS1115
function ads1x15(ic, address, i2c_dev) {
if(!(ic))
{
ic = IC_ADS1015; // default to ads1015...
}
if(!(address))
{
address = 0x48; // Address pin tied to ground gives us 1001000 (or 0x48)
}
if(!(i2c_dev))
{
i2c_dev = '/dev/i2c-1'; // default to pi 2b/3...
}
if(!(ic == IC_ADS1015 | ic == IC_ADS1115))
{
throw "Error: not a supported device";
}
this.ic = ic; // 0 for ads1015, 1 for ads1115;
this.address = address; //defaults to 0x48 for addr pin tied to ground
this.pga = 6144; //set this to a sane default...
this.wire = new i2c(address, { device : i2c_dev } );
this.busy = false;
}
// Gets a single-ended ADC reading from the specified channel in mV. \
// The sample rate for this mode (single-shot) can be used to lower the noise \
// (low sps) or to lower the power consumption (high sps) by duty cycling, \
// see datasheet page 14 for more info. \
// The pga must be given in mV, see page 13 for the supported values.
ads1x15.prototype.readADCSingleEnded = function(channel, pga, sps, callback) {
var self = this;
if(!self.busy)
{
self.busy = true;
if(!channel)
channel = 0;
if(!pga)
pga = 6144;
if(!sps)
sps = 250;
if(channel > 3 || channel < 0)
{
self.busy = false;
callback("Error: Channel must be between 0 and 3");
}
// Disable comparator, Non-latching, Alert/Rdy active low
// traditional comparator, single-shot mode
var config = ADS1015_REG_CONFIG_CQUE_NONE | ADS1015_REG_CONFIG_CLAT_NONLAT | ADS1015_REG_CONFIG_CPOL_ACTVLOW | ADS1015_REG_CONFIG_CMODE_TRAD | ADS1015_REG_CONFIG_MODE_SINGLE;
// Set sample per seconds, defaults to 250sps
// If sps is in the dictionary (defined in init) it returns the value of the constant
// othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (self.ic == IC_ADS1015)
{
if(spsADS1015[sps])
{
config |= spsADS1015[sps];
}
else callback("ADS1x15: Invalid sps specified");
}
else
{
if (!(spsADS1115[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid sps specified");
}
else
{
config |= spsADS1115[sps];
}
}
// Set PGA/voltage range, defaults to +-6.144V
if (!(pgaADS1x15[pga]))
{
self.busy = false;
callback("ADS1x15: Invalid pga specified");
}
else
{
config |= pgaADS1x15[pga];
}
self.pga = pga
// Set the channel to be converted
if ( channel == 3)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
}
else if(channel == 2)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
}
else if(channel == 1)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
}
else
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
}
// Set 'start single-conversion' bit
config |= ADS1015_REG_CONFIG_OS_SINGLE;
// Write config register to the ADC
var bytes = [(config >> 8) & 0xFF, config & 0xFF]
self.wire.writeBytes(ADS1015_REG_POINTER_CONFIG, bytes, function(err) {
if(err)
{
self.busy = false;
console.log("We've got an Error, Lance Constable Carrot!: " + err.toString());
callback(err);
}
// Wait for the ADC conversion to complete
// The minimum delay depends on the sps: delay >= 1s/sps
// We add 1ms to be sure
var delay = 1000 /sps + 1 ;
setTimeout(function() {
// Read the conversion results
self.wire.readBytes(ADS1015_REG_POINTER_CONVERT, 2, function(err, res) {
if(err)
{
self.busy = false;
console.log("We've got an Error, Lance Constable Carrot!: " + err.toString());
callback(err);
}
var data = -0.1;
if (self.ic == IC_ADS1015)
{
// Shift right 4 bits for the 12-bit ADS1015 and convert to mV
// console.log('res0 = ' + res[0] + ', res1: ' + res[1]);
var data = ( ((res[0] << 8) | (res[1] & 0xFF)) >> 4 ) * self.pga / 2048.0;
self.busy = false;
callback(null, data);
}
else
{
// Return a mV value for the ADS1115
// (Take signed values into account as well)
data = -0.1;
var val = (res[0] << 8) | (res[1])
if (val > 0x7FFF)
{
data = (val - 0xFFFF) * pga / 32768.0;
}
else
{
data = ( (res[0] << 8) | (res[1]) ) * pga / 32768.0;
}
self.busy = false;
callback(null, data);
}
});
}, delay);
});
}
else
{
callback("ADC is busy...");
}
}
// Gets a differential ADC reading from channels chP and chN in mV. \
// The sample rate for this mode (single-shot) can be used to lower the noise \
// (low sps) or to lower the power consumption (high sps) by duty cycling, \
// see data sheet page 14 for more info. \
// The pga must be given in mV, see page 13 for the supported values.
ads1x15.prototype.readADCDifferential = function(chP, chN, pga, sps, callback) {
var self = this;
if(!self.busy)
{
self.busy = true;
//set defaults if not provided
if(!chP)
chP = 0;
if(!chN)
chN = 1;
if(!pga)
pga=6144;
if(!sps)
sps=250;
// Disable comparator, Non-latching, Alert/Rdy active low
// traditional comparator, single-shot mode
config = ADS1015_REG_CONFIG_CQUE_NONE | ADS1015_REG_CONFIG_CLAT_NONLAT |
ADS1015_REG_CONFIG_CPOL_ACTVLOW | ADS1015_REG_CONFIG_CMODE_TRAD |
ADS1015_REG_CONFIG_MODE_SINGLE;
// Set channels
if ( (chP == 0) & (chN == 1) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1;
}
else if ( (chP == 0) & (chN == 3) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_0_3;
}
else if ( (chP == 2) & (chN == 3) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3;
}
else if ( (chP == 1) & (chN == 3) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_1_3;
}
else
{
self.busy = false;
console.log( "ADS1x15: Invalid channels specified");
callback("ADS1x15: Invalid channels specified");
}
// Set sample per seconds, defaults to 250sps
// If sps is in the dictionary (defined in init()) it returns the value of the constant
// othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (this.ic == IC_ADS1015)
{
config |= spsADS1015[sps];
}
else
{
if (!(spsADS1115[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid pga specified");
}
else
{
config |= spsADS1115[sps];
}
}
// Set PGA/voltage range, defaults to +-6.144V
if (!(pgaADS1x15[pga]))
{
self.busy = false;
callback("ADS1x15: Invalid pga specified");
}
else
{
config |= pgaADS1x15[pga];
this.pga = pga;
}
// Set 'start single-conversion' bit
config |= ADS1015_REG_CONFIG_OS_SINGLE;
// Write config register to the ADC
bytes = [(config >> 8) & 0xFF, config & 0xFF];
self.wire.writeBytes(ADS1015_REG_POINTER_CONFIG, bytes, function(err) {
if(err)
{
self.busy = false;
callback("We've got an Error, Lance Constable Carrot!: " + err.toString());
}
});
// Wait for the ADC conversion to complete
// The minimum delay depends on the sps: delay >= 1s/sps
// We add 1ms to be sure
delay = 1000 / sps + 1;
setTimeout(function() {
self.wire.readBytes(ADS1015_REG_POINTER_CONVERT, 2, function(err, res) {
if (self.ic == IC_ADS1015)
{
// Shift right 4 bits for the 12-bit ADS1015 and convert to mV
var data = ( ((res[0] << 8) | (res[1] & 0xFF)) >> 4 ) * pga / 2048.0;
self.busy = false;
callback(null, data);
}
else
{
// Return a mV value for the ADS1115
// (Take signed values into account as well)
var data = -1;
var val = (res[0] << 8) | (res[1]);
if (val > 0x7FFF)
{
data = (val - 0xFFFF) * pga / 32768.0;
}
else
{
data = ( (res[0] << 8) | (res[1]) ) * pga / 32768.0;
}
self.busy = false;
callback(null, data);
}
});
}, delay);
}
else
{
callback("ADC is busy...");
}
}
// Gets a differential ADC reading from channels 0 and 1 in mV
// The sample rate for this mode (single-shot) can be used to lower the noise
// (low sps) or to lower the power consumption (high sps) by duty cycling,
// see data sheet page 14 for more info.
// The pga must be given in mV, see page 13 for the supported values.
ads1x15.prototype.readADCDifferential01 = function(pga, sps, callback) {
if(!pga)
pga=6144;
if(!sps)
sps=250;
return this.readADCDifferential(0, 1, pga, sps, callback);
}
// Gets a differential ADC reading from channels 0 and 3 in mV
// The sample rate for this mode (single-shot) can be used to lower the noise
// (low sps) or to lower the power consumption (high sps) by duty cycling,
// see data sheet page 14 for more info.
// The pga must be given in mV, see page 13 for the supported values.
ads1x15.prototype.readADCDifferential03 = function (pga, sps, callback) {
if(!pga)
pga=6144;
if(!sps)
sps=250;
return this.readADCDifferential(0, 3, pga, sps, callback);
}
// Gets a differential ADC reading from channels 1 and 3 in mV
// The sample rate for this mode (single-shot) can be used to lower the noise
// (low sps) or to lower the power consumption (high sps) by duty cycling,
// see data sheet page 14 for more info.
// The pga must be given in mV, see page 13 for the supported values.
ads1x15.prototype.readADCDifferential13 = function(pga, sps, callback) {
if(!pga)
pga = 6144;
if(!sps)
sps = 250;
return this.readADCDifferential(1, 3, pga, sps, callback);
}
// Gets a differential ADC reading from channels 2 and 3 in mV
// The sample rate for this mode (single-shot) can be used to lower the noise
// (low sps) or to lower the power consumption (high sps) by duty cycling,
// see data sheet page 14 for more info.
// The pga must be given in mV, see page 13 for the supported values.
ads1x15.prototype.readADCDifferential23 = function(pga, sps, callback) {
if(!pga)
pga = 6144;
if(!sps)
sps = 250;
return this.readADCDifferential(2, 3, pga, sps, callback);
}
// Starts the continuous conversion mode and returns the first ADC reading
// in mV from the specified channel.
// The sps controls the sample rate.
// The pga must be given in mV, see datasheet page 13 for the supported values.
// Use getLastConversionResults() to read the next values and
// stopContinuousConversion() to stop converting.
ads1x15.prototype.startContinuousConversion = function(channel, pga, sps, callback) {
var self = this;
if(!self.busy)
{
self.busy = true;
if(!channel)
channel = 0;
if(!pga)
pga = 6144;
if(!sps)
sps = 250;
// Default to channel 0 with invalid channel, or return -1?
if (channel > 3)
{
self.busy = false;
callback( "ADS1x15: Invalid channel specified, Lance Corporal Carrot!");
}
// Disable comparator, Non-latching, Alert/Rdy active low
// traditional comparator, continuous mode
// The last flag is the only change we need, page 11 datasheet
config = ADS1015_REG_CONFIG_CQUE_NONE | ADS1015_REG_CONFIG_CLAT_NONLAT |
ADS1015_REG_CONFIG_CPOL_ACTVLOW | ADS1015_REG_CONFIG_CMODE_TRAD |
ADS1015_REG_CONFIG_MODE_CONTIN;
// Set sample per seconds, defaults to 250sps
// If sps is in the dictionary (defined in init()) it returns the value of the constant
// othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (this.ic == IC_ADS1015)
{
config |= spsADS1015[sps];
}
else
{
if (!(spsADS1115[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid sps specified");
}
else
{
config |= spsADS1115[sps];
}
}
// Set PGA/voltage range, defaults to +-6.144V
if (!(pgaADS1x15[pga]))
{
self.busy = false;
callback("ADS1x15: Invalid pga specified");
}
else
{
config |= pgaADS1x15[pga];
}
this.pga = pga;
// Set the channel to be converted
if (channel == 3)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
}
else if(channel == 2)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
}
else if(channel == 1)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
}
else
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
}
// Set 'start single-conversion' bit to begin conversions
// No need to change this for continuous mode!
config |= ADS1015_REG_CONFIG_OS_SINGLE;
// Write config register to the ADC
// Once we write the ADC will convert continously
// we can read the next values using getLastConversionResult
bytes = [(config >> 8) & 0xFF, config & 0xFF];
self.wire.writeBytes(ADS1015_REG_POINTER_CONFIG, bytes, function(err) {
if(err)
{
self.busy = false;
callback("We've got an Error, Lance Constable Carrot!: " + err.toString());
}
});
// Wait for the ADC conversion to complete
// The minimum delay depends on the sps: delay >= 1s/sps
// We add 1ms to be sure
delay = 1000 / sps + 1;
setTimeout(function() {
self.wire.readBytes(ADS1015_REG_POINTER_CONVERT, 2, function(err, res) {
if (this.ic == IC_ADS1015)
{
// Shift right 4 bits for the 12-bit ADS1015 and convert to mV
var data = ( ((res[0] << 8) | (res[1] & 0xFF)) >> 4 ) * pga / 2048.0;
callback(null, data);
}
else
{
// Return a mV value for the ADS1115
// (Take signed values into account as well)
var data = -1;
var val = (res[0] << 8) | (res[1]);
if (val > 0x7FFF)
{
data = (val - 0xFFFF) * pga / 32768.0;
}
else
{
data = ( (res[0] << 8) | (res[1]) ) * pga / 32768.0;
}
callback(null, data);
}
});
}, delay);
}
else
{
callback("ADC is busy...");
}
}
// Stops the ADC's conversions when in continuous mode \
// and resets the configuration to its default value."
ads1x15.prototype.stopContinuousConversion = function(callback) {
// Write the default config register to the ADC
// Once we write, the ADC will do a single conversion and
// enter power-off mode.
self = this;
config = 0x8583; // Page 18 datasheet.
bytes = [(config >> 8) & 0xFF, config & 0xFF];
this.wire.writeBytes(ADS1015_REG_POINTER_CONFIG, bytes, function(err) {
self.busy = false;
if(err)
{
console.log("Error: " + err);
callback(err);
}
else return true;
});
}
// Returns the last ADC conversion result in mV
ads1x15.prototype.getLastConversionResults = function(callback) {
// Read the conversion results
this.wire.readBytes(ADS1015_REG_POINTER_CONVERT, 2, function(err, res) {
if (this.ic == IC_ADS1015)
{
// Shift right 4 bits for the 12-bit ADS1015 and convert to mV
var data = ( ((res[0] << 8) | (res[1] & 0xFF)) >> 4 ) * this.pga / 2048.0;
callback(null, data);
}
else
{
// Return a mV value for the ADS1115
// (Take signed values into account as well)
var data = -1;
var val = (res[0] << 8) | (res[1])
if (val > 0x7FFF)
{
data = (val - 0xFFFF) * this.pga / 32768.0;
}
else
{
data = ( (res[0] << 8) | (res[1]) ) * this.pga / 32768.0;
}
}
});
}
// Starts the comparator mode on the specified channel, see datasheet pg. 15.
// In traditional mode it alerts (ALERT pin will go low) when voltage exceeds
// thresholdHigh until it falls below thresholdLow (both given in mV).
// In window mode (traditionalMode=False) it alerts when voltage doesn't lie
// between both thresholds.
// In latching mode the alert will continue until the conversion value is read.
// numReadings controls how many readings are necessary to trigger an alert: 1, 2 or 4.
// Use getLastConversionResults() to read the current value (which may differ
// from the one that triggered the alert) and clear the alert pin in latching mode.
// This function starts the continuous conversion mode. The sps controls
// the sample rate and the pga the gain, see datasheet page 13.
ads1x15.prototype.startSingleEndedComparator = function(channel, thresholdHigh, thresholdLow, pga, sps, activeLow, traditionalMode, latching, numReadings, callback) {
self = this;
if(!self.busy)
{
self.busy = true;
if(!(pga))
pga = 6144;
if(!(sps))
sps = 250;
if(!(activeLow))
activeLow = true;
if(!(traditionalMode))
traditionalMode = true;
if(!(latching))
latching = false;
if(!(numReadings))
numReadings = 1;
// With invalid channel return -1
if (channel > 3)
{
self.busy = false;
console.log("ADS1x15: Invalid channel specified");
callback("ADS1x15: Invalid channel specified");
}
// Continuous mode
config = ADS1015_REG_CONFIG_MODE_CONTIN;
if (activeLow == false)
{
config |= ADS1015_REG_CONFIG_CPOL_ACTVHI;
}
else
{
config |= ADS1015_REG_CONFIG_CPOL_ACTVLOW;
}
if (traditionalMode == false)
{
config |= ADS1015_REG_CONFIG_CMODE_WINDOW;
}
else
{
config |= ADS1015_REG_CONFIG_CMODE_TRAD;
}
if (latching == true)
{
config |= ADS1015_REG_CONFIG_CLAT_LATCH;
}
else
{
config |= ADS1015_REG_CONFIG_CLAT_NONLAT;
}
if (numReadings == 4)
{
config |= ADS1015_REG_CONFIG_CQUE_4CONV;
}
else if(numReadings == 2)
{
config |= ADS1015_REG_CONFIG_CQUE_2CONV;
}
else
{
config |= ADS1015_REG_CONFIG_CQUE_1CONV;
}
// Set sample per seconds, defaults to 250sps
// If sps is in the dictionary (defined in init()) it returns the value of the constant
// othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (this.ic == IC_ADS1015)
{
if (!(spsADS1015[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid sps specified");
}
config |= spsADS1015[sps];
}
else
{
if (!(spsADS1115[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid sps specified");
}
config |= spsADS1115[sps];
}
// Set PGA/voltage range, defaults to +-6.144V
if (!(pgaADS1x15[pga]))
{
self.busy = false;
callback("ADS1x15: Invalid pga specified");
}
config |= pgaADS1x15[pga];
this.pga = pga
// Set the channel to be converted
if (channel == 3)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
}
else if( channel == 2)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
}
else if(channel == 1)
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
}
else
{
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
}
// Set 'start single-conversion' bit to begin conversions
config |= DS1015_REG_CONFIG_OS_SINGLE;
// Write threshold high and low registers to the ADC
// V_digital = (2^(n-1)-1)/pga*V_analog
var thresholdHighWord = 0;
if (this.ic == IC_ADS1015)
{
thresholdHighWORD = int(thresholdHigh*(2048.0/pga));
}
else
{
thresholdHighWORD = int(thresholdHigh*(32767.0/pga));
}
var bytes = [(thresholdHighWORD >> 8) & 0xFF, thresholdHighWORD & 0xFF];
this.wire.writeBytes(ADS1015_REG_POINTER_HITHRESH, bytes, function(err){
if(err)
{
self.busy = false;
callback(err);
}
});
var thresholdLowWORD = 0;
if (this.ic == IC_ADS1015)
{
thresholdLowWORD = int(thresholdLow*(2048.0/pga));
}
else
{
thresholdLowWORD = int(thresholdLow*(32767.0/pga));
}
var bytes = [(thresholdLowWORD >> 8) & 0xFF, thresholdLowWORD & 0xFF];
this.wire.writeBytes(ADS1015_REG_POINTER_LOWTHRESH, bytes, function(err) {
if(err){
self.busy = false;
callback(err);
}
});
// Write config register to the ADC
// Once we write the ADC will convert continously and alert when things happen,
// we can read the converted values using getLastConversionResult
bytes = [(config >> 8) & 0xFF, config & 0xFF];
this.wire.writeBytes(ADS1015_REG_POINTER_CONFIG, bytes, function(err) {
if(err){
self.busy = false;
callback(err);
}
});
}
else
{
callback("ADC is busy...");
}
}
// Starts the comparator mode on the specified channel, see datasheet pg. 15. \
// In traditional mode it alerts (ALERT pin will go low) when voltage exceeds \
// thresholdHigh until it falls below thresholdLow (both given in mV). \
// In window mode (traditionalMode=False) it alerts when voltage doesn't lie\
// between both thresholds.\
// In latching mode the alert will continue until the conversion value is read. \
// numReadings controls how many readings are necessary to trigger an alert: 1, 2 or 4.\
// Use getLastConversionResults() to read the current value (which may differ \
// from the one that triggered the alert) and clear the alert pin in latching mode. \
// This function starts the continuous conversion mode. The sps controls \
// the sample rate and the pga the gain, see datasheet page 13. "
ads1x15.prototype.startDifferentialComparator = function(chP, chN, thresholdHigh, thresholdLow, pga, sps, activeLow, traditionalMode, latching, numReadings, callback) {
self = this;
if(!self.busy)
{
self.busy = true;
if(!(pga))
pga = 6144;
if(!(sps))
sps = 250;
if(!(activeLow))
activeLow = true;
if(!(traditionalMode))
traditionalMode = true;
if(!(latching))
latching = false;
if(!(numReadings))
numReadings = 1;
// Continuous mode
config = ADS1015_REG_CONFIG_MODE_CONTIN;
if (activeLow==False)
{
config |= ADS1015_REG_CONFIG_CPOL_ACTVHI;
}
else
{
config |= ADS1015_REG_CONFIG_CPOL_ACTVLOW;
}
if (!traditionalMode)
{
config |= ADS1015_REG_CONFIG_CMODE_WINDOW;
}
else
{
config |= ADS1015_REG_CONFIG_CMODE_TRAD;
}
if (latching)
{
config |= ADS1015_REG_CONFIG_CLAT_LATCH;
}
else
{
config |= ADS1015_REG_CONFIG_CLAT_NONLAT;
}
if (numReadings==4)
{
config |= ADS1015_REG_CONFIG_CQUE_4CONV;
}
else if(numReadings==2)
{
config |= ADS1015_REG_CONFIG_CQUE_2CONV;
}
else
{
config |= ADS1015_REG_CONFIG_CQUE_1CONV;
}
// Set sample per seconds, defaults to 250sps
// If sps is in the dictionary (defined in init()) it returns the value of the constant
// othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (this.ic == IC_ADS1015)
{
if (!(spsADS1015[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid sps specified");
}
config |= spsADS1015[sps];
}
else
{
if (!(spsADS1115[sps]))
{
self.busy = false;
callback("ADS1x15: Invalid sps specified");
}
config |= spsADS1115[sps];
}
// Set PGA/voltage range, defaults to +-6.144V
if (!(pgaADS1x15[pga]))
{
self.busy = false;
callback("ADS1x15: Invalid pga specified");
}
config |= pgaADS1x15[pga];
this.pga = pga;
// Set channels
if ( (chP == 0) & (chN == 1) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1;
}
else if ( (chP == 0) & (chN == 3) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_0_3;
}
else if ( (chP == 2) & (chN == 3) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3;
}
else if ( (chP == 1) & (chN == 3) )
{
config |= ADS1015_REG_CONFIG_MUX_DIFF_1_3;
}
else
{
self.busy = false;
callback("ADS1x15: Invalid channels specified");
}
// Set 'start single-conversion' bit to begin conversions
config |= ADS1015_REG_CONFIG_OS_SINGLE;
// Write threshold high and low registers to the ADC
// V_digital = (2^(n-1)-1)/pga*V_analog;
var thresholdHighWORD = 0;
if (this.ic == IC_ADS1015)
{
thresholdHighWORD = int(thresholdHigh*(2048.0/pga));
}
else
{
thresholdHighWORD = int(thresholdHigh*(32767.0/pga));
}
var bytes = [(thresholdHighWORD >> 8) & 0xFF, thresholdHighWORD & 0xFF];
this.wire.writeBytes(ADS1015_REG_POINTER_HITHRESH, bytes, function(err) {