a

Dependencies:   EthernetNetIf FatFileSystemCpp HTTPClient mbed wave_player

Fork of MSCUsbHost by Igor Skochinsky

Revision:
4:630039c09e43
Parent:
0:e294af8d0e07
--- 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