Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ boolean SFE_MAX1704X::begin(TwoWire &wirePort)

if (isConnected() == false)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("begin: isConnected returned false"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (false);
}

Expand Down Expand Up @@ -87,20 +89,24 @@ boolean SFE_MAX1704X::isConnected(void)
else
{
retries--;
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("SFE_MAX1704X::isConnected: retrying..."));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
delay(50);
}
}

if (!success) // Return now if the version could not be read
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("SFE_MAX1704X::isConnected: failed to detect IC!"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (success);
}

Expand All @@ -120,13 +126,17 @@ boolean SFE_MAX1704X::isConnected(void)
//Enable or disable the printing of debug messages
void SFE_MAX1704X::enableDebugging(Stream &debugPort)
{
#if MAX1704X_ENABLE_DEBUGLOG
_debugPort = &debugPort; //Grab which port the user wants us to use for debugging
_printDebug = true; //Should we print the commands we send? Good for debugging
#endif // if MAX1704X_ENABLE_DEBUGLOG
}

void SFE_MAX1704X::disableDebugging(void)
{
#if MAX1704X_ENABLE_DEBUGLOG
_printDebug = false; //Turn off extra print statements
#endif // if MAX1704X_ENABLE_DEBUGLOG
}

uint8_t SFE_MAX1704X::quickStart()
Expand Down Expand Up @@ -192,10 +202,12 @@ uint8_t SFE_MAX1704X::getID()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getID: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand All @@ -209,10 +221,12 @@ uint8_t SFE_MAX1704X::setResetVoltage(uint8_t threshold)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("setResetVoltage: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -233,10 +247,12 @@ uint8_t SFE_MAX1704X::getResetVoltage(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getResetVoltage: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand All @@ -248,10 +264,12 @@ uint8_t SFE_MAX1704X::enableComparator(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("enableComparator: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -264,10 +282,12 @@ uint8_t SFE_MAX1704X::disableComparator(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("disableComparator: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -280,10 +300,12 @@ float SFE_MAX1704X::getChangeRate(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getChangeRate: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0.0);
}

Expand All @@ -296,10 +318,12 @@ uint8_t SFE_MAX1704X::getStatus(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getStatus: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand Down Expand Up @@ -421,10 +445,12 @@ bool SFE_MAX1704X::enableSOCAlert()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("enableSOCAlert: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (false);
}

Expand All @@ -446,10 +472,12 @@ bool SFE_MAX1704X::disableSOCAlert()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("disableSOCAlert: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (false);
}

Expand All @@ -473,10 +501,12 @@ uint8_t SFE_MAX1704X::enableAlert(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("enableAlert: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -489,10 +519,12 @@ uint8_t SFE_MAX1704X::disableAlert(void)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("disableAlert: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand Down Expand Up @@ -552,10 +584,12 @@ uint8_t SFE_MAX1704X::sleep()
uint16_t configReg = read16(MAX17043_CONFIG);
if (configReg & MAX17043_CONFIG_SLEEP)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("sleep: MAX17043 is already sleeping!"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return MAX17043_GENERIC_ERROR; // Already sleeping, do nothing but return an error
}

Expand All @@ -570,10 +604,12 @@ uint8_t SFE_MAX1704X::wake()
uint16_t configReg = read16(MAX17043_CONFIG);
if (!(configReg & MAX17043_CONFIG_SLEEP))
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("wake: MAX17043 is already awake!"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return MAX17043_GENERIC_ERROR; // Already sleeping, do nothing but return an error
}
configReg &= ~MAX17043_CONFIG_SLEEP; // Clear sleep bit
Expand Down Expand Up @@ -640,10 +676,12 @@ uint8_t SFE_MAX1704X::setVALRTMax(uint8_t threshold)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("setVALRTMax: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -662,10 +700,12 @@ uint8_t SFE_MAX1704X::getVALRTMax()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getVALRTMax: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand All @@ -678,10 +718,12 @@ uint8_t SFE_MAX1704X::setVALRTMin(uint8_t threshold)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("setVALRTMin: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -700,10 +742,12 @@ uint8_t SFE_MAX1704X::getVALRTMin()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getVALRTMin: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand All @@ -716,10 +760,12 @@ bool SFE_MAX1704X::isHibernating()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("isHibernating: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (false);
}

Expand All @@ -731,10 +777,12 @@ uint8_t SFE_MAX1704X::getHIBRTActThr()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getHIBRTActThr: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand All @@ -747,10 +795,12 @@ uint8_t SFE_MAX1704X::setHIBRTActThr(uint8_t threshold)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("setHIBRTActThr: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -770,10 +820,12 @@ uint8_t SFE_MAX1704X::getHIBRTHibThr()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("getHIBRTHibThr: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (0);
}

Expand All @@ -786,10 +838,12 @@ uint8_t SFE_MAX1704X::setHIBRTHibThr(uint8_t threshold)
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("setHIBRTHibThr: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -809,10 +863,12 @@ uint8_t SFE_MAX1704X::enableHibernate()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("enableHibernate: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand All @@ -823,10 +879,12 @@ uint8_t SFE_MAX1704X::disableHibernate()
{
if (_device <= MAX1704X_MAX17044)
{
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("disableHibernate: not supported on this device"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
return (MAX17043_GENERIC_ERROR);
}

Expand Down Expand Up @@ -867,19 +925,23 @@ uint16_t SFE_MAX1704X::read16(uint8_t address)
else
{
retries--;
#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
_debugPort->println(F("SFE_MAX1704X::read16: retrying..."));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG
delay(50);
}
}

#if MAX1704X_ENABLE_DEBUGLOG
if (_printDebug == true)
{
if (!success)
_debugPort->println(F("SFE_MAX1704X::read16: failed to read data!"));
}
#endif // if MAX1704X_ENABLE_DEBUGLOG

return (result);
}
Loading