|
| 1 | +/* |
| 2 | + WT2003 S Example1_Terminal_MP3_Player |
| 3 | +
|
| 4 | + This example makes an MP3 player that can be controlled from the terminal window on a computer. |
| 5 | + It exposes the full set of commands that are available for the WT2003S |
| 6 | + |
| 7 | + Using the WT2003S MP3 Decoder IC on the SparkFun breakout board with Arduino Uno |
| 8 | + By: Owen Lyke, Tina Tenbergen |
| 9 | + SparkFun Electronics |
| 10 | + Date: July 5th 2018, Aug 9 2018 |
| 11 | + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). |
| 12 | +
|
| 13 | + Feel like supporting our work? Buy a board from SparkFun! |
| 14 | + https://www.sparkfun.com/products/14810 |
| 15 | +
|
| 16 | + Hardware Connections: |
| 17 | + WT2003S Breakout Pin ---> Arduino Pin |
| 18 | + ------------------------------------- |
| 19 | + TXO ---> 2 |
| 20 | + RXI ---> 3 |
| 21 | + 5V ---> 5V |
| 22 | + GND ---> GND |
| 23 | + BUSY ---> 7 (Optional) |
| 24 | +
|
| 25 | + Don't forget to load some MP3s on your sdCard and plug it in too! |
| 26 | +*/ |
| 27 | + |
| 28 | +#include "SparkFun_WT2003S_MP3_Decoder.h" // Click here to get the library: http://librarymanager/All#SparkFun_WT2003S |
| 29 | + |
| 30 | +// Defining some status codes from the WT2003S |
| 31 | +#define STATUS_PLAY 0x01 |
| 32 | +#define STATUS_STOP 0x02 |
| 33 | +#define STATUS_PAUSE 0x03 |
| 34 | + |
| 35 | +#define songNameGetDelay 500 // ms |
| 36 | + |
| 37 | +SoftwareSerial mp3_sws(2,3); // Use software serial so that the Arduino hardware serial can be used for control commands |
| 38 | + |
| 39 | +WT2003S MP3; // Create an object of the WT2003S class called MP3 |
| 40 | + |
| 41 | +void setup() { |
| 42 | + Serial.begin(9600); |
| 43 | + while(!Serial){}; |
| 44 | + MP3.begin(mp3_sws); // Beginning the MP3 player requires a serial port (either hardware or software!!!) |
| 45 | + |
| 46 | + Serial.println("MP3 Breakout - Example1 Serial Control"); |
| 47 | + Serial.println(); |
| 48 | + |
| 49 | + printMenu(); |
| 50 | + |
| 51 | + Serial.print("Number of tracks on SD card: "); |
| 52 | + Serial.println(MP3.getSongCount()); |
| 53 | + |
| 54 | + if(MP3.getSongCount() == 0) |
| 55 | + { |
| 56 | + Serial.println("Oh no! No songs found... try adding songs or plugging in the sd card then try again"); |
| 57 | + while(1); // Hold here |
| 58 | + } |
| 59 | + |
| 60 | + MP3.setVolume(4); |
| 61 | +} |
| 62 | + |
| 63 | +void loop() { |
| 64 | + if(Serial.available()) |
| 65 | + { |
| 66 | + char cmd = Serial.read(); |
| 67 | + if(cmd == '+') |
| 68 | + { |
| 69 | + Serial.print("Volume up: "); |
| 70 | + uint8_t volume = MP3.getVolume(); |
| 71 | + MP3.setVolume(++volume); |
| 72 | + Serial.print(volume); |
| 73 | + Serial.println("/31"); |
| 74 | + } |
| 75 | + else if(cmd == '-') |
| 76 | + { |
| 77 | + Serial.print("Volume down: "); |
| 78 | + uint8_t volume = MP3.getVolume(); |
| 79 | + if(--volume > 31) |
| 80 | + { |
| 81 | + volume = 0; |
| 82 | + } |
| 83 | + MP3.setVolume(volume); |
| 84 | + Serial.print(volume); |
| 85 | + Serial.println("/31"); |
| 86 | + } |
| 87 | + else if(cmd == 'n') |
| 88 | + { |
| 89 | + Serial.println("Next track:"); |
| 90 | + MP3.playNext(); |
| 91 | + delay(songNameGetDelay); // It takes a moment for the device to start playing the next song |
| 92 | + MP3.getSongName(); |
| 93 | + Serial.print("Playing: "); |
| 94 | + Serial.write(MP3.songName, 8); |
| 95 | + Serial.println(); |
| 96 | + } |
| 97 | + else if(cmd == 'l') |
| 98 | + { |
| 99 | + Serial.println("Last track:"); |
| 100 | + MP3.playPrevious(); |
| 101 | + delay(songNameGetDelay); // It takes a moment for the device to know the next song name |
| 102 | + MP3.getSongName(); |
| 103 | + Serial.print("Playing: "); |
| 104 | + Serial.write(MP3.songName, 8); |
| 105 | + Serial.println(); |
| 106 | + } |
| 107 | + else if(cmd == 'p') |
| 108 | + { |
| 109 | + uint8_t status = MP3.getPlayStatus(); |
| 110 | + if(status == STATUS_PLAY) |
| 111 | + { |
| 112 | + Serial.println("Paused: ('p' to resume)"); |
| 113 | + MP3.pause(); // Should pause when currently playing |
| 114 | + } |
| 115 | + else if(status == STATUS_PAUSE) |
| 116 | + { |
| 117 | + MP3.pause(); // Should play when already paused |
| 118 | + delay(songNameGetDelay); // It takes a moment for the device to know the next song name |
| 119 | + Serial.print("Resuming: "); |
| 120 | + MP3.getSongName(); |
| 121 | + Serial.write(MP3.songName, 8); |
| 122 | + Serial.println(); |
| 123 | + } |
| 124 | + else if(status == STATUS_STOP) |
| 125 | + { |
| 126 | + MP3.pause(); // Plays from beginning of current track |
| 127 | + delay(songNameGetDelay); // It takes a moment for the device to know the next song name |
| 128 | + MP3.getSongName(); |
| 129 | + Serial.print("Playing: "); |
| 130 | + Serial.write(MP3.songName, 8); |
| 131 | + Serial.println(); |
| 132 | + } |
| 133 | + } |
| 134 | + else if(cmd == 's') |
| 135 | + { |
| 136 | + Serial.println("Stopping: ('p' to play)"); |
| 137 | + MP3.stopPlaying(); |
| 138 | + } |
| 139 | + else if(cmd == '?') |
| 140 | + { |
| 141 | + uint8_t status = MP3.getPlayStatus(); |
| 142 | + if(status == STATUS_PLAY) |
| 143 | + { |
| 144 | + delay(songNameGetDelay); |
| 145 | + MP3.getSongName(); |
| 146 | + Serial.print("Playing: "); |
| 147 | + Serial.write(MP3.songName, 8); |
| 148 | + Serial.println(); |
| 149 | + } |
| 150 | + else if(status == STATUS_STOP) |
| 151 | + { |
| 152 | + Serial.println("MP3 player is stopped"); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + Serial.println("MP3 player is paused"); |
| 157 | + } |
| 158 | + } |
| 159 | + else if(cmd == 'w') |
| 160 | + { |
| 161 | + Serial.println("Set play mode to 'SingleNoLoop'"); |
| 162 | + MP3.setPlaymodeSingleNoLoop(); |
| 163 | + } |
| 164 | + else if(cmd == 'x') |
| 165 | + { |
| 166 | + Serial.println("Set play mode to 'SingleLoop'"); |
| 167 | + MP3.setPlaymodeSingleLoop(); |
| 168 | + } |
| 169 | + else if(cmd == 'y') |
| 170 | + { |
| 171 | + Serial.println("Set play mode to 'AllLoop'"); |
| 172 | + MP3.setPlaymodeAllLoop(); |
| 173 | + } |
| 174 | + else if(cmd == 'z') |
| 175 | + { |
| 176 | + Serial.println("Set play mode to 'Random'"); |
| 177 | + MP3.setPlaymodeRandom(); |
| 178 | + } |
| 179 | + else if((cmd <= '9') && (cmd >= '1')) |
| 180 | + { |
| 181 | + uint8_t numSongs = MP3.getSongCount(); |
| 182 | + if(numSongs >= (cmd - '0')) |
| 183 | + { |
| 184 | + MP3.playTrackNumber(cmd - '0'); |
| 185 | + Serial.print("Playing track #"); |
| 186 | + Serial.print(cmd - '0'); |
| 187 | + Serial.print(" (in copy-to-sd order)"); |
| 188 | + delay(songNameGetDelay); // Give it time to switch songs |
| 189 | + MP3.getSongName(); |
| 190 | + Serial.print(": "); |
| 191 | + Serial.write(MP3.songName, 8); |
| 192 | + Serial.println(); |
| 193 | + } |
| 194 | + else |
| 195 | + { |
| 196 | + Serial.print("Error: There are only "); |
| 197 | + Serial.print(numSongs); |
| 198 | + Serial.println(" songs on the SD card."); |
| 199 | + Serial.print("You asked to play song # "); |
| 200 | + Serial.print(cmd - '0'); |
| 201 | + Serial.println("."); |
| 202 | + } |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + printMenu(); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +void printMenu( void ) |
| 212 | +{ |
| 213 | + Serial.println("MP3 Command List:"); |
| 214 | + Serial.println("-----------------"); |
| 215 | + Serial.println("'+' or '-' : raise/lower volume"); |
| 216 | + Serial.println("'n' or 'l' : next/last song"); |
| 217 | + Serial.println("'p' : play or pause"); |
| 218 | + Serial.println("'s' : stop playing"); |
| 219 | + Serial.println("'?' : what is playing?"); |
| 220 | + Serial.println("'w' : set playmode single no loop"); |
| 221 | + Serial.println("'x' : set playmode single loop"); |
| 222 | + Serial.println("'y' : set playmode all loop"); |
| 223 | + Serial.println("'z' : set playmode random"); |
| 224 | + Serial.println("'X' : play the file that was copied to the SD card as the Xth file - you just type the number you want - (x in [1,9])"); |
| 225 | + Serial.println(" (Yes, this really does go by copy order.)"); |
| 226 | + Serial.println(); |
| 227 | + Serial.println("Any other key to show this menu"); |
| 228 | + Serial.println(); |
| 229 | +} |
| 230 | + |
0 commit comments