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

Files at this revision

API Documentation at this revision

Comitter:
cwang365
Date:
Wed Mar 06 00:36:15 2013 +0000
Parent:
0:9cb0b3d1b9e6
Child:
2:447591cbba27
Commit message:
lab3-news_reader

Changed in this revision

FATFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
NetServices.lib Show annotated file Show diff for this revision Revisions of this file
RPG.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
VS1002.cpp Show annotated file Show diff for this revision Revisions of this file
fetch.cpp Show annotated file Show diff for this revision Revisions of this file
fetch.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
spxml.lib Show annotated file Show diff for this revision Revisions of this file
--- a/FATFileSystem.lib	Tue Oct 12 21:25:35 2010 +0000
+++ b/FATFileSystem.lib	Wed Mar 06 00:36:15 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_unsupported/code/fatfilesystem/
\ No newline at end of file
+http://mbed.org/users/mbed_unsupported/code/FatFileSystem/#333d6e93e58f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mamezu/code/HTTPClient/#62fac7f06c8d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetServices.lib	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/segundo/code/NetServices/#1fb1dbb9ad9e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RPG.lib	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/canderson199/code/RPG/#0b389c2c21b5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/cwang365/code/SDFileSystem/#7b4c861fe9ff
--- a/VS1002.cpp	Tue Oct 12 21:25:35 2010 +0000
+++ b/VS1002.cpp	Wed Mar 06 00:36:15 2013 +0000
@@ -10,31 +10,32 @@
  * Constructor
  * =================================================================*/
 VS1002::VS1002(
-PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name,
-         PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst,
-         PinName dreq, PinName dcs, PinName vol)
+    PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name,
+    PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst,
+    PinName dreq, PinName dcs, PinName vol)
     :
-     _sd(mmosi, mmiso, ssck, ccs, name),
-     _spi(mosi, miso, sck), 
-     _CS(cs), 
-     _RST(rst), 
-     _DREQ(dreq),
-     _DCS(dcs), 
-     _VOL(vol) {
-    
-    }    
+    _sd(mmosi, mmiso, ssck, ccs, name),
+    _spi(mosi, miso, sck),
+    _CS(cs),
+    _RST(rst),
+    _DREQ(dreq),
+    _DCS(dcs),
+    _VOL(vol)
+{
+
+}
 
 /*===================================================================
  * Functions
  *==================================================================*/
