/* Copyright 2025 by COG9 Enterprises LLC
 * All rights reserved.
 * Title: bat.ino
 * Author: Jed Marti KI7NNP
 * Description: Battery icon with fill. In 5% increments. 13 pixels high, 8 wide.
 * Created: Thu Jan 29 19:43:07 2026
 * Revision History:
 */

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include "powerard.h"
#include "ssd1306.h"

// The I2C addresses of the (potentially) 4 sensors.
uint8_t aTemp_i2c;
uint8_t bTemp_i2c;
uint8_t cTemp_i2c;
uint8_t auxTemp_i2c;

extern Adafruit_SSD1306 display;

// Where the icon should be.
uint8_t batteryIconX = 120;
uint8_t batteryIconY = 50;
uint8_t remainingPercent = 0;

#define SLOPE 0.00468653
#define INTERCEPT 0.648076

#define Y(y) (63 - (y))

void batteryIcon(void)
/*     This shows up on all screens giving a 10% indication of remaining battery.
 * Parameters:
 *   v: Percent remaining 0 - 100.
 *   x, y: lower left corner.
 */
{
  uint8_t vert;
  uint8_t i;

  // Outline
  display.drawLine(batteryIconX, Y(batteryIconY), batteryIconX + 7, Y(batteryIconY), 1);
  display.drawLine(batteryIconX + 7, Y(batteryIconY), batteryIconX + 7, Y(batteryIconY + 11), 1);
  display.drawLine(batteryIconX + 7, Y(batteryIconY + 11), batteryIconX, Y(batteryIconY + 11), 1);
  display.drawLine(batteryIconX, Y(batteryIconY + 11), batteryIconX, Y(batteryIconY), 1);

  // top.
  display.drawLine(batteryIconX + 2, Y(batteryIconY + 12),
		   batteryIconX + 5, Y(batteryIconY + 12), 1);

  // How many lines to draw.
  vert = remainingPercent / 10;
  for (i = 0; i < vert; ++i)
    display.drawLine(batteryIconX + 1, Y(batteryIconY + i + 1),
		     batteryIconX + 6, Y(batteryIconY + i + 1), 1);

  // Draw half a line if more then 5% above.
  if ((remainingPercent - vert * 10) >= 5)
    display.drawLine(batteryIconX + 4, Y(batteryIconY + vert + 1),
		     batteryIconX + 6, Y(batteryIconY + vert + 1), 1);
}



uint8_t realBatteryCapacity(void)
/*     Read the auxiliary temperature. If present, the update the battery capacity in real time.
 *   If no auxiliary, then average the temperatures from the power supply. If none available, then
 *   jsut leave alone.
 * Parameters: none
 * External References:
 *  aTemp_i2c: 5v regulator temp. i2c @.
 *  bTemp_i2c: 12v regulator temp. i2c @.
 *  cTemp_i2c: 19v regulator temp. i2c @.
 *  auxTemp_i2c: Auxiliary temp. i2c @.
 *  E: Copy of EEPROM values.
 *  INTERCEPT, SLOPE: constants for battery percent by temperature linear equation.
 */
{
  float   temp;
  float   pct;
  uint8_t cnt;
  
  // Case 1: The auxiliary temperature is attached to the battery.
  if (auxTemp_i2c) temp = getFahrenheit(auxTemp_i2c);

  // Case 2: The mean of the other attached sensors.
  else if (aTemp_i2c || bTemp_i2c || cTemp_i2c)
  {
    temp = 0.0f;
    cnt = 0;
    if (aTemp_i2c) { temp += getFahrenheit(aTemp_i2c); ++cnt; }
    if (bTemp_i2c) { temp += getFahrenheit(bTemp_i2c); ++cnt; }
    if (cTemp_i2c) { temp += getFahrenheit(cTemp_i2c); ++cnt; }
    temp /= (float) cnt;
  }

  // Case 3: Default to 65 F.
  else temp = 65.0f;

  // Now compute the percent reduct
  return (uint8_t) ((INTERCEPT + SLOPE * temp) * E.Wh);
}

// EOF
