Skip to content

Commit e2d1729

Browse files
committed
Add min max current functions
1 parent e1d9a6a commit e2d1729

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

examples/Battery/Battery.ino

+25-18
Original file line numberDiff line numberDiff line change
@@ -58,52 +58,59 @@ void setup() {
5858
}
5959

6060
battery.resetMaximumMinimumVoltage();
61+
battery.resetMaximumMinimumCurrent();
6162
}
6263

6364
void printTimeToEmpty(){
6465
auto timeToEmptySeconds = battery.timeToEmpty();
6566
if(timeToEmptySeconds == -1){
66-
Serial.println("* Time to empty: N/A");
67+
Serial.println("* ⏱️ Time to empty: N/A");
6768
return;
6869
}
6970
auto timeToEmptyHours = timeToEmptySeconds / 3600;
7071
timeToEmptySeconds = timeToEmptySeconds % 3600;
7172
auto timeToEmptyMinutes = timeToEmptySeconds / 60;
7273
timeToEmptySeconds = timeToEmptySeconds % 60;
73-
Serial.println("* Time to empty: " + String(timeToEmptyHours) + "h " + String(timeToEmptyMinutes) + "m " + String(timeToEmptySeconds) + "s");
74+
Serial.println("* ⏱️ Time to empty: " + String(timeToEmptyHours) + "h " + String(timeToEmptyMinutes) + "m " + String(timeToEmptySeconds) + "s");
7475
}
7576

7677
void printTimeToFull(){
7778
auto timeToFullSeconds = battery.timeToFull();
7879
if(timeToFullSeconds == -1){
79-
Serial.println("* Time to full: N/A");
80+
Serial.println("* ⏱️ Time to full: N/A");
8081
return;
8182
}
8283
auto timeToFullHours = timeToFullSeconds / 3600;
8384
timeToFullSeconds = timeToFullSeconds % 3600;
8485
auto timeToFullMinutes = timeToFullSeconds / 60;
8586
timeToFullSeconds = timeToFullSeconds % 60;
86-
Serial.println("* Time to full: " + String(timeToFullHours) + "h " + String(timeToFullMinutes) + "m " + String(timeToFullSeconds) + "s");
87+
Serial.println("* ⏱️ Time to full: " + String(timeToFullHours) + "h " + String(timeToFullMinutes) + "m " + String(timeToFullSeconds) + "s");
8788
}
8889

