Control an LCD connected to your mbed with a Twitter account.
Dependencies: EthernetInterface HTTPClientAuthAndPathExtension NokiaLCDScreenErrorFixed mbed-rtos mbed
main.cpp@0:b3d9fc5861c6, 2012-10-12 (annotated)
- Committer:
- jlind6
- Date:
- Fri Oct 12 16:49:30 2012 +0000
- Revision:
- 0:b3d9fc5861c6
Initial commit. Can be used with specific Twitter accounts.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jlind6 | 0:b3d9fc5861c6 | 1 | |
jlind6 | 0:b3d9fc5861c6 | 2 | #include "mbed.h" |
jlind6 | 0:b3d9fc5861c6 | 3 | #include "EthernetInterface.h" |
jlind6 | 0:b3d9fc5861c6 | 4 | #include "HTTPClient.h" |
jlind6 | 0:b3d9fc5861c6 | 5 | #include "NokiaLCD.h" |
jlind6 | 0:b3d9fc5861c6 | 6 | #include <string> |
jlind6 | 0:b3d9fc5861c6 | 7 | |
jlind6 | 0:b3d9fc5861c6 | 8 | EthernetInterface eth; |
jlind6 | 0:b3d9fc5861c6 | 9 | HTTPClient client; |
jlind6 | 0:b3d9fc5861c6 | 10 | DigitalOut indicatorLED(LED1); |
jlind6 | 0:b3d9fc5861c6 | 11 | NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
jlind6 | 0:b3d9fc5861c6 | 12 | char *str; |
jlind6 | 0:b3d9fc5861c6 | 13 | char url[1024]; |
jlind6 | 0:b3d9fc5861c6 | 14 | |
jlind6 | 0:b3d9fc5861c6 | 15 | int main() { |
jlind6 | 0:b3d9fc5861c6 | 16 | string text; |
jlind6 | 0:b3d9fc5861c6 | 17 | int background = 0x0000FF; |
jlind6 | 0:b3d9fc5861c6 | 18 | int squareColor = 0x00FF00; |
jlind6 | 0:b3d9fc5861c6 | 19 | int squareLength = 0; |
jlind6 | 0:b3d9fc5861c6 | 20 | |
jlind6 | 0:b3d9fc5861c6 | 21 | string previousID; |
jlind6 | 0:b3d9fc5861c6 | 22 | |
jlind6 | 0:b3d9fc5861c6 | 23 | eth.init(); |
jlind6 | 0:b3d9fc5861c6 | 24 | eth.connect(); |
jlind6 | 0:b3d9fc5861c6 | 25 | |
jlind6 | 0:b3d9fc5861c6 | 26 | str = (char *) malloc(8192); |
jlind6 | 0:b3d9fc5861c6 | 27 | |
jlind6 | 0:b3d9fc5861c6 | 28 | |
jlind6 | 0:b3d9fc5861c6 | 29 | while(1) { |
jlind6 | 0:b3d9fc5861c6 | 30 | int ret; |
jlind6 | 0:b3d9fc5861c6 | 31 | if(previousID.empty()) { |
jlind6 | 0:b3d9fc5861c6 | 32 | ret = client.get("http://api.supertweet.net/1.1/statuses/mentions_timeline.json?count=1", str, 4096); |
jlind6 | 0:b3d9fc5861c6 | 33 | } else { |
jlind6 | 0:b3d9fc5861c6 | 34 | sprintf(url, "http://api.supertweet.net/1.1/statuses/mentions_timeline.json?since_id=%s\0", previousID.c_str()); |
jlind6 | 0:b3d9fc5861c6 | 35 | ret = client.get(url, str, 4096); |
jlind6 | 0:b3d9fc5861c6 | 36 | } |
jlind6 | 0:b3d9fc5861c6 | 37 | if (ret) { |
jlind6 | 0:b3d9fc5861c6 | 38 | printf("Error - ret = %d - HTTP return code = %d\n", ret, client.getHTTPResponseCode()); |
jlind6 | 0:b3d9fc5861c6 | 39 | return 1; |
jlind6 | 0:b3d9fc5861c6 | 40 | } |
jlind6 | 0:b3d9fc5861c6 | 41 | |
jlind6 | 0:b3d9fc5861c6 | 42 | string tweetInfo(str); |
jlind6 | 0:b3d9fc5861c6 | 43 | string tweetCommand; |
jlind6 | 0:b3d9fc5861c6 | 44 | string tweetSetting; |
jlind6 | 0:b3d9fc5861c6 | 45 | size_t foundStart = string::npos; |
jlind6 | 0:b3d9fc5861c6 | 46 | if(tweetInfo.compare("[]")) { |
jlind6 | 0:b3d9fc5861c6 | 47 | foundStart = tweetInfo.rfind("\"text\":\"@"); |
jlind6 | 0:b3d9fc5861c6 | 48 | } |
jlind6 | 0:b3d9fc5861c6 | 49 | size_t foundEnd; |
jlind6 | 0:b3d9fc5861c6 | 50 | while(foundStart != string::npos) { |
jlind6 | 0:b3d9fc5861c6 | 51 | foundStart += 24; |
jlind6 | 0:b3d9fc5861c6 | 52 | foundEnd = tweetInfo.find(" ",foundStart); |
jlind6 | 0:b3d9fc5861c6 | 53 | if(foundEnd != string::npos) { |
jlind6 | 0:b3d9fc5861c6 | 54 | tweetCommand = tweetInfo.substr(foundStart, foundEnd - foundStart); |
jlind6 | 0:b3d9fc5861c6 | 55 | } else { |
jlind6 | 0:b3d9fc5861c6 | 56 | continue; |
jlind6 | 0:b3d9fc5861c6 | 57 | } |
jlind6 | 0:b3d9fc5861c6 | 58 | foundStart = foundEnd + 1; |
jlind6 | 0:b3d9fc5861c6 | 59 | foundEnd = tweetInfo.find("\"",foundStart); |
jlind6 | 0:b3d9fc5861c6 | 60 | tweetSetting = tweetInfo.substr(foundStart, foundEnd - foundStart); |
jlind6 | 0:b3d9fc5861c6 | 61 | |
jlind6 | 0:b3d9fc5861c6 | 62 | printf("%s*%s*\n", tweetCommand.c_str(), tweetSetting.c_str()); |
jlind6 | 0:b3d9fc5861c6 | 63 | |
jlind6 | 0:b3d9fc5861c6 | 64 | if(!tweetCommand.compare("background")) { |
jlind6 | 0:b3d9fc5861c6 | 65 | if(!tweetSetting.compare("blue")) { |
jlind6 | 0:b3d9fc5861c6 | 66 | background = 0x0000FF; |
jlind6 | 0:b3d9fc5861c6 | 67 | squareColor = 0x00FF00; |
jlind6 | 0:b3d9fc5861c6 | 68 | } else if(!tweetSetting.compare("green")) { |
jlind6 | 0:b3d9fc5861c6 | 69 | background = 0x00FF00; |
jlind6 | 0:b3d9fc5861c6 | 70 | squareColor = 0xFF0000; |
jlind6 | 0:b3d9fc5861c6 | 71 | } else if(!tweetSetting.compare("red")) { |
jlind6 | 0:b3d9fc5861c6 | 72 | background = 0xFF0000; |
jlind6 | 0:b3d9fc5861c6 | 73 | squareColor = 0x0000FF; |
jlind6 | 0:b3d9fc5861c6 | 74 | } else { |
jlind6 | 0:b3d9fc5861c6 | 75 | text = "Invalid tweet setting"; |
jlind6 | 0:b3d9fc5861c6 | 76 | } |
jlind6 | 0:b3d9fc5861c6 | 77 | } else if(!tweetCommand.compare("write")) { |
jlind6 | 0:b3d9fc5861c6 | 78 | text = tweetSetting; |
jlind6 | 0:b3d9fc5861c6 | 79 | } else if(!tweetCommand.compare("square")) { |
jlind6 | 0:b3d9fc5861c6 | 80 | squareLength = atoi(tweetSetting.c_str()); |
jlind6 | 0:b3d9fc5861c6 | 81 | } else { |
jlind6 | 0:b3d9fc5861c6 | 82 | text = "Invalid tweet command"; |
jlind6 | 0:b3d9fc5861c6 | 83 | } |
jlind6 | 0:b3d9fc5861c6 | 84 | |
jlind6 | 0:b3d9fc5861c6 | 85 | foundStart -= 60; |
jlind6 | 0:b3d9fc5861c6 | 86 | foundStart = tweetInfo.rfind("\"text\":\"@", foundStart); |
jlind6 | 0:b3d9fc5861c6 | 87 | printf("%d*%d*\n", foundStart, string::npos); |
jlind6 | 0:b3d9fc5861c6 | 88 | } |
jlind6 | 0:b3d9fc5861c6 | 89 | |
jlind6 | 0:b3d9fc5861c6 | 90 | lcd.cls(); |
jlind6 | 0:b3d9fc5861c6 | 91 | lcd.fill(0, 0, 130, 130, background); |
jlind6 | 0:b3d9fc5861c6 | 92 | lcd.background(background); |
jlind6 | 0:b3d9fc5861c6 | 93 | lcd.locate(0, 7); |
jlind6 | 0:b3d9fc5861c6 | 94 | lcd.printf(text.c_str()); |
jlind6 | 0:b3d9fc5861c6 | 95 | if(squareLength > 0) { |
jlind6 | 0:b3d9fc5861c6 | 96 | int i; |
jlind6 | 0:b3d9fc5861c6 | 97 | for(int i=65-(squareLength/2); i<65+(squareLength/2);i++) { |
jlind6 | 0:b3d9fc5861c6 | 98 | lcd.pixel(i, 65-(squareLength/2), squareColor); |
jlind6 | 0:b3d9fc5861c6 | 99 | lcd.pixel(i, 65+(squareLength/2), squareColor); |
jlind6 | 0:b3d9fc5861c6 | 100 | } |
jlind6 | 0:b3d9fc5861c6 | 101 | for(i=65-(squareLength/2); i<65+(squareLength/2);i++) { |
jlind6 | 0:b3d9fc5861c6 | 102 | lcd.pixel(65-(squareLength/2), i, squareColor); |
jlind6 | 0:b3d9fc5861c6 | 103 | lcd.pixel(65+(squareLength/2), i, squareColor); |
jlind6 | 0:b3d9fc5861c6 | 104 | } |
jlind6 | 0:b3d9fc5861c6 | 105 | } |
jlind6 | 0:b3d9fc5861c6 | 106 | |
jlind6 | 0:b3d9fc5861c6 | 107 | if(tweetInfo.compare("[]")) { |
jlind6 | 0:b3d9fc5861c6 | 108 | foundStart = tweetInfo.find("\"id_str\":\""); |
jlind6 | 0:b3d9fc5861c6 | 109 | foundStart += 10; |
jlind6 | 0:b3d9fc5861c6 | 110 | foundEnd = tweetInfo.find("\"", foundStart); |
jlind6 | 0:b3d9fc5861c6 | 111 | previousID = tweetInfo.substr(foundStart, foundEnd - foundStart); |
jlind6 | 0:b3d9fc5861c6 | 112 | printf("%s\n", previousID.c_str()); |
jlind6 | 0:b3d9fc5861c6 | 113 | } |
jlind6 | 0:b3d9fc5861c6 | 114 | |
jlind6 | 0:b3d9fc5861c6 | 115 | wait(30); |
jlind6 | 0:b3d9fc5861c6 | 116 | } |
jlind6 | 0:b3d9fc5861c6 | 117 | |
jlind6 | 0:b3d9fc5861c6 | 118 | } |