/**
 * @file BasicProgress.ino
 * @ingroup examples
 * @brief Basic library usage example
 *
 * This example shows how to display a single horizontal smooth progress bar on a character LCD display.
 */

// Wiring
// For serial (I2C) wiring follow the recommendations for your adapter board
// or for the most common boards you can find a guide at https://create.arduino.cc/projecthub/arduino_uno_guy/i2c-liquid-crystal-displays-5b806c
// The most typical serial wiring for the <LiquidCrystal_I2C.h> would be:
// 
// For Arduino Uno, Nano, Micro:
// Connect SCL to analog 5
// Connect SDA to analog 4
// Connect VDD to 5V DC
// Connect GROUND to common ground
// 
// For ESP8266 (NodeMCU):
// Connect SCL to D1 (GPIO5)
// Connect SDA to D2 (GPIO4)
// Connect VDD to 5V DC (3.3V) DC
// Connect GROUND to common ground
// !!! ESP8266 is 3.3V device, while most of the widely available character LCD displays (1602, 2004) are usually for 5V (although such 3.3V displays do exist)
// If your LCD is 3.3V you can connect the signals directly and 3.3V to VDD
// but if your LCD is 5V, for reliable operation you will have to power it by 5V and also consider using level shifter for the signals


//First include the Liquid Crystal library <LiquidCrystal.h> or <LiquidCrystal_I2C.h>  
#include <LiquidCrystal_I2C.h> 

// than include the Smooth progress bars library
#include "SmoothProgress.h"

// SmoothProgress supports different styles, each stored in separate .h file that should be included next
// Let's include the style for horizontal progress bar as in a square frame with 1 pixel margin
#include "BarStyle1.h"
#include "BarStyle4.h"

// Define the lcd object for the display as usual
LiquidCrystal_I2C myLCD(0x27, 20, 4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// The library supports multiple progress bars (up to 4 on a single display), different styles and multiple displays 
// The hardware limitation of these displays implies that all progress bars on a single display have to share the same style
// Next create the association between the lcd display object and the progress bar style 
// as a bar_display (LCD) object (dispA here) to be further used when creating 
// the smooth progress bar object and/or changing the style of the displayed progress bars
LCD dispA(myLCD, barStyle4);

// Create the objects representing the smooth progress bar 
//SmoothProgressBar spb1(dispA, 10, 0, 0, 0); // progress bar 10 characters wide, at 0-th row, 0-th column, as progress bar 0
SmoothProgressBar spb2(dispA, 14, 1, 2, 3); // progress bar 16 characters wide, at 1-th row, 0-th column, as progress bar 1

void setup()
{
    // do the usual lcd initialization for the used liquid crystal library
    myLCD.init();

    myLCD.backlight();                  // enable backlight for the LCD module

    // also initialize the bar_display object (loads the style)
    dispA.begin();

    myLCD.clear();
}

char buffer[16];
const float range_battery = 0.72;
const float batt_scarica = 3.2; //A 3.2 volt è scarica (0%)
float volt = 3.99;  //es batteria completamente carica
float temp_volt = volt-batt_scarica;
int battery_percent = (temp_volt*100)/range_battery;


void loop()
{

    battery_percent = 50;
    int printBatteryPerc = battery_percent;
    if (battery_percent > 100){
    battery_percent = 100;
    printBatteryPerc = 100;
    }

    sprintf(buffer, "%3d%%", printBatteryPerc);
    myLCD.setCursor(15, 2);  // move cursor to top left
    if (battery_percent < 0){
    battery_percent = 0;
    myLCD.print(" --");
    }
    else myLCD.print(buffer);  // print the string on the display

    spb2.showProgressPct(battery_percent);
    //b2 -= 0.005;
    //if (b2 < 0)
    //{
    //    b2 = 100;
    //    if(&dispA.getStyle() == &barStyle1)
            dispA.setStyle(barStyle4);
    //    else
    //        dispA.setStyle(barStyle1);
    //}

    delay(25);
}