Live RSS News feed is displayed on a Nokia LCD. See http://mbed.org/users/4180_1/notebook/news-nokia-lcd-display/
Dependencies: NetServices mbed spxml HTTPClient_ToBeRemoved
main.cpp@0:91658d171678, 2012-05-25 (annotated)
- Committer:
- 4180_1
- Date:
- Fri May 25 01:08:51 2012 +0000
- Revision:
- 0:91658d171678
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:91658d171678 | 1 | #include "mbed.h" |
4180_1 | 0:91658d171678 | 2 | #include "EthernetNetIf.h" |
4180_1 | 0:91658d171678 | 3 | #include "HTTPClient.h" |
4180_1 | 0:91658d171678 | 4 | #include "NokiaLCD.h" |
4180_1 | 0:91658d171678 | 5 | //CNN Tech News RSS Feed - get web page with XML |
4180_1 | 0:91658d171678 | 6 | // displays titles on LCD from XML "<title>....title text...</title>" |
4180_1 | 0:91658d171678 | 7 | NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
4180_1 | 0:91658d171678 | 8 | EthernetNetIf eth; |
4180_1 | 0:91658d171678 | 9 | HTTPClient http; |
4180_1 | 0:91658d171678 | 10 | HTTPResult result; |
4180_1 | 0:91658d171678 | 11 | bool completed = false; |
4180_1 | 0:91658d171678 | 12 | void request_callback(HTTPResult r) { |
4180_1 | 0:91658d171678 | 13 | result = r; |
4180_1 | 0:91658d171678 | 14 | completed = true; |
4180_1 | 0:91658d171678 | 15 | } |
4180_1 | 0:91658d171678 | 16 | |
4180_1 | 0:91658d171678 | 17 | int main() { |
4180_1 | 0:91658d171678 | 18 | char *tstartXML = "<title>"; //RSS XML start title |
4180_1 | 0:91658d171678 | 19 | char *tendXML ="</title>"; //RSS XML end title |
4180_1 | 0:91658d171678 | 20 | char *tsptr; |
4180_1 | 0:91658d171678 | 21 | char *teptr; |
4180_1 | 0:91658d171678 | 22 | int i=0,j=0; |
4180_1 | 0:91658d171678 | 23 | // the eth and HTTP code has be taken directly from the HTTPStream documentation page |
4180_1 | 0:91658d171678 | 24 | // see http://mbed.org/cookbook/HTTP-Client-Data-Containers |
4180_1 | 0:91658d171678 | 25 | lcd.cls(); |
4180_1 | 0:91658d171678 | 26 | lcd.locate(0,1); |
4180_1 | 0:91658d171678 | 27 | lcd.printf("net setup"); |
4180_1 | 0:91658d171678 | 28 | EthernetErr ethErr = eth.setup(); |
4180_1 | 0:91658d171678 | 29 | if (ethErr) { |
4180_1 | 0:91658d171678 | 30 | lcd.printf("net error"); |
4180_1 | 0:91658d171678 | 31 | return -1; |
4180_1 | 0:91658d171678 | 32 | } |
4180_1 | 0:91658d171678 | 33 | lcd.locate(0,2); |
4180_1 | 0:91658d171678 | 34 | lcd.printf("net ok"); |
4180_1 | 0:91658d171678 | 35 | wait(1); |
4180_1 | 0:91658d171678 | 36 | lcd.cls(); |
4180_1 | 0:91658d171678 | 37 | HTTPStream stream; |
4180_1 | 0:91658d171678 | 38 | char BigBuf[2048 + 1] = {0}; |
4180_1 | 0:91658d171678 | 39 | stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read |
4180_1 | 0:91658d171678 | 40 | //CNN Tech News RSS Feed - get web page with XML |
4180_1 | 0:91658d171678 | 41 | HTTPResult r = http.get("HTTP://rss.cnn.com/rss/cnn_tech.rss", &stream, request_callback); |
4180_1 | 0:91658d171678 | 42 | while (!completed) { |
4180_1 | 0:91658d171678 | 43 | Net::poll(); // Polls the Networking stack |
4180_1 | 0:91658d171678 | 44 | if (stream.readable()) { // check for end of file |
4180_1 | 0:91658d171678 | 45 | BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string |
4180_1 | 0:91658d171678 | 46 | tsptr = BigBuf; |
4180_1 | 0:91658d171678 | 47 | // displays titles on LCD from XML "<title>....title text...</title>" |
4180_1 | 0:91658d171678 | 48 | do { |
4180_1 | 0:91658d171678 | 49 | tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not |
4180_1 | 0:91658d171678 | 50 | teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not |
4180_1 | 0:91658d171678 | 51 | if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>" |
4180_1 | 0:91658d171678 | 52 | if ((tsptr!=NULL)&&(teptr!=NULL)) { |
4180_1 | 0:91658d171678 | 53 | i=0; |
4180_1 | 0:91658d171678 | 54 | // loop to display lines on LCD |
4180_1 | 0:91658d171678 | 55 | for (j=0; (j)<(strlen(tsptr)-strlen(teptr)); j=j+16) { |
4180_1 | 0:91658d171678 | 56 | // lcd.cls(); // clear screen before writing a new line |
4180_1 | 0:91658d171678 | 57 | lcd.locate(0,(2+(j/16))); |
4180_1 | 0:91658d171678 | 58 | // loop to output a line on the LCD |
4180_1 | 0:91658d171678 | 59 | for (i=0; ((i<16)&&(tsptr[i+j] != '<')); i++) { |
4180_1 | 0:91658d171678 | 60 | lcd.putc(tsptr[i+j]); |
4180_1 | 0:91658d171678 | 61 | } |
4180_1 | 0:91658d171678 | 62 | } |
4180_1 | 0:91658d171678 | 63 | wait(4); |
4180_1 | 0:91658d171678 | 64 | lcd.cls(); //clear LCD between news items |
4180_1 | 0:91658d171678 | 65 | lcd.locate(0,2); |
4180_1 | 0:91658d171678 | 66 | } |
4180_1 | 0:91658d171678 | 67 | } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display |
4180_1 | 0:91658d171678 | 68 | stream.readNext((byte*)BigBuf, 2048); //Buffer has been read, now we can put more data in it |
4180_1 | 0:91658d171678 | 69 | } |
4180_1 | 0:91658d171678 | 70 | } |
4180_1 | 0:91658d171678 | 71 | lcd.cls(); |
4180_1 | 0:91658d171678 | 72 | if (result == HTTP_OK) { |
4180_1 | 0:91658d171678 | 73 | lcd.cls(); |
4180_1 | 0:91658d171678 | 74 | lcd.locate(0,1); |
4180_1 | 0:91658d171678 | 75 | lcd.printf(" Read complete"); |
4180_1 | 0:91658d171678 | 76 | } else { |
4180_1 | 0:91658d171678 | 77 | lcd.printf(" Error %d\", result"); |
4180_1 | 0:91658d171678 | 78 | return -1; |
4180_1 | 0:91658d171678 | 79 | } |
4180_1 | 0:91658d171678 | 80 | } |