CCCPaste

Audio Menu rev_04

/********* 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 Menu
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-10-15 21:16:25

********* 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 *****/


/********* User code review feedback **********
#### Feedback 1 ####
- add Ticker.h for ms,menu cases display draw menu0-4 add menu set
tings ,audio,clock, rotary handle volume ,bass,treble,gain,loud,
mute,in,i,bal_l,bal_r
#### Feedback 2 ####
- add 
// Function prototypes
void IRAM_ATTR encoderISR(); // Inte
rrupt Service Routine for encoder
void IRAM_ATTR buttonISR();  /
/ Interrupt Service Routine for button press 
void updateDisplay
Ticker();   // Function to update display using Ticker
void load
Settings();          // Function to load set
#### Feedback 3 ####
- add 
// Rotary encoder object
Rotary myEnc(encoderPinA, encoderP
inB);

// Audio settings
int volume = 10;
int bass = 0;
int treb
le = 0;
int gain = 0;
int balanceL = 100; // Left balance (bal_l
)
int balanceR = 100; // Right balance (bal_r)
bool mute = false
;
bool loudness = false;
int inputSelect
********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include   // Library for rotary encoder
#include        // TFT library for ILI9488 display
#include          // Library for PT2314 audio processor
#include          // Library for timing events

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void IRAM_ATTR encoderISR(); // Interrupt Service Routine for encoder
void IRAM_ATTR buttonISR();  // Interrupt Service Routine for button press 
void updateDisplayTicker();   // Function to update display using Ticker
void loadSettings();          // Function to load settings

// Menu states
enum MenuState {
    MENU_MAIN,
    MENU_SETTINGS,
    MENU_AUDIO,
    MENU_CLOCK,
    MENU_EXIT
};

// Current menu state
MenuState currentMenu = MENU_MAIN;

/***** 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
Ticker ticker; // Ticker instance for periodic tasks

// Rotary encoder object
Rotary myEnc(ro_KY_040_CLK_PIN_D4, ro_KY_040_DT_PIN_D13); // Initialize rotary encoder

// Audio settings variables
int volume = 10;          // Volume level
int bass = 0;            // Bass level
int treble = 0;          // Treble level
int gain = 0;            // Gain level
int balanceL = 100;      // Left balance (bal_l)
int balanceR = 100;      // Right balance (bal_r)
bool mute = false;       // Mute status
bool loudness = false;   // Loudness status
int inputSelect = 0;     // Input selection

// Function to draw the main menu
void drawMenu() {
    tft.fillScreen(TFT_BLACK); // Clear screen
    tft.setTextColor(TFT_WHITE);
    tft.setTextSize(2);
    tft.setCursor(10, 10);
    
    switch (currentMenu) {
        case MENU_MAIN:
            tft.println("Main Menu");
            tft.println("1. Settings");
            tft.println("2. Audio");
            tft.println("3. Clock");
            tft.println("4. Exit");
            break;
        case MENU_SETTINGS:
            tft.println("Settings Menu");
            tft.println("Adjust settings");
            break;
        case MENU_AUDIO:
            tft.println("Audio Menu");
            tft.printf("Volume: %d\nBass: %d\nTreble: %d\n", volume, bass, treble);
            break;
        case MENU_CLOCK:
            tft.println("Clock Menu");
            tft.println("Set the time");
            break;
        case MENU_EXIT:
            tft.println("Exiting...");
            break;
    }
}

// Function to update audio settings based on encoder value
void updateAudioSettings(int value) {
    if (value >= 0 && value <= 63) {
        audioProcessor.setVolume(value); // Set volume based on encoder value
        volume = value; // Update local variable
    } else if (value > 63 && value <= 127) {
        audioProcessor.setBass(value - 64); // Set bass if value exceeds 63
        bass = value - 64; // Update local variable
    } else if (value < 0 && value >= -63) {
        audioProcessor.setTreble(-value); // Set treble if value is negative
        treble = -value; // Update local variable
    }
}

// Interrupt Service Routine for encoder
void IRAM_ATTR encoderISR() {
    encoder.update(); // Update encoder state
}

// Interrupt Service Routine for button press
void IRAM_ATTR buttonISR() {
    encoder.buttonPressed(); // Handle button press
}

// Function to update display using Ticker
void updateDisplayTicker() {
    drawMenu(); // Redraw the menu periodically
}

// Function to load settings (placeholder for future implementation)
void loadSettings() {
    // Load settings from EEPROM or other storage
}

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(volume); // Set initial volume
    audioProcessor.setBass(bass);     // Set initial bass
    audioProcessor.setTreble(treble); // Set initial treble
    audioProcessor.setMute(mute);      // Unmute audio

    // Attach interrupt service routines
    attachInterrupt(digitalPinToInterrupt(ro_KY_040_CLK_PIN_D4), encoderISR, CHANGE);
    attachInterrupt(digitalPinToInterrupt(ro_KY_040_SW_PIN_D14), buttonISR, FALLING);

    // Draw the initial menu
    drawMenu();

    // Start the ticker to update the display periodically
    ticker.attach(1.0, updateDisplayTicker); // Update display every second
}

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
        updateAudioSettings(value);

        // Update the display with the current settings
        drawMenu();
    }

    // Handle menu navigation
    if (encoder.BUTTON_PRESSED) {
        // Change menu state based on current state
        switch (currentMenu) {
            case MENU_MAIN:
                currentMenu = MENU_SETTINGS; // Go to settings menu
                break;
            case MENU_SETTINGS:
                currentMenu = MENU_AUDIO; // Go to audio menu
                break;
            case MENU_AUDIO:
                currentMenu = MENU_CLOCK; // Go to clock menu
                break;
            case MENU_CLOCK:
                currentMenu = MENU_EXIT; // Go to exit menu
                break;
            case MENU_EXIT:
                currentMenu = MENU_MAIN; // Return to main menu
                break;
        }
        drawMenu(); // Redraw the menu
    }
}

/* END CODE */