8990
void loop() {
9091
bool batteryConnected = battery.isConnected();
91-
Serial.println("* Battery is connected: " + ( batteryConnected ? String("Yes") : String("No")));
92+
Serial.println("* 🔌 Battery is connected: " + ( batteryConnected ? String("Yes") : String("No")));
9293

9394
if(batteryConnected){
94-
Serial.println("* Battery is empty: " + ( battery.isEmpty() ? String("Yes") : String("No")));
95-
Serial.println("* Is fully charged: " + ( battery.isFullyCharged() ? String("Yes") : String("No")));
96-
Serial.println("* Voltage: " + String(battery.voltage()) + " V");
97-
Serial.println("* Average Voltage: " + String(battery.averageVoltage()) + " V");
98-
Serial.println("* Minimum Voltage: " + String(battery.minimumVoltage()) + " V");
99-
Serial.println("* Maximum Voltage: " + String(battery.maximumVoltage()) + " V");
100-
Serial.println("* Current: " + String(battery.current()) + " mA");
101-
Serial.println("* Average Current: " + String(battery.averageCurrent()) + " mA");
102-
Serial.println("* Percentage: " + String(battery.percentage()) + "%");
103-
Serial.println("* Remaining Capacity: " + String(battery.remainingCapacity()) + " mAh");
104-
Serial.println("* Full Capacity: " + String(battery.fullCapacity()) + " mAh");
105-
Serial.println("* Internal Temperature: " + String(battery.internalTemperature()) + "°C");
106-
Serial.println("* Average internal Temperature: " + String(battery.averageInternalTemperature()) + "°C");
95+
Serial.println("* 🪫 Battery is empty: " + ( battery.isEmpty() ? String("Yes") : String("No")));
96+
Serial.println("* 💯 Charging complete: " + ( battery.chargingComplete() ? String("Yes") : String("No")));
97+
98+
Serial.println("* ⚡️ Voltage: " + String(battery.voltage()) + " V");
99+
Serial.println("* ⚡️ Average Voltage: " + String(battery.averageVoltage()) + " V");
100+
Serial.println("* ⚡️ Minimum Voltage since reset: " + String(battery.minimumVoltage()) + " V");
101+
Serial.println("* ⚡️ Maximum Voltage since reset: " + String(battery.maximumVoltage()) + " V");
102+
Serial.println("* ⚡️ Current: " + String(battery.current()) + " mA");
103+
Serial.println("* ⚡️ Average Current: " + String(battery.averageCurrent()) + " mA");
104+
Serial.println("* ⚡️ Minimum Current since reset (160mA resolution): " + String(battery.minimumCurrent()) + " mA");
105+
Serial.println("* ⚡️ Maximum Current since reset (160mA resolution): " + String(battery.maximumCurrent()) + " mA");
106+
Serial.println("* ⚡️ Power: " + String(battery.power()) + " mW");
107+
Serial.println("* ⚡️ Average Power: " + String(battery.averagePower()) + " mW");
108+
109+
Serial.println("* 🔋 Percentage: " + String(battery.percentage()) + "%");
110+
Serial.println("* 🔋 Remaining Capacity: " + String(battery.remainingCapacity()) + " mAh");
111+
Serial.println("* 🔋 Full Capacity: " + String(battery.fullCapacity()) + " mAh");
112+
Serial.println("* 🌡️ Internal Temperature: " + String(battery.internalTemperature()) + "°C");
113+
Serial.println("* 🌡️ Average internal Temperature: " + String(battery.averageInternalTemperature()) + "°C");
107114
printTimeToEmpty();
108115
printTimeToFull();
109116
Serial.println();

src/Battery.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,39 @@ int16_t Battery::averageCurrent(){
255255
return (int16_t)readRegister16Bits(this->wire, FUEL_GAUGE_ADDRESS, AVG_CURRENT_REG) * CURRENT_MULTIPLIER_MA;
256256
}
257257

258+
int16_t Battery::minimumCurrent(){
259+
if(!isConnected()){
260+
return -1;
261+
}
262+
263+
uint16_t registerValue = readRegister16Bits(this->wire, FUEL_GAUGE_ADDRESS, MAXMIN_CURRENT_REG);
264+
if(registerValue == MAXMIN_CURRENT_INITIAL_VALUE){
265+
return 0; // The minimum current is not valid
266+
}
267+
268+
int8_t lowerByte = static_cast<int8_t>( registerValue & 0xFF);
269+
return static_cast<int16_t>(lowerByte * MAXMIN_CURRENT_MULTIPLIER_MA);
270+
}
271+
272+
int16_t Battery::maximumCurrent(){
273+
if(!isConnected()){
274+
return -1;
275+
}
276+
277+
uint16_t registerValue = readRegister16Bits(this->wire, FUEL_GAUGE_ADDRESS, MAXMIN_CURRENT_REG);
278+
if(registerValue == MAXMIN_CURRENT_INITIAL_VALUE){
279+
return 0; // The minimum current is not valid
280+
}
281+
282+
// The maximum current is stored in the upper byte
283+
int8_t upperByte = static_cast<int8_t>( registerValue >> 8);
284+
return static_cast<int16_t>(upperByte * MAXMIN_CURRENT_MULTIPLIER_MA);
285+
}
286+
287+
bool Battery::resetMaximumMinimumCurrent(){
288+
return writeRegister16Bits(this->wire, FUEL_GAUGE_ADDRESS, MAXMIN_CURRENT_REG, MAXMIN_CURRENT_INITIAL_VALUE) == 0;
289+
}
290+
258291
int16_t Battery::power(){
259292
if(!isConnected()){
260293
return -1;

src/Battery.h

+21
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ class Battery {
109109
*/
110110
int16_t averageCurrent();
111111

112+
/**
113+
* @brief Reads the minimum current values measured since the last device reset.
114+
* Note: The resolution of the minimum current value is 160mA so the value
115+
* is rounded to the nearest 160mA.
116+
* @return The minimum current values in milli amperes (mA).
117+
*/
118+
int16_t minimumCurrent();
119+
120+
/**
121+
* @brief Reads the maximum current values measured since the last device reset.
122+
* Note: The resolution of the minimum current value is 160mA so the value
123+
* is rounded to the nearest 160mA.
124+
* @return The maximum current values in milli amperes (mA).
125+
*/
126+
int16_t maximumCurrent();
127+
128+
/**
129+
* @brief Resets the minimum and maximum current values.
130+
* @return True if the minimum and maximum current values were successfully reset, false otherwise.
131+
*/
132+
bool resetMaximumMinimumCurrent();
112133

113134
/**
114135
* @brief Reads the current power of the battery in milliwatts (mW).

src/BatteryConstants.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
// Initial Values (for resetting registers)
66
#define MAXMIN_VOLT_INITIAL_VALUE 0x00FF
7+
#define MAXMIN_CURRENT_INITIAL_VALUE 0x807F
78

89
// Conversion factors (See section "ModelGauge m5 Register Standard Resolutions" in the datasheet)
910
#define VOLTAGE_MULTIPLIER_MV (1.25 / 16) // Resolution: 78.125 μV per LSB
1011
#define CURRENT_MULTIPLIER_MA 0.15625 // Resolution: 0.15625 mA per LSB for MAX17262R
12+
#define MAXMIN_CURRENT_MULTIPLIER_MA 160 // Resolution: 160mA per LSB
1113
#define CAPACITY_MULTIPLIER_MAH 0.5 // Resolution: 0.5mAh per LSB for MAX17262R
1214
#define PERCENTAGE_MULTIPLIER (1.0 / 256) // Resolution: 1/256% per LSB
1315
#define TIME_MULTIPLIER_S 5.625 // Resolution: 5.625 seconds per LSB
@@ -49,6 +51,7 @@
4951
#define AT_RATE_REG 0x04
5052
#define CURRENT_REG 0x0A // The MAX17262 uses internal current sensing to monitor the current through the SYS pin. The measurement value is stored in two's-complement format.
5153
#define AVG_CURRENT_REG 0x0B // The AvgCurrent register reports an average of Current register readings.
54+
#define MAXMIN_CURRENT_REG 0x1C // The MaxMinCurr register maintains the maximum and minimum Current register values since the last IC reset or until cleared by host software.
5255
#define TTE_REG 0x11 // The TTE register holds the estimated time to empty for the application under present temperature and load conditions. TTE register is only valid when current register is negative.
5356
#define TTF_REG 0x20 // The TTF register holds the estimated time to full for the application under present conditions. The TTF register is only valid when the current register is positive.
5457
#define TIMER_REG 0x3E

0 commit comments

Comments
 (0)