Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "radio.h"
- void setup()
- {
- Serial.begin(115200);
- Serial.println("Serial Txd is on pin: "+String(TX));
- Serial.println("Serial Rxd is on pin: "+String(RX));
- pinMode(33, OUTPUT);
- digitalWrite(33, HIGH); // pin 33 for channel select
- SPI.begin(SD_sck, SD_miso, SD_mosi, SD_cs);
- 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);
- a2dp_sink.set_avrc_metadata_callback(avrc_metadata_callback);
- a2dp_sink.start("ESP32_Bluetooth", false);
- // Non sound initializaions ===================================================
- pinMode(RE_CLK,INPUT_PULLUP);
- pinMode(RE_DT,INPUT_PULLUP);
- aLastState = digitalRead(RE_CLK);
- btn.onPress(pressHandler)
- .onDoublePress(pressHandler) // default timeout
- .onPressFor(pressHandler, 1000); // custom timeout for 1 second
- pinMode(LED_red, OUTPUT);
- // digitalWrite(LED_red, HIGH);
- pinMode(LED_green, OUTPUT);
- // digitalWrite(LED_green, HIGH);
- pinMode(LED_blue, OUTPUT);
- // digitalWrite(LED_blue, HIGH);
- }
- void loop()
- {
- aState = digitalRead(RE_CLK);
- btn.read();
- if((currentState == initBluetooth) ||
- (currentState == bluetoothMode) ||
- (currentState == deInitBluetooth))
- {
- handleBluetooth();
- }
- else if((currentState == initSDCard) ||
- (currentState == SDcardMode) ||
- (currentState == deInitSDCard))
- {
- handleSDcard();
- }
- //Encoder rotation tracking
- if (aState != aLastState)
- {
- if (digitalRead(RE_DT) != aState)
- {
- counter ++;
- }
- else
- {
- counter--;
- }
- if (counter >= max_RE_Value )
- {
- counter = max_RE_Value;
- }
- if (counter <= min_RE_Value )
- {
- counter = min_RE_Value;
- }
- Serial.printf("RE counter: %d\n", counter);
- }
- aLastState = aState;
- }
- void handleBluetooth()
- {
- digitalWrite(LED_red, LOW);
- digitalWrite(LED_green, LOW);
- digitalWrite(LED_blue, HIGH);
- if (currentState == initBluetooth)
- {
- a2dp_sink.end();
- delay(20);
- BTConfig.copyFrom(BlueToothAudioInfo);
- BTConfig.port_no = 1;
- BTConfig.pin_bck = 26; // BCLK
- BTConfig.pin_ws = 25; // LRC
- BTConfig.pin_data = 22; // DIN
- BTConfig.buffer_count = 2;
- BTConfig.buffer_size = 1024;
- i2s.begin(BTConfig);
- a2dp_sink.start("ESP32_Bluetooth", false);
- a2dp_sink.set_output(i2s);
- currentState = bluetoothMode;
- Serial.printf("Bluetooth init done\n");
- }
- else if (currentState == bluetoothMode)
- {
- }
- else if (currentState == deInitBluetooth)
- {
- a2dp_sink.pause();
- delay(20);
- a2dp_sink.set_output(i2s_NoOutput);
- a2dp_sink.set_connected(false);
- a2dp_sink.end();
- delay(20);
- i2s.end();
- delay(20);
- currentState = initSDCard;
- }
- }
- void handleSDcard()
- {
- digitalWrite(LED_red, LOW);
- digitalWrite(LED_green, HIGH);
- digitalWrite(LED_blue, LOW);
- if (currentState == initSDCard)
- {
- SDConfig.port_no = 1;
- SDConfig.pin_bck = 26; // BCLK
- SDConfig.pin_ws = 25; // LRC
- SDConfig.pin_data = 22; // DIN
- SDConfig.buffer_count = 2;
- SDConfig.buffer_size = 1024;
- i2s.begin(SDConfig);
- delay(20);
- player.setMetadataCallback(printMetaData);
- player.begin();
- delay(20);
- player.setVolume(0.3);
- Serial.printf("SD card init done\n");
- currentState = SDcardMode;
- }
- else if (currentState == SDcardMode)
- {
- if (!player.copy())
- {
- player.begin();
- Serial.printf("SD card song restarting\n");
- }
- }
- else if (currentState == deInitSDCard)
- {
- Serial.printf("SD card deInit start\n");
- player.stop();
- i2s.end();
- delay(20);
- currentState = initBluetooth;
- Serial.printf("SD card deInit end\n");
- }
- }
- void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
- // Serial.printf("AVRC metadata rsp: attribute id 0x%x, %s\n", data1, data2);
- parseBluetoothData(data1, data2);
- tftNeedsUpdate = true;
- }
- void parseBluetoothData(uint8_t address, const uint8_t *data)
- {
- if(address == 0x1) // clear out old info when new song is recieved;
- {
- currentSongTitle = "";
- currentSongArtist = "";
- currentSongAlbum = "";
- currentSongGenre = "";
- }
- switch (address)
- {
- case 0x1:
- currentSongTitle = reinterpret_cast<const char*>(data);
- break;
- case 0x2:
- currentSongArtist = reinterpret_cast<const char*>(data);
- break;
- case 0x4:
- currentSongAlbum = reinterpret_cast<const char*>(data);
- break;
- case 0x20:
- currentSongGenre = reinterpret_cast<const char*>(data);
- break;
- }
- }
- void pressHandler (BfButton *btn, BfButton::press_pattern_t pattern) {
- Serial.print(btn->getID());
- switch (pattern) {
- case BfButton::SINGLE_PRESS:
- Serial.println(" single pressed.");
- if(currentState == bluetoothMode)
- {
- currentState = deInitBluetooth;
- }
- else if(currentState == SDcardMode)
- {
- currentState = deInitSDCard;
- }
- Serial.printf("Playback mode: %s\n", currentState == bluetoothMode ? "Bluetooth" : "SD-Card");
- break;
- case BfButton::DOUBLE_PRESS:
- Serial.println(" double pressed.");
- break;
- case BfButton::LONG_PRESS:
- Serial.println(" long pressed.");
- break;
- }
- }
- void printMetaData(MetaDataType type, const char* str, int len){
- Serial.print("==> ");
- Serial.print(toStr(type));
- Serial.print(": ");
- Serial.println(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment