Control an LCD connected to your mbed with a Twitter account.

Dependencies:   EthernetInterface HTTPClientAuthAndPathExtension NokiaLCDScreenErrorFixed mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "HTTPClient.h"
00005 #include "NokiaLCD.h"
00006 #include <string>
00007 
00008 EthernetInterface eth;
00009 HTTPClient client;
00010 DigitalOut indicatorLED(LED1);
00011 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
00012 char *str;
00013 char url[1024];
00014 
00015 int main() {
00016     string text;
00017     int background = 0x0000FF;
00018     int squareColor = 0x00FF00;
00019     int squareLength = 0;
00020     
00021     string previousID;
00022     
00023     eth.init();
00024     eth.connect();
00025     
00026     str = (char *) malloc(8192);
00027     
00028     
00029     while(1) {
00030         int ret;
00031         if(previousID.empty()) {
00032             ret = client.get("http://api.supertweet.net/1.1/statuses/mentions_timeline.json?count=1", str, 4096);
00033         } else {
00034             sprintf(url, "http://api.supertweet.net/1.1/statuses/mentions_timeline.json?since_id=%s\0", previousID.c_str());
00035             ret = client.get(url, str, 4096);
00036         }
00037         if (ret) {
00038           printf("Error - ret = %d - HTTP return code = %d\n", ret, client.getHTTPResponseCode());
00039           return 1;
00040         }
00041         
00042         string tweetInfo(str);
00043         string tweetCommand;
00044         string tweetSetting;
00045         size_t foundStart = string::npos;
00046         if(tweetInfo.compare("[]")) {
00047             foundStart = tweetInfo.rfind("\"text\":\"@");
00048         }
00049         size_t foundEnd;
00050         while(foundStart != string::npos) {
00051             foundStart += 24;
00052             foundEnd = tweetInfo.find(" ",foundStart);
00053             if(foundEnd != string::npos) {
00054                 tweetCommand = tweetInfo.substr(foundStart, foundEnd - foundStart);
00055             } else {
00056                 continue;
00057             }
00058             foundStart = foundEnd + 1;
00059             foundEnd = tweetInfo.find("\"",foundStart);
00060             tweetSetting = tweetInfo.substr(foundStart, foundEnd - foundStart);
00061             
00062             printf("%s*%s*\n", tweetCommand.c_str(), tweetSetting.c_str());
00063             
00064             if(!tweetCommand.compare("background")) {
00065                 if(!tweetSetting.compare("blue")) {
00066                     background = 0x0000FF;
00067                     squareColor = 0x00FF00;
00068                 } else if(!tweetSetting.compare("green")) {
00069                     background = 0x00FF00;
00070                     squareColor = 0xFF0000;
00071                 } else if(!tweetSetting.compare("red")) {
00072                     background = 0xFF0000;
00073                     squareColor = 0x0000FF;
00074                 } else {
00075                     text = "Invalid tweet setting";
00076                 }
00077             } else if(!tweetCommand.compare("write")) {
00078                 text = tweetSetting;
00079             } else if(!tweetCommand.compare("square")) {
00080                 squareLength = atoi(tweetSetting.c_str());
00081             } else {
00082                 text = "Invalid tweet command";
00083             }
00084             
00085             foundStart -= 60;
00086             foundStart = tweetInfo.rfind("\"text\":\"@", foundStart);
00087             printf("%d*%d*\n", foundStart, string::npos);
00088         }
00089         
00090         lcd.cls();
00091         lcd.fill(0, 0, 130, 130, background);
00092         lcd.background(background);
00093         lcd.locate(0, 7);
00094         lcd.printf(text.c_str());
00095         if(squareLength > 0) {
00096             int i;
00097             for(int i=65-(squareLength/2); i<65+(squareLength/2);i++) {
00098                 lcd.pixel(i, 65-(squareLength/2), squareColor);
00099                 lcd.pixel(i, 65+(squareLength/2), squareColor);
00100             }
00101             for(i=65-(squareLength/2); i<65+(squareLength/2);i++) {
00102                 lcd.pixel(65-(squareLength/2), i, squareColor);
00103                 lcd.pixel(65+(squareLength/2), i, squareColor);
00104             }
00105         }
00106         
00107         if(tweetInfo.compare("[]")) {
00108             foundStart = tweetInfo.find("\"id_str\":\"");
00109             foundStart += 10;
00110             foundEnd = tweetInfo.find("\"", foundStart);
00111             previousID = tweetInfo.substr(foundStart, foundEnd - foundStart);
00112             printf("%s\n", previousID.c_str());
00113         }
00114         
00115         wait(30);
00116     }
00117     
00118 }