a
Dependencies: EthernetNetIf FatFileSystemCpp HTTPClient mbed wave_player
Fork of MSCUsbHost by
Revision 4:630039c09e43, committed 2014-11-17
- Comitter:
- thanhnhiel
- Date:
- Mon Nov 17 15:43:55 2014 +0000
- Parent:
- 3:95e55809ecdb
- Commit message:
- a
Changed in this revision
diff -r 95e55809ecdb -r 630039c09e43 EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Mon Nov 17 15:43:55 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mamezu/code/EthernetNetIf/#0f6c82fcde82
diff -r 95e55809ecdb -r 630039c09e43 HTTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Mon Nov 17 15:43:55 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mamezu/code/HTTPClient/#62fac7f06c8d
diff -r 95e55809ecdb -r 630039c09e43 main.cpp --- a/main.cpp Mon Jul 30 13:49:56 2012 +0000 +++ b/main.cpp Mon Nov 17 15:43:55 2014 +0000 @@ -1,3 +1,144 @@ +#include "mbed.h" + +#include "MSCFileSystem.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" +#include <wave_player.h> + +// Network and USB filesystem +EthernetNetIf eth; +HTTPClient http; +MSCFileSystem usb("usb"); +LocalFileSystem local("local"); + +// Audio Output +AnalogOut DACout(p18); +wave_player audio(&DACout); + +DigitalOut myled(LED1); + +// Simple HTTP helper functions +int get_file(char *url, char *file) { + printf("Getting url to file [%s]...\n", url); + HTTPFile f(file); + HTTPResult r = http.get(url, &f); + if (r != HTTP_OK) { + printf("HTTPResult error %d\n", r); + + return 0; + } + return 1; +} + +int get_string(char *url, char *str) { + printf("Getting url [%s] to string...\n", url); + HTTPText t; + HTTPResult r = http.get(url, &t); + if (r != HTTP_OK) { + printf("HTTPResult error %d\n", r); + str[0] = 0; + return 0; + } + strcpy(str, t.gets()); + return 1; +} + +FILE *wave_file; +void PlayWav(char *filename) +{ + wave_file=fopen(filename, "r"); + audio.play(wave_file); + fclose(wave_file); +} + + + + +char receiveBuffer[100]; +char randomNumber[5]; +const char* genURL = "http://www.yourdomain.com/voice.php?msg="; +char urlBuffer[70]; +char nameBuffer[20]; +FILE *fp; + +int main() { + printf("Setup network...\n"); + EthernetErr ethErr = eth.setup(); + if(ethErr) { + printf("Error %d in setup\n", ethErr); + } + + /* Just make sure USB is initialized */ + DIR *d; + struct dirent *p; + d = opendir("/usb"); + printf("\nList of files on the flash drive:\n"); + if ( d != NULL ) + { + while ( (p = readdir(d)) != NULL ) + { + printf(" - %s\n", p->d_name); + } + } + else + { + error("Could not open directory!\n"); + } + printf("\n\n"); + + + printf("Getting overall-message\n"); + get_string("http://www.yourdomain.com/voice.php?msg=The+random+number+is", receiveBuffer); + strcat(receiveBuffer, "\n"); + printf(receiveBuffer); + + get_file("http://www.yourdomain.com/voice.wav", "/usb/random.wav"); + + + printf("\nStarting Random-Number Generator cycle\n\n"); + while(1) { + if (get_string("http://www.yourdomain.com/rand.php", receiveBuffer) == 0) + continue; + strcpy(randomNumber, receiveBuffer); + + strcpy(receiveBuffer, "The random number is "); + strcat(receiveBuffer, randomNumber); + strcat(receiveBuffer, "\n\n"); + printf(receiveBuffer); // Print the random number + + myled = 1; + + strcpy(nameBuffer, "/usb/number"); + strcat(nameBuffer, randomNumber); + strcat(nameBuffer, ".wav"); + + /* Check if the file exists already */ + fp = fopen(nameBuffer, "r"); + if ( fp == NULL ) + { + /* If not, download the number */ + strcpy(urlBuffer, genURL); + strcat(urlBuffer, randomNumber); + + get_string(urlBuffer, receiveBuffer); + strcat(receiveBuffer, "\n\n"); + printf(receiveBuffer); + + get_file("www.yourdomain.com/voice.wav", nameBuffer); + } else { + fclose(fp); + } + + PlayWav("/usb/random.wav"); + PlayWav(nameBuffer); + + + myled = 0; + } +} + + +#if 0 #include "mbed.h" #include "MSCFileSystem.h" //#include <stat.h> @@ -67,3 +208,4 @@ fclose(fp); printf("\n - OK, read string: '%s'\n\n", buf); } +#endif \ No newline at end of file
diff -r 95e55809ecdb -r 630039c09e43 mbed.bld --- a/mbed.bld Mon Jul 30 13:49:56 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/10b9abbe79a6 \ No newline at end of file
diff -r 95e55809ecdb -r 630039c09e43 mbed.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.lib Mon Nov 17 15:43:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/#10b9abbe79a6
diff -r 95e55809ecdb -r 630039c09e43 wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Mon Nov 17 15:43:55 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/sravet/code/wave_player/#acc3e18e77ad