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