// ==========================
//   LIBRARY YANG DIGUNAKAN
// ==========================

#include <MD_Parola.h>             // Library untuk tampilan teks animasi LED matrix
#include <MD_MAX72xx.h>            // Library untuk mengontrol modul MAX7219
#include <SPI.h>                   // Komunikasi SPI (digunakan MAX7219)
#include <DHT.h>                   // Sensor suhu dan kelembaban DHT22
#include <Wire.h>                  // Komunikasi I2C
#include <LiquidCrystal_I2C.h>     // Library LCD I2C 16x2

//#define USE_UI_CONTROL 1         // Aktifkan jika ingin kontrol tampilan dari tombol/potensio
#define USE_UI_CONTROL 0           // Nonaktifkan fitur kontrol UI manual

#if USE_UI_CONTROL
#include <MD_UISwitch.h>           // Library untuk tombol digital
#endif

// =======================
//    KONFIGURASI DEBUG
// =======================

#define DEBUG 0                    // Ubah ke 1 untuk mengaktifkan log Serial
#if DEBUG
#define PRINT(s, x)   { Serial.print(F(s)); Serial.print(x); }
#define PRINTS(x)     Serial.print(F(x))
#define PRINTX(x)     Serial.println(x, HEX)
#else
#define PRINT(s, x)
#define PRINTS(x)
#define PRINTX(x)
#endif

// ===========================
//   KONFIGURASI LED MATRIX
// ===========================

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 11             // Jumlah modul matrix MAX7219
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);  // Inisialisasi objek Parola

// ============================
//   KONTROL UI (opsional)
// ============================

#if USE_UI_CONTROL
const uint8_t SPEED_IN = A5;       // Potensiometer untuk kontrol kecepatan scroll
const uint8_t DIRECTION_SET = 8;   // Tombol untuk ubah arah scroll
const uint8_t INVERT_SET = 9;      // Tombol untuk invert tampilan
const uint8_t SPEED_DEADBAND = 5;  // Toleransi perubahan kecepatan

MD_UISwitch_Digital uiDirection(DIRECTION_SET);
MD_UISwitch_Digital uiInvert(INVERT_SET);
#endif

// =============================
//   KONFIGURASI TAMPILAN TEKS
// =============================

uint8_t scrollSpeed = 25;
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT;
uint16_t scrollPause = 2000;

#define BUF_SIZE 100
char curMessage[BUF_SIZE] = { "" };
char newMessage[BUF_SIZE] = { "IRGHI MAULANA HAFIDZ IBNU-241091900197 " };
bool newMessageAvailable = true;

// ===========================
//     SENSOR DHT & LCD
// ===========================

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Alamat LCD dan ukuran 16x2

// ===========================
//     KONTROL OUTPUT
// ===========================

const int relayPin = 7;
const int ledPin = 8;

const int led1 = 3;
const int led2 = 4;
const int led3 = 5;
const int led4 = 6;

// ===========================
//     FUNGSI: UI CONTROL
// ===========================

#if USE_UI_CONTROL
void doUI(void)
{
  int16_t speed = map(analogRead(SPEED_IN), 0, 1023, 10, 150);

  // Ubah kecepatan scroll jika perubahan cukup besar
  if ((speed >= ((int16_t)P.getSpeed() + SPEED_DEADBAND)) || (speed <= ((int16_t)P.getSpeed() - SPEED_DEADBAND)))
  {
    P.setSpeed(speed);
    scrollSpeed = speed;
    PRINT("\nChanged speed to ", P.getSpeed());
  }

  // Ubah arah scroll jika tombol ditekan
  if (uiDirection.read() == MD_UISwitch::KEY_PRESS)
  {
    PRINTS("\nChanging scroll direction");
    scrollEffect = (scrollEffect == PA_SCROLL_LEFT ? PA_SCROLL_RIGHT : PA_SCROLL_LEFT);
    P.setTextEffect(scrollEffect, scrollEffect);
    P.displayClear();
    P.displayReset();
  }
  // Invert tampilan jika tombol ditekan
  if (uiInvert.read() == MD_UISwitch::KEY_PRESS)
  {
    PRINTS("\nChanging invert mode");
    P.setInvert(!P.getInvert());
  }
}
#endif

