Dependencies:   EthernetNetIf TextLCD mbed HTTPClient

Files at this revision

API Documentation at this revision

Comitter:
blmarket
Date:
Wed Jun 01 00:49:54 2011 +0000
Commit message:

Changed in this revision

CSVParser.cpp Show annotated file Show diff for this revision Revisions of this file
CSVParser.h Show annotated file Show diff for this revision Revisions of this file
EthernetNetIf.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
TextLCD.lib 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d73b14dd8351 CSVParser.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CSVParser.cpp	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,25 @@
+#include "CSVParser.h"
+
+using namespace std;
+
+std::vector<std::pair<std::string,std::string> > CSVParser::fields; // field id, description pair vector.
+
+StockData* CSVParser::parseResult(const char *indata)
+{
+    StockData *ret = new StockData;
+    
+    memcpy(ret->buffer, indata, 256);
+    ret->buffer[255] = 0;
+    
+    char *ptr = strtok(ret->buffer, ",");
+    for(int i=0;i<fields.size();i++)
+    {
+        if(ptr == NULL) break;
+        
+        printf("%s : %s\n", fields[i].second.c_str(), ptr);
+        ret->field[i] = ptr;
+        ptr = strtok(NULL, ",");    
+    }
+    
+    return ret;
+}
\ No newline at end of file
diff -r 000000000000 -r d73b14dd8351 CSVParser.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CSVParser.h	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+#include <string>
+#include <vector>
+
+struct StockData;
+
+class CSVParser
+{
+public:
+    static std::vector<std::pair<std::string,std::string> > fields; // field id, description pair vector.
+    static StockData* parseResult(const char *data);    
+    
+    static const int nFields = 7;
+};
+
+struct StockData
+{
+private:
+    char buffer[256];
+    
+public:
+    char *field[CSVParser::nFields];
+    
+    friend class CSVParser;
+};
+
+
+
diff -r 000000000000 -r d73b14dd8351 EthernetNetIf.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mamezu/code/EthernetNetIf/#0f6c82fcde82
diff -r 000000000000 -r d73b14dd8351 HTTPClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mamezu/code/HTTPClient/#62fac7f06c8d
diff -r 000000000000 -r d73b14dd8351 TextLCD.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/dreschpe/code/TextLCD/#a7c74d4c6911
diff -r 000000000000 -r d73b14dd8351 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,262 @@
+#include "mbed.h"
+#include "CSVParser.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+#include "TextLCD.h"
+#include <vector>
+#include <functional>
+
+#define ARRAYSIZE(arr) (int)(sizeof(arr) / sizeof(arr[0]))
+#define min(a,b) (((a)<(b))?(a):(b))
+
+typedef void (*menuftn)(void *);
+
+using namespace std;
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX);
+EthernetNetIf eth;
+/*
+(
+  IpAddr(110,76,72,244), //IP Address
+  IpAddr(255,255,255,0), //Network Mask
+  IpAddr(110,76,72,1), //Gateway
+  IpAddr(143,248,1,177)  //DNS
+);
+*/
+HTTPClient http;
+TextLCD lcd(p21, p23, p22, p27, p28, p29, p30); // rs, rw, e, d0-d3
+
+vector<StockData *> stocks;
+
+void donothing(void *) {}
+
+void fetchStockData(void *)
+{
+    lcd.cls();
+    lcd.printf("Fetching...");
+    stocks.clear();
+    
+    HTTPText txt;
+    HTTPResult r2 = http.get("http://download.finance.yahoo.com/d/quotes.csv?f=snl1d1c1p2&e=.csv&s=000660.KS,067250.KS", &txt);
+    if(r2 == HTTP_OK)
+    {
+        printf("HTTP_OK\n");
+                
+        char data[1024];
+        int len = strlen(txt.gets());
+        
+        if(len >= 1024)
+        {
+            pc.printf("Result too Long!\n");
+            return;
+        }        
+        
+        strcpy(data, txt.gets());
+        int prevpos = 0;
+        for(int i=0;i<len;i++)
+        {
+            if(data[i] == 13 && data[i+1] == 10)
+            {
+                data[i]=0;
+                stocks.push_back(CSVParser::parseResult(data + prevpos)); // needs to be freed using delete
+                prevpos = i+2;
+            }
+        }
+    }
+    else
+    {
+        pc.printf("Error %d\n", r2);
+    }
+}
+
+void clearStocks() { for(int i=0;i<stocks.size();i++) delete stocks[i]; stocks.clear(); }
+
+void showMenu(int menucnt, const char *menus[], int cursor, int screenpos)
+{
+    if(menucnt == 0) return;
+    lcd.cls();
+    lcd.printf("%s\n", menus[screenpos]);
+    lcd.printf("%s\n", menus[(screenpos+1)%menucnt]);
+    lcd.locate(15, cursor==screenpos?0:1);
+    lcd.putc(0);
+}
+
+void showStock(StockData *data)
+{
+    char menu[20][50];
+    char *menuptr[20];
+    
+    int menucnt = min(CSVParser::fields.size(),20);
+    
+    for(int i=0;i<menucnt;i++)
+    {
+        sprintf(menu[i], "%s : %s", CSVParser::fields[i].second.c_str(), data->field[i]);
+        menuptr[i] = menu[i];
+    }
+    
+    int cursor = 0;    
+    while(true)
+    {
+        lcd.cls();
+        lcd.printf("%s\n", menuptr[cursor]);
+        char ch = getchar();
+        switch(ch)
+        {
+            case 'i':
+                return;
+            case 'j':
+                cursor = (cursor + 1) % menucnt;
+                break;
+            case 'k':
+                cursor = (cursor - 1 + menucnt) % menucnt;
+                break;
+        }
+    }    
+    return;
+}
+
+void listStocks(void *)
+{
+    const char *menu[20];
+    int menucnt = stocks.size();
+    for(int i=0;i<menucnt;i++)
+        menu[i] = stocks[i]->field[1];
+        
+    if(menucnt == 0) return;
+        
+    int cursor = 0, screenpos = 0;
+    while(true)
+    {
+        showMenu(menucnt, menu, cursor, screenpos);
+        char ch = getchar();
+        switch(ch)
+        {
+            case 'i':
+                return;
+            case 'j':
+                if(screenpos != cursor)
+                {
+                    screenpos = (screenpos + 1) % menucnt;
+                }
+                cursor = (cursor + 1) % menucnt;
+                break;
+            case 'k':
+                if(screenpos == cursor)
+                {
+                    cursor = (cursor - 1 + menucnt) % menucnt;
+                    screenpos = cursor;
+                }
+                else
+                {
+                    cursor = (cursor - 1 + menucnt) % menucnt;
+                }
+                break;
+            case 32: // space
+                showStock(stocks[cursor]);
+                break;            
+        }
+    }
+}
+
+void startMenu(void *)
+{
+    int pattern[8];
+    pattern[0] = 0x1;
+    pattern[1] = 0x3;
+    pattern[2] = 0x7;
+    pattern[3] = 0xf;
+    pattern[4] = 0xf;
+    pattern[5] = 0x7;
+    pattern[6] = 0x3;
+    pattern[7] = 0x1;
+  
+    lcd.writeCGRAM(0, pattern);
+  
+    //lcd.locate(15,0);
+    //lcd.putc(0);   // user pattern 0
+    
+    const char *menu[] = { "Get Data", "List Data", "Do Some", "Menu Test" };    
+    menuftn ftn[] = { fetchStockData, listStocks, donothing, donothing };
+    int menucnt = 4;    
+    
+    int cursor = 0, screenpos = 0;
+
+    while(true)
+    {
+        showMenu(menucnt, menu, cursor, screenpos);    
+
+        char ch = getchar();
+        switch(ch)
+        {
+            case 'j':
+                if(screenpos != cursor)
+                {
+                    screenpos = (screenpos + 1) % menucnt;
+                }
+                cursor = (cursor + 1) % menucnt;
+                break;
+            case 'k':
+                if(screenpos == cursor)
+                {
+                    cursor = (cursor - 1 + menucnt) % menucnt;
+                    screenpos = cursor;
+                }
+                else
+                {
+                    cursor = (cursor - 1 + menucnt) % menucnt;
+                }
+                break;
+            case 32: // space
+                ftn[cursor](NULL);
+                break;            
+        }
+    }    
+}
+
+int main() 
+{
+    lcd.cls();
+    /* setting fields information of Yahoo! finance */
+    char *fields[] = { "s", "n", "l1", "d1", "c1", "p2" };
+    char *desc[] = { "Symbol", "Name", "Last Trade", "Last Trade Date", "Change", "Change in Percent" };   
+    
+    lcd.printf("Init...");
+
+    pc.baud(9600);
+    EthernetErr ethErr = eth.setup();
+    if(ethErr)
+    {
+        pc.printf("Error %d in setup.\n", ethErr);
+        return -1;
+    }
+    pc.printf("\r\nSetup OK\r\n");
+    
+    pc.printf("CSVParser Fields Input\r\n");
+    
+    CSVParser::fields.clear();
+
+    for(int i=0;i<ARRAYSIZE(fields);i++)
+    {
+        pc.printf("pushing %s %s\n", fields[i], desc[i]);
+        CSVParser::fields.push_back(make_pair(fields[i], desc[i]));
+    }
+    
+    pc.printf("CSVParser Setup OK\r\n");    
+    
+    lcd.cls();
+    
+    startMenu(NULL);
+    
+/*
+    for(int i=0;i<min(2, stocks.size());i++)
+    {
+        StockData *data = stocks[stocks.size()-1-i];
+        lcd.printf("%s\n",data->field[1]);
+    }
+*/    
+
+    clearStocks();
+    
+    while(1) {}
+}
diff -r 000000000000 -r d73b14dd8351 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jun 01 00:49:54 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912