chong wang / Mbed 2 deprecated lab3-News_Reader1

Dependencies:   FatFileSystem HTTPClient NetServices RPG SDFileSystem TextLCD mbed spxml

Fork of Arduino_MP3_Shield_MP3Player_UI by Matthew Petersen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "VS1002.h"
00004 #include "TextLCD.h"
00005 #include "RPG.h"
00006 #include "EthernetNetIf.h"
00007 #include "HTTPClient.h"
00008 #include "SDFileSystem.h"
00009 #include "fetch.h"
00010 
00011 #include <string>
00012 #include <iostream>
00013 #include <sstream>
00014 
00015 HTTPClient http1;
00016 HTTPResult result1;
00017 EthernetNetIf eth;
00018 RPG rpg1(p26,p27,p25);
00019 TextLCD lcd1(p10,p18,p24,p23,p22,p21,TextLCD::LCD16x2);
00020 VS1002 mp3(p11,p12,p13,p8,"sd", p5,p6,p7,p14,p15,p16,p17,p20);
00021 SDFileSystem sd(p11, p12, p13, p8, "sd");
00022 char *ct[10]= {"/sd/0.txt","/sd/1.txt","/sd/2.txt","/sd/3.txt","/sd/4.txt","/sd/5.txt","/sd/6.txt","/sd/7.txt","/sd/8.txt","/sd/9.txt"};
00023 char *mp3ct[10]= {"/sd/0.mp3","/sd/1.mp3","/sd/2.mp3","/sd/3.mp3","/sd/4.mp3","/sd/5.mp3","/sd/6.mp3","/sd/7.mp3","/sd/8.mp3","/sd/9.mp3"};
00024 
00025 void download(string voiceurl, int a)
00026 {
00027     HTTPFile f(mp3ct[a]);
00028     HTTPResult r = http1.get(voiceurl.c_str(), &f);
00029     if(r==HTTP_OK) {
00030         printf("download Result OK\n");
00031     } else {
00032         printf("download Error %d\n", r);
00033     }
00034     wait(1.5);
00035     HTTPFile clear(f);
00036     voiceurl.erase();
00037 
00038 }
00039 
00040 int main ()
00041 {
00042     /*============================================================
00043      * MP3 Initialising
00044      *==========================================================*/
00045 #ifndef FS_ONLY
00046     mp3._RST = 1;
00047     mp3.cs_high();                                   //chip disabled
00048     mp3.sci_initialise();                            //initialise MBED
00049     mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM+SM_DIFF));
00050     mp3.sci_write(0x03, 0x9800);
00051     mp3.sdi_initialise();
00052 #endif
00053 
00054     // make debug port Fast
00055     Serial pc(USBTX, USBRX);
00056     pc.baud(9600);
00057 
00058     lcd1.cls();
00059     lcd1.printf("net setup");
00060     EthernetErr ethErr = eth.setup();
00061     if (ethErr) {
00062         lcd1.printf("\n\r net error");
00063 
00064     }
00065     lcd1.printf("\n\r net ok");
00066     wait(0.5);
00067     lcd1.printf("streaming news");
00068     fetch();
00069     wait(1);
00070     lcd1.cls();
00071     int a=1;
00072     printf("%i\n",a);
00073     int count = 0;
00074     int oldcount=0;
00075     int dirt = 0;
00076 
00077     FILE * pFile;
00078     long lSize;
00079     char * buffer;
00080     size_t  result;
00081 
00082     while (true) {
00083         lcd1.printf(" news %d \n\n",a);
00084         dirt = rpg1.dir(); //Get Dir
00085         count = count + dirt; //Ad Dir to count
00086         if (count>oldcount) {
00087             oldcount=count;
00088             a++;
00089             lcd1.printf(" news %d \n\n",a);
00090 
00091         } else if(count<oldcount) {
00092             oldcount=count;
00093             a--;
00094             lcd1.printf(" news %d \n\n",a);
00095         }
00096 
00097         if (rpg1.pb()) {
00098 
00099 
00100             lcd1.printf("You have chosen the news %d \n",a);
00101 
00102 
00103             pFile = fopen ( ct[a], "rb" );
00104             if (pFile==NULL) {
00105                 printf ("File error");
00106                 exit (1);
00107             }
00108 
00109             // obtain file size:
00110             fseek (pFile , 0 , SEEK_END);
00111             lSize = ftell (pFile);
00112             rewind (pFile);
00113 
00114             // allocate memory to contain the whole file:
00115             buffer = (char*) malloc (sizeof(char)*lSize);
00116             if (buffer == NULL) {
00117                 printf ("Memory error");
00118                 exit (2);
00119             }
00120 
00121             // copy the file into the buffer:
00122             result = fread (buffer,1,lSize,pFile);
00123             if (result != lSize) {
00124                 printf ("Reading error");
00125                 exit (3);
00126             }
00127             string voiceurl=string(buffer);
00128 
00129             /* the whole file is now loaded in the memory buffer. */
00130 
00131             // terminate
00132             fclose (pFile);
00133             free (buffer);
00134             lcd1.printf("You have chosen the news %d \n",a);
00135             printf("%s\n",voiceurl.c_str());
00136             download( voiceurl,a);
00137             voiceurl.erase();
00138             wait(1);
00139             lcd1.cls();
00140             mp3.play_song(a+10);
00141 
00142             wait(1);
00143 
00144         }
00145         wait(0.2);
00146         printf("pb: %i  ", rpg1.pb()); //Print out Count
00147 
00148         printf("count:%d  dirt:%d\n",count,dirt);
00149     }
00150     printf("Done.\r\n");
00151 }
00152