display your latest tweet with WiFly RN-XV and AD128160-UART
Dependencies: AD128160_kazushi_branch HTTPClient mbed PowerControl WiflyInterface
Fork of FontTest3 by
Please see at http://kazushi-lab.c.fun.ac.jp/pukiwiki/index.php?TwitterMbed
main.cpp
- Committer:
- kazushi2008
- Date:
- 2012-12-27
- Revision:
- 1:6b495e0f3b0c
- Parent:
- 0:304c871df0a8
- Child:
- 2:56a2ab00e743
File content as of revision 1:6b495e0f3b0c:
// Twitter Mbed with Gain Span WiFi // // coded by Kazushi Mukaiyama (http://www.kazushi.info/) // refer to gs fan's notebook (http://mbed.org/users/gsfan/notebook/gainspan_wifi/) // refer to Sim's notebook (http://mbed.org/users/Sim/code/FontTest3/) #include "mbed.h" #include "GSwifi.h" #include "spilcd.h" #include "kfont8.h" #include "sjis_utf16.h" #define countof(x) ( sizeof(x) / sizeof(x[0]) ) #define SECURE GSSEC_WPA_PSK #define SSID "mima-lab" #define PASS "ikaika5656" #define HTTP_HOST "api.supertweet.net" // SuperTweet.net //#define HTTP_URI "/1.1/statuses/show.json?id=281639966776369152" #define HTTP_URI "/1.1/statuses/user_timeline.json?screen_name=pla3c&count=1" #define HTTP_USER "pla3c" #define HTTP_PASS "pla3c" #define INTERVAL_SEC 60.0 //#define _DEBUG SPILCD lcd(p21, p22, p23, p5, p6, p7); GSwifi gs(p13, p14); // TX, RX (no flow control) // GSwifi gs(p13, p14, p12, P0_22); // TX, RX, CTS, RTS DigitalOut led1(LED1), led2(LED2); #ifdef _DEBUG Serial pc(USBTX, USBRX); #endif int csrx = 0; int csry = 0; int offsety = 0; unsigned char kstate = 0; bool estate = false; unsigned char kbuf[4]; void locate(int x, int y){ csrx = x; csry = y; if(csrx < 0) csrx = 0; if(csry < 0) csry = 0; csrx &= 31; csry &= 7; lcd.moveto(csrx << 3, (offsety + csry) & 7); }; void scroll(void){ int i; offsety = (offsety + 1) & 7; lcd.cmd(0x40 | offsety << 3); lcd.moveto(0, (offsety + 7) & 7); for(i = 0; i < 128; i++) lcd.write(0); } void newline(void){ csrx = 0; if(++csry == 8){ scroll(); csry = 7; } locate(csrx, csry); } // draw 4x8 font void drawfont4(unsigned char c){ const unsigned char *p = &font4[c << 2]; lcd.write(p[0]); lcd.write(p[1]); lcd.write(p[2]); lcd.write(p[3]); // cursor control if(++csrx == 32) newline(); } const unsigned char *findface(unsigned short c){ const unsigned char *p = NULL; int i, sum; for(sum = i = 0; i < countof(font8table); i++){ if(font8table[i].start <= c && c <= font8table[i].end){ p = &font8[(sum + c - font8table[i].start) << 3]; break; } sum += font8table[i].end - font8table[i].start + 1; } return p; } // draw 8x8 font void drawkanji(unsigned short c){ const unsigned char *p = findface(c); if(p == NULL) return; if(csrx >= 31) newline(); lcd.write(p[0]); lcd.write(p[1]); lcd.write(p[2]); lcd.write(p[3]); lcd.write(p[4]); lcd.write(p[5]); lcd.write(p[6]); lcd.write(p[7]); csrx += 2; if(csrx == 32) newline(); } unsigned short convert(unsigned short utf){ unsigned short sjis = NULL; for(int i=0; i<countof(sjis2utf16)/2; i++){ if(utf==sjis2utf16[i*2+1]){ sjis = sjis2utf16[i*2]; break; } } return sjis; } void drawc(unsigned char c){ if(estate && kstate==4){ // 1st digit of utf-16 kbuf[3] = c; char *endptr; unsigned short code = convert(strtol((char*)kbuf, &endptr, 16)); drawkanji(code); #ifdef _DEBUG pc.printf("%s, %x\r\n", kbuf, code); #endif kstate = 0; estate = false; } else if(estate && kstate==3){ // 2nd digit of utf-16 kbuf[2] = c; kstate = 4; } else if(estate && kstate==2){ // 3rd digit of utf-16 kbuf[1] = c; kstate = 3; } else if(estate && kstate==1){ // 4th digit of utf-16 kbuf[0] = c; kstate = 2; } else if(estate && c=='u'){ // escape code kstate = 1; } else if(estate && !c=='u'){ // escape code drawfont4(c); kstate = 0; estate = false; } else if(c=='\\'){ // escape code estate = true; } else { // 4x8font drawfont4(c); } } void draws(const unsigned char *s){ unsigned char c; while((c = *s++) != '\0') drawc(c); } void onGsReceive (int cid, int len) { int i; char buf[GS_DATA_SIZE + 1]; led2 = 1; i = gs.recv(cid, buf, len); buf[i] = 0; // parse buffer char* c = strtok(buf, "\r\n"); if(strcmp(c, "200 OK")==0){ while(c!=NULL){ c = strtok(NULL, "\""); char key[4]; sprintf(key, "%.4s", c); if(strcmp(key, "text")==0){ c = strtok(NULL, "\""); c = strtok(NULL, "\""); #ifdef _DEBUG pc.printf("%s\r\n", c); #endif lcd.cls(); locate(0,0); c = strtok(c, "@"); draws((unsigned char*)c); newline(); c = strtok(NULL, "@"); draws((unsigned char*)c); newline(); break; } } } led2 = 0; } int main() { int r; IpAddr ipaddr, netmask, gateway, nameserver; Host host; led1 = 1; #ifdef _DEBUG pc.baud(115200); #endif char* msg; msg = "connect"; draws((unsigned char*)msg); newline(); if (gs.connect(SECURE, SSID, PASS, 0)) { return -1; } gs.getAddress(ipaddr, netmask, gateway, nameserver); if(nameserver.isNull()) { nameserver = IpAddr(192,168,1,1); r = gs.setAddress(ipaddr, netmask, gateway, nameserver); gs.getAddress(ipaddr, netmask, gateway, nameserver); } char localip[20]; sprintf(localip, "ip %d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); draws ((unsigned char*)localip); newline(); sprintf(localip, "gw %d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]); draws ((unsigned char*)localip); newline(); sprintf(localip, "ns %d.%d.%d.%d", nameserver[0], nameserver[1], nameserver[2], nameserver[3]); draws ((unsigned char*)localip); newline(); msg = "start twitter"; draws((unsigned char*)msg); newline(); host.setName(HTTP_HOST); //r = gs.httpGet(host, HTTP_URI, HTTP_USER, HTTP_PASS, 0, &onGsReceive); for (;;) { led1 = !led1; wait_ms(60); led1 = !led1; // blink gs.poll(); r = gs.httpGet(host, HTTP_URI, HTTP_USER, HTTP_PASS, 0, &onGsReceive); //if (!gs.isConnected(r)) break; wait(INTERVAL_SEC); } //msg = "exit"; //lcd.cls(); locate(0,0); //draws((unsigned char*)msg); }