Advertisement
pleasedontcode

Audio Menu rev_04

Oct 15th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Audio Menu
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-15 21:16:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* use TFT ILI9488 SPI and PT2314 chip for audio int */
  21.     /* vol,bass,treble,gain,loudness,mute,attnR,attnL */
  22.     /* channel,bal_l,bal_r menu,options update display */
  23.     /* rotar values */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - add Ticker.h for ms,menu cases display draw menu0-4 add menu set
  30. tings ,audio,clock, rotary handle volume ,bass,treble,gain,loud,
  31. mute,in,i,bal_l,bal_r
  32. #### Feedback 2 ####
  33. - add
  34. // Function prototypes
  35. void IRAM_ATTR encoderISR(); // Inte
  36. rrupt Service Routine for encoder
  37. void IRAM_ATTR buttonISR();  /
  38. / Interrupt Service Routine for button press
  39. void updateDisplay
  40. Ticker();   // Function to update display using Ticker
  41. void load
  42. Settings();          // Function to load set
  43. #### Feedback 3 ####
  44. - add
  45. // Rotary encoder object
  46. Rotary myEnc(encoderPinA, encoderP
  47. inB);
  48.  
  49. // Audio settings
  50. int volume = 10;
  51. int bass = 0;
  52. int treb
  53. le = 0;
  54. int gain = 0;
  55. int balanceL = 100; // Left balance (bal_l
  56. )
  57. int balanceR = 100; // Right balance (bal_r)
  58. bool mute = false
  59. ;
  60. bool loudness = false;
  61. int inputSelect
  62. ********* User code review feedback **********/
  63.  
  64. /****** DEFINITION OF LIBRARIES *****/
  65. #include <SimpleEncoder.h>  // Library for rotary encoder
  66. #include <TFT_eSPI.h>       // TFT library for ILI9488 display
  67. #include <PT2314.h>         // Library for PT2314 audio processor
  68. #include <Ticker.h>         // Library for timing events
  69.  
  70. /****** FUNCTION PROTOTYPES *****/
  71. void setup(void);
  72. void loop(void);
  73. void IRAM_ATTR encoderISR(); // Interrupt Service Routine for encoder
  74. void IRAM_ATTR buttonISR();  // Interrupt Service Routine for button press
  75. void updateDisplayTicker();   // Function to update display using Ticker
  76. void loadSettings();          // Function to load settings
  77.  
  78. // Menu states
  79. enum MenuState {
  80.     MENU_MAIN,
  81.     MENU_SETTINGS,
  82.     MENU_AUDIO,
  83.     MENU_CLOCK,
  84.     MENU_EXIT
  85. };
  86.  
  87. // Current menu state
  88. MenuState currentMenu = MENU_MAIN;
  89.  
  90. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  91. const uint8_t ro_KY_040_CLK_PIN_D4 = 4;    // Rotary encoder CLK pin
  92. const uint8_t ro_KY_040_DT_PIN_D13 = 13;   // Rotary encoder DT pin
  93. const uint8_t ro_KY_040_SW_PIN_D14 = 14;   // Rotary encoder switch pin
  94.  
  95. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  96. SimpleEncoder encoder(ro_KY_040_SW_PIN_D14, ro_KY_040_CLK_PIN_D4, ro_KY_040_DT_PIN_D13); // Encoder instance
  97. TFT_eSPI tft = TFT_eSPI(); // TFT display instance
  98. PT2314 audioProcessor; // PT2314 audio processor instance
  99. Ticker ticker; // Ticker instance for periodic tasks
  100.  
  101. // Rotary encoder object
  102. Rotary myEnc(ro_KY_040_CLK_PIN_D4, ro_KY_040_DT_PIN_D13); // Initialize rotary encoder
  103.  
  104. // Audio settings variables
  105. int volume = 10;          // Volume level
  106. int bass = 0;            // Bass level
  107. int treble = 0;          // Treble level
  108. int gain = 0;            // Gain level
  109. int balanceL = 100;      // Left balance (bal_l)
  110. int balanceR = 100;      // Right balance (bal_r)
  111. bool mute = false;       // Mute status
  112. bool loudness = false;   // Loudness status
  113. int inputSelect = 0;     // Input selection
  114.  
  115. // Function to draw the main menu
  116. void drawMenu() {
  117.     tft.fillScreen(TFT_BLACK); // Clear screen
  118.     tft.setTextColor(TFT_WHITE);
  119.     tft.setTextSize(2);
  120.     tft.setCursor(10, 10);
  121.    
  122.     switch (currentMenu) {
  123.         case MENU_MAIN:
  124.             tft.println("Main Menu");
  125.             tft.println("1. Settings");
  126.             tft.println("2. Audio");
  127.             tft.println("3. Clock");
  128.             tft.println("4. Exit");
  129.             break;
  130.         case MENU_SETTINGS:
  131.             tft.println("Settings Menu");
  132.             tft.println("Adjust settings");
  133.             break;
  134.         case MENU_AUDIO:
  135.             tft.println("Audio Menu");
  136.             tft.printf("Volume: %d\nBass: %d\nTreble: %d\n", volume, bass, treble);
  137.             break;
  138.         case MENU_CLOCK:
  139.             tft.println("Clock Menu");
  140.             tft.println("Set the time");
  141.             break;
  142.         case MENU_EXIT:
  143.             tft.println("Exiting...");
  144.             break;
  145.     }
  146. }
  147.  
  148. // Function to update audio settings based on encoder value
  149. void updateAudioSettings(int value) {
  150.     if (value >= 0 && value <= 63) {
  151.         audioProcessor.setVolume(value); // Set volume based on encoder value
  152.         volume = value; // Update local variable
  153.     } else if (value > 63 && value <= 127) {
  154.         audioProcessor.setBass(value - 64); // Set bass if value exceeds 63
  155.         bass = value - 64; // Update local variable
  156.     } else if (value < 0 && value >= -63) {
  157.         audioProcessor.setTreble(-value); // Set treble if value is negative
  158.         treble = -value; // Update local variable
  159.     }
  160. }
  161.  
  162. // Interrupt Service Routine for encoder
  163. void IRAM_ATTR encoderISR() {
  164.     encoder.update(); // Update encoder state
  165. }
  166.  
  167. // Interrupt Service Routine for button press
  168. void IRAM_ATTR buttonISR() {
  169.     encoder.buttonPressed(); // Handle button press
  170. }
  171.  
  172. // Function to update display using Ticker
  173. void updateDisplayTicker() {
  174.     drawMenu(); // Redraw the menu periodically
  175. }
  176.  
  177. // Function to load settings (placeholder for future implementation)
  178. void loadSettings() {
  179.     // Load settings from EEPROM or other storage
  180. }
  181.  
  182. void setup(void)
  183. {
  184.     // Initialize serial communication for debugging
  185.     Serial.begin(9600);
  186.  
  187.     // Initialize the TFT display
  188.     tft.init();
  189.     tft.setRotation(1); // Set rotation of the display
  190.     tft.fillScreen(TFT_BLACK); // Clear screen with black color
  191.  
  192.     // Initialize the audio processor
  193.     if (audioProcessor.begin()) {
  194.         Serial.println("PT2314 initialized successfully.");
  195.     } else {
  196.         Serial.println("Failed to initialize PT2314.");
  197.     }
  198.  
  199.     // Set initial audio settings
  200.     audioProcessor.setVolume(volume); // Set initial volume
  201.     audioProcessor.setBass(bass);     // Set initial bass
  202.     audioProcessor.setTreble(treble); // Set initial treble
  203.     audioProcessor.setMute(mute);      // Unmute audio
  204.  
  205.     // Attach interrupt service routines
  206.     attachInterrupt(digitalPinToInterrupt(ro_KY_040_CLK_PIN_D4), encoderISR, CHANGE);
  207.     attachInterrupt(digitalPinToInterrupt(ro_KY_040_SW_PIN_D14), buttonISR, FALLING);
  208.  
  209.     // Draw the initial menu
  210.     drawMenu();
  211.  
  212.     // Start the ticker to update the display periodically
  213.     ticker.attach(1.0, updateDisplayTicker); // Update display every second
  214. }
  215.  
  216. void loop(void)
  217. {
  218.     // Check if the encoder value is changing
  219.     if (encoder.CHANGING) {
  220.         // Get the current value from the encoder
  221.         int value = encoder.VALUE;
  222.  
  223.         // Update audio settings based on encoder value
  224.         updateAudioSettings(value);
  225.  
  226.         // Update the display with the current settings
  227.         drawMenu();
  228.     }
  229.  
  230.     // Handle menu navigation
  231.     if (encoder.BUTTON_PRESSED) {
  232.         // Change menu state based on current state
  233.         switch (currentMenu) {
  234.             case MENU_MAIN:
  235.                 currentMenu = MENU_SETTINGS; // Go to settings menu
  236.                 break;
  237.             case MENU_SETTINGS:
  238.                 currentMenu = MENU_AUDIO; // Go to audio menu
  239.                 break;
  240.             case MENU_AUDIO:
  241.                 currentMenu = MENU_CLOCK; // Go to clock menu
  242.                 break;
  243.             case MENU_CLOCK:
  244.                 currentMenu = MENU_EXIT; // Go to exit menu
  245.                 break;
  246.             case MENU_EXIT:
  247.                 currentMenu = MENU_MAIN; // Return to main menu
  248.                 break;
  249.         }
  250.         drawMenu(); // Redraw the menu
  251.     }
  252. }
  253.  
  254. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement