// Used to set and clear bits in a control register.  bits_to_set will be bitwise OR'd with the register.
// bits_to_clear will be inverted and bitwise AND'd with the register so that every location with a 1 will result in a 0 in the register.
int8_t LTC2943_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
{
  uint8_t register_data;
  int8_t ack = 0;

  ack |= LTC2943_read(i2c_address, register_address, &register_data);
  register_data = register_data & (~bits_to_clear);
  register_data = register_data | bits_to_set;
  ack |= LTC2943_write(i2c_address, register_address, register_data);
  return(ack);
}
int8_t LTC::Update()
{
  int8_t LTC2943_mode;
  int8_t ack = 0;
  uint16_t prescalarValue = 4096;
  uint8_t status_code, hightemp_code, lowtemp_code;
  uint16_t charge_code, current_code, voltage_code, temperature_code, chargeReading;

  // reset valid flag
  valid = true;
 
  LTC2943_mode = LTC2943_AUTOMATIC_MODE | LTC2943_PRESCALAR_M_4096 | LTC2943_ALERT_MODE ;
  ack |= LTC2943_write(LTC2943_I2C_ADDRESS, LTC2943_CONTROL_REG, LTC2943_mode);                     //! Writes the set mode to the LTC2943 control register
  ack |= LTC2943_read_16_bits(LTC2943_I2C_ADDRESS, LTC2943_ACCUM_CHARGE_MSB_REG, &charge_code);     //! Read MSB and LSB Accumulated Charge Registers for 16 bit charge code
  ack |= LTC2943_read_16_bits(LTC2943_I2C_ADDRESS, LTC2943_VOLTAGE_MSB_REG, &voltage_code);         //! Read MSB and LSB Voltage Registers for 16 bit voltage code
  ack |= LTC2943_read_16_bits(LTC2943_I2C_ADDRESS, LTC2943_CURRENT_MSB_REG, &current_code);         //! Read MSB and LSB Current Registers for 16 bit current code
  ack |= LTC2943_read_16_bits(LTC2943_I2C_ADDRESS, LTC2943_TEMPERATURE_MSB_REG, &temperature_code); //! Read MSB and LSB Temperature Registers for 16 bit temperature code
  ack |= LTC2943_read(LTC2943_I2C_ADDRESS, LTC2943_STATUS_REG, &status_code);                       //! Read Status Register for 8 bit status code

  debugPrint("Verbruik  uit LTC (register value): ");
  debugPrint(charge_code);
  debugPrint(",  ack: ");
  debugPrint(ack);
  debugPrint(",  status_code: ");
  debugPrint(status_code);
  debugPrint(",  chargeState: ");
  debugPrintln(chargeState);

  if (ack == 0) { // I2C reading ok
     chargeState = true;
     
     chargeReading = LTC2943_code_to_mAh(charge_code, resistor, prescalarValue);
     debugPrint("Verbruik  uit LTC (in mAh): ");
     debugPrintln(chargeReading);
     
     if (status_code && 1 == 1) { // UVLO bit = 1, Accu is los geweest (UVLO bit wordt automatically gereset door lezen van Status register
        debugPrintln("Case 1");

        // charge -> flash (eenmalig)
        params.setChargeOffset(charge);
        debugPrint("Verbruik van RAM naar Flash (chargeOffset in mAh): ");
        debugPrintln(charge);
     } else {
        debugPrintln("Case 2");
     }
     // Calculate new charge value (in mAh) based on an offset from flash and the charge value from the LTC (in mAh)
     charge = (float)params.getChargeOffset() + chargeReading;
     debugPrint("Verbruik naar RAM (charge in mAh): ");
     debugPrintln(charge);

  }  else {        // I2C error
     if (chargeState == true) {  // Accu is los(1e keer gedetecteerd)
        debugPrintln("Case 3");

        params.setChargeOffset(charge); // charge -> flash (eenmalig)
        debugPrint("Verbruik van RAM naar Flash (chargeOffset in mAh): ");
        debugPrintln(charge);

        chargeState = false; 
     } else {
        debugPrintln("Case 4");
     }
  }

  current = LTC2943_code_to_current(current_code, resistor);                //! Convert current code to Amperes
  voltage = LTC2943_code_to_voltage(voltage_code);                          //! Convert voltage code to Volts
  temperature = LTC2943_code_to_celcius_temperature(temperature_code);      //! Convert temperature code to Celcius

  if (ack > 0) 
  {
    valid = false;
  }
  return (ack);
}