// 11 Januari 2022, by Koepel, Public Domain
// Version 1 : Initial version, working in linux in Wokwi.
// Version 2 : added some fun code in the loop()
// Version 3 : fixed big bug for the Arduino IDE in Windows.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd( 0x27, 20, 4);
char buffer[40];
float version = 1.00;


void setup()
{
  Serial.begin(115200);
  Serial.println( "__FILE__    : " __FILE__);
  Serial.println( "__DATE__    : " __DATE__);
  Serial.println( "__TIME__    : " __TIME__);
  Serial.println( "__VERSION__ : " __VERSION__);
  Serial.print(   "ARDUINO     : ");
  Serial.println( ARDUINO);

  // Trying to extract the name of the sketch
  // This part is bad code, my apologies in advance
  char *p = strrchr( __FILE__, '/');    // find last forward slash
  if( p == nullptr)
  {
    p = strrchr( __FILE__, '\\');  // perhaps it is a backward slash
  }
  
  if( p != nullptr)
  {
    p++;                                // strip the leading forward or backward slash
    strcpy( buffer, p);

    // strip the extension
    char *pDot = strrchr( buffer, '.');
    if( pDot != nullptr)
    {
      *pDot = '\0';
    }
    Serial.print( "Name of the sketch : ");
    Serial.print( '\"');
    Serial.print( buffer);
    Serial.print( '\"');
    Serial.println();
  }
  else
  {
    Serial.println( "Oops, can not find the sketch name");
  }

  lcd.init();
  lcd.backlight();

  lcd.setCursor( 0, 0);
  lcd.print( "\"Real\" Live Example");
  lcd.setCursor( 0, 1);
  lcd.print( "Version ");
  lcd.print( version);

  lcd.setCursor( 0, 2);
  lcd.print( "Name : ");
  lcd.print( '\"');
  lcd.print( buffer);
  lcd.print( '\"');

  lcd.setCursor( 0, 3);
  lcd.print( __DATE__ " " __TIME__);  // One space in the middle for exactly 20 characters

  delay( 10000);
}


void loop() 
{
  // Have some fun with the digits of the time and the number of the version
  int col = random( 0, 6);
  col += col / 2;
  int digit = random( 0, 10);
  lcd.setCursor( col + 12, 3);
  lcd.print( digit);

  if( millis() % 4000 < 2000)
    version += float( digit) / 100.0;
  else
    version -= float( digit) / 100.0;

  lcd.setCursor( 8, 1);
  lcd.print( version, 2);

  delay( 150);
}