Library to interface to the TI BQ27441, a fuel gauge monitor
Fork of battery-gauge-bq27441 by
Diff: bq27441.cpp
- Revision:
- 6:998cc334f8f2
- Parent:
- 5:63b325f2c21a
diff -r 63b325f2c21a -r 998cc334f8f2 bq27441.cpp --- a/bq27441.cpp Wed Jun 14 17:11:40 2017 +0100 +++ b/bq27441.cpp Wed Dec 13 17:14:51 2017 +0000 @@ -82,6 +82,29 @@ return success; } +bool BatteryGaugeBq27441::getTwoBytesSigned (uint8_t registerAddress, int16_t *pBytes) +{ + bool success = false; + char data[3]; + + if (gpI2c != NULL) { + // Send a command to read from registerAddress + data[0] = registerAddress; + data[1] = 0; + data[2] = 0; + + if ((gpI2c->write(gAddress, &(data[0]), 1) == 0) && + (gpI2c->read(gAddress, &(data[1]), 2) == 0)) { + success = true; + if (pBytes) { + *pBytes = (((int16_t) data[2]) << 8) + data[1]; + } + } + } + + return success; +} + // Compute the checksum over a block of data. uint8_t BatteryGaugeBq27441::computeChecksum(const char * pData) { @@ -941,28 +964,56 @@ return success; } +// Get the power output of the battery +bool BatteryGaugeBq27441::getPower (int32_t *pPowerMW) +{ + bool success = false; + int16_t data = 0; + + if (gReady && (gpI2c != NULL)) { + // Make sure there's a recent reading + if (gGaugeOn || makeAdcReading()) { + gpI2c->lock(); + // Read from the power register address + if (getTwoBytesSigned (0x18, &data)) { + success = true; + + // The answer is in mW + if (pPowerMW) { + *pPowerMW = (int32_t) data; + } + + } + + // Return to sleep if we are allowed to + if (!gGaugeOn && !setHibernate()) { + success = false; + } + + gpI2c->unlock(); + } + } + + return success; +} + // Get the current flowing from the battery. bool BatteryGaugeBq27441::getCurrent (int32_t *pCurrentMA) { bool success = false; - int32_t currentMA = 0; - uint16_t data; + int16_t data; if (gReady && (gpI2c != NULL)) { // Make sure there's a recent reading if (gGaugeOn || makeAdcReading()) { gpI2c->lock(); // Read from the average current register address - if (getTwoBytes (0x10, &data)) { + if (getTwoBytesSigned (0x10, &data)) { success = true; if (pCurrentMA) { - *pCurrentMA = currentMA; + *pCurrentMA = (int32_t) data; } - -#ifdef DEBUG_BQ27441 - printf("BatteryGaugeBq27441 (I2C 0x%02x): current %d mA.\n", gAddress >> 1, (int) currentMA); -#endif } // Return to sleep if we are allowed to