/**
 * Sketch_19_Lvgl_Multifunctionality.ino
 * Debug/restore version for Freenove Media Kit for ESP32-S3
 *
 * Notes:
 * - Uses Serial0 because that is the working serial interface on this setup.
 * - Original SD/audio lines are restored below, but left commented out for controlled re-enable testing.
 * - Current known-good state: camera + display + LVGL UI boot successfully.
 */

bool bootFlagIncludes = false;
bool bootFlagDefs = false;
bool bootFlagDisp = false;

#include "public.h"

#define SD_MMC_CMD 38         // Please do not modify it.
#define SD_MMC_CLK 39         // Please do not modify it.
#define SD_MMC_D0  40         // Please do not modify it.

#define AUDIO_INPUT_SCK  3    // Please do not modify it.
#define AUDIO_INPUT_WS   14   // Please do not modify it.
#define AUDIO_INPUT_DIN  46   // Please do not modify it.

#define AUDIO_OUTPUT_BCLK 42  // Please do not modify it.
#define AUDIO_OUTPUT_LRC  41  // Please do not modify it.
#define AUDIO_OUTPUT_DOUT 1   // Please do not modify it.

Display screen;

void setup() {
  bootFlagIncludes = true;
  bootFlagDefs = true;
  bootFlagDisp = true;

  Serial0.begin(115200);
  delay(1000);
  Serial0.println();
  Serial0.println("DEBUG CHKPT - Serial0 Init");

  Serial0.println("DEBUG CHKPT - Before Camera Init");
  camera_init(0);
  Serial0.println("DEBUG CHKPT - Camera Init");

  // ---------------------------------------------------------------------------
  // ORIGINAL SD INIT (currently disabled because it was hanging during testing)
  // ---------------------------------------------------------------------------
   Serial0.println("DEBUG CHKPT - Before SD Init");
   sdmmc_init(SD_MMC_CLK, SD_MMC_CMD, SD_MMC_D0);
 Serial0.println("DEBUG CHKPT - SD Init");

  // ---------------------------------------------------------------------------
  // ORIGINAL AUDIO INIT (currently disabled because it was faulting during testing)
  // ---------------------------------------------------------------------------
   Serial0.println("DEBUG CHKPT - Before Audio Input Init");
   audio_input_init(AUDIO_INPUT_SCK, AUDIO_INPUT_WS, AUDIO_INPUT_DIN);
   Serial0.println("DEBUG CHKPT - Audio Input Init");

   Serial0.println("DEBUG CHKPT - Before Audio Output Init");
   audio_output_init(AUDIO_OUTPUT_BCLK, AUDIO_OUTPUT_LRC, AUDIO_OUTPUT_DOUT);
   Serial0.println("DEBUG CHKPT - Audio Output Init");

   audio_output_set_volume(21);
   Serial0.println("DEBUG CHKPT - Volume Set");
  Serial0.println("DEBUG CHKPT - Audio Init");

  // ---------------------------------------------------------------------------
  // ORIGINAL DIRECTORY CREATION (restored but disabled until SD is re-enabled)
  // ---------------------------------------------------------------------------
   Serial0.println("DEBUG CHKPT - Before create_dir");
   create_dir(CAMERA_FOLDER);
   create_dir(MUSIC_FOLDER);
   create_dir(RECORDER_FOLDER);
   Serial0.println("DEBUG CHKPT - Directories Created");
  Serial0.println("DEBUG CHKPT - Directory Creation");

  Serial0.println("DEBUG CHKPT - Before Screen Init");
  screen.init(TFT_DIRECTION);
  Serial0.println("DEBUG CHKPT - Screen Init");

  String LVGL_Arduino = "Hello Arduino! ";
  LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
  Serial0.println(LVGL_Arduino);
  Serial0.println("I am LVGL_Arduino");

  Serial0.println("DEBUG CHKPT - Before UI Create");
  setup_scr_main(&guider_main_ui);
  Serial0.println("DEBUG CHKPT - UI Created");

  Serial0.println("DEBUG CHKPT - Before UI Load");
  lv_scr_load(guider_main_ui.main);
  Serial0.println("DEBUG CHKPT - UI Loaded");

  Serial0.println("Setup done");
}

void loop() {
  screen.routine();
  delay(5);
}