Advertisement
pleasedontcode

Audio Navigation rev_03

Oct 15th, 2024
66
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 Navigation
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-15 21:13:20
  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. ********* User code review feedback **********/
  44.  
  45. /****** DEFINITION OF LIBRARIES *****/
  46. #include <SimpleEncoder.h>  // Library for rotary encoder
  47. #include <TFT_eSPI.h>       // TFT library for ILI9488 display
  48. #include <PT2314.h>         // Library for PT2314 audio processor
  49. #include <Ticker.h>         // Library for timing events
  50.  
  51. /****** FUNCTION PROTOTYPES *****/
  52. void setup(void);
  53. void loop(void);
  54. void IRAM_ATTR encoderISR(); // Interrupt Service Routine for encoder
  55. void IRAM_ATTR buttonISR();  // Interrupt Service Routine for button press
  56. void updateDisplayTicker();   // Function to update display using Ticker
  57. void loadSettings();          // Function to load settings
  58.  
  59. // Menu states
  60. enum MenuState {
  61.     MENU_MAIN,
  62.     MENU_SETTINGS,
  63.     MENU_AUDIO,
  64.     MENU_CLOCK,
  65.     MENU_EXIT
  66. };
  67.  
  68. // Current menu state
  69. MenuState currentMenu = MENU_MAIN;
  70.  
  71. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  72. const uint8_t ro_KY_040_CLK_PIN_D4 = 4;    // Rotary encoder CLK pin
  73. const uint8_t ro_KY_040_DT_PIN_D13 = 13;   // Rotary encoder DT pin
  74. const uint8_t ro_KY_040_SW_PIN_D14 = 14;   // Rotary encoder switch pin
  75.  
  76. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  77. SimpleEncoder encoder(ro_KY_040_SW_PIN_D14, ro_KY_040_CLK_PIN_D4, ro_KY_040_DT_PIN_D13); // Encoder instance
  78. TFT_eSPI tft = TFT_eSPI(); // TFT display instance
  79. PT2314 audioProcessor; // PT2314 audio processor instance
  80. Ticker ticker; // Ticker instance for periodic tasks
  81.  
  82. // Audio settings variables
  83. int volume = 30;
  84. int bass = 0;
  85. int treble = 0;
  86. int gain = 0;
  87. bool loudness = false;
  88. bool mute = false;
  89.  
  90. // Function to draw the main menu
  91. void drawMenu() {
  92.     tft.fillScreen(TFT_BLACK); // Clear screen
  93.     tft.setTextColor(TFT_WHITE);
  94.     tft.setTextSize(2);
  95.     tft.setCursor(10, 10);
  96.    
  97.     switch (currentMenu) {
  98.         case MENU_MAIN:
  99.             tft.println("Main Menu");
  100.             tft.println("1. Settings");
  101.             tft.println("2. Audio");
  102.             tft.println("3. Clock");
  103.             tft.println("4. Exit");
  104.             break;
  105.         case MENU_SETTINGS:
  106.             tft.println("Settings Menu");
  107.             tft.println("Adjust settings");
  108.             break;
  109.         case MENU_AUDIO:
  110.             tft.println("Audio Menu");
  111.             tft.printf("Volume: %d\nBass: %d\nTreble: %d\n", volume, bass, treble);
  112.             break;
  113.         case MENU_CLOCK:
  114.             tft.println("Clock Menu");
  115.             tft.println("Set the time");
  116.             break;
  117.         case MENU_EXIT:
  118.             tft.println("Exiting...");
  119.             break;
  120.     }
  121. }
  122.  
  123. // Function to update audio settings based on encoder value
  124. void updateAudioSettings(int value) {
  125.     if (value >= 0 && value <= 63) {
  126.         audioProcessor.setVolume(value); // Set volume based on encoder value
  127.         volume = value; // Update local variable
  128.     } else if (value > 63 && value <= 127) {
  129.         audioProcessor.setBass(value - 64); // Set bass if value exceeds 63
  130.         bass = value - 64; // Update local variable
  131.     } else if (value < 0 && value >= -63) {
  132.         audioProcessor.setTreble(-value); // Set treble if value is negative
  133.         treble = -value; // Update local variable
  134.     }
  135. }
  136.  
  137. // Interrupt Service Routine for encoder
  138. void IRAM_ATTR encoderISR() {
  139.     encoder.update(); // Update encoder state
  140. }
  141.  
  142. // Interrupt Service Routine for button press
  143. void IRAM_ATTR buttonISR() {
  144.     encoder.buttonPressed(); // Handle button press
  145. }
  146.  
  147. // Function to update display using Ticker
  148. void updateDisplayTicker() {
  149.     drawMenu(); // Redraw the menu periodically
  150. }
  151.  
  152. // Function to load settings (placeholder for future implementation)
  153. void loadSettings() {
  154.     // Load settings from EEPROM or other storage
  155. }
  156.  
  157. void setup(void)
  158. {
  159.     // Initialize serial communication for debugging
  160.     Serial.begin(9600);
  161.  
  162.     // Initialize the TFT display
  163.     tft.init();
  164.     tft.setRotation(1); // Set rotation of the display
  165.     tft.fillScreen(TFT_BLACK); // Clear screen with black color
  166.  
  167.     // Initialize the audio processor
  168.     if (audioProcessor.begin()) {
  169.         Serial.println("PT2314 initialized successfully.");
  170.     } else {
  171.         Serial.println("Failed to initialize PT2314.");
  172.     }
  173.  
  174.     // Set initial audio settings
  175.     audioProcessor.setVolume(volume); // Set initial volume
  176.     audioProcessor.setBass(bass);     // Set initial bass
  177.     audioProcessor.setTreble(treble); // Set initial treble
  178.     audioProcessor.setMute(mute);      // Unmute audio
  179.  
  180.     // Attach interrupt service routines
  181.     attachInterrupt(digitalPinToInterrupt(ro_KY_040_CLK_PIN_D4), encoderISR, CHANGE);
  182.     attachInterrupt(digitalPinToInterrupt(ro_KY_040_SW_PIN_D14), buttonISR, FALLING);
  183.  
  184.     // Draw the initial menu
  185.     drawMenu();
  186.  
  187.     // Start the ticker to update the display periodically
  188.     ticker.attach(1.0, updateDisplayTicker); // Update display every second
  189. }
  190.  
  191. void loop(void)
  192. {
  193.     // Check if the encoder value is changing
  194.     if (encoder.CHANGING) {
  195.         // Get the current value from the encoder
  196.         int value = encoder.VALUE;
  197.  
  198.         // Update audio settings based on encoder value
  199.         updateAudioSettings(value);
  200.  
  201.         // Update the display with the current settings
  202.         drawMenu();
  203.     }
  204.  
  205.     // Handle menu navigation
  206.     if (encoder.BUTTON_PRESSED) {
  207.         // Change menu state based on current state
  208.         switch (currentMenu) {
  209.             case MENU_MAIN:
  210.                 currentMenu = MENU_SETTINGS; // Go to settings menu
  211.                 break;
  212.             case MENU_SETTINGS:
  213.                 currentMenu = MENU_AUDIO; // Go to audio menu
  214.                 break;
  215.             case MENU_AUDIO:
  216.                 currentMenu = MENU_CLOCK; // Go to clock menu
  217.                 break;
  218.             case MENU_CLOCK:
  219.                 currentMenu = MENU_EXIT; // Go to exit menu
  220.                 break;
  221.             case MENU_EXIT:
  222.                 currentMenu = MENU_MAIN; // Return to main menu
  223.                 break;
  224.         }
  225.         drawMenu(); // Redraw the menu
  226.     }
  227. }
  228.  
  229. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement