Skip to content

Commit 7d72da2

Browse files
committed
Adding the ability to clear each status flag after reading
1 parent 465500d commit 7d72da2

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.cpp

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,35 +266,81 @@ uint8_t SFE_MAX1704X::getStatus(void)
266266
return (statusReg & 0x7F); //Highest bit is don't care
267267
}
268268

269-
bool SFE_MAX1704X::isReset(void)
269+
bool SFE_MAX1704X::isReset(bool clear)
270270
{
271271
uint8_t status = getStatus();
272-
return (status & MAX1704x_STATUS_RI);
272+
bool flag = (status & MAX1704x_STATUS_RI) > 0;
273+
if (flag && clear) // Clear the flag if requested
274+
{
275+
// Clear the aligned bit in the status register
276+
clearStatusRegBits(MAX1704x_STATUS_RI << 8);
277+
}
278+
return (flag);
273279
}
274-
bool SFE_MAX1704X::isVoltageHigh(void)
280+
bool SFE_MAX1704X::isVoltageHigh(bool clear)
275281
{
276282
uint8_t status = getStatus();
277-
return (status & MAX1704x_STATUS_VH);
283+
bool flag = (status & MAX1704x_STATUS_VH) > 0;
284+
if (flag && clear) // Clear the flag if requested
285+
{
286+
// Clear the aligned bit in the status register
287+
clearStatusRegBits(MAX1704x_STATUS_VH << 8);
288+
}
289+
return (flag);
278290
}
279-
bool SFE_MAX1704X::isVoltageLow(void)
291+
bool SFE_MAX1704X::isVoltageLow(bool clear)
280292
{
281293
uint8_t status = getStatus();
282-
return (status & MAX1704x_STATUS_VL);
294+
bool flag = (status & MAX1704x_STATUS_VL) > 0;
295+
if (flag && clear) // Clear the flag if requested
296+
{
297+
// Clear the aligned bit in the status register
298+
clearStatusRegBits(MAX1704x_STATUS_VL << 8);
299+
}
300+
return (flag);
283301
}
284-
bool SFE_MAX1704X::isVoltageReset(void)
302+
bool SFE_MAX1704X::isVoltageReset(bool clear)
285303
{
286304
uint8_t status = getStatus();
287-
return (status & MAX1704x_STATUS_VR);
305+
bool flag = (status & MAX1704x_STATUS_VR) > 0;
306+
if (flag && clear) // Clear the flag if requested
307+
{
308+
// Clear the aligned bit in the status register
309+
clearStatusRegBits(MAX1704x_STATUS_VR << 8);
310+
}
311+
return (flag);
288312
}
289-
bool SFE_MAX1704X::isLow(void)
313+
bool SFE_MAX1704X::isLow(bool clear)
290314
{
291315
uint8_t status = getStatus();
292-
return (status & MAX1704x_STATUS_HD);
316+
bool flag = (status & MAX1704x_STATUS_HD) > 0;
317+
if (flag && clear) // Clear the flag if requested
318+
{
319+
// Clear the aligned bit in the status register
320+
clearStatusRegBits(MAX1704x_STATUS_HD << 8);
321+
}
322+
return (flag);
293323
}
294-
bool SFE_MAX1704X::isChange(void)
324+
bool SFE_MAX1704X::isChange(bool clear)
295325
{
296326
uint8_t status = getStatus();
297-
return (status & MAX1704x_STATUS_SC);
327+
bool flag = (status & MAX1704x_STATUS_SC) > 0;
328+
if (flag && clear) // Clear the flag if requested
329+
{
330+
// Clear the aligned bit in the status register
331+
clearStatusRegBits(MAX1704x_STATUS_SC << 8);
332+
}
333+
return (flag);
334+
}
335+
336+
// Clear the specified bit in the MAX17048/49 status register (PRIVATE)
337+
// This requires the bits in mask to be correctly aligned.
338+
// MAX1704x_STATUS_RI etc. will need to be shifted left by 8 bits to become aligned.
339+
uint8_t SFE_MAX1704X::clearStatusRegBits(uint16_t mask)
340+
{
341+
uint16_t statusReg = read16(MAX17048_STATUS);
342+
statusReg &= ~mask; // Clear the specified bits
343+
return (write16(statusReg, MAX17048_STATUS)); // Write the contents back again
298344
}
299345

