Advertisement
pleasedontcode

Audio Control rev_02

Oct 15th, 2024
63
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 Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-15 21:08:40
  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. ********* User code review feedback **********/
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <SimpleEncoder.h>  // Library for rotary encoder
  36. #include <TFT_eSPI.h>       // TFT library for ILI9488 display
  37. #include <PT2314.h>         // Library for PT2314 audio processor
  38. #include <Ticker.h>         // Library for timing events
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43.  
  44. // Menu states
  45. enum MenuState {
  46.     MENU_MAIN,
  47.     MENU_SETTINGS,
  48.     MENU_AUDIO,
  49.     MENU_CLOCK,
  50.     MENU_EXIT
  51. };
  52.  
  53. // Current menu state
  54. MenuState currentMenu = MENU_MAIN;
  55.  
  56. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  57. const uint8_t ro_KY_040_CLK_PIN_D4 = 4;    // Rotary encoder CLK pin
  58. const uint8_t ro_KY_040_DT_PIN_D13 = 13;   // Rotary encoder DT pin
  59. const uint8_t ro_KY_040_SW_PIN_D14 = 14;   // Rotary encoder switch pin
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  62. SimpleEncoder encoder(ro_KY_040_SW_PIN_D14, ro_KY_040_CLK_PIN_D4, ro_KY_040_DT_PIN_D13); // Encoder instance
  63. TFT_eSPI tft = TFT_eSPI(); // TFT display instance
  64. PT2314 audioProcessor; // PT2314 audio processor instance
  65. Ticker ticker; // Ticker instance for periodic tasks
  66.  
  67. // Audio settings variables
  68. int volume = 30;
  69. int bass = 0;
  70. int treble = 0;
  71. int gain = 0;
  72. bool loudness = false;
  73. bool mute = false;
  74.  
  75. // Function to draw the main menu
  76. void drawMenu() {
  77.     tft.fillScreen(TFT_BLACK); // Clear screen
  78.     tft.setTextColor(TFT_WHITE);
  79.     tft.setTextSize(2);
  80.     tft.setCursor(10, 10);
  81.    
  82.     switch (currentMenu) {
  83.         case MENU_MAIN:
  84.             tft.println("Main Menu");
  85.             tft.println("1. Settings");
  86.             tft.println("2. Audio");
  87.             tft.println("3. Clock");
  88.             tft.println("4. Exit");
  89.             break;
  90.         case MENU_SETTINGS:
  91.             tft.println("Settings Menu");
  92.             tft.println("Adjust settings");
  93.             break;
  94.         case MENU_AUDIO:
  95.             tft.println("Audio Menu");
  96.             tft.printf("Volume: %d\nBass: %d\nTreble: %d\n", volume, bass, treble);
  97.             break;
  98.         case MENU_CLOCK:
  99.             tft.println("Clock Menu");
  100.             tft.println("Set the time");
  101.             break;
  102.         case MENU_EXIT:
  103.             tft.println("Exiting...");
  104.             break;
  105.     }
  106. }
  107.  
  108. // Function to update audio settings based on encoder value
  109. void updateAudioSettings(int value) {
  110.     if (value >= 0 && value <= 63) {
  111.         audioProcessor.setVolume(value); // Set volume based on encoder value
  112.         volume = value; // Update local variable
  113.     } else if (value > 63 && value <= 127) {
  114.         audioProcessor.setBass(value - 64); // Set bass if value exceeds 63
  115.         bass = value - 64; // Update local variable
  116.     } else if (value < 0 && value >= -63) {
  117.         audioProcessor.setTreble(-value); // Set treble if value is negative
  118.         treble = -value; // Update local variable
  119.     }
  120. }
  121.  
  122. void setup(void)
  123. {
  124.     // Initialize serial communication for debugging
  125.     Serial.begin(9600);
  126.  
  127.     // Initialize the TFT display
  128.     tft.init();
  129.     tft.setRotation(1); // Set rotation of the display
  130.     tft.fillScreen(TFT_BLACK); // Clear screen with black color
  131.  
  132.     // Initialize the audio processor
  133.     if (audioProcessor.begin()) {
  134.         Serial.println("PT2314 initialized successfully.");
  135.     } else {
  136.         Serial.println("Failed to initialize PT2314.");
  137.     }
  138.  
  139.     // Set initial audio settings
  140.     audioProcessor.setVolume(volume); // Set initial volume
  141.     audioProcessor.setBass(bass);     // Set initial bass
  142.     audioProcessor.setTreble(treble); // Set initial treble
  143.     audioProcessor.setMute(mute);      // Unmute audio
  144.  
  145.     // Draw the initial menu
  146.     drawMenu();
  147. }
  148.  
  149. void loop(void)
  150. {
  151.     // Check if the encoder value is changing
  152.     if (encoder.CHANGING) {
  153.         // Get the current value from the encoder
  154.         int value = encoder.VALUE;
  155.  
  156.         // Update audio settings based on encoder value
  157.         updateAudioSettings(value);
  158.  
  159.         // Update the display with the current settings
  160.         drawMenu();
  161.     }
  162.  
  163.     // Handle menu navigation
  164.     if (encoder.BUTTON_PRESSED) {
  165.         // Change menu state based on current state
  166.         switch (currentMenu) {
  167.             case MENU_MAIN:
  168.                 currentMenu = MENU_SETTINGS; // Go to settings menu
  169.                 break;
  170.             case MENU_SETTINGS:
  171.                 currentMenu = MENU_AUDIO; // Go to audio menu
  172.                 break;
  173.             case MENU_AUDIO:
  174.                 currentMenu = MENU_CLOCK; // Go to clock menu
  175.                 break;
  176.             case MENU_CLOCK:
  177.                 currentMenu = MENU_EXIT; // Go to exit menu
  178.                 break;
  179.             case MENU_EXIT:
  180.                 currentMenu = MENU_MAIN; // Return to main menu
  181.                 break;
  182.         }
  183.         drawMenu(); // Redraw the menu
  184.     }
  185. }
  186.  
  187. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement