Advertisement
pleasedontcode

Audio Interface rev_06

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