// ===========================
//   FUNGSI: SERIAL MESSAGE
// ===========================

void readSerial(void)
{
  static char *cp = newMessage;

  while (Serial.available())
  {
    *cp = (char)Serial.read();

    if ((*cp == '\n') || (cp - newMessage >= BUF_SIZE - 2))
    {
      *cp = '\0';
      cp = newMessage;
      newMessageAvailable = true;
    }
    else
    {
      cp++;
    }
  }
}

// ===========================
//        SETUP AWAL
// ===========================

void setup() {
  Serial.begin(57600);
  Serial.println("[Parola Scrolling Display]");
  Serial.println("Type a message for the scrolling display");

#if USE_UI_CONTROL
  uiDirection.begin();
  uiInvert.begin();
  pinMode(SPEED_IN, INPUT);
  doUI();  // Inisialisasi kontrol
#endif

  P.begin();
  P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);

  dht.begin();     // Mulai sensor DHT
  lcd.init();      // Inisialisasi LCD
  lcd.backlight(); // Nyalakan backlight

  // Atur semua pin output
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

// ===========================
//           LOOP
// ===========================

void loop() {
#if USE_UI_CONTROL
  doUI();  // Cek input UI jika aktif
#endif

  // Jalankan animasi scroll teks
  if (P.displayAnimate())
  {
    if (newMessageAvailable)
    {
      strcpy(curMessage, newMessage);
      newMessageAvailable = false;
    }
    P.displayReset();
  }

  readSerial();  // Baca pesan baru dari serial
  delay(80);     // Delay kecil agar tidak terlalu cepat

  // ========================
  //   BACA SENSOR DHT22
  // ========================

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Gagal membaca sensor DHT22!");
    return;
  }

  // ========================
  //   TAMPILKAN KE LCD
  // ========================

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Suhu: ");
  lcd.print(temperature);
  lcd.print(" C ");
  lcd.setCursor(0, 1);
  lcd.print("Lembab: ");
  lcd.print(humidity);
  lcd.print(" %");

  // ========================
  //   KONTROL RELAY & LED
  // ========================

  if (temperature > 30) {
    digitalWrite(relayPin, HIGH);
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(relayPin, LOW);
    digitalWrite(ledPin, LOW);
  }

  // ========================
  //   EFEK LED BERGANTIAN
  // ========================

  digitalWrite(led1, HIGH); delay(500); digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH); delay(500); digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH); delay(500); digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH); delay(500); digitalWrite(led4, LOW);

  // ========================
  //   (OPSIONAL) ANIMASI KARAKTER
  // ========================
  /*
  // Contoh dasar animasi karakter bitmap menggunakan array kolom manual
  // Saat ini nonaktif — bisa diaktifkan & dikembangkan

  char message[] = "PEMBUAT: DOSEN SISTEM KOMPUTER";
  int fontSize = 7;
  for (int i = 0; i < strlen(message) * 8; i++) {
    for (int j = 0; j < 8; j++) {
      int charIndex = (i + j) / 8;
      int columnIndex = (i + j) % 8;
      char currentChar = message[charIndex];

      byte charColumns[][8] = {
        {B0111110, B1000001, B1000001, B1000001, B1000001, B1000001, B1000001, B0111110}, // A
        {B1111110, B1000001, B1000001, B1111110, B1000001, B1000001, B1000001, B1111110}, // B
        {B0000000, B0000000, B0000000, B0000000, B0000000, B0000000, B0000000}            // Spasi
      };

      byte charColumn = charColumns[currentChar - 'A'][columnIndex];

      // lc.clearDisplay(0);
      // lc.setColumn(0, j, charColumn);
    }
    delay(10);
  }
  */
}
NOCOMNCVCCGNDINLED1PWRRelay Module