-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIna219.java
More file actions
1827 lines (1541 loc) · 55 KB
/
Ina219.java
File metadata and controls
1827 lines (1541 loc) · 55 KB
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
/**
* Library for interfacing an INA219 High DC Current Sensor with a Raspberry Pi
* @author Alexandre Scieux
* @version 1.0
*
* Caution : Be sure to include /opt/pi4j/lib/'*' in your classpath (http://pi4j.com/)
*
*/
package sensor;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class Ina219
{
private I2CBus bus;
private I2CDevice ina219;
/* Default configuration values */
private BusVoltageRange busVoltageRange = BusVoltageRange.INA219_CONFIG_BVOLTAGERANGE_32V;
private Gain gain = Gain.INA219_CONFIG_GAIN_8_320MV;
private BusADCResolution busADCResolution = BusADCResolution.INA219_CONFIG_BADCRES_12BIT;
private ShuntADCResolution shuntADCResolution = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_1S_532US;
private OperatingMode operatingMode = OperatingMode.INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
/**
* INA219 constants
*/
public enum Constants
{
BUS_MAX_V (32.0),
SHUNT_MAX_V (0.32),
R_SHUNT_OHM (5.0),
MAX_POSSIBLE_CURRENT_A (0.064);
private double value;
private Constants (double value)
{
this.value = value;
}
public double getValue()
{
return value;
}
public void setValue(double value)
{
this.value = value;
}
}
/**
* Addresses of the INA219 registers
*/
public enum Registers
{
INA219_I2C_BUS (1),
INA219_I2C_ADDRESS (0x40),
INA219_CONFIG_RESET (0x8000),
INA219_CONFIG_DEFAULT (0x399F),
INA219_CALIBRATION_DEFAULT (0x1000),
INA219_REG_CONFIG (0x00),
INA219_REG_SHUNTVOLTAGE (0x01),
INA219_REG_BUSVOLTAGE (0x02),
INA219_REG_POWER (0x03),
INA219_REG_CURRENT (0x04),
INA219_REG_CALIBRATION (0x05);
private int value;
private Registers (int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
/**
* INA219 Bus Voltage Range configuration values
*/
public enum BusVoltageRange
{
INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000),
INA219_CONFIG_BVOLTAGERANGE_16V (0x0000),
INA219_CONFIG_BVOLTAGERANGE_32V (0x2000);
private int value;
private BusVoltageRange()
{
this.value = (0x2000);
}
private BusVoltageRange (int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
/**
* INA219 Gain configuration values
*/
public enum Gain
{
INA219_CONFIG_GAIN_MASK (0x1800),
INA219_CONFIG_GAIN_1_40MV (0x0000),
INA219_CONFIG_GAIN_2_80MV (0x0800),
INA219_CONFIG_GAIN_4_160MV (0x1000),
INA219_CONFIG_GAIN_8_320MV (0x1800);
private int value;
private Gain()
{
this.value = (0x1800);
}
private Gain (int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
/**
* INA219 Bus ADC Resolution configuration values
*/
public enum BusADCResolution
{
INA219_CONFIG_BADCRES_MASK (0x0780),
INA219_CONFIG_BADCRES_9BIT (0x0080),
INA219_CONFIG_BADCRES_10BIT (0x0100),
INA219_CONFIG_BADCRES_11BIT (0x0200),
INA219_CONFIG_BADCRES_12BIT (0x0400);
private int value;
private BusADCResolution()
{
this.value = (0x0400);
}
private BusADCResolution (int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
/**
* INA219 Shunt ADC resolution configuration values
*/
public enum ShuntADCResolution
{
INA219_CONFIG_SADCRES_MASK (0x0078),
INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000),
INA219_CONFIG_SADCRES_10BIT_1S_148US (0x0008),
INA219_CONFIG_SADCRES_11BIT_1S_276US (0x0010),
INA219_CONFIG_SADCRES_12BIT_1S_532US (0x0018),
INA219_CONFIG_SADCRES_12BIT_2S_1060US (0x0048),
INA219_CONFIG_SADCRES_12BIT_4S_2130US (0x0050),
INA219_CONFIG_SADCRES_12BIT_8S_4260US (0x0058),
INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060),
INA219_CONFIG_SADCRES_12BIT_32S_17MS (0x0068),
INA219_CONFIG_SADCRES_12BIT_64S_34MS (0x0070),
INA219_CONFIG_SADCRES_12BIT_128S_69MS (0x0078);
private int value;
private ShuntADCResolution()
{
this.value = (0x0018);
}
private ShuntADCResolution (int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
/**
* INA219 Operating mode configuration values
*/
public enum OperatingMode
{
INA219_CONFIG_MODE_MASK (0x0007),
INA219_CONFIG_MODE_POWERDOWN (0x0000),
INA219_CONFIG_MODE_SVOLT_TRIGGERED (0x0001),
INA219_CONFIG_MODE_BVOLT_TRIGGERED (0x0002),
INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003),
INA219_CONFIG_MODE_ADCOFF (0x0004),
INA219_CONFIG_MODE_SVOLT_CONTINUOUS (0x0005),
INA219_CONFIG_MODE_BVOLT_CONTINUOUS (0x0006),
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007);
private int value;
private OperatingMode()
{
this.value = (0x0007);
}
private OperatingMode (int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
// Use global variable instead of enum
public void setValue(int value)
{
this.value = value;
}
}
/* I2C */
private static final int i2cbus = Registers.INA219_I2C_BUS.getValue(); // Raspberry Pi's I2C Bus (0 on Raspberry Pi Rev A, 1 on Raspberry Pi Rev B)
private static final int address = Registers.INA219_I2C_ADDRESS.getValue(); // INA219 Device address one the I2C Bus. Run i2cdetect -y 1 to see the address. Default is 0x40.
/* Registers */
/* Shunt Resistor Register (Read only) */
private static final int ina219_reg_shuntvoltage = Registers.INA219_REG_SHUNTVOLTAGE.getValue(); // Address of the register containing the shunt voltage value. Structure at full-scale range : SIGN SD14_8 SD13_8 SD12_8 SD11_8 SD10_8 SD9_8 SD8_8 SD7_8 SD6_8 SD5_8 SD4_8 SD3_8 SD2_8 SD1_8 SD0_8 (16 bits)
/* Bus Voltage Register (Read only) */
private static final int ina219_reg_busvoltage = Registers.INA219_REG_BUSVOLTAGE.getValue(); // Address of the register containing the bus voltage value. Structure : BD12 BD11 BD10 BD9 BD8 BD7 BD6 BD5 BD4 BD3 BD2 BD1 BD0 - CNVR OVF (16 bits)
/* Power Register (Read only) */
private static final int ina219_reg_power = Registers.INA219_REG_POWER.getValue(); // Address of the register containing the power value. Structure : PD15 PD14 PD13 PD12 PD11 PD10 PD9 PD8 PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 (16 bits)
/* Current Register (Read only) */
private static final int ina219_reg_current = Registers.INA219_REG_CURRENT.getValue(); // Address of the register containing the current value. Structure : CSIGN CD14 CD13 CD12 CD11 CD10 CD9 CD8 CD7 CD6 CD5 CD4 CD3 CD2 CD1 CD (16 bits)
/* Calibration Register (Read / Write) */
private static final int ina219_reg_calibration = Registers.INA219_REG_CALIBRATION.getValue(); // Address of the calibration register. Structure : FS15 FS14 FS13 FS12 FS11 FS10 FS9 FS8 FS7 FS6 FS5 FS4 FS3 FS2 FS1 FS0 (16 bits). FS0 is a void bit and will always be 0. It is not possible to write a 1 to FS0. Calibration is the value stored in FS15:FS1.
private static final int ina219_calibration_default = Registers.INA219_CALIBRATION_DEFAULT.getValue(); // Default calibration value. No overflow, maximum range.
/* Configuration register (Read / Write) */
private static final int ina219_reg_config = Registers.INA219_I2C_ADDRESS.getValue(); // Adress of the configuration register. Structure : RST - BRNG PG1 PG0 BADC4 BADC3 BADC2 BADC1 SADC4 SADC3 SADC2 SADC1 MODE3 MODE2 MODE1
private static final int ina219_config_reset = Registers.INA219_CONFIG_RESET.getValue(); // Reset Bit. Setting this bit to '1' generates a system reset that is the same as power-on reset. Resets all registers to default values. This bit self-clears.
private static final int ina219_config_default = Registers.INA219_CONFIG_DEFAULT.getValue(); // Default configuration value.
/* Configuration */
/* Bus Voltage Range */
private static final int ina219_config_bvoltagerange_mask = BusVoltageRange.INA219_CONFIG_BVOLTAGERANGE_MASK.getValue(); // Bus Voltage Range Mask
private static final int ina219_config_bvoltagerange_16v = BusVoltageRange.INA219_CONFIG_BVOLTAGERANGE_16V.getValue(); // 0-16V Range
private static final int ina219_config_bvoltagerange_32v = BusVoltageRange.INA219_CONFIG_BVOLTAGERANGE_32V.getValue(); // 0-32V Range
/* Gain */
private static final int ina219_config_gain_mask = Gain.INA219_CONFIG_GAIN_MASK.getValue(); // Gain Mask
private static final int ina219_config_gain_1_40mv = Gain.INA219_CONFIG_GAIN_1_40MV.getValue(); // Gain 1, 40mV Range
private static final int ina219_config_gain_2_80mv = Gain.INA219_CONFIG_GAIN_2_80MV.getValue(); // Gain 2, 80mV Range
private static final int ina219_config_gain_4_160mv = Gain.INA219_CONFIG_GAIN_4_160MV.getValue(); // Gain 4, 160mV Range
private static final int ina219_config_gain_8_320mv = Gain.INA219_CONFIG_GAIN_8_320MV.getValue(); // Gain 8, 320mV Range
/* Bus ADC Resolution */
private static final int ina219_config_badcres_mask = BusADCResolution.INA219_CONFIG_BADCRES_MASK.getValue(); // Bus ADC resolution mask
private static final int ina219_config_badcres_9bit = BusADCResolution.INA219_CONFIG_BADCRES_9BIT.getValue(); // 9-bit bus resolution = 0..511
private static final int ina219_config_badcres_10bit = BusADCResolution.INA219_CONFIG_BADCRES_10BIT.getValue(); // 10-bit bus resolution = 0..1023
private static final int ina219_config_badcres_11bit = BusADCResolution.INA219_CONFIG_BADCRES_11BIT.getValue(); // 11-bit bus res = 0..2047
private static final int ina219_config_badcres_12bit = BusADCResolution.INA219_CONFIG_BADCRES_12BIT.getValue(); // 12-bit bus res = 0..4097
/* Shunt ADC Resolution */
private static final int ina219_config_sadcres_mask = ShuntADCResolution.INA219_CONFIG_SADCRES_MASK.getValue(); // Shunt ADC resolution and averaging mask
private static final int ina219_config_sadcres_9bit_1s_84us = ShuntADCResolution.INA219_CONFIG_SADCRES_9BIT_1S_84US.getValue(); // 1 x 9-bit shunt sample
private static final int ina219_config_sadcres_10bit_1s_148us = ShuntADCResolution.INA219_CONFIG_SADCRES_10BIT_1S_148US.getValue(); // 1 x 10-bit shunt sample
private static final int ina219_config_sadcres_11bit_1s_276us = ShuntADCResolution.INA219_CONFIG_SADCRES_11BIT_1S_276US.getValue(); // 1 x 11-bit shunt sample
private static final int ina219_config_sadcres_12bit_1s_532us = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_1S_532US.getValue(); // 1 x 12-bit shunt sample
private static final int ina219_config_sadcres_12bit_2s_1060us = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_2S_1060US.getValue(); // 2 x 12-bit shunt samples averaged together
private static final int ina219_config_sadcres_12bit_4s_2130us = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_4S_2130US.getValue(); // 4 x 12-bit shunt samples averaged together
private static final int ina219_config_sadcres_12bit_8s_4260us = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_8S_4260US.getValue(); // 8 x 12-bit shunt samples averaged together
private static final int ina219_config_sadcres_12bit_16s_8510us = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_16S_8510US.getValue(); // 16 x 12-bit shunt samples averaged together
private static final int ina219_config_sadcres_12bit_32s_17ms = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_32S_17MS.getValue(); // 32 x 12-bit shunt samples averaged together
private static final int ina219_config_sadcres_12bit_64s_34ms = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_64S_34MS.getValue(); // 64 x 12-bit shunt samples averaged together
private static final int ina219_config_sadcres_12bit_128s_69ms = ShuntADCResolution.INA219_CONFIG_SADCRES_12BIT_128S_69MS.getValue(); // 128 x 12-bit shunt samples averaged together
/* Operating Mode */
private static final int ina219_config_mode_mask = OperatingMode.INA219_CONFIG_MODE_MASK.getValue(); // Operating mode mask
private static final int ina219_config_mode_powerdown = OperatingMode.INA219_CONFIG_MODE_POWERDOWN.getValue(); // Powerdown
private static final int ina219_config_mode_svolt_triggered = OperatingMode.INA219_CONFIG_MODE_SVOLT_TRIGGERED.getValue(); // Shunt-Volt Triggered
private static final int ina219_config_mode_bvolt_triggered = OperatingMode.INA219_CONFIG_MODE_BVOLT_TRIGGERED.getValue(); // Bus-Volt Triggered
private static final int ina219_config_mode_sandbvolt_triggered = OperatingMode.INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED.getValue(); // Shunt-Volt/Bus-Volt Triggered
private static final int ina219_config_mode_adcoff = OperatingMode.INA219_CONFIG_MODE_ADCOFF.getValue(); // Analog to Digital Converter OFF
private static final int ina219_config_mode_svolt_continuous = OperatingMode.INA219_CONFIG_MODE_SVOLT_CONTINUOUS.getValue(); // Shunt-Volt Continuous
private static final int ina219_config_mode_bvolt_continuous = OperatingMode.INA219_CONFIG_MODE_BVOLT_CONTINUOUS.getValue(); // Bus-Volt Continuous
private static final int ina219_config_mode_sandbvolt_continuous = OperatingMode.INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS.getValue(); // Shunt-Volt/Bus-Volt Continuous
/* Constructor */
public Ina219() throws IOException
{
try
{
// Connection to the I2C Bus
this.bus = I2CFactory.getInstance(I2CBus.BUS_1);
System.out.println("Connection to bus OK");
// Connection to the I2C Device
this.ina219 = bus.getDevice(address);
System.out.println("Connection to device OK");
}
catch (IOException ioe)
{
System.err.println("Exception during I2C initialization");
System.err.println("Exception : " + ioe.getMessage());
}
}
/**
* Calibrates the sensor by filling the calibration register with default values
* Calibrating the sensor with no possibility for overflow
* Value (0x1000) = 4096 calibrates INA219 for maximum range (but less resolution).
* The power register and current register default to 0 because the calibration register defaults to 0, yielding a zero current value until the calibration register is programmed.
*/
public void calibrate_default() throws IOException
{
System.out.println("Calibrating device using default values ...");
try
{
setCalibration(ina219_calibration_default);
System.out.println("Device successfully calibrated with default values");
// show_calibration();
}
catch (IOException ioe)
{
System.err.println("Exception during default sensor calibration");
System.err.println("Exception : " + ioe.getMessage());
}
}
/**
* Set the sensor calibration register value
* @param calibration The calibration to be set (decimal value)
* @throws IOException Input/Output Exception
*/
public void setCalibration(int calibration) throws IOException
{
// Buffer/Register overflow check
if (calibration >= 0 && calibration <= 65535)
{
try
{
byte[] buffer = ByteBuffer.allocate(4).putInt(calibration).array();
/*
void write(int address, byte[] buffer, int offset, int size) throws IOException
address - local address in the i2c device
buffer - buffer of data to be written to the i2c device in one go
offset - offset in buffer
size - number of bytes to be written
*/
ina219.write(ina219_reg_calibration, buffer, 2, 2);
// System.out.println("Calibration value : " + calibration);
// show_calibration();
}
catch (IOException ioe)
{
System.err.println("Exception during sensor calibration");
System.err.println("Exception : " + ioe.getMessage());
}
}
// Overflow handling
else
{
System.err.println("Calibration value out of range");
}
}
/**
* Configures the sensor by filling the configuration register with default value
* For a full reset of the device (all registers to their default values), please use reset()
*/
public void configure_default() throws IOException
{
int default_config = (0x399F);
byte[] buffer = ByteBuffer.allocate(4).putInt(default_config).array();
System.out.println("Configuring device with default configuration ...");
try
{
ina219.write(ina219_reg_config, buffer, 2, 2);
System.out.println("Device successfully configured with default configuration");
show_configuration();
}
catch (IOException ioe)
{
System.err.println("Exception during default sensor configuration");
System.err.println("Exception : " + ioe.getMessage());
}
}
/**
* Configures the INA219 by filling the configuration register with provided configuration
* @param busVoltageRange 16V range or 32V range (default)
* @param gain From gain 1 --> 40 mV range to gain 1/8 --> 320 mV range (default)
* @param busADCResolution 9 bit to 12 bit bus resolution
* @param shuntADCResolution 9 bit sample to 12 bit averaged resolution
* @param operatingMode S-Volt and/or B-Volt triggered/continuous
*/
public void configure_custom(BusVoltageRange busVoltageRange, Gain gain, BusADCResolution busADCResolution, ShuntADCResolution shuntADCResolution, OperatingMode operatingMode) throws IOException
{
System.out.println("Configuring device with custom configuration ...");
try
{
setBusVoltageRange(busVoltageRange);
setGain(gain);
setBusADCResolution(busADCResolution);
setShuntADCResolution(shuntADCResolution);
setOperatingMode(operatingMode);
System.out.println("Device successfully configurated with custom configuration");
show_configuration();
}
catch (IOException ioe)
{
System.err.println("Exception during custom sensor configuration");
System.err.println("Exception : " + ioe.getMessage());
}
}
/**
* Shows the INA219 configuration register in a user-friendly way
*/
public void show_configuration() throws IOException
{
int reg_config = 0;
String reg_config_string = "";
try
{
reg_config = getConfigurationRegister();
}
catch (IOException ioe)
{
System.err.println("Exception during configuration showing");
System.err.println("Exception : " + ioe.getMessage());
}
System.out.println("Configuration register : ");
System.out.println("RST \t - \t BRNG \t PG1 \t PG0 \t BADC4 \t BADC3 \t BADC2 \t BADC1 \t SADC4 \t SADC3 \t SADC2 \t SADC1 \t MODE3 \t MODE2 \t MODE1");
// Formatting
reg_config_string = String.format("%16s", Integer.toBinaryString(reg_config));
reg_config_string = reg_config_string.replace(' ', '0');
// Showing configuration register
for (int i = 0; i < reg_config_string.length(); i++)
{
System.out.print(reg_config_string.charAt(i) + "\t ");
}
System.out.println();
}
/**
* Shows the INA219 calibration register in a user-friendly way
*/
public void show_calibration() throws IOException
{
int reg_calibration = 0;
String reg_calibration_string = "";
try
{
reg_calibration = getCalibrationRegister();
}
catch (IOException ioe)
{
System.err.println("Exception during calibration showing");
System.err.println("Exception : " + ioe.getMessage());
}
System.out.println("Calibration register : ");
System.out.println("FS15 \t FS14 \t FS13 \t FS12 \t FS11 \t FS10 \t FS9 \t FS8 \t FS7 \t FS6 \t FS5 \t FS4 \t FS3 \t FS2 \t FS1 \t FS0");
// Formatting
reg_calibration_string = String.format("%16s", Integer.toBinaryString(reg_calibration));
reg_calibration_string = reg_calibration_string.replace(' ', '0');
// Showing calibration register
for (int i = 0; i < reg_calibration_string.length(); i++)
{
System.out.print(reg_calibration_string.charAt(i) + "\t ");
}
System.out.println();
}
/**
* Shows the INA219 current register in a user-friendly way
*/
public void show_current_register() throws IOException
{
int reg_current = 0;
String reg_current_string = "";
try
{
reg_current = getCurrentRegister();
}
catch (IOException ioe)
{
System.err.println("Exception during current register showing");
System.err.println("Exception : " + ioe.getMessage());
}
System.out.println("Current register : ");
System.out.println("CSIGN \t CD14 \t CD13 \t CD12 \t CD11 \t CD10 \t CD9 \t CD8 \t CD7 \t CD6 \t CD5 \t CD4 \t CD3 \t CD2 \t CD1 \t CD ");
// Formatting
reg_current_string = String.format("%16s", Integer.toBinaryString(reg_current));
reg_current_string = reg_current_string.replace(' ', '0');
// Showing current register
for (int i = 0; i < reg_current_string.length(); i++)
{
System.out.print(reg_current_string.charAt(i) + "\t ");
}
System.out.println();
}
/**
* Shows the INA219 power register in a user-friendly way
*/
public void show_power_register() throws IOException
{
int reg_power = 0;
String reg_power_string = "";
try
{
reg_power = getPowerRegister();
}
catch (IOException ioe)
{
System.err.println("Exception during power register showing");
System.err.println("Exception : " + ioe.getMessage());
}
System.out.println("Power register : ");
System.out.println("PD15 \t PD14 \t PD13 \t PD12 \t PD11 \t PD10 \t PD9 \t PD8 \t PD7 \t PD6 \t PD5 \t PD4 \t PD3 \t PD2 \t PD1 \t PD0");
// Formatting
reg_power_string = String.format("%16s", Integer.toBinaryString(reg_power));
reg_power_string = reg_power_string.replace(' ', '0');
// Showing power register
for (int i = 0; i < reg_power_string.length(); i++)
{
System.out.print(reg_power_string.charAt(i) + "\t ");
}
System.out.println();
}
/**
* Shows the INA219 shunt voltage register in a user-friendly way
*/
public void show_shunt_voltage_register() throws IOException
{
int reg_shuntvoltage = 0;
int gain_setting = getGainSetting();
String reg_shuntvoltage_string = "";
try
{
reg_shuntvoltage = getShuntVoltageRegister();
}
catch (IOException ioe)
{
System.err.println("Exception during shunt voltage register showing");
System.err.println("Exception : " + ioe.getMessage());
}
System.out.println("Shunt voltage register : ");
// Adpatation to the PGA range selected
switch(gain_setting)
{
// PGA = 1/8
case 4:
System.out.println("SIGN \t SD14 \t SD13 \t SD12 \t SD11 \t SD10 \t SD9 \t SD8 \t SD7 \t SD6 \t SD5 \t SD4 \t SD3 \t SD2 \t SD1 \t SD0");
break;
// PGA = 1/4
case 3:
System.out.println("SIGN \t SIGN \t SD13 \t SD12 \t SD11 \t SD10 \t SD9 \t SD8 \t SD7 \t SD6 \t SD5 \t SD4 \t SD3 \t SD2 \t SD1 \t SD0");
break;
// PGA = 1/2
case 2:
System.out.println("SIGN \t SIGN \t SIGN \t SD12 \t SD11 \t SD10 \t SD9 \t SD8 \t SD7 \t SD6 \t SD5 \t SD4 \t SD3 \t SD2 \t SD1 \t SD0");
break;
// PGA = 1
case 1:
System.out.println("SIGN \t SIGN \t SIGN \t SIGN \t SD11 \t SD10 \t SD9 \t SD8 \t SD7 \t SD6 \t SD5 \t SD4 \t SD3 \t SD2 \t SD1 \t SD0");
break;
default:
System.out.println("Error getting Gain setting : value out of range");
break;
}
// Formatting
reg_shuntvoltage_string = String.format("%16s", Integer.toBinaryString(reg_shuntvoltage));
reg_shuntvoltage_string = reg_shuntvoltage_string.replace(' ', '0');
// Showing shunt voltage register
for (int i = 0; i < reg_shuntvoltage_string.length(); i++)
{
System.out.print(reg_shuntvoltage_string.charAt(i) + "\t ");
}
System.out.println();
}
/**
* Shows the INA219 bus voltage register in a user-friendly way
*/
public void show_bus_voltage_register() throws IOException
{
int reg_busvoltage = 0;
String reg_busvoltage_string = "";
try
{
reg_busvoltage = getBusVoltageRegister();
}
catch (IOException ioe)
{
System.err.println("Exception during bus voltage register showing");
System.err.println("Exception : " + ioe.getMessage());
}
System.out.println("Bus voltage register : ");
System.out.println("BD12 \t BD11 \t BD10 \t BD9 \t BD8 \t BD7 \t BD6 \t BD5 \t BD4 \t BD3 \t BD2 \t BD1 \t BD0 \t - \t CNVR \t OVF");
// Formatting
reg_busvoltage_string = String.format("%16s", Integer.toBinaryString(reg_busvoltage));
reg_busvoltage_string = reg_busvoltage_string.replace(' ', '0');
// Showing bus voltage register
for (int i = 0; i < reg_busvoltage_string.length(); i++)
{
System.out.print(reg_busvoltage_string.charAt(i) + "\t ");
}
System.out.println();
}
/**
* Shows all the INA219 registers in a user-friendly way
*/
public void show_all_registers() throws IOException
{
show_configuration();
show_calibration();
show_shunt_voltage_register();
show_bus_voltage_register();
show_current_register();
show_power_register();
}
/**
* Reads the shunt resistor voltage (Vin+ - Vin-) in V
* @return Shunt resistor voltage in V
*/
public double read_shunt_voltage() throws IOException
{
short shunt_voltage = 0;
int register = 0;
double shunt_voltage_V = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
// int read(int address, byte[] buffer, int offset, int size) throws IOException
// Reading 2 bytes (16 bits) from the register
register = ina219.read(ina219_reg_shuntvoltage, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
shunt_voltage = Ina219In.readShort();
// Conversion in Volts
shunt_voltage_V = shunt_voltage;
// TODO : Adapt divider to configuration / calibration values in accordance to resolution
shunt_voltage_V = twosComplement(shunt_voltage) / 10.0;
System.out.println("Shunt voltage : " + shunt_voltage_V + " V");
}
catch (IOException ioe)
{
System.err.println("Exception during shunt voltage reading");
System.err.println("Exception : " + ioe.getMessage());
}
return shunt_voltage_V;
}
/**
* Reads the bus voltage (Vin- - GND) voltage in V
* @return Bus voltage in V
*/
public double read_bus_voltage() throws IOException
{
int register = 0;
int bus_voltage = 0;
double bus_voltage_V = 0;
String bus_voltage_string_complete = "";
String bus_voltage_string_data = "";
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
// Reading 2 bytes (16 bits) from the register
register = ina219.read(ina219_reg_busvoltage, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
bus_voltage = Ina219In.readShort();
bus_voltage_string_complete = Integer.toString(bus_voltage);
// Truncate the last 3 bits (CNV, OVF)
for (int i = 0; i < bus_voltage_string_complete.length() - 3; i++)
{
bus_voltage_string_data += bus_voltage_string_complete.charAt(i);
}
if (bus_voltage_string_data != "")
{
bus_voltage = Integer.parseInt(bus_voltage_string_data);
}
else
{
bus_voltage = 0;
}
// Conversion in Volts
bus_voltage_V = bus_voltage / 100.0;
System.out.println("Bus voltage : " + bus_voltage_V + " V");
}
catch (IOException ioe)
{
System.err.println("Exception during bus voltage reading");
System.err.println("Exception : " + ioe.getMessage());
}
return bus_voltage_V;
}
/**
* Reads the current flowing through the shunt resistor in A
* Current register defaults to 0 because the calibration register defaults to 0, yielding a zero current value until the calibration register is programmed.
* Call a calibration function before calling read_current()
* @return Current flowing through the shunt resistor in A
*/
public double read_current() throws IOException
{
double current = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
current = ina219.read(ina219_reg_current, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
current = Ina219In.readShort() / 100.0;
System.out.println("Current : " + current + " A");
}
catch (IOException ioe)
{
System.err.println("Exception during current reading");
System.err.println("Exception : " + ioe.getMessage());
}
return current;
}
/**
* Reads the power ((current x bus voltage) / 5000) in W
* Power register defaults to 0 because the calibration register defaults to 0, yielding a zero power value until the calibration register is programmed.
* Call a calibration function before calling read_power()
* @return Power in W
*/
public double read_power() throws IOException
{
double power = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
power = ina219.read(ina219_reg_power, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
power = Ina219In.readShort();
System.out.println("Power : " + power + " W");
}
catch (IOException ioe)
{
System.err.println("Exception during power reading");
System.err.println("Exception : " + ioe.getMessage());
}
return power;
}
/**
* Gets the content of the Configuration register
*/
public int getConfigurationRegister() throws IOException
{
int reg_config = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
reg_config = ina219.read(ina219_reg_config, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
reg_config = Ina219In.readShort();
}
catch (IOException ioe)
{
System.err.println("Exception during configuration register reading");
System.err.println("Exception : " + ioe.getMessage());
}
return reg_config;
}
/**
* Gets the content of the Calibration register
*/
public int getCalibrationRegister() throws IOException
{
int reg_calibration = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
reg_calibration = ina219.read(ina219_reg_calibration, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
reg_calibration = Ina219In.readShort();
}
catch (IOException ioe)
{
System.err.println("Exception during calibration register reading");
System.err.println("Exception : " + ioe.getMessage());
}
return reg_calibration;
}
/**
* Gets the content of the Shunt Voltage register
*/
public int getShuntVoltageRegister() throws IOException
{
int reg_shuntvoltage = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
ina219.read(ina219_reg_shuntvoltage, buffer, 0, 2);
Ina219In = new DataInputStream(new ByteArrayInputStream(buffer));
// Short = 2 bytes = 16 bits
reg_shuntvoltage = Ina219In.readShort();
}
catch (IOException ioe)
{
System.err.println("Exception during shunt voltage register reading");
System.err.println("Exception : " + ioe.getMessage());
}
return reg_shuntvoltage;
}
/**
* Gets the content of the Bus Voltage register
*/
public int getBusVoltageRegister() throws IOException
{
int reg_busvoltage = 0;
byte[] buffer = new byte[2];
DataInputStream Ina219In;
try
{
ina219.read(ina219_reg_busvoltage, buffer, 0, 2);