Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/
Dependencies: BlinkLed HTTPClient EthernetInterface FatFileSystemCpp MSCFileSystem mbed-rtos mbed
Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/
Revision 4:ab3092d15121, committed 2012-09-01
- Comitter:
- togayan
- Date:
- Sat Sep 01 04:12:37 2012 +0000
- Parent:
- 3:07562878d3c3
- Commit message:
- HTTPFile was changed to follow the latest HTTPClient library.; BlinkLed was isolated as a library.; Change LED port from LED1,2 to LED3,4 for the specification of pwmout.
Changed in this revision
--- a/BlinkLed.cpp Sun Aug 19 15:57:55 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -#include "BlinkLed.h" - -BlinkLed::BlinkLed(PinName pin, float dutyChangeStep, const char* name) : -led(pin, name), -dutyChangeStep(dutyChangeStep), -thread(0) -{ -} - -BlinkLed::~BlinkLed() -{ -} - -void BlinkLed::startBlink() -{ - if(thread == 0) - { - thread = new Thread(blink, this, osPriorityNormal, 128, NULL); - } -} - -void BlinkLed::finishBlink() -{ - if(thread != 0) - { - thread->terminate(); - delete thread; - thread = 0; - led = 0.0; - } -} - -void BlinkLed::blink(void const *argument) -{ - BlinkLed* blinkLed = (BlinkLed*)argument; - - int up = 1; - float brightness = 0.0; - while (1) { - if (up == 1 && brightness < 1.0) { - ; - } else if (up == 1 && brightness >= 1.0) { - up = 0; - } else if (up == 0 && brightness > 0) { - ; - } else if (up == 0 && brightness <= 0.0) { - up = 1; - } else { - error("LED PWM error\n"); - } - - float dutyChangeStep = blinkLed->dutyChangeStep; - if (up == 1) { - brightness += dutyChangeStep; - } else { - brightness -= dutyChangeStep; - } - blinkLed->led = brightness; - - Thread::wait(20); - } -}
--- a/BlinkLed.h Sun Aug 19 15:57:55 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* BlinkLed.h */ -#ifndef BLINKLED_H_ -#define BLINKLED_H_ - -#include "mbed.h" -#include "rtos.h" - -/** LED which blinks automatically with RTOS -*/ -class BlinkLed -{ -public: - /** Constructor - */ - BlinkLed(PinName pin, float dutyChangeStep, const char* name = NULL); - - /** Destructor - */ - ~BlinkLed(); - - /** Start biinking - */ - void startBlink(); - - /** Finish biinking - */ - void finishBlink(); - -private: - /** Copy constructor - * Disable because it is only declaration - */ - BlinkLed(const BlinkLed&); - - /** Copy assignment operators - * Disable because it is only declaration - */ - BlinkLed& operator=(const BlinkLed&); - - /** Function for blinking - * This function will be bind to new thread - */ - static void blink(void const *argument); - - /** Target Led - */ - PwmOut led; - - /** Duty ratio step of changing every 20ms - */ - float dutyChangeStep; - - /** Pointer to thread for blinking - */ - Thread* thread; -}; - - -#endif /* BLINKLED_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BlinkLed.lib Sat Sep 01 04:12:37 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/togayan/code/BlinkLed/#a55a3351317d
--- a/HTTPClient.lib Sun Aug 19 15:57:55 2012 +0000 +++ b/HTTPClient.lib Sat Sep 01 04:12:37 2012 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/donatien/code/HTTPClient/#be61104f4e91 +http://mbed.org/users/donatien/code/HTTPClient/#1f743885e7de
--- a/HTTPFile.cpp Sun Aug 19 15:57:55 2012 +0000 +++ b/HTTPFile.cpp Sat Sep 01 04:12:37 2012 +0000 @@ -1,120 +1,132 @@ -/* HTTPFile.cpp */ -#include "HTTPFile.h" - -#include <cstring> - -#define OK 0 -#define NG -1 - -using std::memcpy; -using std::strncpy; -using std::strlen; - -HTTPFile::HTTPFile(const char* path) : - m_fp(NULL), - m_path(path), - m_len(0), - m_chunked(false) -{ -} - -HTTPFile::~HTTPFile() -{ - closeFile(); -} - -void HTTPFile::clear() -{ - closeFile(); - //Force reopening -} - -/*virtual*/ int HTTPFile::read(char* buf, size_t len, size_t* pReadLen) -{ - if(!openFile("r")) //File does not exist, or I/O error... - return NG; - - *pReadLen = fread(buf, 1, len, m_fp); - if( feof(m_fp) ) - { - //File read completely, we can close it - closeFile(); - } - return OK; -} - -/*virtual*/ int HTTPFile::write(const char* buf, size_t len) -{ - if(!openFile("w")) //File does not exist, or I/O error... - return NG; - - len = fwrite(buf, 1, len, m_fp); - //DBG("Written %d bytes in %d\n", len, m_fp); - if( (!m_chunked && (ftell(m_fp) >= m_len)) || (m_chunked && !len) ) - { - //File received completely, we can close it - closeFile(); - } - return len; -} - -/*virtual*/ int HTTPFile::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header -{ - strncpy(type, "text/plain", maxTypeLen-1); - type[maxTypeLen-1] = '\0'; - return OK; -} - -/*virtual*/ void HTTPFile::setDataType(const char* type) //Internet media type from Content-Type header -{ - //Do not really care here -} - -/*virtual*/ bool HTTPFile::getIsChunked() //For Transfer-Encoding header -{ - return false; -} - -/*virtual*/ void HTTPFile::setIsChunked(bool chunked) //For Transfer-Encoding header -{ - m_chunked = chunked; -} - -/*virtual*/ size_t HTTPFile::getDataLen() //For Content-Length header -{ - return m_len; -} - -/*virtual*/ void HTTPFile::setDataLen(size_t len) //For Content-Length header, or if the transfer is chunked, next chunk length -{ - if(!m_chunked) - m_len = len; //Useful so that we can close file when last byte is written -} - -bool HTTPFile::openFile(const char* mode) //true on success, false otherwise -{ - if(m_fp) - return true; - - m_fp = fopen(m_path.c_str(), mode); - if(m_fp && mode[0]=='r') - { - //Seek EOF to get length - fseek(m_fp, 0, SEEK_END); - m_len = ftell(m_fp); - fseek(m_fp, 0, SEEK_SET); //Goto SOF - } - - //DBG("fd = %d\n", m_fp); - - if(!m_fp) - return false; - - return true; -} - -void HTTPFile::closeFile() -{ - if(m_fp) - fclose(m_fp); -} +/* HTTPFile.cpp */ +#include "HTTPFile.h" + +#include <cstring> + +#define OK 0 +#define NG -1 + +using std::memcpy; +using std::strncpy; +using std::strlen; + +HTTPFile::HTTPFile(const char* path) : + m_fp(NULL), + m_path(path), + m_len(0), + m_chunked(false) +{ +} + +HTTPFile::~HTTPFile() +{ + closeFile(); +} + +void HTTPFile::clear() +{ + closeFile(); + //Force reopening +} + +/*virtual*/ void HTTPFile::readReset() +{ + if(m_fp) + fseek(m_fp, 0, SEEK_SET); +} + +/*virtual*/ int HTTPFile::read(char* buf, size_t len, size_t* pReadLen) +{ + if(!openFile("r")) //File does not exist, or I/O error... + return NG; + + *pReadLen = fread(buf, 1, len, m_fp); + if( feof(m_fp) ) + { + //File read completely, we can close it + closeFile(); + } + return OK; +} + +/*virtual*/ void HTTPFile::writeReset() +{ + if(m_fp) + fseek(m_fp, 0, SEEK_SET); +} + +/*virtual*/ int HTTPFile::write(const char* buf, size_t len) +{ + if(!openFile("w")) //File does not exist, or I/O error... + return NG; + + len = fwrite(buf, 1, len, m_fp); + //DBG("Written %d bytes in %d\n", len, m_fp); + if( (!m_chunked && (ftell(m_fp) >= m_len)) || (m_chunked && !len) ) + { + //File received completely, we can close it + closeFile(); + } + return len; +} + +/*virtual*/ int HTTPFile::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header +{ + strncpy(type, "text/plain", maxTypeLen-1); + type[maxTypeLen-1] = '\0'; + return OK; +} + +/*virtual*/ void HTTPFile::setDataType(const char* type) //Internet media type from Content-Type header +{ + //Do not really care here +} + +/*virtual*/ bool HTTPFile::getIsChunked() //For Transfer-Encoding header +{ + return false; +} + +/*virtual*/ void HTTPFile::setIsChunked(bool chunked) //For Transfer-Encoding header +{ + m_chunked = chunked; +} + +/*virtual*/ size_t HTTPFile::getDataLen() //For Content-Length header +{ + return m_len; +} + +/*virtual*/ void HTTPFile::setDataLen(size_t len) //For Content-Length header, or if the transfer is chunked, next chunk length +{ + if(!m_chunked) + m_len = len; //Useful so that we can close file when last byte is written +} + +bool HTTPFile::openFile(const char* mode) //true on success, false otherwise +{ + if(m_fp) + return true; + + m_fp = fopen(m_path.c_str(), mode); + if(m_fp && mode[0]=='r') + { + //Seek EOF to get length + fseek(m_fp, 0, SEEK_END); + m_len = ftell(m_fp); + fseek(m_fp, 0, SEEK_SET); //Goto SOF + } + + //DBG("fd = %d\n", m_fp); + + if(!m_fp) + return false; + + return true; +} + +void HTTPFile::closeFile() +{ + if(m_fp) + fclose(m_fp); +}
--- a/HTTPFile.h Sun Aug 19 15:57:55 2012 +0000 +++ b/HTTPFile.h Sat Sep 01 04:12:37 2012 +0000 @@ -25,6 +25,11 @@ void clear(); protected: + /** Reset stream to its beginning + * Called by the HTTPClient on each new request + */ + virtual void readReset(); + /** Read a piece of data to be transmitted * @param buf Pointer to the buffer on which to copy the data * @param len Length of the buffer @@ -32,6 +37,11 @@ */ virtual int read(char* buf, size_t len, size_t* pReadLen); + /** Reset stream to its beginning + * Called by the HTTPClient on each new request + */ + virtual void writeReset(); + /** Write a piece of data transmitted by the server * @param buf Pointer to the buffer from which to copy the data * @param len Length of the buffer
--- a/main.cpp Sun Aug 19 15:57:55 2012 +0000 +++ b/main.cpp Sat Sep 01 04:12:37 2012 +0000 @@ -13,12 +13,11 @@ EthernetInterface eth; HTTPClient http; MSCFileSystem usb("usb"); -BlinkLed led1(LED1, 0.02); -BlinkLed led2(LED2, 0.2); +BlinkLed led3(LED3, 0.02); +BlinkLed led4(LED4, 0.2); BlinkLed ethGreen(p26, 0.02); BlinkLed ethYellow(p25, 0.2); DigitalOut fsusb30s(p9); -Timer timer; const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml"; const char* rssPath = "/usb/english.xml"; @@ -30,6 +29,10 @@ printf("mpod NHK English news Downloader\n"); printf("================================\n\n"); + // Indicate downloading + led4.startBlink(); + ethYellow.startBlink(); + // FSUSB30 switches to HSD1 (mbed) printf("USB host was switched to HSD1(mbed).\n\n"); fsusb30s = 0; // HSD1 @@ -137,8 +140,10 @@ printf("\nUSB host was switched to HSD2(External Device).\n"); fsusb30s = 1; // HSD2 - // blink LED - led1.startBlink(); + // Indicate finish downloading + led4.finishBlink(); + ethYellow.finishBlink(); + led3.startBlink(); ethGreen.startBlink(); while(true){} @@ -146,13 +151,7 @@ int GetFile(const char *path, const char *url) { - led2.startBlink(); - ethYellow.startBlink(); - printf("\nGetting %s\n", url); - - timer.stop(); - timer.reset(); - timer.start(); + printf("Getting %s -> %s\n", url, path); HTTPFile file(path); HTTPResult retGet = http.get(url, &file); @@ -163,10 +162,5 @@ } file.clear(); - timer.stop(); - printf("timer.read_ms(): %d\n", timer.read_ms()); - - led2.finishBlink(); - ethYellow.finishBlink(); return (0); }