Guest User

Radio Source Main

a guest
Jul 27th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #include "radio.h"
  2.  
  3. void setup()
  4. {
  5. Serial.begin(115200);
  6. Serial.println("Serial Txd is on pin: "+String(TX));
  7. Serial.println("Serial Rxd is on pin: "+String(RX));
  8.  
  9. pinMode(33, OUTPUT);
  10. digitalWrite(33, HIGH); // pin 33 for channel select
  11.  
  12. SPI.begin(SD_sck, SD_miso, SD_mosi, SD_cs);
  13.  
  14. a2dp_sink.set_avrc_metadata_attribute_mask(ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_GENRE);
  15. a2dp_sink.set_avrc_metadata_callback(avrc_metadata_callback);
  16. a2dp_sink.start("ESP32_Bluetooth", false);
  17.  
  18. // Non sound initializaions ===================================================
  19. pinMode(RE_CLK,INPUT_PULLUP);
  20. pinMode(RE_DT,INPUT_PULLUP);
  21. aLastState = digitalRead(RE_CLK);
  22.  
  23. btn.onPress(pressHandler)
  24. .onDoublePress(pressHandler) // default timeout
  25. .onPressFor(pressHandler, 1000); // custom timeout for 1 second
  26.  
  27. pinMode(LED_red, OUTPUT);
  28. // digitalWrite(LED_red, HIGH);
  29. pinMode(LED_green, OUTPUT);
  30. // digitalWrite(LED_green, HIGH);
  31. pinMode(LED_blue, OUTPUT);
  32. // digitalWrite(LED_blue, HIGH);
  33. }
  34.  
  35. void loop()
  36. {
  37. aState = digitalRead(RE_CLK);
  38. btn.read();
  39.  
  40. if((currentState == initBluetooth) ||
  41. (currentState == bluetoothMode) ||
  42. (currentState == deInitBluetooth))
  43. {
  44. handleBluetooth();
  45. }
  46. else if((currentState == initSDCard) ||
  47. (currentState == SDcardMode) ||
  48. (currentState == deInitSDCard))
  49. {
  50. handleSDcard();
  51. }
  52.  
  53. //Encoder rotation tracking
  54. if (aState != aLastState)
  55. {
  56. if (digitalRead(RE_DT) != aState)
  57. {
  58. counter ++;
  59. }
  60. else
  61. {
  62. counter--;
  63. }
  64. if (counter >= max_RE_Value )
  65. {
  66. counter = max_RE_Value;
  67. }
  68. if (counter <= min_RE_Value )
  69. {
  70. counter = min_RE_Value;
  71. }
  72.  
  73. Serial.printf("RE counter: %d\n", counter);
  74. }
  75. aLastState = aState;
  76. }
  77.  
  78. void handleBluetooth()
  79. {
  80. digitalWrite(LED_red, LOW);
  81. digitalWrite(LED_green, LOW);
  82. digitalWrite(LED_blue, HIGH);
  83. if (currentState == initBluetooth)
  84. {
  85. a2dp_sink.end();
  86. delay(20);
  87.  
  88. BTConfig.copyFrom(BlueToothAudioInfo);
  89. BTConfig.port_no = 1;
  90. BTConfig.pin_bck = 26; // BCLK
  91. BTConfig.pin_ws = 25; // LRC
  92. BTConfig.pin_data = 22; // DIN
  93. BTConfig.buffer_count = 2;
  94. BTConfig.buffer_size = 1024;
  95.  
  96. i2s.begin(BTConfig);
  97. a2dp_sink.start("ESP32_Bluetooth", false);
  98. a2dp_sink.set_output(i2s);
  99.  
  100. currentState = bluetoothMode;
  101. Serial.printf("Bluetooth init done\n");
  102. }
  103. else if (currentState == bluetoothMode)
  104. {
  105. }
  106. else if (currentState == deInitBluetooth)
  107. {
  108. a2dp_sink.pause();
  109. delay(20);
  110. a2dp_sink.set_output(i2s_NoOutput);
  111. a2dp_sink.set_connected(false);
  112. a2dp_sink.end();
  113. delay(20);
  114. i2s.end();
  115. delay(20);
  116. currentState = initSDCard;
  117. }
  118. }
  119.  
  120. void handleSDcard()
  121. {
  122. digitalWrite(LED_red, LOW);
  123. digitalWrite(LED_green, HIGH);
  124. digitalWrite(LED_blue, LOW);
  125. if (currentState == initSDCard)
  126. {
  127. SDConfig.port_no = 1;
  128. SDConfig.pin_bck = 26; // BCLK
  129. SDConfig.pin_ws = 25; // LRC
  130. SDConfig.pin_data = 22; // DIN
  131. SDConfig.buffer_count = 2;
  132. SDConfig.buffer_size = 1024;
  133.  
  134. i2s.begin(SDConfig);
  135. delay(20);
  136. player.setMetadataCallback(printMetaData);
  137. player.begin();
  138. delay(20);
  139. player.setVolume(0.3);
  140. Serial.printf("SD card init done\n");
  141. currentState = SDcardMode;
  142. }
  143. else if (currentState == SDcardMode)
  144. {
  145. if (!player.copy())
  146. {
  147. player.begin();
  148. Serial.printf("SD card song restarting\n");
  149. }
  150. }
  151. else if (currentState == deInitSDCard)
  152. {
  153. Serial.printf("SD card deInit start\n");
  154. player.stop();
  155. i2s.end();
  156. delay(20);
  157. currentState = initBluetooth;
  158. Serial.printf("SD card deInit end\n");
  159. }
  160. }
  161.  
  162. void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
  163. // Serial.printf("AVRC metadata rsp: attribute id 0x%x, %s\n", data1, data2);
  164. parseBluetoothData(data1, data2);
  165. tftNeedsUpdate = true;
  166. }
  167.  
  168. void parseBluetoothData(uint8_t address, const uint8_t *data)
  169. {
  170. if(address == 0x1) // clear out old info when new song is recieved;
  171. {
  172. currentSongTitle = "";
  173. currentSongArtist = "";
  174. currentSongAlbum = "";
  175. currentSongGenre = "";
  176. }
  177. switch (address)
  178. {
  179. case 0x1:
  180. currentSongTitle = reinterpret_cast<const char*>(data);
  181. break;
  182. case 0x2:
  183. currentSongArtist = reinterpret_cast<const char*>(data);
  184. break;
  185. case 0x4:
  186. currentSongAlbum = reinterpret_cast<const char*>(data);
  187. break;
  188. case 0x20:
  189. currentSongGenre = reinterpret_cast<const char*>(data);
  190. break;
  191. }
  192. }
  193.  
  194. void pressHandler (BfButton *btn, BfButton::press_pattern_t pattern) {
  195. Serial.print(btn->getID());
  196. switch (pattern) {
  197. case BfButton::SINGLE_PRESS:
  198. Serial.println(" single pressed.");
  199. if(currentState == bluetoothMode)
  200. {
  201. currentState = deInitBluetooth;
  202. }
  203. else if(currentState == SDcardMode)
  204. {
  205. currentState = deInitSDCard;
  206. }
  207. Serial.printf("Playback mode: %s\n", currentState == bluetoothMode ? "Bluetooth" : "SD-Card");
  208. break;
  209. case BfButton::DOUBLE_PRESS:
  210. Serial.println(" double pressed.");
  211. break;
  212. case BfButton::LONG_PRESS:
  213. Serial.println(" long pressed.");
  214. break;
  215. }
  216. }
  217.  
  218. void printMetaData(MetaDataType type, const char* str, int len){
  219. Serial.print("==> ");
  220. Serial.print(toStr(type));
  221. Serial.print(": ");
  222. Serial.println(str);
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
Advertisement
Add Comment
Please, Sign In to add comment