Advertisement
pleasedontcode

Audio Interface rev_05

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