/*
 *  SimpleClock.cpp
 *
 *  Shows a (fast) running clock with big numbers on a 2004 LCD.
  * https://wokwi.com/projects/346661429974139474
 *
 *  Copyright (C) 2022  Armin Joachimsmeyer
 *  armin.joachimsmeyer@gmail.com
 *
 *  This file is part of LCDBigNumbers https://github.com/ArminJo/LCDBigNumbers.
 *
 *  LCDBigNumbers is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
 *
 */

#include <Arduino.h>

/*
 * Choose your display
 */
//#define USE_PARALLEL_2004_LCD // Is default
//#define USE_PARALLEL_1602_LCD
#define USE_SERIAL_2004_LCD
//#define USE_SERIAL_1602_LCD
#include "LCDBigNumbers.hpp"  // Include sources for LCD big number generation

#define LCD_I2C_ADDRESS 0x27                                      // Default LCD address is 0x27 for a 20 chars and 4 line / 2004 display
LiquidCrystal_I2C myLCD(LCD_I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS);  // LCD_COLUMNS and LCD_ROWS are set by LCDBigNumbers.hpp depending on the defined display

/*
 * Available big number fonts are: BIG_NUMBERS_FONT_1_COLUMN_2_ROWS_VARIANT_1, BIG_NUMBERS_FONT_2_COLUMN_2_ROWS_VARIANT_1,
 * BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_1, BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_2, BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_3,
 * BIG_NUMBERS_FONT_2_COLUMN_3_ROWS_VARIANT_1, BIG_NUMBERS_FONT_2_COLUMN_3_ROWS_VARIANT_2, BIG_NUMBERS_FONT_3_COLUMN_3_ROWS_VARIANT_1,
 * BIG_NUMBERS_FONT_3_COLUMN_4_ROWS_VARIANT_1, BIG_NUMBERS_FONT_3_COLUMN_4_ROWS_VARIANT_2
 */

LCDBigNumbers bigNumberLCD(&myLCD, BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_1);

bool timeclock = false;
int mode = 0;                        // Currently-active animation mode, 0-9
unsigned long lcdPreviousCasual = 0;  // Previous lcd mode Millis
int lcdInterval = 2000;               // Lcd Interval (ms)

void setup() {
  Serial.begin(115200);
  // Just to know which program is running on my Arduino
  Serial.println(F("START " __FILE__ " from " __DATE__));

  //SDA GPIO21 (verde nel mio caso)  SCL GPIO22 (giallo)
  myLCD.init();
  myLCD.clear();
  myLCD.backlight();

  bigNumberLCD.begin();  // Creates custom character used for generating big numbers
  myLCD.setCursor(0, 0);
  myLCD.print(F("Bombolancia Wifi"));
  myLCD.setCursor(0, 1);
  myLCD.print(__DATE__);
  delay(1000);
  myLCD.clear();
}

int Seconds = 0;
int Minutes = 40;
int Hours = 21;
unsigned long lastMillisOfClockUpdate = 0;

void loop() {

  unsigned long currentMillisCasual = millis();                  //  Update current time
  if (currentMillisCasual - lcdPreviousCasual >= lcdInterval) {  //  Check for expired time
    lcdPreviousCasual = currentMillisCasual;
    myLCD.clear();

    //if (++mode > 3) mode = 0;  // Advance to next mode, wrap around after #8

    switch (mode) {  // Start the new animation...
      case 0:
        //timeclock = true;

        bigNumberLCD.init(BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_2);
        bigNumberLCD.begin();  // Creates custom character used for generating big numbers
        timeclock = false;
        myLCD.clear();
        
        bigNumberLCD.setBigNumberCursor(2, 0);
        bigNumberLCD.print(33);

        bigNumberLCD.setBigNumberCursor(12, 0);
        bigNumberLCD.print(12);

        bigNumberLCD.setBigNumberCursor(4, 2);
        bigNumberLCD.print(2023);

        break;
      case 1:
        bigNumberLCD.init(BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_1);
        bigNumberLCD.begin();  // Creates custom character used for generating big numbers
        timeclock = false;
        myLCD.clear();
        myLCD.setCursor(0, 0);  // posizione carattere (0-19 nel mio caso), numero riga (0-3 nel mio caso)
        myLCD.print("GAS");

        bigNumberLCD.setBigNumberCursor(11);
        bigNumberLCD.print("9");

        myLCD.setCursor(18, 0);
        myLCD.print("Kg");

        myLCD.setCursor(0, 2);
        myLCD.print("UL ");
        myLCD.setCursor(3, 2);
        myLCD.print("14/03/23 19:45:38");

        myLCD.setCursor(0, 3);
        myLCD.print("Precedente GAS");
        myLCD.setCursor(15, 3);
        myLCD.print("11");
        myLCD.setCursor(18, 3);
        myLCD.print("Kg");

        break;
      case 2:
        timeclock = false;
        myLCD.clear();

        myLCD.setCursor(0, 0);
        myLCD.print("Peso attuale");
        myLCD.setCursor(15, 0);
        myLCD.print("18");
        myLCD.setCursor(18, 0);
        myLCD.print("Kg");

        myLCD.setCursor(0, 1);
        myLCD.print("Taglia");
        myLCD.setCursor(15, 1);
        myLCD.print("10");
        myLCD.setCursor(18, 1);
        myLCD.print("Kg");

        myLCD.setCursor(0, 2);  // posizione carattere (0-19 nel mio caso), numero riga (0-3 nel mio caso)
        myLCD.print("Peso iniziale");
        myLCD.setCursor(15, 2);
        myLCD.print("28");
        myLCD.setCursor(18, 2);
        myLCD.print("Kg");

        myLCD.setCursor(0, 3);
        myLCD.print("al");
        myLCD.setCursor(3, 3);
        myLCD.print("11/03/23 10:15:22");
        
        break;
      case 3:
        timeclock = false;
        myLCD.clear();

        myLCD.setCursor(0, 0);
        myLCD.print("Invio periodico");
        myLCD.setCursor(17, 0);
        myLCD.print("Mer");

        myLCD.setCursor(0, 1);
        myLCD.print("Deep sleep");
        myLCD.setCursor(18, 1);
        myLCD.print("ON");

        myLCD.setCursor(0, 2);  // posizione carattere (0-19 nel mio caso), numero riga (0-3 nel mio caso)
        myLCD.print("Alert fine gas");
        myLCD.setCursor(16, 2);
        myLCD.print("<5");
        myLCD.setCursor(18, 2);
        myLCD.print("Kg");

        myLCD.setCursor(0, 3);
        myLCD.print("Temp esterna");
        myLCD.setCursor(14, 3);
        myLCD.print("18.3");
        myLCD.setCursor(18, 3);
        myLCD.write(0xDF); // Stampa il simbolo dei gradi centigradi (°C)
        myLCD.print("C"); // Stampa la lettera "C" per indicare la scala di misura Celsius
        
        break;
    }
  }

  if (timeclock) {
    unsigned long clockMillis = millis();
    if (clockMillis - lastMillisOfClockUpdate > 1000) {  // Fast mode :-), do not wait 60 seconds
      lastMillisOfClockUpdate = clockMillis;
      bigNumberLCD.init(BIG_NUMBERS_FONT_3_COLUMN_4_ROWS_VARIANT_2);
      bigNumberLCD.begin();  // Creates custom character used for generating big numbers

      /*
         * Print hours
         */
      bigNumberLCD.setBigNumberCursor(2);
      if (Hours < 10) {
        bigNumberLCD.print('0');
      }
      bigNumberLCD.print(Hours);

      /*
         * Do blinking colon
         */
      if (Seconds % 2 == 1) {
        bigNumberLCD.print(':');
      } else {
        bigNumberLCD.print(ONE_COLUMN_SPACE_CHARACTER);
      }
      bigNumberLCD.setBigNumberCursor(12);

      /*
         * Print minutes
         */
      if (Minutes < 10) {
        bigNumberLCD.print('0');
      }
      bigNumberLCD.print(Minutes);

      /*
         * Go to next minute
         */
      Seconds++;
      //if (Seconds >= 2) { // Demo fast mode with blinking colon
      // if (Seconds >= 60) {
      //   Seconds = 0;
      //   Minutes++;
      //   if (Minutes >= 60) {
      //     Minutes = 0;
      //     Hours++;
      //     if (Hours >= 24) {
      //       Hours = 0;
      //     }
      //   }
      // }
    }
  }
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL