/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Audio Control
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-10-15 20:52:09

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* use TFT ILI9488 SPI and PT2314 chip for audio int */
	/* vol,bass,treble,gain,loudness,mute,attnR,attnL */
	/* channel,bal_l,bal_r menu,options update display */
	/* rotar values */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <SimpleEncoder.h>  // Library for rotary encoder
#include <TFT_eSPI.h>       // TFT library for ILI9488 display
#include <PT2314.h>         // Library for PT2314 audio processor

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t ro_KY_040_CLK_PIN_D4 = 4;    // Rotary encoder CLK pin
const uint8_t ro_KY_040_DT_PIN_D13 = 13;   // Rotary encoder DT pin
const uint8_t ro_KY_040_SW_PIN_D14 = 14;   // Rotary encoder switch pin

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
SimpleEncoder encoder(ro_KY_040_SW_PIN_D14, ro_KY_040_CLK_PIN_D4, ro_KY_040_DT_PIN_D13); // Encoder instance
TFT_eSPI tft = TFT_eSPI(); // TFT display instance
PT2314 audioProcessor; // PT2314 audio processor instance

void setup(void)
{
    // Initialize serial communication for debugging
    Serial.begin(9600);

    // Initialize the TFT display
    tft.init();
    tft.setRotation(1); // Set rotation of the display
    tft.fillScreen(TFT_BLACK); // Clear screen with black color

    // Initialize the audio processor
    if (audioProcessor.begin()) {
        Serial.println("PT2314 initialized successfully.");
    } else {
        Serial.println("Failed to initialize PT2314.");
    }

    // Set initial audio settings
    audioProcessor.setVolume(30); // Set initial volume
    audioProcessor.setBass(0);     // Set initial bass
    audioProcessor.setTreble(0);   // Set initial treble
    audioProcessor.setMute(false);  // Unmute audio
}

void loop(void)
{
    // Check if the encoder value is changing
    if (encoder.CHANGING) {
        // Get the current value from the encoder
        int value = encoder.VALUE;

        // Update audio settings based on encoder value
        if (value >= 0 && value <= 63) {
            audioProcessor.setVolume(value); // Set volume based on encoder value
            Serial.print("Volume set to: ");
            Serial.println(value);
        } else if (value > 63 && value <= 127) {
            audioProcessor.setBass(value - 64); // Set bass if value exceeds 63
            Serial.print("Bass set to: ");
            Serial.println(value - 64);
        } else if (value < 0 && value >= -63) {
            audioProcessor.setTreble(-value); // Set treble if value is negative
            Serial.print("Treble set to: ");
            Serial.println(-value);
        }

        // Update the display with the current settings
        tft.setTextColor(TFT_WHITE);
        tft.setTextSize(2);
        tft.setCursor(10, 10);
        tft.printf("Volume: %d\nBass: %d\nTreble: %d\n", audioProcessor.getVolume(), audioProcessor.getBass(), audioProcessor.getTreble());
    }
}

/* END CODE */