Live RSS news feeds are displayed on a basic text LCD See http://mbed.org/users/4180_1/notebook/news-lcd-display/
Dependencies: NetServices TextLCDfor mbed spxml
Fork of News_LCD_display by
Reads the RSS-Fedd of the ZVV and prints the current state of the public transports on a LCD
main.cpp@1:27033a7fe75c, 2014-02-22 (annotated)
- Committer:
- 18padx08
- Date:
- Sat Feb 22 22:19:53 2014 +0000
- Revision:
- 1:27033a7fe75c
- Parent:
- 0:582c6436e759
- Child:
- 2:6aaaf927e994
changed link to ZVV rss feed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:582c6436e759 | 1 | #include "mbed.h" |
4180_1 | 0:582c6436e759 | 2 | #include "EthernetNetIf.h" |
4180_1 | 0:582c6436e759 | 3 | #include "HTTPClient.h" |
4180_1 | 0:582c6436e759 | 4 | #include "TextLCD.h" |
4180_1 | 0:582c6436e759 | 5 | //CNN Tech News RSS Feed - get web page with XML |
4180_1 | 0:582c6436e759 | 6 | // displays titles on LCD from XML "<title>....title text...</title>" |
18padx08 | 1:27033a7fe75c | 7 | TextLCD lcd(p26,p25,p24,p23,p22,p20,p19,TextLCD::LCD20x4); // rs, e, d0-d3 |
4180_1 | 0:582c6436e759 | 8 | EthernetNetIf eth; |
4180_1 | 0:582c6436e759 | 9 | HTTPClient http; |
4180_1 | 0:582c6436e759 | 10 | HTTPResult result; |
4180_1 | 0:582c6436e759 | 11 | bool completed = false; |
18padx08 | 1:27033a7fe75c | 12 | void loadZVVNews(); |
18padx08 | 1:27033a7fe75c | 13 | void request_callback(HTTPResult r) |
18padx08 | 1:27033a7fe75c | 14 | { |
4180_1 | 0:582c6436e759 | 15 | result = r; |
4180_1 | 0:582c6436e759 | 16 | completed = true; |
4180_1 | 0:582c6436e759 | 17 | } |
4180_1 | 0:582c6436e759 | 18 | |
18padx08 | 1:27033a7fe75c | 19 | int main() |
18padx08 | 1:27033a7fe75c | 20 | { |
18padx08 | 1:27033a7fe75c | 21 | |
4180_1 | 0:582c6436e759 | 22 | // the eth and HTTP code has be taken directly from the HTTPStream documentation page |
4180_1 | 0:582c6436e759 | 23 | // see http://mbed.org/cookbook/HTTP-Client-Data-Containers |
4180_1 | 0:582c6436e759 | 24 | lcd.cls(); |
4180_1 | 0:582c6436e759 | 25 | lcd.printf("net setup"); |
4180_1 | 0:582c6436e759 | 26 | EthernetErr ethErr = eth.setup(); |
4180_1 | 0:582c6436e759 | 27 | if (ethErr) { |
4180_1 | 0:582c6436e759 | 28 | lcd.printf("\n\r net error"); |
4180_1 | 0:582c6436e759 | 29 | return -1; |
4180_1 | 0:582c6436e759 | 30 | } |
4180_1 | 0:582c6436e759 | 31 | lcd.printf("\n\r net ok"); |
18padx08 | 1:27033a7fe75c | 32 | wait(0.5); |
18padx08 | 1:27033a7fe75c | 33 | lcd.cls(); |
18padx08 | 1:27033a7fe75c | 34 | /* HTTPStream stream; |
18padx08 | 1:27033a7fe75c | 35 | char BigBuf[2048 + 1] = {0}; |
18padx08 | 1:27033a7fe75c | 36 | stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read |
18padx08 | 1:27033a7fe75c | 37 | //CNN Tech News RSS Feed - get web page with XML |
18padx08 | 1:27033a7fe75c | 38 | HTTPResult r = http.get("http://online.fahrplan.zvv.ch/bin/help.exe/dn?tpl=rss_feed_VBZ", &stream, request_callback); |
18padx08 | 1:27033a7fe75c | 39 | while (!completed) { |
18padx08 | 1:27033a7fe75c | 40 | Net::poll(); // Polls the Networking stack |
18padx08 | 1:27033a7fe75c | 41 | if (stream.readable()) { // check for end of file |
18padx08 | 1:27033a7fe75c | 42 | BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string |
18padx08 | 1:27033a7fe75c | 43 | tsptr = BigBuf; |
18padx08 | 1:27033a7fe75c | 44 | // displays titles on LCD from XML "<title>....title text...</title>" |
18padx08 | 1:27033a7fe75c | 45 | do { |
18padx08 | 1:27033a7fe75c | 46 | tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not |
18padx08 | 1:27033a7fe75c | 47 | teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not |
18padx08 | 1:27033a7fe75c | 48 | if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>" |
18padx08 | 1:27033a7fe75c | 49 | if ((tsptr!=NULL)&&(teptr!=NULL)) { |
18padx08 | 1:27033a7fe75c | 50 | //i=0; |
18padx08 | 1:27033a7fe75c | 51 | // loop to scroll characters slowly across LCD |
18padx08 | 1:27033a7fe75c | 52 | for (j=0; j<(strlen(tsptr)-strlen(teptr)); j++) { |
18padx08 | 1:27033a7fe75c | 53 | //lcd.cls(); // clear screen before writing a new line |
18padx08 | 1:27033a7fe75c | 54 | // loop to output a line on the LCD |
18padx08 | 1:27033a7fe75c | 55 | for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) { |
18padx08 | 1:27033a7fe75c | 56 | lcd.putc(tsptr[i+j]); |
18padx08 | 1:27033a7fe75c | 57 | } |
18padx08 | 1:27033a7fe75c | 58 | lcd.putc(tsptr[j]); |
18padx08 | 1:27033a7fe75c | 59 | //if (j==0) wait(1.2); //add first line delays for scroll timing |
18padx08 | 1:27033a7fe75c | 60 | wait(.1); //delay for charcter scroll timing |
18padx08 | 1:27033a7fe75c | 61 | } |
18padx08 | 1:27033a7fe75c | 62 | wait(.4); |
18padx08 | 1:27033a7fe75c | 63 | lcd.cls(); //clear LCD between news items |
18padx08 | 1:27033a7fe75c | 64 | wait(.2); |
18padx08 | 1:27033a7fe75c | 65 | } |
18padx08 | 1:27033a7fe75c | 66 | } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display |
18padx08 | 1:27033a7fe75c | 67 | stream.readNext((byte*)BigBuf, 2048); //Buffer has been read, now we can put more data in it |
18padx08 | 1:27033a7fe75c | 68 | } |
18padx08 | 1:27033a7fe75c | 69 | }*/ |
18padx08 | 1:27033a7fe75c | 70 | loadZVVNews(); |
18padx08 | 1:27033a7fe75c | 71 | while(true) { |
18padx08 | 1:27033a7fe75c | 72 | completed = false; |
18padx08 | 1:27033a7fe75c | 73 | if (result == HTTP_OK) { |
18padx08 | 1:27033a7fe75c | 74 | |
18padx08 | 1:27033a7fe75c | 75 | lcd.printf("Reload in 5 sec\n"); |
18padx08 | 1:27033a7fe75c | 76 | |
18padx08 | 1:27033a7fe75c | 77 | wait(5); |
18padx08 | 1:27033a7fe75c | 78 | lcd.cls(); |
18padx08 | 1:27033a7fe75c | 79 | loadZVVNews(); |
18padx08 | 1:27033a7fe75c | 80 | |
18padx08 | 1:27033a7fe75c | 81 | //lcd.cls(); |
18padx08 | 1:27033a7fe75c | 82 | //lcd.printf(" Read complete\n\r"); |
18padx08 | 1:27033a7fe75c | 83 | } else { |
18padx08 | 1:27033a7fe75c | 84 | lcd. printf(" Error %d\n", result); |
18padx08 | 1:27033a7fe75c | 85 | wait(1.2); |
18padx08 | 1:27033a7fe75c | 86 | lcd.cls(); |
18padx08 | 1:27033a7fe75c | 87 | loadZVVNews(); |
18padx08 | 1:27033a7fe75c | 88 | } |
18padx08 | 1:27033a7fe75c | 89 | } |
18padx08 | 1:27033a7fe75c | 90 | } |
18padx08 | 1:27033a7fe75c | 91 | |
18padx08 | 1:27033a7fe75c | 92 | void loadZVVNews() |
18padx08 | 1:27033a7fe75c | 93 | { |
18padx08 | 1:27033a7fe75c | 94 | char *tstartXML = "<title>"; //RSS XML start title |
18padx08 | 1:27033a7fe75c | 95 | char *tendXML ="</title>"; //RSS XML end title |
18padx08 | 1:27033a7fe75c | 96 | char *tsptr; |
18padx08 | 1:27033a7fe75c | 97 | char *teptr; |
18padx08 | 1:27033a7fe75c | 98 | int j=0; |
4180_1 | 0:582c6436e759 | 99 | HTTPStream stream; |
18padx08 | 1:27033a7fe75c | 100 | char BigBuf[4096 + 1] = {0}; |
18padx08 | 1:27033a7fe75c | 101 | stream.readNext((byte*)BigBuf, 4096); //Point to buffer for the first read |
4180_1 | 0:582c6436e759 | 102 | //CNN Tech News RSS Feed - get web page with XML |
18padx08 | 1:27033a7fe75c | 103 | HTTPResult r = http.get("http://online.fahrplan.zvv.ch/bin/help.exe/dn?tpl=rss_feed_VBZ", &stream, request_callback); |
4180_1 | 0:582c6436e759 | 104 | while (!completed) { |
4180_1 | 0:582c6436e759 | 105 | Net::poll(); // Polls the Networking stack |
4180_1 | 0:582c6436e759 | 106 | if (stream.readable()) { // check for end of file |
4180_1 | 0:582c6436e759 | 107 | BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string |
4180_1 | 0:582c6436e759 | 108 | tsptr = BigBuf; |
4180_1 | 0:582c6436e759 | 109 | // displays titles on LCD from XML "<title>....title text...</title>" |
4180_1 | 0:582c6436e759 | 110 | do { |
4180_1 | 0:582c6436e759 | 111 | tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not |
4180_1 | 0:582c6436e759 | 112 | teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not |
4180_1 | 0:582c6436e759 | 113 | if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>" |
4180_1 | 0:582c6436e759 | 114 | if ((tsptr!=NULL)&&(teptr!=NULL)) { |
18padx08 | 1:27033a7fe75c | 115 | //i=0; |
4180_1 | 0:582c6436e759 | 116 | // loop to scroll characters slowly across LCD |
18padx08 | 1:27033a7fe75c | 117 | for (j=0; j<(strlen(tsptr)-strlen(teptr)); j++) { |
18padx08 | 1:27033a7fe75c | 118 | //lcd.cls(); // clear screen before writing a new line |
4180_1 | 0:582c6436e759 | 119 | // loop to output a line on the LCD |
18padx08 | 1:27033a7fe75c | 120 | /*for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) { |
4180_1 | 0:582c6436e759 | 121 | lcd.putc(tsptr[i+j]); |
18padx08 | 1:27033a7fe75c | 122 | }*/ |
18padx08 | 1:27033a7fe75c | 123 | lcd.putc(tsptr[j]); |
18padx08 | 1:27033a7fe75c | 124 | //if (j==0) wait(1.2); //add first line delays for scroll timing |
18padx08 | 1:27033a7fe75c | 125 | //delay for charcter scroll timing |
4180_1 | 0:582c6436e759 | 126 | } |
18padx08 | 1:27033a7fe75c | 127 | wait(2.5); |
4180_1 | 0:582c6436e759 | 128 | lcd.cls(); //clear LCD between news items |
18padx08 | 1:27033a7fe75c | 129 | |
4180_1 | 0:582c6436e759 | 130 | } |
4180_1 | 0:582c6436e759 | 131 | } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display |
18padx08 | 1:27033a7fe75c | 132 | stream.readNext((byte*)BigBuf, 4096); //Buffer has been read, now we can put more data in it |
4180_1 | 0:582c6436e759 | 133 | } |
4180_1 | 0:582c6436e759 | 134 | } |
4180_1 | 0:582c6436e759 | 135 | lcd.cls(); |
18padx08 | 1:27033a7fe75c | 136 | } |