a

Dependencies:   EthernetNetIf FatFileSystemCpp HTTPClient mbed wave_player

Fork of MSCUsbHost by Igor Skochinsky

Committer:
thanhnhiel
Date:
Mon Nov 17 15:43:55 2014 +0000
Revision:
4:630039c09e43
Parent:
0:e294af8d0e07
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thanhnhiel 4:630039c09e43 1 #include "mbed.h"
thanhnhiel 4:630039c09e43 2
thanhnhiel 4:630039c09e43 3 #include "MSCFileSystem.h"
thanhnhiel 4:630039c09e43 4 #include "EthernetNetIf.h"
thanhnhiel 4:630039c09e43 5 #include "HTTPClient.h"
thanhnhiel 4:630039c09e43 6 #include <wave_player.h>
thanhnhiel 4:630039c09e43 7
thanhnhiel 4:630039c09e43 8 // Network and USB filesystem
thanhnhiel 4:630039c09e43 9 EthernetNetIf eth;
thanhnhiel 4:630039c09e43 10 HTTPClient http;
thanhnhiel 4:630039c09e43 11 MSCFileSystem usb("usb");
thanhnhiel 4:630039c09e43 12 LocalFileSystem local("local");
thanhnhiel 4:630039c09e43 13
thanhnhiel 4:630039c09e43 14 // Audio Output
thanhnhiel 4:630039c09e43 15 AnalogOut DACout(p18);
thanhnhiel 4:630039c09e43 16 wave_player audio(&DACout);
thanhnhiel 4:630039c09e43 17
thanhnhiel 4:630039c09e43 18 DigitalOut myled(LED1);
thanhnhiel 4:630039c09e43 19
thanhnhiel 4:630039c09e43 20 // Simple HTTP helper functions
thanhnhiel 4:630039c09e43 21 int get_file(char *url, char *file) {
thanhnhiel 4:630039c09e43 22 printf("Getting url to file [%s]...\n", url);
thanhnhiel 4:630039c09e43 23 HTTPFile f(file);
thanhnhiel 4:630039c09e43 24 HTTPResult r = http.get(url, &f);
thanhnhiel 4:630039c09e43 25 if (r != HTTP_OK) {
thanhnhiel 4:630039c09e43 26 printf("HTTPResult error %d\n", r);
thanhnhiel 4:630039c09e43 27
thanhnhiel 4:630039c09e43 28 return 0;
thanhnhiel 4:630039c09e43 29 }
thanhnhiel 4:630039c09e43 30 return 1;
thanhnhiel 4:630039c09e43 31 }
thanhnhiel 4:630039c09e43 32
thanhnhiel 4:630039c09e43 33 int get_string(char *url, char *str) {
thanhnhiel 4:630039c09e43 34 printf("Getting url [%s] to string...\n", url);
thanhnhiel 4:630039c09e43 35 HTTPText t;
thanhnhiel 4:630039c09e43 36 HTTPResult r = http.get(url, &t);
thanhnhiel 4:630039c09e43 37 if (r != HTTP_OK) {
thanhnhiel 4:630039c09e43 38 printf("HTTPResult error %d\n", r);
thanhnhiel 4:630039c09e43 39 str[0] = 0;
thanhnhiel 4:630039c09e43 40 return 0;
thanhnhiel 4:630039c09e43 41 }
thanhnhiel 4:630039c09e43 42 strcpy(str, t.gets());
thanhnhiel 4:630039c09e43 43 return 1;
thanhnhiel 4:630039c09e43 44 }
thanhnhiel 4:630039c09e43 45
thanhnhiel 4:630039c09e43 46 FILE *wave_file;
thanhnhiel 4:630039c09e43 47 void PlayWav(char *filename)
thanhnhiel 4:630039c09e43 48 {
thanhnhiel 4:630039c09e43 49 wave_file=fopen(filename, "r");
thanhnhiel 4:630039c09e43 50 audio.play(wave_file);
thanhnhiel 4:630039c09e43 51 fclose(wave_file);
thanhnhiel 4:630039c09e43 52 }
thanhnhiel 4:630039c09e43 53
thanhnhiel 4:630039c09e43 54
thanhnhiel 4:630039c09e43 55
thanhnhiel 4:630039c09e43 56
thanhnhiel 4:630039c09e43 57 char receiveBuffer[100];
thanhnhiel 4:630039c09e43 58 char randomNumber[5];
thanhnhiel 4:630039c09e43 59 const char* genURL = "http://www.yourdomain.com/voice.php?msg=";
thanhnhiel 4:630039c09e43 60 char urlBuffer[70];
thanhnhiel 4:630039c09e43 61 char nameBuffer[20];
thanhnhiel 4:630039c09e43 62 FILE *fp;
thanhnhiel 4:630039c09e43 63
thanhnhiel 4:630039c09e43 64 int main() {
thanhnhiel 4:630039c09e43 65 printf("Setup network...\n");
thanhnhiel 4:630039c09e43 66 EthernetErr ethErr = eth.setup();
thanhnhiel 4:630039c09e43 67 if(ethErr) {
thanhnhiel 4:630039c09e43 68 printf("Error %d in setup\n", ethErr);
thanhnhiel 4:630039c09e43 69 }
thanhnhiel 4:630039c09e43 70
thanhnhiel 4:630039c09e43 71 /* Just make sure USB is initialized */
thanhnhiel 4:630039c09e43 72 DIR *d;
thanhnhiel 4:630039c09e43 73 struct dirent *p;
thanhnhiel 4:630039c09e43 74 d = opendir("/usb");
thanhnhiel 4:630039c09e43 75 printf("\nList of files on the flash drive:\n");
thanhnhiel 4:630039c09e43 76 if ( d != NULL )
thanhnhiel 4:630039c09e43 77 {
thanhnhiel 4:630039c09e43 78 while ( (p = readdir(d)) != NULL )
thanhnhiel 4:630039c09e43 79 {
thanhnhiel 4:630039c09e43 80 printf(" - %s\n", p->d_name);
thanhnhiel 4:630039c09e43 81 }
thanhnhiel 4:630039c09e43 82 }
thanhnhiel 4:630039c09e43 83 else
thanhnhiel 4:630039c09e43 84 {
thanhnhiel 4:630039c09e43 85 error("Could not open directory!\n");
thanhnhiel 4:630039c09e43 86 }
thanhnhiel 4:630039c09e43 87 printf("\n\n");
thanhnhiel 4:630039c09e43 88
thanhnhiel 4:630039c09e43 89
thanhnhiel 4:630039c09e43 90 printf("Getting overall-message\n");
thanhnhiel 4:630039c09e43 91 get_string("http://www.yourdomain.com/voice.php?msg=The+random+number+is", receiveBuffer);
thanhnhiel 4:630039c09e43 92 strcat(receiveBuffer, "\n");
thanhnhiel 4:630039c09e43 93 printf(receiveBuffer);
thanhnhiel 4:630039c09e43 94
thanhnhiel 4:630039c09e43 95 get_file("http://www.yourdomain.com/voice.wav", "/usb/random.wav");
thanhnhiel 4:630039c09e43 96
thanhnhiel 4:630039c09e43 97
thanhnhiel 4:630039c09e43 98 printf("\nStarting Random-Number Generator cycle\n\n");
thanhnhiel 4:630039c09e43 99 while(1) {
thanhnhiel 4:630039c09e43 100 if (get_string("http://www.yourdomain.com/rand.php", receiveBuffer) == 0)
thanhnhiel 4:630039c09e43 101 continue;
thanhnhiel 4:630039c09e43 102 strcpy(randomNumber, receiveBuffer);
thanhnhiel 4:630039c09e43 103
thanhnhiel 4:630039c09e43 104 strcpy(receiveBuffer, "The random number is ");
thanhnhiel 4:630039c09e43 105 strcat(receiveBuffer, randomNumber);
thanhnhiel 4:630039c09e43 106 strcat(receiveBuffer, "\n\n");
thanhnhiel 4:630039c09e43 107 printf(receiveBuffer); // Print the random number
thanhnhiel 4:630039c09e43 108
thanhnhiel 4:630039c09e43 109 myled = 1;
thanhnhiel 4:630039c09e43 110
thanhnhiel 4:630039c09e43 111 strcpy(nameBuffer, "/usb/number");
thanhnhiel 4:630039c09e43 112 strcat(nameBuffer, randomNumber);
thanhnhiel 4:630039c09e43 113 strcat(nameBuffer, ".wav");
thanhnhiel 4:630039c09e43 114
thanhnhiel 4:630039c09e43 115 /* Check if the file exists already */
thanhnhiel 4:630039c09e43 116 fp = fopen(nameBuffer, "r");
thanhnhiel 4:630039c09e43 117 if ( fp == NULL )
thanhnhiel 4:630039c09e43 118 {
thanhnhiel 4:630039c09e43 119 /* If not, download the number */
thanhnhiel 4:630039c09e43 120 strcpy(urlBuffer, genURL);
thanhnhiel 4:630039c09e43 121 strcat(urlBuffer, randomNumber);
thanhnhiel 4:630039c09e43 122
thanhnhiel 4:630039c09e43 123 get_string(urlBuffer, receiveBuffer);
thanhnhiel 4:630039c09e43 124 strcat(receiveBuffer, "\n\n");
thanhnhiel 4:630039c09e43 125 printf(receiveBuffer);
thanhnhiel 4:630039c09e43 126
thanhnhiel 4:630039c09e43 127 get_file("www.yourdomain.com/voice.wav", nameBuffer);
thanhnhiel 4:630039c09e43 128 } else {
thanhnhiel 4:630039c09e43 129 fclose(fp);
thanhnhiel 4:630039c09e43 130 }
thanhnhiel 4:630039c09e43 131
thanhnhiel 4:630039c09e43 132 PlayWav("/usb/random.wav");
thanhnhiel 4:630039c09e43 133 PlayWav(nameBuffer);
thanhnhiel 4:630039c09e43 134
thanhnhiel 4:630039c09e43 135
thanhnhiel 4:630039c09e43 136 myled = 0;
thanhnhiel 4:630039c09e43 137 }
thanhnhiel 4:630039c09e43 138 }
thanhnhiel 4:630039c09e43 139
thanhnhiel 4:630039c09e43 140
thanhnhiel 4:630039c09e43 141 #if 0
igorsk 0:e294af8d0e07 142 #include "mbed.h"
igorsk 0:e294af8d0e07 143 #include "MSCFileSystem.h"
igorsk 0:e294af8d0e07 144 //#include <stat.h>
igorsk 0:e294af8d0e07 145
igorsk 0:e294af8d0e07 146 #define FSNAME "msc"
igorsk 0:e294af8d0e07 147 MSCFileSystem msc(FSNAME);
igorsk 0:e294af8d0e07 148
igorsk 0:e294af8d0e07 149 int main()
igorsk 0:e294af8d0e07 150 {
igorsk 0:e294af8d0e07 151 DIR *d;
igorsk 0:e294af8d0e07 152 struct dirent *p;
igorsk 0:e294af8d0e07 153 //struct stat st;
igorsk 0:e294af8d0e07 154 //char path[PATH_MAX];
igorsk 0:e294af8d0e07 155
igorsk 0:e294af8d0e07 156 printf("\n\n================================\n");
igorsk 0:e294af8d0e07 157 printf("USB Mass storage demo program for mbed LPC1768\n");
igorsk 0:e294af8d0e07 158 printf("================================\n\n");
igorsk 0:e294af8d0e07 159
igorsk 0:e294af8d0e07 160 d = opendir("/" FSNAME);
igorsk 0:e294af8d0e07 161
igorsk 0:e294af8d0e07 162 printf("\nList of files on the flash drive:\n");
igorsk 0:e294af8d0e07 163 if ( d != NULL )
igorsk 0:e294af8d0e07 164 {
igorsk 0:e294af8d0e07 165 while ( (p = readdir(d)) != NULL )
igorsk 0:e294af8d0e07 166 {
igorsk 0:e294af8d0e07 167 printf(" - %s\n", p->d_name);
igorsk 0:e294af8d0e07 168 /* no <stat.h> on mbed, it seems :/
igorsk 0:e294af8d0e07 169 sprintf(path, "/"FSNAME"/%s", p->d_name);
igorsk 0:e294af8d0e07 170 if ( stat(path, &st) == 0 )
igorsk 0:e294af8d0e07 171 {
igorsk 0:e294af8d0e07 172 if ( S_ISDIR(st.st_mode) )
igorsk 0:e294af8d0e07 173 printf(" <directory>\n");
igorsk 0:e294af8d0e07 174 else
igorsk 0:e294af8d0e07 175 printf(" %d\n", st.st_size);
igorsk 0:e294af8d0e07 176 }
igorsk 0:e294af8d0e07 177 else
igorsk 0:e294af8d0e07 178 {
igorsk 0:e294af8d0e07 179 printf(" ???\n");
igorsk 0:e294af8d0e07 180 }*/
igorsk 0:e294af8d0e07 181 }
igorsk 0:e294af8d0e07 182 }
igorsk 0:e294af8d0e07 183 else
igorsk 0:e294af8d0e07 184 {
igorsk 0:e294af8d0e07 185 error("Could not open directory!");
igorsk 0:e294af8d0e07 186 }
igorsk 0:e294af8d0e07 187 printf("\nTesting file write:\n");
igorsk 0:e294af8d0e07 188 FILE *fp = fopen( "/" FSNAME "/msctest.txt", "w");
igorsk 0:e294af8d0e07 189 if ( fp == NULL )
igorsk 0:e294af8d0e07 190 {
igorsk 0:e294af8d0e07 191 error("Could not open file for write\n");
igorsk 0:e294af8d0e07 192 }
igorsk 0:e294af8d0e07 193 fprintf(fp, "Hello mass storage!");
igorsk 0:e294af8d0e07 194 fclose(fp);
igorsk 0:e294af8d0e07 195 printf("\n - OK\n");
igorsk 0:e294af8d0e07 196
igorsk 0:e294af8d0e07 197 printf("\nTesting file read:\n");
igorsk 0:e294af8d0e07 198 fp = fopen( "/" FSNAME "/msctest.txt", "r");
igorsk 0:e294af8d0e07 199 if ( fp == NULL )
igorsk 0:e294af8d0e07 200 {
igorsk 0:e294af8d0e07 201 error("Could not open file for read\n");
igorsk 0:e294af8d0e07 202 }
igorsk 0:e294af8d0e07 203 char buf[256];
igorsk 0:e294af8d0e07 204 if ( NULL == fgets(buf, sizeof(buf), fp) )
igorsk 0:e294af8d0e07 205 {
igorsk 0:e294af8d0e07 206 error("Error reading from file\n");
igorsk 0:e294af8d0e07 207 }
igorsk 0:e294af8d0e07 208 fclose(fp);
igorsk 0:e294af8d0e07 209 printf("\n - OK, read string: '%s'\n\n", buf);
igorsk 0:e294af8d0e07 210 }
thanhnhiel 4:630039c09e43 211 #endif