diff --git a/client/libs/LC7090203F/LC709203F.cpp b/client/libs/LC7090203F/LC709203F.cpp index 7d93d52888e83f29a5e20d052d489d30cdc76dc1..037130060b547c019b1ba30e8769f9d52c3f51e0 100644 --- a/client/libs/LC7090203F/LC709203F.cpp +++ b/client/libs/LC7090203F/LC709203F.cpp @@ -31,22 +31,21 @@ LC709203F::LC709203F(void) {} LC709203F::~LC709203F(void) {} -uint8_t i2c_address = LC709203F_I2C_ADDR ; +uint8_t i2c_address = LC709203F_I2C_ADDR; /*! * @brief Sets up the hardware and initializes I2C * @param * @return True if initialization was successful, otherwise false. */ -bool LC709203F::begin( void ) -{ - Wire.begin(); - setPowerMode(LC709203F_POWER_OPERATE); - setCellCapacity(LC709203F_APA_1000MAH); - setTemperatureMode(LC709203F_TEMPERATURE_I2C); - setCellProfile(LC709203_NOM3p7_Charge4p2); - - return true; +bool LC709203F::begin(void) { + Wire.begin(); + setPowerMode(LC709203F_POWER_OPERATE); + setCellCapacity(LC709203F_APA_1000MAH); + setTemperatureMode(LC709203F_TEMPERATURE_I2C); + setCellProfile(LC709203_NOM3p7_Charge4p2); + + return true; } @@ -54,11 +53,10 @@ bool LC709203F::begin( void ) * @brief Get IC version * @return 16-bit value read from LC709203F_RO_ICVERSION registers */ -uint16_t LC709203F::getICversion(void) -{ - uint16_t vers = 0; - vers = read16(LC709203F_RO_ICVERSION); - return vers; +uint16_t LC709203F::getICversion(void) { + uint16_t vers = 0; + vers = read16(LC709203F_RO_ICVERSION); + return vers; } @@ -66,9 +64,8 @@ uint16_t LC709203F::getICversion(void) * @brief Initialize the RSOC algorithm * @return */ -void LC709203F::initRSOC(void) -{ - write16(LC709203F_WO_INITRSOC, 0xAA55); +void LC709203F::initRSOC(void) { + write16(LC709203F_WO_INITRSOC, 0xAA55); } @@ -76,11 +73,10 @@ void LC709203F::initRSOC(void) * @brief Get battery voltage * @return Cell voltage in milliVolt */ -uint16_t LC709203F::cellVoltage_mV(void) -{ - uint16_t mV = 0; - mV = read16(LC709203F_RO_CELLVOLTAGE); - return 1000 * ( mV / 1000.0 ) ; +uint16_t LC709203F::cellVoltage_mV(void) { + uint16_t mV = 0; + mV = read16(LC709203F_RO_CELLVOLTAGE); + return 1000 * (mV / 1000.0); } @@ -88,22 +84,20 @@ uint16_t LC709203F::cellVoltage_mV(void) * @brief Get cell remaining charge in percent (0-100%) * @return point value from 0 to 1000 */ -uint16_t LC709203F::cellRemainingPercent10(void) -{ - uint16_t percent = 0; - percent = read16(LC709203F_RO_ITE ); - return percent ; +uint16_t LC709203F::cellRemainingPercent10(void) { + uint16_t percent = 0; + percent = read16(LC709203F_RO_ITE); + return percent; } /*! * @brief Get battery state of charge in percent (0-100%) * @return point value from 0 to 100 */ -uint16_t LC709203F::cellStateOfCharge(void) -{ - uint16_t percent = 0; - percent = read16(LC709203F_RW_RSOC ); - return percent ; +uint16_t LC709203F::cellStateOfCharge(void) { + uint16_t percent = 0; + percent = read16(LC709203F_RW_RSOC); + return percent; } @@ -111,11 +105,10 @@ uint16_t LC709203F::cellStateOfCharge(void) * @brief Get battery thermistor temperature * @return value from -20 to 60 *C // CdB Needs testing, no thermistor on ESP32_Bat_R2 board */ -uint16_t LC709203F::getCellTemperature(void) -{ - uint16_t temp = 0; - temp = read16(LC709203F_RW_CELLTEMPERATURE ); - return temp ; +uint16_t LC709203F::getCellTemperature(void) { + uint16_t temp = 0; + temp = read16(LC709203F_RW_CELLTEMPERATURE); + return temp; } @@ -124,9 +117,8 @@ uint16_t LC709203F::getCellTemperature(void) * @param t The desired mode: LC709203F_TEMPERATURE_I2C or * LC709203F_TEMPERATURE_THERMISTOR */ -void LC709203F::setTemperatureMode(lc709203_tempmode_t t) -{ - return write16(LC709203F_RW_STATUSBIT, (uint16_t)t); +void LC709203F::setTemperatureMode(lc709203_tempmode_t t) { + return write16(LC709203F_RW_STATUSBIT, (uint16_t) t); } @@ -134,9 +126,8 @@ void LC709203F::setTemperatureMode(lc709203_tempmode_t t) * @brief Set the cell capacity, * @param apa The lc709203_adjustment_t enumerated approximate cell capacity */ -void LC709203F::setCellCapacity(lc709203_adjustment_t apa) -{ - write16(LC709203F_RW_APA, (uint16_t)apa); +void LC709203F::setCellCapacity(lc709203_adjustment_t apa) { + write16(LC709203F_RW_APA, (uint16_t) apa); } @@ -144,9 +135,8 @@ void LC709203F::setCellCapacity(lc709203_adjustment_t apa) * @brief Set the alarm pin to respond to an RSOC percentage level * @param percent The threshold value, set to 0 to disable alarm */ -void LC709203F::setAlarmRSOC(uint8_t percent) -{ - write16(LC709203F_RW_ALARMRSOC, percent); +void LC709203F::setAlarmRSOC(uint8_t percent) { + write16(LC709203F_RW_ALARMRSOC, percent); } @@ -154,9 +144,8 @@ void LC709203F::setAlarmRSOC(uint8_t percent) * @brief Set the alarm pin to respond to a battery voltage level * @param voltage The threshold value, set to 0 to disable alarm */ -void LC709203F::setAlarmVoltage(float voltage) -{ - write16(LC709203F_RW_ALARMVOLT, voltage * 1000); +void LC709203F::setAlarmVoltage(float voltage) { + write16(LC709203F_RW_ALARMVOLT, voltage * 1000); } @@ -166,9 +155,8 @@ void LC709203F::setAlarmVoltage(float voltage) * @param t The power mode desired * @return */ -void LC709203F::setPowerMode(lc709203_powermode_t t) -{ - write16(LC709203F_RW_POWERMODE, (uint16_t)t); +void LC709203F::setPowerMode(lc709203_powermode_t t) { + write16(LC709203F_RW_POWERMODE, (uint16_t) t); } /*! @@ -176,20 +164,18 @@ void LC709203F::setPowerMode(lc709203_powermode_t t) * @param t The profile, Table 8. Normally 1 for 3.7 nominal 4.2V Full carge cells * @return */ -void LC709203F::setCellProfile(lc709203_cell_profile_t t) -{ - write16(LC709203F_RW_PROFILE, (uint16_t)t); +void LC709203F::setCellProfile(lc709203_cell_profile_t t) { + write16(LC709203F_RW_PROFILE, (uint16_t) t); } /*! * @brief Get the thermistor Beta value //For completeness since we have to write it. * @return The uint16_t Beta value */ -uint16_t LC709203F::getThermistorBeta(void) -{ - uint16_t val = 0; - val = read16(LC709203F_RW_THERMISTORB); - return val; +uint16_t LC709203F::getThermistorBeta(void) { + uint16_t val = 0; + val = read16(LC709203F_RW_THERMISTORB); + return val; } @@ -198,20 +184,18 @@ uint16_t LC709203F::getThermistorBeta(void) * @param b The value to set it to * @return */ -void LC709203F::setThermistorB(uint16_t beta) -{ - write16(LC709203F_RW_THERMISTORB, beta); +void LC709203F::setThermistorB(uint16_t beta) { + write16(LC709203F_RW_THERMISTORB, beta); } -std::list<Message> LC709203F::buildMessages() -{ - std::list<Message> messages; - float data =cellVoltage_mV()/1000.0; - //FIXME: use enum instead of string - MeasurementData IncrementData{data, 0, {},"Voltage-Batterie"}; - messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds()); - return messages; +std::list<Message> LC709203F::buildMessages() { + std::list<Message> messages; + float data = cellVoltage_mV() / 1000.0; + MeasurementData IncrementData{data, 0, {}, + measurementTypeToString.at(MeasurementType::BATTERY_VOLTAGE)}; + messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds()); + return messages; } @@ -231,56 +215,53 @@ std::list<Message> LC709203F::buildMessages() * * @return The computed CRC8 value. */ -static uint8_t crc8(uint8_t *data, int len) -{ - const uint8_t POLYNOMIAL(0x07); - uint8_t crc(0x00); +static uint8_t crc8(uint8_t *data, int len) { + const uint8_t POLYNOMIAL(0x07); + uint8_t crc(0x00); - for (int j = len; j; --j) { - crc ^= *data++; + for (int j = len; j; --j) { + crc ^= *data++; - for (int i = 8; i; --i) { - crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); + for (int i = 8; i; --i) { + crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); + } } - } - return crc; + return crc; } // writes a 16-bit word (d) to register pointer regAddress // when selecting a register pointer to read from, data = 0 -void LC709203F::write16(uint8_t regAddress, uint16_t data) -{ - // Setup array to hold bytes to send including CRC-8 - uint8_t crcArray[5]; - crcArray[0] = 0x16; - crcArray[1] = regAddress; - crcArray[2] = lowByte(data); - crcArray[3] = highByte(data); - // Calculate crc of preceding four bytes and place in crcArray[4] - crcArray[4] = crc8( crcArray, 4 ); - // Device address - Wire.beginTransmission(i2c_address); - // Register address - Wire.write(regAddress); - // low byte - Wire.write(crcArray[2]); - // high byte - Wire.write(crcArray[3]); - // Send crc8 - Wire.write(crcArray[4]); - Wire.endTransmission(); +void LC709203F::write16(uint8_t regAddress, uint16_t data) { + // Setup array to hold bytes to send including CRC-8 + uint8_t crcArray[5]; + crcArray[0] = 0x16; + crcArray[1] = regAddress; + crcArray[2] = lowByte(data); + crcArray[3] = highByte(data); + // Calculate crc of preceding four bytes and place in crcArray[4] + crcArray[4] = crc8(crcArray, 4); + // Device address + Wire.beginTransmission(i2c_address); + // Register address + Wire.write(regAddress); + // low byte + Wire.write(crcArray[2]); + // high byte + Wire.write(crcArray[3]); + // Send crc8 + Wire.write(crcArray[4]); + Wire.endTransmission(); } -int16_t LC709203F::read16( uint8_t regAddress) -{ - int16_t data = 0; - Wire.beginTransmission(i2c_address); - Wire.write(regAddress); - Wire.endTransmission(false); - Wire.requestFrom(i2c_address, 2); - uint8_t lowByteData = Wire.read(); - uint8_t highByteData = Wire.read(); - data = word(highByteData, lowByteData); - return( data ); +int16_t LC709203F::read16(uint8_t regAddress) { + int16_t data = 0; + Wire.beginTransmission(i2c_address); + Wire.write(regAddress); + Wire.endTransmission(false); + Wire.requestFrom(i2c_address, static_cast<uint8_t>(2)); + uint8_t lowByteData = Wire.read(); + uint8_t highByteData = Wire.read(); + data = word(highByteData, lowByteData); + return (data); } diff --git a/client/libs/LC7090203F/LC709203F.h b/client/libs/LC7090203F/LC709203F.h index c776d9b387139886d5083d91563447ac6a139b0b..75ed26c319f43810ea13e331a9b28854d3611081 100644 --- a/client/libs/LC7090203F/LC709203F.h +++ b/client/libs/LC7090203F/LC709203F.h @@ -19,54 +19,54 @@ // Registers per datasheet Table 6 #define LC709203F_WO_BEFORE_RSOC 0x04 // Executes RSOC initialization with sampled maximum voltage when 0xAA55 is set -#define LC709203F_RW_THERMISTORB 0x06 // Sets B−constant of the thermistor to be measured. -#define LC709203F_WO_INITRSOC 0x07 // Executes RSOC initialization when 0xAA55 is set. -#define LC709203F_RW_CELLTEMPERATURE 0x08 // Read or Write Cell Temperature For ESP32-Bar-Rev2 must write temperature, no thermistor on board -#define LC709203F_RO_CELLVOLTAGE 0x09 // Read cell voltage +#define LC709203F_RW_THERMISTORB 0x06 // Sets B−constant of the thermistor to be measured. +#define LC709203F_WO_INITRSOC 0x07 // Executes RSOC initialization when 0xAA55 is set. +#define LC709203F_RW_CELLTEMPERATURE 0x08 // Read or Write Cell Temperature For ESP32-Bar-Rev2 must write temperature, no thermistor on board +#define LC709203F_RO_CELLVOLTAGE 0x09 // Read cell voltage #define LC709203F_RW_CURRENTDIRECTION 0x0A // Selects Auto/Charge/Discharge mode 0x0000: Auto mode, 0x0001: Charge mode, 0xFFFF: Discharge mode -#define LC709203F_RW_APA 0x0B // APA Register, Table 7 +#define LC709203F_RW_APA 0x0B // APA Register, Table 7 #define LC709203F_RW_APTHERMISTOR 0x0C // Sets a value to adjust temperature measurement delay timing. Defaults to0x001E #define LC709203F_RW_RSOC 0x0D // Read cell indicator to empty in 1% steps -#define LC709203F_RO_ITE 0x0F // Read cell indicator to empty in 0.1% steps -#define LC709203F_RO_ICVERSION 0x11 // Contains the ID number of the IC -#define LC709203F_RW_PROFILE 0x12 // Adjusts the battery profile for nominal and fully charged voltages, see Table 8 -#define LC709203F_RW_ALARMRSOC 0x13 // Alarm on percent threshold -#define LC709203F_RW_ALARMVOLT 0x14 // Alarm on voltage threshold -#define LC709203F_RW_POWERMODE 0x15 // Sets sleep/power mode 0x0001: Operational mode 0x0002: Sleep mode -#define LC709203F_RW_STATUSBIT 0x16 // Temperature method, 0x0000: I2C mode, 0x0001: Thermistor mode +#define LC709203F_RO_ITE 0x0F // Read cell indicator to empty in 0.1% steps +#define LC709203F_RO_ICVERSION 0x11 // Contains the ID number of the IC +#define LC709203F_RW_PROFILE 0x12 // Adjusts the battery profile for nominal and fully charged voltages, see Table 8 +#define LC709203F_RW_ALARMRSOC 0x13 // Alarm on percent threshold +#define LC709203F_RW_ALARMVOLT 0x14 // Alarm on voltage threshold +#define LC709203F_RW_POWERMODE 0x15 // Sets sleep/power mode 0x0001: Operational mode 0x0002: Sleep mode +#define LC709203F_RW_STATUSBIT 0x16 // Temperature method, 0x0000: I2C mode, 0x0001: Thermistor mode #define LC709203F_RO_CELLPROFILE 0x1A // Displays battery profile code static uint8_t crc8(uint8_t *data, int len); /*! Approx cell capacity Table 7 */ typedef enum { - LC709203F_APA_100MAH = 0x08, - LC709203F_APA_200MAH = 0x0B, - LC709203F_APA_500MAH = 0x10, - LC709203F_APA_1000MAH = 0x19, - LC709203F_APA_2000MAH = 0x2D, - LC709203F_APA_3000MAH = 0x36, + LC709203F_APA_100MAH = 0x08, + LC709203F_APA_200MAH = 0x0B, + LC709203F_APA_500MAH = 0x10, + LC709203F_APA_1000MAH = 0x19, + LC709203F_APA_2000MAH = 0x2D, + LC709203F_APA_3000MAH = 0x36, } lc709203_adjustment_t; /*! Cell profile */ typedef enum { - LC709203_NOM3p7_Charge4p2 = 1, - LC709203_NOM3p8_Charge4p35 = 3, - LC709203_NOM3p8_Charge4p35_Less500mAh = 6, - LC709203_ICR18650_SAMSUNG = 5, - LC709203_ICR18650_PANASONIC = 4 -}lc709203_cell_profile_t ; + LC709203_NOM3p7_Charge4p2 = 1, + LC709203_NOM3p8_Charge4p35 = 3, + LC709203_NOM3p8_Charge4p35_Less500mAh = 6, + LC709203_ICR18650_SAMSUNG = 5, + LC709203_ICR18650_PANASONIC = 4 +} lc709203_cell_profile_t; /*! Cell temperature source */ typedef enum { - LC709203F_TEMPERATURE_I2C = 0x0000, - LC709203F_TEMPERATURE_THERMISTOR = 0x0001, + LC709203F_TEMPERATURE_I2C = 0x0000, + LC709203F_TEMPERATURE_THERMISTOR = 0x0001, } lc709203_tempmode_t; /*! Chip power state */ typedef enum { - LC709203F_POWER_OPERATE = 0x0001, - LC709203F_POWER_SLEEP = 0x0002, + LC709203F_POWER_OPERATE = 0x0001, + LC709203F_POWER_SLEEP = 0x0002, } lc709203_powermode_t; /*! @@ -75,38 +75,56 @@ typedef enum { */ class LC709203F { public: - LC709203F(); - ~LC709203F(); + LC709203F(); - bool begin( void ); - void initRSOC(void); + ~LC709203F(); - void setPowerMode(lc709203_powermode_t t); - void setCellCapacity(lc709203_adjustment_t apa); - void setCellProfile(lc709203_cell_profile_t t); - - uint16_t getICversion(void); + bool begin(void); - uint16_t cellVoltage_mV(void); - uint16_t cellRemainingPercent10(void); // Remaining capacity in increments of 0.1% as integer - uint16_t cellStateOfCharge(void); // In increments of 1% as integer - + void initRSOC(void); - uint16_t getThermistorBeta(void); - void setThermistorB(uint16_t beta); + void setPowerMode(lc709203_powermode_t t); - void setTemperatureMode(lc709203_tempmode_t t); - uint16_t getCellTemperature(void); + void setCellCapacity(lc709203_adjustment_t apa); - void setAlarmRSOC(uint8_t percent); - void setAlarmVoltage(float voltage); - std::list<Message> buildMessages(); + void setCellProfile(lc709203_cell_profile_t t); + + uint16_t getICversion(void); + + uint16_t cellVoltage_mV(void); + + uint16_t cellRemainingPercent10(void); // Remaining capacity in increments of 0.1% as integer + uint16_t cellStateOfCharge(void); // In increments of 1% as integer + + + uint16_t getThermistorBeta(void); + + void setThermistorB(uint16_t beta); + + void setTemperatureMode(lc709203_tempmode_t t); + + uint16_t getCellTemperature(void); + + void setAlarmRSOC(uint8_t percent); + + void setAlarmVoltage(float voltage); + + std::list<Message> buildMessages(); protected: - void write16( uint8_t regAddress, uint16_t data); - int16_t read16(uint8_t regAddress); + void write16(uint8_t regAddress, uint16_t data); + + int16_t read16(uint8_t regAddress); + private: - const SensorInformation sensorInformation{"LC709203", Protocol::I2C}; + const SensorInformation sensorInformation{"LC709203", Protocol::I2C}; + + enum class MeasurementType { + BATTERY_VOLTAGE, + }; + + std::map<MeasurementType, const char *> measurementTypeToString = { + {MeasurementType::BATTERY_VOLTAGE, "BATTERY_VOLTAGE"}}; }; #endif