#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Constants for screen configuration
const uint8_t lcdAddr = 0x27;   // LCD I2C address might be different
const uint8_t lcdCols = 16;     // Number of columns in the LCD
const uint8_t lcdRows = 2;      // Number of rows in the LCD

// Initialize the LCD object
LiquidCrystal_I2C lcd(lcdAddr, lcdCols, lcdRows);

/*
 * This function is responsible for initializing the LCD
 * and displaying a welcome message.
 */
void setup() {
  // Start the LCD and initialize it
  lcd.init(); // Initialize LCD with default configurations
  lcd.backlight(); // Turn on the backlight

  // Prepare welcome message display
  lcd.clear(); // Clear any junk data on the display
  lcd.setCursor(0, 0);
  lcd.print(" SHERMAN AUDIO");
}

/*
 * The loop function runs indefinitely,
 * handling the main display of the custom scrolling text.
 */
void loop() {
  // Custom message to scroll across the screen
  String message = "Jack Audio System Solution";

  // Scroll the text across the specified row
  scrollText(message, 1);

  // Delay before restarting the scroll animation to improve readability
  delay(1000);
}

/*
 * This function handles the scrolling text functionality.
 * @param message: The text to scroll across the screen.
 * @param row: The row on which to scroll the text.
 */
void scrollText(String message, int row) {
  // Ensure the message length is valid and non-empty
  if (message.length() == 0) {
    Serial.println("Empty message, nothing to scroll.");
    return; // Exit if no message to scroll
  }

  // Define the number of characters that fit on the screen
  int screenColumns = lcdCols;

  // Add spaces to the message to ensure it scrolls fully
  message = "                " + message + "                "; // Adding spaces before and after

  // Loop through the entire message, scrolling part of it onto the screen
  for (int i = 0; i <= message.length() - screenColumns; i++) {
    // Set cursor to the start of the desired row
    lcd.setCursor(0, row);

    // Print only the part of the message visible on the screen
    lcd.print(message.substring(i, i + screenColumns));

    // Delay to control scrolling speed
    delay(250);
  }
}