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

#define SCREEN_WIDTH 128 // Ширина OLED дисплею в пікселях
#define SCREEN_HEIGHT 64 // Висота OLED дисплею в пікселях

// Ініціалізація дисплею SSD1306
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/*
byte customChar[9] = {
  0b00100,
  0b00110,
  0b10101,
  0b01110,
  0b00100,
  0b01110,
  0b10101,
  0b00110,
  0b00100
};
*/
void setup() {
  // Ініціалізація дисплею
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  
  display.clearDisplay();

  
  //display.drawBitmap(0, 0, customChar, 8, 9, WHITE);

 
  
  //display.drawRect(113, 10, 14, 6, WHITE);
  
  display.display();
}

void loop() {
  display.clearDisplay();
  
   MdId_battery();

  display.display();
}

void MdId_battery() {
    
    const float batLevels[] = {4.15, 4.0, 3.82, 3.76, 3.65, 3.6, 3.5, 3.4, 3.3, 3.0};
    /*
    float shuntVoltage_mV = ina219.getShuntVoltage_mV();
    float busVoltage_V = ina219.getBusVoltage_V();
    float loadVoltage_V  = busVoltage_V + (shuntVoltage_mV / 1000);
    */
    static float loadVoltage_V  = 3.0;// test
    static byte chargeLevel;
    static uint32_t tmr;
    
    if (tmr < millis()) { 
      
      tmr = millis() +60000;
     
      for (int i = 0; i <= 9 ; i++) {
    
        if (loadVoltage_V > batLevels[i]) {
        
         chargeLevel = map(i, 0, 9, 0, 11);
         break;
        
        }
      
      }
    
    }
    
    MdId_p_battery(chargeLevel);  
  
  }

  byte MdId_p_battery(byte receivedData) {

    display.drawRect(111, 2, 1, 4, WHITE);//картинка батареї
    display.drawRect(112, 0, 16, 8, WHITE);
    
    display.fillRect(114+receivedData, 2, 12-receivedData, 4, WHITE);//шкала заряду батареї
   
   return 0;
  
  }