-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINA219Base.java
More file actions
139 lines (127 loc) · 4.97 KB
/
Copy pathINA219Base.java
File metadata and controls
139 lines (127 loc) · 4.97 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package powermonitor;
/*
* INA219Base.java
*
* Copyright 2017 Greg Steckman
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and limitations under the License.
*
*/
import java.io.IOException;
/**
* Base class for the INA219 driver implementation. Register read/writes are abstracted using the INA219RegisterIF to allow testing without an I2C device present.
*
*/
class INA219Base {
private static final double SHUNT_VOLTAGE_LSB = 10e-6;
private static final double BUS_VOLTAGE_LSB = 4e-3;
private static final int POWER_LSB_SCALE = 20;
private double rShunt;
private double currentLSB;
private int cal;
private INA219RegisterIF register;
/**
* Constructs an INA219Base with the specified values.
*
* @param reg
* Register interface to use for read/write access.
* @param shuntResistance
* Value in ohms of the current sense shunt resistor.
* @param maxExpectedCurrent
* Maximum expected current, in Amps.
* @param busVoltageRange
* Either 16V or 32V.
* @param pga
* Gain range.
* @param badc
* Bus voltage ADC sample size and averaging setting.
* @param sadc
* Shunt resistor voltage ADC sample size and averaging setting.
* @throws IOException
* If the configuration or calibration registers cannot be written.
*/
public INA219Base(final INA219RegisterIF reg, final double shuntResistance, final double maxExpectedCurrent,
final INA219.Brng busVoltageRange, final INA219.Pga pga, final INA219.Adc badc, final INA219.Adc sadc)
throws IOException {
register = reg;
rShunt = shuntResistance;
currentLSB = (maxExpectedCurrent / 32768);
cal = (int) (((0.04096 * 32768) / (maxExpectedCurrent * rShunt)));
configure(busVoltageRange, pga, badc, sadc);
register.writeRegister(RegisterAddress.CALIBRATION, cal);
}
/**
* Reads and returns the shunt voltage.
*
* @return The shunt voltage.
* @throws IOException
* If the shunt voltage register could not be read.
*/
public double getShuntVoltage() throws IOException {
int rval = register.readSignedRegister(RegisterAddress.SHUNT_VOLTAGE);
return rval * SHUNT_VOLTAGE_LSB;
}
/**
* Reads and returns the bus voltage.
*
* @return The bus voltage.
* @throws IOException
* If the bus voltage register could not be read.
*/
public double getBusVoltage() throws IOException {
int rval = register.readRegister(RegisterAddress.BUS_VOLTAGE);
return (rval >> 3) * BUS_VOLTAGE_LSB;
}
/**
* Reads and returns the power.
*
* @return The power value.
* @throws IOException
* If the power register could not be read.
*/
public double getPower() throws IOException {
int rval = register.readRegister(RegisterAddress.POWER);
return rval * POWER_LSB_SCALE * currentLSB;
}
/**
* Reads and returns the current.
*
* @return The current value.
* @throws IOException
* If the current register could not be read.
*/
public double getCurrent() throws IOException {
int rval = register.readSignedRegister(RegisterAddress.CURRENT);
return rval * currentLSB;
}
/**
* Configures the INA219 with the specified parameters.
*
* @param busVoltageRange
* Bus voltage range to write to the configuration register.
* @param pga
* Gain range to write to the configuration register.
* @param badc
* Bus voltage ADC setting to write to the configuration register.
* @param sadc
* Shunt voltage ADC setting to write to the configuration register.
* @throws IOException
* If the configuration register could not be written.
*/
private void configure(final INA219.Brng busVoltageRange, final INA219.Pga pga, final INA219.Adc badc,
final INA219.Adc sadc) throws IOException {
int regValue = (busVoltageRange.getValue() << 13) | (pga.getValue() << 11) | (badc.getValue() << 7)
| (sadc.getValue() << 3) | 0x7;
register.writeRegister(RegisterAddress.CONFIGURATION, regValue);
}
}