News_Reader
Dependencies: FatFileSystem HTTPClient NetServices RPG SDFileSystem TextLCD mbed spxml
Fork of Arduino_MP3_Shield_MP3Player_UI by
Diff: main.cpp
- Revision:
- 1:0014879fa94f
- Parent:
- 0:9cb0b3d1b9e6
--- 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"); +} +