This program utilizes an LCD, RPG, 8 ohm speaker, and SD card to provide the basic functions of a music player. The project cycles through the SD card for .wav files and tracks elapsed time. See http://mbed.org/users/rsartin3/notebook/wav-player-with-RPG-based-menu/ for more information.
Dependencies: DebounceIn RPG SDFileSystem TextLCD mbed wav_header wave_player
Fork of Audio_Player by
main.cpp@2:d9953793444c, 2013-03-05 (annotated)
- Committer:
- rsartin3
- Date:
- Tue Mar 05 16:36:15 2013 +0000
- Revision:
- 2:d9953793444c
- Parent:
- 1:f31b40073d35
Changed name for clarification;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rsartin3 | 0:040129c3d961 | 1 | /* |
rsartin3 | 1:f31b40073d35 | 2 | * Audio Player |
rsartin3 | 2:d9953793444c | 3 | * Josh Dunn & Ryan Sartin |
rsartin3 | 1:f31b40073d35 | 4 | */ |
rsartin3 | 0:040129c3d961 | 5 | |
rsartin3 | 0:040129c3d961 | 6 | #include "mbed.h" |
rsartin3 | 0:040129c3d961 | 7 | #include "DebounceIn.h" |
rsartin3 | 0:040129c3d961 | 8 | #include "SDFileSystem.h" |
rsartin3 | 0:040129c3d961 | 9 | #include "wave_player.h" |
rsartin3 | 0:040129c3d961 | 10 | #include "TextLCD.h" |
rsartin3 | 0:040129c3d961 | 11 | #include "RPG.h" |
rsartin3 | 0:040129c3d961 | 12 | #include "Wav_Support_File.h" |
rsartin3 | 0:040129c3d961 | 13 | #include "string" |
rsartin3 | 0:040129c3d961 | 14 | |
rsartin3 | 0:040129c3d961 | 15 | // Debug mode on: 1 |
rsartin3 | 0:040129c3d961 | 16 | #define DEBUG 0 |
rsartin3 | 0:040129c3d961 | 17 | |
rsartin3 | 0:040129c3d961 | 18 | // -- Debug I/O -- |
rsartin3 | 0:040129c3d961 | 19 | DigitalOut myled(LED1); |
rsartin3 | 0:040129c3d961 | 20 | DigitalOut myled2(LED2); |
rsartin3 | 0:040129c3d961 | 21 | DigitalOut myled3(LED3); |
rsartin3 | 0:040129c3d961 | 22 | DigitalOut myled4(LED4); |
rsartin3 | 0:040129c3d961 | 23 | Serial pc(USBTX, USBRX); |
rsartin3 | 0:040129c3d961 | 24 | |
rsartin3 | 0:040129c3d961 | 25 | // -- RPG I/O -- |
rsartin3 | 0:040129c3d961 | 26 | RPG rpg(p17, p16, p19); |
rsartin3 | 0:040129c3d961 | 27 | |
rsartin3 | 0:040129c3d961 | 28 | // -- Text LCD I/O -- |
rsartin3 | 0:040129c3d961 | 29 | TextLCD lcd(p25, p26, p27, p28, p29, p30); // rs, e, d4-d7 |
rsartin3 | 0:040129c3d961 | 30 | |
rsartin3 | 0:040129c3d961 | 31 | // -- SD card I/O -- |
rsartin3 | 0:040129c3d961 | 32 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
rsartin3 | 0:040129c3d961 | 33 | DigitalIn sdDetect(p9); // Grounded if no card is inserted, Requires pull up |
rsartin3 | 0:040129c3d961 | 34 | |
rsartin3 | 0:040129c3d961 | 35 | // -- Wave Player I/O -- |
rsartin3 | 0:040129c3d961 | 36 | AnalogOut DACout(p18); |
rsartin3 | 0:040129c3d961 | 37 | wave_player waver(&DACout); |
rsartin3 | 0:040129c3d961 | 38 | |
rsartin3 | 1:f31b40073d35 | 39 | // Ticker for interrupting song |
rsartin3 | 0:040129c3d961 | 40 | Ticker songTick; |
rsartin3 | 0:040129c3d961 | 41 | time_t time_i; |
rsartin3 | 0:040129c3d961 | 42 | |
rsartin3 | 0:040129c3d961 | 43 | struct playlist { |
rsartin3 | 0:040129c3d961 | 44 | // Doubly-linked list to easily traverse between filenames |
rsartin3 | 1:f31b40073d35 | 45 | char s_name[30]; // File name without .wav extension |
rsartin3 | 1:f31b40073d35 | 46 | playlist *prev; // Pointer to next filename |
rsartin3 | 1:f31b40073d35 | 47 | playlist *next; // Pointer to previous filename |
rsartin3 | 1:f31b40073d35 | 48 | }; |
rsartin3 | 0:040129c3d961 | 49 | |
rsartin3 | 0:040129c3d961 | 50 | void insert( struct playlist *root, string songName) |
rsartin3 | 0:040129c3d961 | 51 | { |
rsartin3 | 0:040129c3d961 | 52 | /* Insert |
rsartin3 | 0:040129c3d961 | 53 | Put a new songname at the end of the linked list |
rsartin3 | 0:040129c3d961 | 54 | @param *head: Pointer to root node of playlist |
rsartin3 | 0:040129c3d961 | 55 | @param songName: String holding the song name to be inserted |
rsartin3 | 0:040129c3d961 | 56 | */ |
rsartin3 | 0:040129c3d961 | 57 | playlist *new1=(playlist *) malloc(sizeof(playlist)); // Create new playlist node |
rsartin3 | 0:040129c3d961 | 58 | playlist *temp = root; // Start at root node |
rsartin3 | 0:040129c3d961 | 59 | |
rsartin3 | 0:040129c3d961 | 60 | while(temp -> next != NULL) |
rsartin3 | 0:040129c3d961 | 61 | { |
rsartin3 | 0:040129c3d961 | 62 | temp = temp -> next; // Traverse playlist until at last node |
rsartin3 | 0:040129c3d961 | 63 | } |
rsartin3 | 0:040129c3d961 | 64 | |
rsartin3 | 0:040129c3d961 | 65 | strcpy(new1 -> s_name, songName.c_str()); // Copy song name into the file name of the node |
rsartin3 | 0:040129c3d961 | 66 | new1 -> next = NULL; // Set next pointer as null |
rsartin3 | 0:040129c3d961 | 67 | new1 -> prev = temp; // Set previous pointer to the last mode |
rsartin3 | 0:040129c3d961 | 68 | temp -> next = new1; // Hhave last node point to newly created node |
rsartin3 | 0:040129c3d961 | 69 | }; |
rsartin3 | 0:040129c3d961 | 70 | |
rsartin3 | 0:040129c3d961 | 71 | int createPlaylist(const char *location, playlist *root) { |
rsartin3 | 0:040129c3d961 | 72 | /* createPlaylist |
rsartin3 | 0:040129c3d961 | 73 | Read from given directory and if a file has a .wav extension, add it to the playlist. Output the number of songs added to playlist |
rsartin3 | 0:040129c3d961 | 74 | @param *location: Location to look for .wav files, tested for base SD card directory "/sd/" |
rsartin3 | 0:040129c3d961 | 75 | @param *root: Pointer to root of a playlist. The playlist should be empty except for the root node which will be overwritten |
rsartin3 | 0:040129c3d961 | 76 | @out count: Outputs the number of songs added to the playlist |
rsartin3 | 0:040129c3d961 | 77 | */ |
rsartin3 | 0:040129c3d961 | 78 | DIR *d; // Directory pointer |
rsartin3 | 0:040129c3d961 | 79 | dirent *file; // dirent to get file name |
rsartin3 | 1:f31b40073d35 | 80 | // playlist *curr = root; |
rsartin3 | 0:040129c3d961 | 81 | int count = 0; // count of songs added to playlist |
rsartin3 | 0:040129c3d961 | 82 | |
rsartin3 | 0:040129c3d961 | 83 | if(DEBUG) pc.printf("\n\rWell... Let's get started!\r\n"); |
rsartin3 | 1:f31b40073d35 | 84 | if((d = opendir(location)) == NULL) { // Attempt to open directory at given location |
rsartin3 | 0:040129c3d961 | 85 | // If Error |
rsartin3 | 0:040129c3d961 | 86 | lcd.cls(); |
rsartin3 | 0:040129c3d961 | 87 | lcd.printf("Unable to open directory!"); |
rsartin3 | 0:040129c3d961 | 88 | if(DEBUG) pc.printf("[!] Unable to open directory\r\n"); |
rsartin3 | 0:040129c3d961 | 89 | }else{ |
rsartin3 | 0:040129c3d961 | 90 | // If Successful |
rsartin3 | 0:040129c3d961 | 91 | while((file = readdir(d)) !=NULL) { // While able to read files in directory |
rsartin3 | 0:040129c3d961 | 92 | if(DEBUG) pc.printf("Parsing file: %s\n\r", file->d_name); |
rsartin3 | 0:040129c3d961 | 93 | |
rsartin3 | 0:040129c3d961 | 94 | string str(file->d_name); // Parse filename |
rsartin3 | 0:040129c3d961 | 95 | size_t exten = str.find_last_of("."); // Find final '.' |
rsartin3 | 0:040129c3d961 | 96 | |
rsartin3 | 0:040129c3d961 | 97 | if(DEBUG) pc.printf("path: %s\n\r", (str.substr(0,exten)).c_str()); // Prints everything before final '.' |
rsartin3 | 0:040129c3d961 | 98 | if(DEBUG) pc.printf("file: %s\n\r", (str.substr(exten+1)).c_str()); // Prints everything after final '.' |
rsartin3 | 0:040129c3d961 | 99 | |
rsartin3 | 0:040129c3d961 | 100 | // If the extension is wav or WAV, add to playlist. Otherwise read in next file |
rsartin3 | 0:040129c3d961 | 101 | if((str.substr(exten+1)).compare("wav") == 0 || (str.substr(exten+1)).compare("WAV") == 0 ){ |
rsartin3 | 0:040129c3d961 | 102 | if(DEBUG) pc.printf("This is a file that I like very much!\r\n"); |
rsartin3 | 0:040129c3d961 | 103 | |
rsartin3 | 0:040129c3d961 | 104 | if(count == 0){ // If still at root node |
rsartin3 | 0:040129c3d961 | 105 | count++; // Increment count |
rsartin3 | 1:f31b40073d35 | 106 | strcpy(root->s_name, (str.substr(0,exten)).c_str()); // Copy filename into root node |
rsartin3 | 1:f31b40073d35 | 107 | // curr = root; |
rsartin3 | 0:040129c3d961 | 108 | }else{ |
rsartin3 | 1:f31b40073d35 | 109 | count++; // Increment count |
rsartin3 | 0:040129c3d961 | 110 | insert(root,str.substr(0,exten)); // Add new song to playlist |
rsartin3 | 0:040129c3d961 | 111 | } |
rsartin3 | 0:040129c3d961 | 112 | } |
rsartin3 | 0:040129c3d961 | 113 | } |
rsartin3 | 0:040129c3d961 | 114 | closedir(d); // Close directory |
rsartin3 | 0:040129c3d961 | 115 | } |
rsartin3 | 1:f31b40073d35 | 116 | return count; // Return number of songnames added to playlist |
rsartin3 | 0:040129c3d961 | 117 | } |
rsartin3 | 0:040129c3d961 | 118 | |
rsartin3 | 0:040129c3d961 | 119 | void printList(playlist *root) { |
rsartin3 | 0:040129c3d961 | 120 | /* printList |
rsartin3 | 0:040129c3d961 | 121 | (DEBUG function) Traverses the playlist and writes all songnames to the pc |
rsartin3 | 0:040129c3d961 | 122 | @param *root: pointer to playlist to print |
rsartin3 | 0:040129c3d961 | 123 | */ |
rsartin3 | 0:040129c3d961 | 124 | int count = 1; // Track number, starts at 1 |
rsartin3 | 0:040129c3d961 | 125 | struct playlist *curr = root; // playlist node used to traverse playlist |
rsartin3 | 0:040129c3d961 | 126 | pc.printf("\n\rPrinting File Names:\n\r"); |
rsartin3 | 0:040129c3d961 | 127 | pc.printf("Track #%d: %s\n\r", count, curr->s_name); //Root node song name |
rsartin3 | 0:040129c3d961 | 128 | |
rsartin3 | 0:040129c3d961 | 129 | while(curr -> next != NULL) { // While there is a following node... |
rsartin3 | 0:040129c3d961 | 130 | curr = curr -> next; // Move to it |
rsartin3 | 0:040129c3d961 | 131 | count++; // Increment song count |
rsartin3 | 0:040129c3d961 | 132 | pc.printf("Track #%d: %s\n\r", count, curr->s_name); // And print the current song name |
rsartin3 | 0:040129c3d961 | 133 | } |
rsartin3 | 0:040129c3d961 | 134 | } |
rsartin3 | 0:040129c3d961 | 135 | |
rsartin3 | 0:040129c3d961 | 136 | void freeList(playlist ** root) { |
rsartin3 | 0:040129c3d961 | 137 | /* freeList |
rsartin3 | 0:040129c3d961 | 138 | * Deallocate playlist structure memory, including root node |
rsartin3 | 0:040129c3d961 | 139 | * Sets root pointer to NULL so old memory location cannot be accessed |
rsartin3 | 0:040129c3d961 | 140 | * usage: freeList(&root); |
rsartin3 | 0:040129c3d961 | 141 | */ |
rsartin3 | 0:040129c3d961 | 142 | |
rsartin3 | 0:040129c3d961 | 143 | playlist * node = *root; |
rsartin3 | 0:040129c3d961 | 144 | playlist * temp; |
rsartin3 | 0:040129c3d961 | 145 | |
rsartin3 | 0:040129c3d961 | 146 | if(node == NULL) |
rsartin3 | 0:040129c3d961 | 147 | return; // Protect against an empty root node (no playlist) |
rsartin3 | 0:040129c3d961 | 148 | |
rsartin3 | 0:040129c3d961 | 149 | while(node != NULL) { |
rsartin3 | 0:040129c3d961 | 150 | temp = node->next; |
rsartin3 | 0:040129c3d961 | 151 | free(node); |
rsartin3 | 0:040129c3d961 | 152 | node = temp; |
rsartin3 | 0:040129c3d961 | 153 | } |
rsartin3 | 0:040129c3d961 | 154 | *root = NULL; |
rsartin3 | 0:040129c3d961 | 155 | } |
rsartin3 | 0:040129c3d961 | 156 | |
rsartin3 | 0:040129c3d961 | 157 | void deleteList(playlist * root) { |
rsartin3 | 0:040129c3d961 | 158 | /* deleteList |
rsartin3 | 0:040129c3d961 | 159 | * Deallocates all non-root playlist structures, leaving the root |
rsartin3 | 0:040129c3d961 | 160 | * node with a default "No wavefile" string and null'ed pointers |
rsartin3 | 0:040129c3d961 | 161 | */ |
rsartin3 | 0:040129c3d961 | 162 | |
rsartin3 | 0:040129c3d961 | 163 | if(root == NULL) |
rsartin3 | 0:040129c3d961 | 164 | return; |
rsartin3 | 0:040129c3d961 | 165 | |
rsartin3 | 0:040129c3d961 | 166 | freeList(&(root->next)); |
rsartin3 | 0:040129c3d961 | 167 | |
rsartin3 | 0:040129c3d961 | 168 | root->next = NULL; |
rsartin3 | 0:040129c3d961 | 169 | root->prev = NULL; |
rsartin3 | 0:040129c3d961 | 170 | strcpy(root->s_name, "No wavefile"); |
rsartin3 | 0:040129c3d961 | 171 | } |
rsartin3 | 0:040129c3d961 | 172 | |
rsartin3 | 0:040129c3d961 | 173 | void songClock() { |
rsartin3 | 2:d9953793444c | 174 | /* |
rsartin3 | 2:d9953793444c | 175 | * songClock |
rsartin3 | 2:d9953793444c | 176 | * Reads in time and compares it to time_i, the global variable |
rsartin3 | 2:d9953793444c | 177 | * Used with Ticker API to count number of seconds elapsed since song started playing |
rsartin3 | 2:d9953793444c | 178 | */ |
rsartin3 | 0:040129c3d961 | 179 | time_t time_f; |
rsartin3 | 0:040129c3d961 | 180 | time_f = time(NULL); |
rsartin3 | 0:040129c3d961 | 181 | |
rsartin3 | 0:040129c3d961 | 182 | // if(DEBUG) printf("Time Elapsed: %d. (Start: %d, Stop: %d)\n\r", time_f - time_i, time_i, time_f); |
rsartin3 | 0:040129c3d961 | 183 | lcd.locate(6,1); |
rsartin3 | 0:040129c3d961 | 184 | lcd.printf("%d", time_f - time_i + 1); |
rsartin3 | 0:040129c3d961 | 185 | } |
rsartin3 | 0:040129c3d961 | 186 | |
rsartin3 | 0:040129c3d961 | 187 | int main() { |
rsartin3 | 0:040129c3d961 | 188 | // --- Control Variables --- |
rsartin3 | 0:040129c3d961 | 189 | int songCount = 0; // Number of songs in the playlist linked list |
rsartin3 | 0:040129c3d961 | 190 | |
rsartin3 | 0:040129c3d961 | 191 | bool sdInsert = false; |
rsartin3 | 0:040129c3d961 | 192 | bool firstList = false; // Is it first instance of reading from SD card |
rsartin3 | 0:040129c3d961 | 193 | bool playlistExist = false; |
rsartin3 | 0:040129c3d961 | 194 | |
rsartin3 | 2:d9953793444c | 195 | // RPG Variables |
rsartin3 | 0:040129c3d961 | 196 | int rpgTick = 0; // Slow down RPG turning speed |
rsartin3 | 0:040129c3d961 | 197 | const int rpgThresh = 5; |
rsartin3 | 0:040129c3d961 | 198 | |
rsartin3 | 2:d9953793444c | 199 | // File interaction variables |
rsartin3 | 0:040129c3d961 | 200 | FILE *wave_file; |
rsartin3 | 0:040129c3d961 | 201 | string mySongName = "Invalid"; |
rsartin3 | 0:040129c3d961 | 202 | unsigned char wave_array[30]; |
rsartin3 | 0:040129c3d961 | 203 | |
rsartin3 | 0:040129c3d961 | 204 | |
rsartin3 | 0:040129c3d961 | 205 | // --- Debug That Junk --- |
rsartin3 | 0:040129c3d961 | 206 | lcd.printf("Hello World!"); |
rsartin3 | 0:040129c3d961 | 207 | if(DEBUG) pc.printf("Hello World!\n\r"); |
rsartin3 | 0:040129c3d961 | 208 | |
rsartin3 | 0:040129c3d961 | 209 | // --- Initialize Playlist --- |
rsartin3 | 0:040129c3d961 | 210 | struct playlist *root; // Create root node |
rsartin3 | 0:040129c3d961 | 211 | root = (playlist *)malloc(sizeof(struct playlist)); |
rsartin3 | 0:040129c3d961 | 212 | root -> next = NULL; // Only node |
rsartin3 | 0:040129c3d961 | 213 | root -> prev = NULL; |
rsartin3 | 0:040129c3d961 | 214 | strcpy(root->s_name, "No wavefile"); // Put song name as "No wavfile" |
rsartin3 | 0:040129c3d961 | 215 | if(DEBUG) printList(root); |
rsartin3 | 0:040129c3d961 | 216 | |
rsartin3 | 0:040129c3d961 | 217 | struct playlist *curr = root; // Current node for playlist traversal |
rsartin3 | 0:040129c3d961 | 218 | |
rsartin3 | 0:040129c3d961 | 219 | // --- Internal Pull-ups --- |
rsartin3 | 0:040129c3d961 | 220 | sdDetect.mode(PullUp); |
rsartin3 | 0:040129c3d961 | 221 | wait(.001); |
rsartin3 | 0:040129c3d961 | 222 | |
rsartin3 | 0:040129c3d961 | 223 | /* ================================ |
rsartin3 | 0:040129c3d961 | 224 | =========== Start Main Program ============ |
rsartin3 | 0:040129c3d961 | 225 | ================================ |
rsartin3 | 0:040129c3d961 | 226 | */ |
rsartin3 | 0:040129c3d961 | 227 | while(1) { |
rsartin3 | 0:040129c3d961 | 228 | // ------------- No SD Card Detected ------------- |
rsartin3 | 0:040129c3d961 | 229 | // Current bug... When SD card is removed and replaced, stays in not detected loop... or something |
rsartin3 | 2:d9953793444c | 230 | if(DEBUG) myled = sdDetect; |
rsartin3 | 2:d9953793444c | 231 | if(DEBUG) myled2 = 0; |
rsartin3 | 0:040129c3d961 | 232 | if(!sdDetect) |
rsartin3 | 0:040129c3d961 | 233 | { |
rsartin3 | 0:040129c3d961 | 234 | if(DEBUG) { |
rsartin3 | 0:040129c3d961 | 235 | myled2 = 1 - myled2; |
rsartin3 | 0:040129c3d961 | 236 | wait_ms(50); |
rsartin3 | 0:040129c3d961 | 237 | } |
rsartin3 | 0:040129c3d961 | 238 | sdInsert = false; // Note no card is inserted |
rsartin3 | 2:d9953793444c | 239 | if(playlistExist){ // If a playlist has been made |
rsartin3 | 0:040129c3d961 | 240 | playlistExist = false; |
rsartin3 | 2:d9953793444c | 241 | deleteList(root); // Clear playlist |
rsartin3 | 0:040129c3d961 | 242 | curr = root; |
rsartin3 | 0:040129c3d961 | 243 | } |
rsartin3 | 0:040129c3d961 | 244 | lcd.cls(); |
rsartin3 | 0:040129c3d961 | 245 | lcd.printf("No SD Card!"); |
rsartin3 | 0:040129c3d961 | 246 | if(DEBUG) pc.printf("No SD card detected! Please insert one\n\r"); |
rsartin3 | 0:040129c3d961 | 247 | } |
rsartin3 | 0:040129c3d961 | 248 | // ------------ SD Card Detected ------------ |
rsartin3 | 0:040129c3d961 | 249 | else { |
rsartin3 | 0:040129c3d961 | 250 | // ---------- Set-up Playlist ---------- |
rsartin3 | 0:040129c3d961 | 251 | if(!sdInsert) { |
rsartin3 | 0:040129c3d961 | 252 | // If first loop after inserting SD card |
rsartin3 | 0:040129c3d961 | 253 | sdInsert = true; |
rsartin3 | 0:040129c3d961 | 254 | songCount = createPlaylist("/sd/", root); // Create playlist from .wav files in "/sd/" |
rsartin3 | 0:040129c3d961 | 255 | playlistExist = true; // A playlist now exists |
rsartin3 | 0:040129c3d961 | 256 | firstList = true; // First time through playlist |
rsartin3 | 0:040129c3d961 | 257 | curr = root; // Put current at root node |
rsartin3 | 0:040129c3d961 | 258 | if(DEBUG) printList(root); |
rsartin3 | 0:040129c3d961 | 259 | lcd.cls(); // Display number of songs found |
rsartin3 | 0:040129c3d961 | 260 | lcd.printf("Songs found: %d", songCount); |
rsartin3 | 0:040129c3d961 | 261 | } |
rsartin3 | 0:040129c3d961 | 262 | |
rsartin3 | 0:040129c3d961 | 263 | // ----------------- Turn RPG Clockwise ----------------- |
rsartin3 | 0:040129c3d961 | 264 | if(rpg.dir() == 1) { |
rsartin3 | 0:040129c3d961 | 265 | // If turned clockwise, see if increment is greater than threshold |
rsartin3 | 0:040129c3d961 | 266 | if(rpgTick < rpgThresh){ |
rsartin3 | 0:040129c3d961 | 267 | rpgTick++; //if not increment and do nothing else |
rsartin3 | 0:040129c3d961 | 268 | if(DEBUG) printf("rpgThresh: %d\r\n", rpgThresh); |
rsartin3 | 0:040129c3d961 | 269 | } |
rsartin3 | 0:040129c3d961 | 270 | else { |
rsartin3 | 0:040129c3d961 | 271 | rpgTick = 0; // Clear rpg counter |
rsartin3 | 0:040129c3d961 | 272 | |
rsartin3 | 0:040129c3d961 | 273 | if(firstList){ // If first time controlling just put display node song name |
rsartin3 | 0:040129c3d961 | 274 | firstList = false; |
rsartin3 | 0:040129c3d961 | 275 | lcd.cls(); |
rsartin3 | 0:040129c3d961 | 276 | lcd.printf("%s", curr->s_name); |
rsartin3 | 0:040129c3d961 | 277 | } |
rsartin3 | 0:040129c3d961 | 278 | else { // Otherwise increment if possible and display song name |
rsartin3 | 0:040129c3d961 | 279 | if(curr -> next != NULL){ |
rsartin3 | 0:040129c3d961 | 280 | curr = curr -> next; |
rsartin3 | 0:040129c3d961 | 281 | lcd.cls(); |
rsartin3 | 0:040129c3d961 | 282 | lcd.printf("%s",curr->s_name); |
rsartin3 | 0:040129c3d961 | 283 | } |
rsartin3 | 0:040129c3d961 | 284 | } |
rsartin3 | 0:040129c3d961 | 285 | } |
rsartin3 | 0:040129c3d961 | 286 | } |
rsartin3 | 0:040129c3d961 | 287 | // ----------------- Turn RPG Counter Clockwise ----------------- |
rsartin3 | 0:040129c3d961 | 288 | else { |
rsartin3 | 0:040129c3d961 | 289 | if(rpg.dir() == -1) { |
rsartin3 | 0:040129c3d961 | 290 | // Check if increment is greater than threshold |
rsartin3 | 0:040129c3d961 | 291 | if(rpgTick < rpgThresh) { |
rsartin3 | 0:040129c3d961 | 292 | rpgTick++; |
rsartin3 | 0:040129c3d961 | 293 | } |
rsartin3 | 0:040129c3d961 | 294 | else { |
rsartin3 | 0:040129c3d961 | 295 | rpgTick = 0; // Clear increment |
rsartin3 | 0:040129c3d961 | 296 | |
rsartin3 | 0:040129c3d961 | 297 | if(!firstList ){ //if on song selection, decrement if possible and displays song name |
rsartin3 | 0:040129c3d961 | 298 | if(curr -> prev != NULL){ |
rsartin3 | 0:040129c3d961 | 299 | curr = curr -> prev; |
rsartin3 | 0:040129c3d961 | 300 | lcd.cls(); |
rsartin3 | 0:040129c3d961 | 301 | lcd.printf("%s",curr->s_name); |
rsartin3 | 0:040129c3d961 | 302 | } |
rsartin3 | 0:040129c3d961 | 303 | } |
rsartin3 | 0:040129c3d961 | 304 | } |
rsartin3 | 0:040129c3d961 | 305 | } |
rsartin3 | 0:040129c3d961 | 306 | } |
rsartin3 | 0:040129c3d961 | 307 | // ----------------- Press RPG Button ----------------- |
rsartin3 | 0:040129c3d961 | 308 | if(rpg.pb()) { |
rsartin3 | 2:d9953793444c | 309 | // if push button is pressed: |
rsartin3 | 0:040129c3d961 | 310 | lcd.cls(); |
rsartin3 | 2:d9953793444c | 311 | lcd.printf("Song playing"); // Inform user song is playing |
rsartin3 | 2:d9953793444c | 312 | |
rsartin3 | 2:d9953793444c | 313 | // File location name |
rsartin3 | 0:040129c3d961 | 314 | mySongName.erase(); |
rsartin3 | 2:d9953793444c | 315 | mySongName.append("/sd/"); // ======== Change if not using base sd card location ===== |
rsartin3 | 2:d9953793444c | 316 | mySongName.append(curr->s_name); // Add current song name |
rsartin3 | 2:d9953793444c | 317 | mySongName.append(".wav"); // Add .wav extension |
rsartin3 | 0:040129c3d961 | 318 | if(DEBUG) printf("Appended Name: %s\n\r", mySongName.c_str()); |
rsartin3 | 0:040129c3d961 | 319 | |
rsartin3 | 2:d9953793444c | 320 | // Wav file header info |
rsartin3 | 2:d9953793444c | 321 | wave_file=fopen(mySongName.c_str(),"r"); // Open file to get header information |
rsartin3 | 2:d9953793444c | 322 | wave_file_check(wave_array, wave_file); // Put info in global variable |
rsartin3 | 2:d9953793444c | 323 | if(DEBUG) printf("Sample Rate: %d\r\n", wave_file_info.samples_per_second); |
rsartin3 | 2:d9953793444c | 324 | fclose(wave_file); // Close file |
rsartin3 | 0:040129c3d961 | 325 | |
rsartin3 | 2:d9953793444c | 326 | // Initialize bottom row of LCD screen |
rsartin3 | 0:040129c3d961 | 327 | lcd.locate(0,1); |
rsartin3 | 0:040129c3d961 | 328 | lcd.printf("Time: / sec"); |
rsartin3 | 0:040129c3d961 | 329 | lcd.locate(10,1); |
rsartin3 | 2:d9953793444c | 330 | // Print length of selected wavfile |
rsartin3 | 0:040129c3d961 | 331 | lcd.printf("%d", (int)(wave_file_info.length_of_file / (wave_file_info.samples_per_second * wave_file_info.number_of_channels * wave_file_info.bytes_per_sample)) ); |
rsartin3 | 0:040129c3d961 | 332 | |
rsartin3 | 2:d9953793444c | 333 | // Open song for playing |
rsartin3 | 0:040129c3d961 | 334 | wave_file=fopen(mySongName.c_str(),"r"); |
rsartin3 | 2:d9953793444c | 335 | // Start Ticker function that counts time elapsed each second |
rsartin3 | 0:040129c3d961 | 336 | songTick.attach(&songClock, 1.0); |
rsartin3 | 2:d9953793444c | 337 | // Set reference time |
rsartin3 | 0:040129c3d961 | 338 | set_time(1357020000); |
rsartin3 | 0:040129c3d961 | 339 | time_i = time(NULL); |
rsartin3 | 2:d9953793444c | 340 | // Play song |
rsartin3 | 0:040129c3d961 | 341 | if(DEBUG) printf("Start Song\r\n"); |
rsartin3 | 0:040129c3d961 | 342 | waver.play(wave_file); |
rsartin3 | 2:d9953793444c | 343 | // Close file |
rsartin3 | 0:040129c3d961 | 344 | if(DEBUG) printf("Close Song!\r\n"); |
rsartin3 | 0:040129c3d961 | 345 | fclose(wave_file); |
rsartin3 | 0:040129c3d961 | 346 | |
rsartin3 | 2:d9953793444c | 347 | // End ticker function |
rsartin3 | 0:040129c3d961 | 348 | songTick.detach(); |
rsartin3 | 0:040129c3d961 | 349 | time_i = 0; |
rsartin3 | 0:040129c3d961 | 350 | |
rsartin3 | 2:d9953793444c | 351 | // Clear LCD and show selected wav file |
rsartin3 | 0:040129c3d961 | 352 | lcd.cls(); |
rsartin3 | 0:040129c3d961 | 353 | lcd.printf(curr->s_name); |
rsartin3 | 0:040129c3d961 | 354 | if(DEBUG) printf("Finished mai SONG!\r\n"); |
rsartin3 | 2:d9953793444c | 355 | } |
rsartin3 | 0:040129c3d961 | 356 | } |
rsartin3 | 0:040129c3d961 | 357 | } |
rsartin3 | 0:040129c3d961 | 358 | } |