|
| 1 | +/* |
| 2 | + This example demonstrates how to use the MY1690X MP3 decoder IC with an |
| 3 | + ESP32 board (communicating over hardware serial). |
| 4 | + |
| 5 | + If you are using the R3 form-factor "MY1690X Serial MP3 Player Shield", with |
| 6 | + the "SparkFun IoT Redboard - ESP32 Development Board" this example sets up |
| 7 | + the hardware serial port on the correct pins (26 and 27). |
| 8 | +
|
| 9 | + Play an MP3 over software serial using the MY1690X MP3 IC |
| 10 | + By: Nathan Seidle |
| 11 | + SparkFun Electronics |
| 12 | + Date: December 10th, 2021 |
| 13 | + License: MIT. See license file for more information but you can |
| 14 | + basically do whatever you want with this code. |
| 15 | +
|
| 16 | + The MY1690 has a large number of features. This example presents the user |
| 17 | + with a serial menu to control the various aspects of the IC. |
| 18 | +
|
| 19 | + Feel like supporting our work? Buy a board from SparkFun! |
| 20 | + MY1690X Serial MP3 Player Shield: https://www.sparkfun.com/sparkfun-serial-mp3-player-shield-my1690x.html |
| 21 | + MY1690X Audio Player Breakout: https://www.sparkfun.com/sparkfun-audio-player-breakout-my1690x-16s.html |
| 22 | +
|
| 23 | + Hardware Connections: |
| 24 | + MY1690 Pin -> Arduino Pin |
| 25 | + ------------------------------------- |
| 26 | + TXO -> 26 |
| 27 | + RXI -> 27 |
| 28 | + VIN -> 5V |
| 29 | + GND -> GND |
| 30 | +
|
| 31 | + Don't forget to load some MP3s on your sdCard and plug it in too! |
| 32 | + Note: Track must be named 0001.mp3 to myMP3.playTrackNumber(1) |
| 33 | +*/ |
| 34 | + |
| 35 | +#include "SparkFun_MY1690_MP3_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_MY1690 |
| 36 | + |
| 37 | +//For boards that have multiple hardware serial ports |
| 38 | +HardwareSerial serialMP3(2); //Create serial port on ESP32 using UART2 |
| 39 | + |
| 40 | +SparkFunMY1690 myMP3; |
| 41 | + |
| 42 | +void setup() |
| 43 | +{ |
| 44 | + Serial.begin(115200); |
| 45 | + Serial.println(F("MY1690 MP3 Example 3 - Kitchen Sink Hardware Serial on ESP32")); |
| 46 | + |
| 47 | + serialMP3.begin(9600, SERIAL_8N1, 26, 27); // ESP32 HW serial arguments: (GPS_BAUD, SERIAL_8N1, RX_GPIO, TX_GPIO); |
| 48 | + |
| 49 | + if (myMP3.begin(serialMP3) == false) // Beginning the MP3 player requires a serial port (either hardware or software) |
| 50 | + { |
| 51 | + Serial.println(F("Device not detected. Check wiring. Freezing.")); |
| 52 | + while (1); |
| 53 | + } |
| 54 | + |
| 55 | + int songCount = myMP3.getSongCount(); |
| 56 | + if (songCount == 0) |
| 57 | + { |
| 58 | + Serial.println(F("Oh no! No songs found. Make sure the SD card is inserted and there are MP3s on it. Freezing.")); |
| 59 | + while (1); |
| 60 | + } |
| 61 | + |
| 62 | + Serial.print(F("Number of tracks on SD card: ")); |
| 63 | + Serial.println(songCount); |
| 64 | + |
| 65 | + Serial.print(F("MY1690 Version: ")); |
| 66 | + Serial.println(myMP3.getVersion()); |
| 67 | + |
| 68 | + myMP3.play(); //Will play the lowest numbered song in the folder |
| 69 | + |
| 70 | + //It takes ~30ms for a track to start playing. If we check immediately, the track has not yet started. |
| 71 | + delay(50); |
| 72 | + |
| 73 | + int playStatus = myMP3.getPlayStatus(); |
| 74 | + // 0 = stop, 1 = play, 2 = pause, 3 = fast forward, 4 = rewind |
| 75 | + |
| 76 | + Serial.print(F("playStatus: ")); |
| 77 | + Serial.print(playStatus); |
| 78 | + if (playStatus == 1) |
| 79 | + Serial.println(F(" (playing)")); |
| 80 | + else if (playStatus == 0) |
| 81 | + Serial.println(F(" (stopped)")); |
| 82 | + |
| 83 | + myMP3.setVolume(15); //30 is loudest. 15 is comfortable with headphones. 0 is mute. |
| 84 | + |
| 85 | + Serial.print(F("Volume: ")); |
| 86 | + Serial.println(myMP3.getVolume()); |
| 87 | + |
| 88 | + myMP3.setPlayModeNoLoop(); |
| 89 | + |
| 90 | + mainMenu(); |
| 91 | +} |
| 92 | + |
| 93 | +void loop() |
| 94 | +{ |
| 95 | + if (Serial.available()) |
| 96 | + { |
| 97 | + byte incoming = Serial.read(); |
| 98 | + if (incoming == 's') |
| 99 | + { |
| 100 | + if(myMP3.stopPlaying() == true) |
| 101 | + Serial.println("Stop success"); |
| 102 | + else |
| 103 | + Serial.println("Stop command failed"); |
| 104 | + } |
| 105 | + else if (incoming == 'x') |
| 106 | + { |
| 107 | + if (myMP3.reset() == true) |
| 108 | + Serial.println("Reset success"); |
| 109 | + else |
| 110 | + Serial.println("Reset command failed"); |
| 111 | + } |
| 112 | + else if (incoming == 'a') |
| 113 | + { |
| 114 | + myMP3.volumeUp(); |
| 115 | + Serial.print("Volume: "); |
| 116 | + Serial.println(myMP3.getVolume()); |
| 117 | + } |
| 118 | + else if (incoming == 'z') |
| 119 | + { |
| 120 | + myMP3.volumeDown(); |
| 121 | + Serial.print("Volume: "); |
| 122 | + Serial.println(myMP3.getVolume()); |
| 123 | + } |
| 124 | + else if (incoming == 'f') |
| 125 | + { |
| 126 | + myMP3.fastForward(); |
| 127 | + } |
| 128 | + else if (incoming == 'r') |
| 129 | + { |
| 130 | + myMP3.rewind(); |
| 131 | + } |
| 132 | + else if (incoming == 'p') |
| 133 | + { |
| 134 | + myMP3.playPause(); |
| 135 | + } |
| 136 | + else if (incoming == 'e') |
| 137 | + { |
| 138 | + int currentEQ = myMP3.getEQ(); |
| 139 | + currentEQ++; //Go to next EQ. Device automatically wraps. |
| 140 | + |
| 141 | + myMP3.setEQ(currentEQ); |
| 142 | + |
| 143 | + currentEQ = myMP3.getEQ(); |
| 144 | + Serial.print(F("Current EQ: ")); |
| 145 | + Serial.println(currentEQ); |
| 146 | + } |
| 147 | + else if (incoming == 'm') |
| 148 | + { |
| 149 | + int currentMode = myMP3.getPlayMode(); |
| 150 | + currentMode++; //Go to next mode. |
| 151 | + |
| 152 | + if(currentMode > 4) |
| 153 | + currentMode = 0; |
| 154 | + |
| 155 | + myMP3.setPlayMode(currentMode); |
| 156 | + |
| 157 | + currentMode = myMP3.getPlayMode(); |
| 158 | + Serial.print(F("Current Mode: ")); |
| 159 | + Serial.println(currentMode); |
| 160 | + } |
| 161 | + else if (incoming == '<') |
| 162 | + { |
| 163 | + myMP3.playPrevious(); |
| 164 | + } |
| 165 | + else if (incoming == '>') |
| 166 | + { |
| 167 | + myMP3.playNext(); |
| 168 | + } |
| 169 | + else if (incoming == '#') |
| 170 | + { |
| 171 | + delay(20); |
| 172 | + while (Serial.available()) Serial.read(); |
| 173 | + |
| 174 | + Serial.println(F("Track number to play: ")); |
| 175 | + while (Serial.available() == 0) delay(1); |
| 176 | + int value = Serial.parseInt(); |
| 177 | + |
| 178 | + // Note: Track must be named 0001.mp3 to myMP3.playTrackNumber(1) |
| 179 | + myMP3.playTrackNumber(value); |
| 180 | + } |
| 181 | + else if (incoming == 'c') |
| 182 | + { |
| 183 | + Serial.print(F("Current track: ")); |
| 184 | + Serial.println(myMP3.getTrackNumber()); |
| 185 | + } |
| 186 | + else if (incoming == 't') |
| 187 | + { |
| 188 | + Serial.print(F("Current track elapsed time (s): ")); |
| 189 | + Serial.println(myMP3.getTrackElapsedTime()); |
| 190 | + } |
| 191 | + else if (incoming == 'T') |
| 192 | + { |
| 193 | + Serial.print(F("Current track length (s): ")); |
| 194 | + Serial.println(myMP3.getTrackTotalTime()); |
| 195 | + } |
| 196 | + else if (incoming == '\r' || incoming == '\n') |
| 197 | + { |
| 198 | + //Ignore these |
| 199 | + } |
| 200 | + else |
| 201 | + { |
| 202 | + Serial.print(F("Unknown command: ")); |
| 203 | + Serial.write(incoming); |
| 204 | + Serial.println(); |
| 205 | + mainMenu(); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +void mainMenu() |
| 211 | +{ |
| 212 | + Serial.println(); |
| 213 | + Serial.println(F("SparkFun MY1690 Menu:")); |
| 214 | + |
| 215 | + Serial.println(F("s) Stop play")); |
| 216 | + Serial.println(F("x) Reset IC")); |
| 217 | + Serial.println(F("a) Volume up")); |
| 218 | + Serial.println(F("z) Volume down")); |
| 219 | + Serial.println(F("f) Fast forward")); |
| 220 | + Serial.println(F("r) Reverse")); |
| 221 | + Serial.println(F("p) Play/Pause toggle")); |
| 222 | + Serial.println(F("e) Set EQ")); |
| 223 | + Serial.println(F("m) Set play mode")); |
| 224 | + Serial.println(F("<) Play previous")); |
| 225 | + Serial.println(F(">) Play next")); |
| 226 | + Serial.println(F("#) Play track number")); |
| 227 | + Serial.println(F("c) Current track number")); |
| 228 | + Serial.println(F("t) Track elapsed time")); |
| 229 | + Serial.println(F("T) Track total time")); |
| 230 | + Serial.println(F("Enter command:")); |
| 231 | +} |
0 commit comments