// Inculde Library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Define the Screen Measurment AND Dislay
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET    4  // Reset pin # (or -1 if sharing reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// // Define the pins for the I2C bus and the OLED display
// #define SDA_PIN 21
// #define SCL_PIN 22


// Define variables for counting time
unsigned long start_time;
unsigned long elapsed_time = 0;


// 'Battery Symbol', 17x9px
const unsigned char epd_bitmap_Battery_Symbol [] PROGMEM = {
  0x7f, 0xfe, 0x00, 0x80, 0x02, 0x00, 0x80, 0x03, 0x00, 0x83, 0x13, 0x80, 0x87, 0xe3, 0x80, 0x89, 
  0x83, 0x80, 0x80, 0x03, 0x00, 0x80, 0x02, 0x00, 0x7f, 0xfe, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 48)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
  epd_bitmap_Battery_Symbol
};

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  // Start the I2C bus
  //Wire.begin(SDA_PIN, SCL_PIN);
  Wire.begin();

// vInitOLED();  
  // Initialize the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
    while (true);
  } 

  // Clear the display
  display.clearDisplay();
  display.display();

  // Set the start time
  start_time = millis();



}

void loop() {
  // Calculate the elapsed time
  elapsed_time = (millis() - start_time) / 1000;



  // Clear the display
  display.clearDisplay();

  // Display the charging status
  display.setTextSize(1.9);
  display.setCursor(0, 24);
  display.println("STATUS"); 

  // Display the elapsed time on the display
  display.setTextSize(1.9);
  display.setTextColor(WHITE);
  display.setCursor(0, 37);
  display.printf("Time - %02d:%02d:%02d", elapsed_time / 3600, (elapsed_time / 60) % 60, elapsed_time % 60);
  
  // vDisplayStatus(xDeviceStatus);
  // vReadBatteryVoltage();
  // vDisplayBatteryVoltage(fBattVolt);
  // vDisplayChargeIcon();
  // vDisplayTime();

  // Display the image of battery
  display.drawBitmap(110, 0, epd_bitmap_Battery_Symbol, 17, 9, WHITE);
  DisplayAnalogValue();
}

void DisplayAnalogValue() {
  // read the input on analog pin GIOP36:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 3.3V):
  float voltage = floatMap(analogValue, 0, 4095, 0, 3.3);
 
 // Display the volts
  display.setTextSize(1.5);
  display.setCursor(0,0);
  display.println("Volt - "); 
  display.setCursor(40,0);
  display.print(voltage);

  display.display();
  delay(1000);
}