300346
uint8_t SFE_MAX1704X::clearAlert()

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ typedef enum {
8787
#define MAX1704x_STATUS_VR (1 << 3) // Assumes the MSB has been shifted >> 8
8888
#define MAX1704x_STATUS_HD (1 << 4) // Assumes the MSB has been shifted >> 8
8989
#define MAX1704x_STATUS_SC (1 << 5) // Assumes the MSB has been shifted >> 8
90-
#define MAX1704x_STATUS_EnVR (1 << 14)
90+
#define MAX1704x_STATUS_EnVR (1 << 14) // ** Unshifted **
9191

9292
////////////////////////////////////////
9393
// MAX17043 Command Register Commands //
@@ -160,9 +160,9 @@ class SFE_MAX1704X
160160
// setThreshold([percent]) - Set the MAX17043's percentage threshold that will
161161
// trigger an alert.
162162
// Input: [percent] - Percentage value that will trigger an alert interrupt.
163-
// Any value between 1 and 32 is valid.
163+
// Any value between 1 and 32 is valid. Default value is 0x1C == 4%
164164
// Output: 0 on success, positive integer on fail.
165-
uint8_t setThreshold(uint8_t percent);
165+
uint8_t setThreshold(uint8_t percent = 4);
166166

167167
// sleep() - Set the MAX17043 into sleep mode.
168168
// Output: 0 on success, positive integer on fail.
@@ -257,12 +257,14 @@ class SFE_MAX1704X
257257
uint8_t getStatus();
258258

259259
//(MAX17048/49) Various helper functions to check bits in status register
260-
bool isReset(); //True after POR
261-
bool isVoltageHigh(); //True when VCELL is above VALRTMAX (see setAlertMinVoltage)
262-
bool isVoltageLow();
263-
bool isVoltageReset();
264-
bool isLow(); //True when SOC crosses the value in ATHD (see setThreshold)
265-
bool isChange(); //True when SOC changes by at least 1%
260+
// INPUT: [clear] - If [clear] is true, the alert flag will be cleared if it
261+
// was set.
262+
bool isReset(bool clear = false); //True after POR
263+
bool isVoltageHigh(bool clear = false); //True when VCELL is above VALRTMAX (see setAlertMinVoltage)
264+
bool isVoltageLow(bool clear = false);
265+
bool isVoltageReset(bool clear = false);
266+
bool isLow(bool clear = false); //True when SOC crosses the value in ATHD (see setThreshold)
267+
bool isChange(bool clear = false); //True when SOC changes by at least 1% and SOCAlert is enabled
266268

267269
// getAlert([clear]) - Check if the MAX1704X's ALRT alert interrupt has been
268270
// triggered.
@@ -367,6 +369,12 @@ class SFE_MAX1704X
367369
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.
368370
boolean _printDebug = false; //Flag to print debugging variables
369371

372+
// Clear the specified bit(s) in the MAX17048/49 status register
373+
// This requires the bits in mask to be correctly aligned.
374+
// MAX1704x_STATUS_RI etc. will need to be shifted left by 8 bits to become aligned.
375+
// Output: 0 on success, positive integer on fail.
376+
uint8_t clearStatusRegBits(uint16_t mask);
377+
370378
int _device = MAX1704X_MAX17043; // Default to MAX17043
371379
float _full_scale = 5.12; // Default: full-scale for the MAX17043
372380
};

0 commit comments

Comments
 (0)