- 
+
 void VS1002::cs_low(void)
 {
-    _CS = 0;                                
+    _CS = 0;
 }
 void VS1002::cs_high(void)
 {
-    _CS = 1;                                
+    _CS = 1;
 }
 void VS1002::dcs_low(void)
 {
@@ -85,11 +86,10 @@
     _RST = 1;                                //no reset
     _spi.format(8,0);                        //spi 8bit interface, steady state low
     _spi.frequency(1000000);                //rising edge data record, freq. 1Mhz
-    
+
     cs_low();
-    for(int i=0; i<4; i++)
-    {
-    _spi.write(0xFF);                        //clock the chip a bit
+    for(int i=0; i<4; i++) {
+        _spi.write(0xFF);                        //clock the chip a bit
     }
     cs_high();
     dcs_high();
@@ -99,51 +99,51 @@
 {
     _spi.format(8,0);
     _spi.frequency(7000000);                //set to 7MHz
-    
+
     cs_high();
     dcs_high();
 }
 void VS1002::sci_write(unsigned char address, unsigned short int data)
 {
     sci_en();                                //enables SCI/disables SDI
-    
+
     while(!_DREQ);                            //wait unitl data request is high
     _spi.write(0x02);                        //SCI write
     _spi.write(address);                    //register address
     _spi.write((data >> 8) & 0xFF);            //write out first half of data word
     _spi.write(data & 0xFF);                //write out second half of data word
-    
+
     sci_dis();                                //enables SDI/disables SCI
     wait_us(5);
 }
 void VS1002::sdi_write(unsigned char datum)
 {
     sdi_en();
-    
+
     while(!_DREQ);
     _spi.write(datum);
-    
+
     sci_dis();
 }
 unsigned short int VS1002::read(unsigned short int address)
 {
     cs_low();                                //enables SCI/disables SDI
-    
+
     while(!_DREQ);                            //wait unitl data request is high
     _spi.write(0x03);                        //SCI write
     _spi.write(address);                    //register address
     unsigned short int received = _spi.write(0x00);    //write out dummy byte
     received <<= 8;
     received += _spi.write(0x00);            //write out dummy byte
-    
+
     cs_high();                                //enables SDI/disables SCI
-    
+
     return received;                        //return received word
 }
 void VS1002::sine_test_activate(unsigned char wave)
 {
     cs_high();                                //enables SDI/disables SCI
-    
+
     while(!_DREQ);                            //wait unitl data request is high
     _spi.write(0x53);                        //SDI write
     _spi.write(0xEF);                        //SDI write
@@ -159,7 +159,7 @@
 void VS1002::sine_test_deactivate(void)
 {
     cs_high();
-    
+
     while(!_DREQ);
     _spi.write(0x45);                        //SDI write
     _spi.write(0x78);                        //SDI write
@@ -172,22 +172,22 @@
 }
 void VS1002::volume(void)
 {
- #ifdef FIXED_VOL
+#ifdef FIXED_VOL
     unsigned char volumize = (0 * 255); // FIXED VOL (not support volume input)
- #else
+#else
     unsigned char volumize = (_VOL * 255);
- #endif
+#endif
     while(!_DREQ);
-    
+
     unsigned short int attenuation = ((256 * volumize) + volumize);
     sci_write(0x0B, attenuation);
-   
+
 }
 
 void VS1002::play_song(int song_number)
 {
     /*====== Song Select ======*/
-    
+
 //    char list[10000] = {0};
     char list[1000] = {0};
     char str[16] = {"/sd/"};
@@ -199,40 +199,31 @@
     DIR *d;
     struct dirent *p;
     d = opendir("/sd");
-    if(d != NULL) 
-           {
-               while((p = readdir(d)) != NULL) 
-           {
-               strcat(list, "*");
-               strcat(list, p->d_name);
-               num_of_files++;
-           }
-    }
-    else 
-    {
-         perror("Could not open directory!");
+    if(d != NULL) {
+        while((p = readdir(d)) != NULL) {
+            strcat(list, "*");
+            strcat(list, p->d_name);
+            num_of_files++;
+        }
+    } else {
+        perror("Could not open directory!");
     }
     strcat(list, "*");                                //terminating *
-    if(num_of_files < song_number)
-    {
+    if(num_of_files < song_number) {
         return;
     }
-    while(play != song_number)
-    {
+    while(play != song_number) {
         char symbol = list[startplace];
         startplace++;
-        if(symbol == 0x2A)                        //0x2A = "*"    
-        {
+        if(symbol == 0x2A) {                      //0x2A = "*"
             play++;
-        }                        
+        }
     }
     play = 0;
-    while(play != (song_number+1))
-    {
+    while(play != (song_number+1)) {
         char symbol = list[endplace];
-        endplace++;    
-        if(symbol == 0x2A)                        //0x2A = "*"    
-        {
+        endplace++;
+        if(symbol == 0x2A) {                      //0x2A = "*"
             play++;
         }
     }
@@ -240,44 +231,42 @@
     strncat(str, &list[startplace], endplace-startplace);
     str[(endplace-startplace)+3] = '\0';
 
-//printf("list: %s\r\n",list); //debug      
+//printf("list: %s\r\n",list); //debug
 
     /*====== File Transfer ======*/
 
-     // return if not MP3 file
+    // return if not MP3 file
     if (!((strstr(str,"MP3")!=NULL)||(strstr(str,"mp3")!=NULL))) return;
     // display filename.mp3 to the lcd screen
     lcd.printf("Now Playing: %s\r\n",str);
-    
+
     FILE *song;
     unsigned char array[512];
-        
+
     song = fopen(str, "rb");
-    
-    if(!song) 
-    {
+
+    if(!song) {
         exit(1);
     }
-   
-       while((!feof(song))&&!pause1)
-    {
-           fread(&array, 1, 512, song);
-           for(int i=0; i<512; i++)
-           {
+
+    while((!feof(song))&&!pause1) {
+        fread(&array, 1, 512, song);
+        for(int i=0; i<512; i++) {
 #ifndef FS_ONLY
             sdi_write(array[i]);
-        //    printf(".");
+            //    printf(".");
 #endif
-           }
+        }
 #ifndef FS_ONLY
-            volume();
+        volume();
 #endif
-       }
-    for(int n=0; n<2048; n++)
-       {
+    }
+    for(int n=0; n<2048; n++) {
 #ifndef FS_ONLY
-           sdi_write(0x00);
+        sdi_write(0x00);
 #endif
-       }
+    }
     fclose(song);                                //close the file
+    lcd.printf("Done Playing: %s\r\n\n",str);
+    lcd.printf("                                                    \n");
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fetch.cpp	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,117 @@
+#include "mbed.h"
+#include "VS1002.h"
+#include "TextLCD.h"
+#include "RPG.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+#include "SDFileSystem.h"
+
+#include <string>
+#include <iostream>
+
+TextLCD lcd2(p10, p18, p24, p23, p22, p21, TextLCD::LCD16x2 ); // rs, e, d0-d3
+
+HTTPClient http;
+HTTPResult result;
+bool completed = false;
+
+void request_callback(HTTPResult r)
+{
+    result = r;
+    completed = true;
+}
+
+
+
+void  fetch ()
+{
+    string voiceurl;
+    string httpurl = "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=";
+    Serial pc(USBTX, USBRX);
+    pc.baud(9600);
+    char* title[10];
+    int counter=0;
+    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"};
+    char *tstartXML = "<title>"; //RSS XML start title
+    char *tendXML ="</title>"; //RSS XML end title
+    char *tsptr;
+    char *teptr;
+    int i=0,j=0;
+    // the eth and HTTP code has be taken directly from the HTTPStream documentation page
+    // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
+   
+    HTTPStream stream;
+    char BigBuf[1024 + 1] = {0};
+    stream.readNext((byte*)BigBuf, 1024); //Point to buffer for the first read
+    //CNN Tech News RSS Feed - get web page with XML
+    HTTPResult r = http.get("HTTP://rss.cnn.com/rss/cnn_tech.rss", &stream, request_callback);
+//fetch new and print
+    while (!completed) {
+        Net::poll(); // Polls the Networking stack
+        if (stream.readable()) { // check for end of file
+            BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
+            tsptr = BigBuf;
+            // displays titles on LCD  from XML "<title>....title text...</title>"
+            do {
+                tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
+                teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
+
+                if (tsptr!=NULL) {
+                    tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
+
+                    voiceurl.erase();
+                    voiceurl=tsptr;
+                    voiceurl.erase(voiceurl.begin()+(strlen(tsptr)-strlen(teptr)),voiceurl.end());
+
+                    printf("title: _____ %s\n",voiceurl);
+                }
+                if ((tsptr!=NULL)&&(teptr!=NULL)) {
+
+                    voiceurl=httpurl+voiceurl;
+                    for(i=0; (i<voiceurl.length()); i++) {
+                        if(voiceurl[i]==' '||voiceurl[i]=='\'') {
+                            voiceurl[i]='+';
+                        }
+                    }
+                    voiceurl=voiceurl+"!!";
+                    printf("%s\n",voiceurl.c_str());
+                    if(counter==10) {
+                        counter=0;
+                    }
+                    voiceurl=voiceurl.c_str();
+                    char temp[voiceurl.length()];
+
+                    for(i=0; (i<=voiceurl.length()); i++) {
+ 
+                        temp[i]=voiceurl[i];
+
+                    }
+
+
+                    FILE *fp = fopen(ct[counter], "w");
+                    if(fp == NULL) {
+                        error("Could not open file for write\n");
+                    }
+                    fprintf(fp, temp);
+                    fclose(fp);
+                    printf("stored: _____ %s\n\n",temp);
+                    printf("Goodbye World!\n");
+                    counter++;
+
+                    wait(.2);
+                }
+            } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
+            stream.readNext((byte*)BigBuf, 1024); //Buffer has been read, now we can put more data in it
+        }
+    }
+    lcd2.cls();
+    if (result == HTTP_OK) {
+        lcd2.cls();
+        lcd2.printf(" Read complete\n\r");
+    } else {
+        lcd2. printf(" Error %d\n", result);
+
+    }
+
+free (BigBuf);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fetch.h	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+#include "VS1002.h"
+#include "TextLCD.h"
+#include "RPG.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+#include "SDFileSystem.h"
+
+#include <string>
+#include <iostream>
+
+void fetch ();
\ No newline at end of file
--- a/main.cpp	Tue Oct 12 21:25:35 2010 +0000
+++ b/main.cpp	Wed Mar 06 00:36:15 2013 +0000
@@ -2,60 +2,43 @@
 #include "mbed.h"
 #include "VS1002.h"
 #include "TextLCD.h"
-
-TextLCD lcd2(p10, p18, p24, p23, p22, p21, TextLCD::LCD16x2 ); // rs, e, d0-d3
-
-
-/*
-
- Arduino MP3 Shield MP3Player_UI
- 2010/10/10
- This program comes from http://mbed.org/cookbook/VS1002-MP3-Decoder and is the work of xshige.
- (Very small modification done to fit latest libs)
-**more small modifications done to add a small user interface via LCD screen, rotary encoder and dipswitch,
-see http://mbed.org/cookbook/MP3-Player for more information on pinout for these devices, these UI mods by (mpetersen3 and kbuck3)
+#include "RPG.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+#include "SDFileSystem.h"
+#include "fetch.h"
 
- Pin Assigenment for Arduino MP3 Shield
- (VS1053 employed, issued by sparkfun.com)
-
- MP3 Sheild Side   |  mbed Side
- ---------------------------------------
-  D2(BSYNC)------------17
-  D3(DREQ)-------------16
-
-  D9(CS)---------------14
-
-  D11(MOSI)------------5
-  D12(MISO)------------6
-  D13(SCK)-------------7
+#include <string>
+#include <iostream>
+#include <sstream>
 
-  GND------------------GND(1)
-  5V-------------------VU(39)
-  RESET----------------15
-
- Volume will not used. (pin20 will not used)
-
-*/
-
+HTTPClient http1;
+HTTPResult result1;
+EthernetNetIf eth;
+RPG rpg1(p26,p27,p25);
+TextLCD lcd1(p10,p18,p24,p23,p22,p21,TextLCD::LCD16x2);
+VS1002 mp3(p11,p12,p13,p8,"sd", p5,p6,p7,p14,p15,p16,p17,p20);
+SDFileSystem sd(p11, p12, p13, p8, "sd");
+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"};
+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"};
 
-//PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name,
-//         PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst,
-//         PinName dreq, PinName dcs, PinName vol)
-VS1002 mp3(p11, p12, p13, p8, "sd",
-           p5, p6, p7, p14, p15,
-           p16, p17, p20);     //p14 in BoB non-functional so replace with p18
-AnalogIn rotary(p19);
-DigitalIn pause(p28);
+void download(string voiceurl, int a)
+{
+    HTTPFile f(mp3ct[a]);
+    HTTPResult r = http1.get(voiceurl.c_str(), &f);
+    if(r==HTTP_OK) {
+        printf("download Result OK\n");
+    } else {
+        printf("download Error %d\n", r);
+    }
+    wait(1.5);
+    HTTPFile clear(f);
+    voiceurl.erase();
 
-int main () {
-    // make debug port Fast
-    Serial pc(USBTX, USBRX);
-    pc.baud(9600);
+}
 
-
-    // pc.baud(115200);
-//  pc.baud(230400);
-
+int main ()
+{
     /*============================================================
      * MP3 Initialising
      *==========================================================*/
@@ -67,48 +50,103 @@
     mp3.sci_write(0x03, 0x9800);
     mp3.sdi_initialise();
 #endif
-    /*============================================================
-     * This is the good part
-     *==========================================================*/
-    int a=0;
+
+    // make debug port Fast
+    Serial pc(USBTX, USBRX);
+    pc.baud(9600);
+
+    lcd1.cls();
+    lcd1.printf("net setup");
+    EthernetErr ethErr = eth.setup();
+    if (ethErr) {
+        lcd1.printf("\n\r net error");
+
+    }
+    lcd1.printf("\n\r net ok");
+    wait(0.5);
+    lcd1.printf("streaming news");
+    fetch();
+    wait(1);
+    lcd1.cls();
+    int a=1;
     printf("%i\n",a);
-    while (true) {
-
-        
+    int count = 0;
+    int oldcount=0;
+    int dirt = 0;
 
-        
-        mp3.play_song(a);
-        a++;
-        wait(1);
-        
-        if (pause) {
-            while (pause) {
-                // shows you which song number you have chosen using the rotary dial
-                lcd2.printf("You have chosen the %i song\n",a);
-                if ((rotary>0)&&(rotary<=0.1)) {
-                    a=1;
+    FILE * pFile;
+    long lSize;
+    char * buffer;
+    size_t  result;
+
+    while (true) {
+        lcd1.printf(" news %d \n\n",a);
+        dirt = rpg1.dir(); //Get Dir
+        count = count + dirt; //Ad Dir to count
+        if (count>oldcount) {
+            oldcount=count;
+            a++;
+            lcd1.printf(" news %d \n\n",a);
+
+        } else if(count<oldcount) {
+            oldcount=count;
+            a--;
+            lcd1.printf(" news %d \n\n",a);
+        }
+
+        if (rpg1.pb()) {
+
+
+            lcd1.printf("You have chosen the news %d \n",a);
+
 
-                } else if ((rotary>0.1)&&(rotary<=0.2)) {
-                    a=2;
-                } else if ((rotary>0.2)&&(rotary<=0.3)) {
-                    a=3;
-                } else if ((rotary>0.3)&&(rotary<=0.4)) {
-                    a=4;
-                } else if ((rotary>0.4)&&(rotary<=0.5)) {
-                    a=5;
-                } else if ((rotary>0.5)&&(rotary<=0.6)) {
-                    a=6;
-                } else if ((rotary>0.6)&&(rotary<=0.7)) {
-                    a=7;
-                } else if ((rotary>0.7)&&(rotary<=0.8)) {
-                    a=8;
-                } else if ((rotary>0.8)&&(rotary<=0.9)) {
-                    a=9;
-                } else if ((rotary>0.9)&&(rotary<=1.0)) {
-                    a=10;
-                }
+            pFile = fopen ( ct[a], "rb" );
+            if (pFile==NULL) {
+                printf ("File error");
+                exit (1);
+            }
+
+            // obtain file size:
+            fseek (pFile , 0 , SEEK_END);
+            lSize = ftell (pFile);
+            rewind (pFile);
+
+            // allocate memory to contain the whole file:
+            buffer = (char*) malloc (sizeof(char)*lSize);
+            if (buffer == NULL) {
+                printf ("Memory error");
+                exit (2);
+            }
+
+            // copy the file into the buffer:
+            result = fread (buffer,1,lSize,pFile);
+            if (result != lSize) {
+                printf ("Reading error");
+                exit (3);
             }
-            }
+            string voiceurl=string(buffer);
+
+            /* the whole file is now loaded in the memory buffer. */
+
+            // terminate
+            fclose (pFile);
+            free (buffer);
+            lcd1.printf("You have chosen the news %d \n",a);
+            printf("%s\n",voiceurl.c_str());
+            download( voiceurl,a);
+            voiceurl.erase();
+            wait(1);
+            lcd1.cls();
+            mp3.play_song(a+10);
+
+            wait(1);
+
         }
-        printf("Done.\r\n");
+        wait(0.2);
+        printf("pb: %i  ", rpg1.pb()); //Print out Count
+
+        printf("count:%d  dirt:%d\n",count,dirt);
     }
+    printf("Done.\r\n");
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spxml.lib	Wed Mar 06 00:36:15 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hlipka/code/spxml/#3fa97f2c0505