text: /* Copyright (c) 2020 UT Longhorn Racing Solar */
  
  /** Temperature.h
   * Temperature file that holds all Temperature related information of BeVolt's
   * battery pack.
   */
  
  // NOTE: All units are in Celsius
  
  #ifndef TEMPERATURE_H__
  #define TEMPERATURE_H__
  
  #include "common.h"
  #include "config.h"
  #include "LTC6811.h"
  
  #define MUX1 0x90
  #define MUX2 0x92
  #define AUX_I2C_START 0x6
  #define AUX_I2C_NACK 0x8
  #define AUX_I2C_NACK_STOP 0x9
  #define AUX_I2C_BLANK 0
  #define AUX_I2C_NO_TRANSMIT 0x7
  
  /** Temperature_Init
   * Initializes device drivers including SPI inside LTC6811_init and LTC6811 for Temperature Monitoring
   * @param boards LTC6811 data structure that contains the values of each register
   */
  ErrorStatus Temperature_Init(cell_asic *boards);
  
  /** Temperature_ChannelConfig
   * Configures which temperature channel you're sampling from
   * Assumes there are only 2 muxes; 0 index based - 0 is sensor 1
   * @precondition tempChannel should be < MAX_TEMP_SENSORS_PER_MINION_BOARD
   * @param channel number (0-indexed based)
   * @return SUCCESS or ERROR
   * @note we clear the otherMux every time a channel is switched even on the same mux; Maybe change depending on speed/optimization
   */
  ErrorStatus Temperature_ChannelConfig(uint8_t tempChannel);
  
  /** convertVoltageToTemperature
   * Converts mv to temperature based on the temperature sensor equation
   * Equation : T(in C) = (((13.582 - sqrt((-13.582)*(-13.582) + 4 * 0.00433 * (2230.8 - milliVolt)))/ (2.0 * -0.00433)) + 30)
   * @param mV from ADC
   * @return temperature in Celsius (Fixed Point with .001 resolution) 
   */
  int milliVoltToCelsius(float milliVolt);
  
  /** Temperature_UpdateSingleChannel
   * Stores and updates the new measurements received on one particular temperature sensor
   * @param channel < MAX_TEMP_SENSORS_PER_MINION_BOARD, 0-indexed
   * @return SUCCESS or ERROR
   */
  ErrorStatus Temperature_UpdateSingleChannel(uint8_t channel);
  
  /** Temperature_UpdateAllMeasurements
   * Stores and updates the new measurements received on all temperature sensors
   * @return SUCCESS or ERROR
   */
  ErrorStatus Temperature_UpdateAllMeasurements(void);
  
  /** Temperature_CheckStatus
   * Checks if all modules are safe
   * @param 1 if pack is charging, 0 if discharging
   * @return SAFE or DANGER
   */
  SafetyStatus Temperature_CheckStatus(uint8_t isCharging);
  
  /** Temperature_SetChargeState
   * Lithium Ion Cells have two separate max temperature limits. There is a limit
   * when the battery is charging and another limit when the battery is discharging.
   * We need to account for these two limits by setting which limit should be used.
   * ChargingState is used for GetModulesInDanger when we can't use the current module to check if charging or discharging
   * @param 1 if pack is charging, 0 if discharging
   */
  void Temperature_SetChargeState(uint8_t isCharging);
  
  /** Temperature_GetModulesInDanger
   * Finds all modules that are in danger and stores them into a list
   * This function is called when you can't use the current module to see if it is charging.
   * @return pointer to index of modules that are in danger
   */
  uint8_t *Temperature_GetModulesInDanger(void);
  
  /** Temperature_GetSingleTempSensor
   * Gets the single sensor from a particular board
   * @precondition: board must be < NUM_MINIONS, sensorIdx < MAX_TEMP_SENSORS_PER_MINION_BOARD
   * @param index of board (0-indexed based)
   * @param index of sensor (0-indexed based)
   * @return temperature of the battery module at specified index
   */
  int32_t Temperature_GetSingleTempSensor(uint8_t board, uint8_t sensorIdx);
  
  /** Temperature_GetModuleTemperature
   * Gets the avg temperature of a certain battery module in the battery pack. Since there
   * are 2 sensors per module, the return value is the average
   * @precondition: moduleIdx must be < NUM_BATTERY_MODULE
   * @param index of module (0-indexed based)
   * @return temperature of the battery module at specified index
   */
  int32_t Temperature_GetModuleTemperature(uint8_t moduleIdx);
  
  /** Temperature_GetTotalPackAvgTemperature
   * Gets the average temperature of the whole battery pack
   * @return average temperature of battery pack
   */
  int32_t Temperature_GetTotalPackAvgTemperature(void);
  
  /** Temperature_SampleADC
   * Starts ADC conversion on GPIO1 on LTC6811's auxiliary registers on all boards
   * @param sets the sampling rate
   * @return SUCCESS or ERROR
   */
  ErrorStatus Temperature_SampleADC(uint8_t ADCMode);
  
  #endif
  