Publish code

Dependencies:   FatFileSystem RPG TextLCD mbed wave_player

Committer:
YongChingTee
Date:
Wed Mar 06 00:21:35 2013 +0000
Revision:
1:ebb980d204ae
Parent:
0:ad48675db853
publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YongChingTee 0:ad48675db853 1 #include "mbed.h"
YongChingTee 0:ad48675db853 2 #include "RPG.h"
YongChingTee 0:ad48675db853 3 #include "TextLCD.h"
YongChingTee 0:ad48675db853 4 #include "wave_player.h"
YongChingTee 0:ad48675db853 5 #include "MSCFileSystem.h"
YongChingTee 0:ad48675db853 6 #include <string.h>
YongChingTee 0:ad48675db853 7 #include <cstdio>
YongChingTee 0:ad48675db853 8
YongChingTee 0:ad48675db853 9 #define FSNAME "msc"
YongChingTee 0:ad48675db853 10 MSCFileSystem msc(FSNAME);
YongChingTee 0:ad48675db853 11
YongChingTee 0:ad48675db853 12 RPG rpg1(p21,p22,p23); //Set up RPG
YongChingTee 0:ad48675db853 13 TextLCD lcd1(p9, p10, p11, p12, p13, p14, TextLCD::LCD20x4); // rs, e, d4-d7
YongChingTee 0:ad48675db853 14 TextLCD lcd2(p5, p6, p7, p8, p19, p20, TextLCD::LCD20x4);
YongChingTee 0:ad48675db853 15 AnalogOut mySpeaker(p18);
YongChingTee 0:ad48675db853 16 DigitalIn pb_add(p29);
YongChingTee 0:ad48675db853 17 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
YongChingTee 0:ad48675db853 18 AnalogIn position(p17); //slider
YongChingTee 0:ad48675db853 19 wave_player waver(&mySpeaker);
YongChingTee 0:ad48675db853 20 Serial term(USBTX, USBRX); // tx, rx
YongChingTee 0:ad48675db853 21 int volume = 0;
YongChingTee 0:ad48675db853 22
YongChingTee 0:ad48675db853 23 int count = 0;
YongChingTee 0:ad48675db853 24 int dirt = 0;
YongChingTee 0:ad48675db853 25 char* songList[50];
YongChingTee 0:ad48675db853 26 char* playList[10];
YongChingTee 0:ad48675db853 27 int index = 0;
YongChingTee 0:ad48675db853 28 int index_play = 0;
YongChingTee 0:ad48675db853 29 int len;
YongChingTee 0:ad48675db853 30 int main()
YongChingTee 0:ad48675db853 31 {
YongChingTee 0:ad48675db853 32
YongChingTee 0:ad48675db853 33 DIR *d;
YongChingTee 0:ad48675db853 34 struct dirent *p;
YongChingTee 0:ad48675db853 35 d = opendir("/" FSNAME);
YongChingTee 0:ad48675db853 36
YongChingTee 0:ad48675db853 37 if ( d != NULL )
YongChingTee 0:ad48675db853 38 {
YongChingTee 0:ad48675db853 39 while ( (p = readdir(d)) != NULL )
YongChingTee 0:ad48675db853 40 {
YongChingTee 0:ad48675db853 41 songList[index] = (char*)malloc(sizeof(char) * strlen(p->d_name));
YongChingTee 0:ad48675db853 42 term.printf(" - %s\n", p->d_name);
YongChingTee 0:ad48675db853 43 strcpy(songList[index++], p->d_name);
YongChingTee 0:ad48675db853 44 }
YongChingTee 0:ad48675db853 45 songList[index] = (char*)malloc(sizeof(char) * 8);
YongChingTee 0:ad48675db853 46 strcpy(songList[index++], "Playlist");
YongChingTee 0:ad48675db853 47 }
YongChingTee 0:ad48675db853 48 else
YongChingTee 0:ad48675db853 49 {
YongChingTee 0:ad48675db853 50 error("Could not open directory!");
YongChingTee 0:ad48675db853 51 }
YongChingTee 0:ad48675db853 52
YongChingTee 0:ad48675db853 53 pb_add.mode(PullUp);
YongChingTee 0:ad48675db853 54 FILE *wave_file;
YongChingTee 0:ad48675db853 55
YongChingTee 0:ad48675db853 56 while(1)
YongChingTee 0:ad48675db853 57 {
YongChingTee 0:ad48675db853 58 char * selectedFile;
YongChingTee 0:ad48675db853 59 volume = position*300;
YongChingTee 0:ad48675db853 60 dirt = rpg1.dir(); //Get Dir
YongChingTee 0:ad48675db853 61 count = count + dirt; //Ad Dir to count
YongChingTee 0:ad48675db853 62 if(count > (index-1)){
YongChingTee 0:ad48675db853 63 count = 0;
YongChingTee 0:ad48675db853 64 }
YongChingTee 0:ad48675db853 65 if(count < 0){
YongChingTee 0:ad48675db853 66 count = index-1;
YongChingTee 0:ad48675db853 67 }
YongChingTee 0:ad48675db853 68
YongChingTee 0:ad48675db853 69 lcd1.cls();
YongChingTee 0:ad48675db853 70 lcd1.printf("Song List: \n"); //Print out Count
YongChingTee 0:ad48675db853 71 lcd1.printf("%s ", songList[count]);
YongChingTee 0:ad48675db853 72
YongChingTee 0:ad48675db853 73 lcd2.cls();
YongChingTee 0:ad48675db853 74 lcd2.printf("Volume: %d \n", volume); //Print out Count
YongChingTee 0:ad48675db853 75 lcd2.printf("Mode: STOP");
YongChingTee 0:ad48675db853 76
YongChingTee 0:ad48675db853 77 if(!pb_add)
YongChingTee 0:ad48675db853 78 {
YongChingTee 0:ad48675db853 79 playList[index_play] = (char*)malloc(sizeof(char) * strlen(songList[count]));
YongChingTee 0:ad48675db853 80 strcpy(playList[index_play++], songList[count]);
YongChingTee 0:ad48675db853 81 lcd2.cls();
YongChingTee 0:ad48675db853 82 lcd2.printf("Song Added to PlayList"); //Print out Count
YongChingTee 0:ad48675db853 83 wait(0.5);
YongChingTee 0:ad48675db853 84 }
YongChingTee 0:ad48675db853 85 if (rpg1.pb())
YongChingTee 0:ad48675db853 86 {
YongChingTee 0:ad48675db853 87 if(count == (index-1)&&index_play > 0)
YongChingTee 0:ad48675db853 88 {
YongChingTee 0:ad48675db853 89 for(int i = 0; i < (index_play);i++)
YongChingTee 0:ad48675db853 90 {
YongChingTee 0:ad48675db853 91 len = 5+strlen(playList[i]);
YongChingTee 0:ad48675db853 92 selectedFile = (char*)malloc(sizeof(char) * len);
YongChingTee 0:ad48675db853 93 strcpy(selectedFile, "/msc/");
YongChingTee 0:ad48675db853 94 strcat(selectedFile, playList[i]);
YongChingTee 0:ad48675db853 95 wave_file=fopen(selectedFile,"r");
YongChingTee 0:ad48675db853 96 lcd2.cls();
YongChingTee 0:ad48675db853 97 lcd2.printf("Volume: %d \n", volume); //Print out Count
YongChingTee 0:ad48675db853 98 lcd2.printf("Mode: PLAY");
YongChingTee 0:ad48675db853 99 waver.play(wave_file);
YongChingTee 0:ad48675db853 100 fclose(wave_file);
YongChingTee 0:ad48675db853 101 free(selectedFile);
YongChingTee 0:ad48675db853 102 if(!pb_add) //stop the whole play list
YongChingTee 0:ad48675db853 103 break;
YongChingTee 0:ad48675db853 104 }
YongChingTee 0:ad48675db853 105 }
YongChingTee 0:ad48675db853 106 else
YongChingTee 0:ad48675db853 107 {
YongChingTee 0:ad48675db853 108 len = 5+strlen(songList[count]);
YongChingTee 0:ad48675db853 109 selectedFile = (char*)malloc(sizeof(char) * len);
YongChingTee 0:ad48675db853 110 strcpy(selectedFile, "/msc/");
YongChingTee 0:ad48675db853 111 strcat(selectedFile, songList[count]);
YongChingTee 0:ad48675db853 112 wave_file=fopen(selectedFile,"r");
YongChingTee 0:ad48675db853 113 lcd2.cls();
YongChingTee 0:ad48675db853 114 lcd2.printf("Volume: %d \n", volume); //Print out Count
YongChingTee 0:ad48675db853 115 lcd2.printf("Mode: PLAY");
YongChingTee 0:ad48675db853 116 waver.play(wave_file);
YongChingTee 0:ad48675db853 117 fclose(wave_file);
YongChingTee 0:ad48675db853 118 free(selectedFile);
YongChingTee 0:ad48675db853 119 }
YongChingTee 0:ad48675db853 120 while(!pb_add){}; //prevent from adding stuff to the play list
YongChingTee 0:ad48675db853 121 }
YongChingTee 0:ad48675db853 122 }
YongChingTee 0:ad48675db853 123
YongChingTee 0:ad48675db853 124 }