日本語表示に対応したRSSヘッドラインリーダです。 詳細は、エレキジャックWebをご参照ください。

Dependencies:   EthernetNetIf NTPClient_NetServices TextLCD mbed HTTPClient

Committer:
techand
Date:
Thu Apr 05 02:57:16 2012 +0000
Revision:
0:0936e2c7769b
RSS HeadLine Feeder for BlackOne(Color LCD) & Japanese Kanji.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
techand 0:0936e2c7769b 1 #include "mbed.h"
techand 0:0936e2c7769b 2 #include "EthernetNetIf.h"
techand 0:0936e2c7769b 3 #include "HTTPClient.h"
techand 0:0936e2c7769b 4 #include "NTPClient.h"
techand 0:0936e2c7769b 5
techand 0:0936e2c7769b 6 #define RSS1M "CQ Interface>"
techand 0:0936e2c7769b 7 #define RSS1 "http://www.kumikomi.net/interface/atom.xml"
techand 0:0936e2c7769b 8 #define RSS2M "Sports Topic>"
techand 0:0936e2c7769b 9 #define RSS2 "http://news.goo.ne.jp/news/rss/topics/sports/index.rdf"
techand 0:0936e2c7769b 10 #define RSS3M "Make:Japan >"
techand 0:0936e2c7769b 11 #define RSS3 "http://jp.makezine.com/blog/index.xml"
techand 0:0936e2c7769b 12 #define RSS4M "Weather NEWS>"
techand 0:0936e2c7769b 13 #define RSS4 "http://feeds.feedburner.jp/hitokuchi_4410"
techand 0:0936e2c7769b 14
techand 0:0936e2c7769b 15 Ticker rss_img; // For RSS animation
techand 0:0936e2c7769b 16
techand 0:0936e2c7769b 17 DigitalIn sw1(p17);
techand 0:0936e2c7769b 18 DigitalIn sw2(p19);
techand 0:0936e2c7769b 19 DigitalIn sw3(p20);
techand 0:0936e2c7769b 20 DigitalIn sw4(p21);
techand 0:0936e2c7769b 21
techand 0:0936e2c7769b 22 int img = 26; // animate img 26-29
techand 0:0936e2c7769b 23 int rss_no = 0;
techand 0:0936e2c7769b 24
techand 0:0936e2c7769b 25 Serial pc(USBTX, USBRX); // tx, rx
techand 0:0936e2c7769b 26 Serial lcd(p9, p10); // tx, rx
techand 0:0936e2c7769b 27
techand 0:0936e2c7769b 28 EthernetNetIf eth;
techand 0:0936e2c7769b 29 HTTPClient http;
techand 0:0936e2c7769b 30 NTPClient ntp;
techand 0:0936e2c7769b 31
techand 0:0936e2c7769b 32 LocalFileSystem local("local");
techand 0:0936e2c7769b 33 HTTPResult result;
techand 0:0936e2c7769b 34 bool completed = false;
techand 0:0936e2c7769b 35
techand 0:0936e2c7769b 36 //* HTTP request callback ******************************************
techand 0:0936e2c7769b 37 void request_callback(HTTPResult r)
techand 0:0936e2c7769b 38 {
techand 0:0936e2c7769b 39 result = r;
techand 0:0936e2c7769b 40 completed = true;
techand 0:0936e2c7769b 41 }
techand 0:0936e2c7769b 42
techand 0:0936e2c7769b 43 //* animate RSS icon ***********************************************
techand 0:0936e2c7769b 44 void animate ()
techand 0:0936e2c7769b 45 {
techand 0:0936e2c7769b 46 img++;
techand 0:0936e2c7769b 47 if (img > 29) img = 26;
techand 0:0936e2c7769b 48 lcd.printf("\x1b@%d;0;30;64I",img);
techand 0:0936e2c7769b 49 }
techand 0:0936e2c7769b 50
techand 0:0936e2c7769b 51 //* Display menu ****************************************************
techand 0:0936e2c7769b 52 void menu ()
techand 0:0936e2c7769b 53 {
techand 0:0936e2c7769b 54 lcd.printf("\x1b@20;0I"); // Display Openning
techand 0:0936e2c7769b 55 lcd.printf("\x1b[1;24H"); //
techand 0:0936e2c7769b 56 lcd.printf(RSS1M); //
techand 0:0936e2c7769b 57 lcd.printf("\x1b[5;24H"); //
techand 0:0936e2c7769b 58 lcd.printf(RSS2M); //
techand 0:0936e2c7769b 59 lcd.printf("\x1b[9;24H"); //
techand 0:0936e2c7769b 60 lcd.printf(RSS3M); //
techand 0:0936e2c7769b 61 lcd.printf("\x1b[13;24H"); //
techand 0:0936e2c7769b 62 lcd.printf(RSS4M); //
techand 0:0936e2c7769b 63
techand 0:0936e2c7769b 64 }
techand 0:0936e2c7769b 65
techand 0:0936e2c7769b 66 //* Blink selected Item *********************************************
techand 0:0936e2c7769b 67 void select(int rss)
techand 0:0936e2c7769b 68 {
techand 0:0936e2c7769b 69 for (int i=0;i<=3;i++)
techand 0:0936e2c7769b 70 {
techand 0:0936e2c7769b 71 lcd.printf("\x1b@25;0;180;%dI", ((rss-1)*64)+14);
techand 0:0936e2c7769b 72 wait_ms(100);
techand 0:0936e2c7769b 73 lcd.printf("\x1b@0;0;133;20;180;%d;0z", ((rss-1)*64)+14);
techand 0:0936e2c7769b 74 wait_ms(50);
techand 0:0936e2c7769b 75 }
techand 0:0936e2c7769b 76 lcd.printf("\x1b@25;0;180;%dI", ((rss-1)*64)+14);
techand 0:0936e2c7769b 77 }
techand 0:0936e2c7769b 78
techand 0:0936e2c7769b 79 //****************************************************************************
techand 0:0936e2c7769b 80 //* M A I N
techand 0:0936e2c7769b 81 //****************************************************************************
techand 0:0936e2c7769b 82 int main() {
techand 0:0936e2c7769b 83
techand 0:0936e2c7769b 84 int color = 0;
techand 0:0936e2c7769b 85 int flag = 0;
techand 0:0936e2c7769b 86 int count = 0;
techand 0:0936e2c7769b 87 char c;
techand 0:0936e2c7769b 88 char buffer[100];
techand 0:0936e2c7769b 89 char BigBuf[512 + 1] = {0};
techand 0:0936e2c7769b 90
techand 0:0936e2c7769b 91 // Init SW
techand 0:0936e2c7769b 92 sw1.mode(PullUp);
techand 0:0936e2c7769b 93 sw2.mode(PullUp);
techand 0:0936e2c7769b 94 sw3.mode(PullUp);
techand 0:0936e2c7769b 95 sw4.mode(PullUp);
techand 0:0936e2c7769b 96
techand 0:0936e2c7769b 97 // Set lcd baudrate
techand 0:0936e2c7769b 98 lcd.printf("\x1b@14Z"); // 57600bps
techand 0:0936e2c7769b 99 lcd.baud(57600);
techand 0:0936e2c7769b 100
techand 0:0936e2c7769b 101 lcd.printf("\x1b@80Z"); // Set Curser off
techand 0:0936e2c7769b 102 lcd.printf("\x1b@0Z"); // clear all
techand 0:0936e2c7769b 103 lcd.printf("\x1b@30Z"); // set buffer 0
techand 0:0936e2c7769b 104 lcd.printf("\x1b[1;23;m"); // bold;white
techand 0:0936e2c7769b 105
techand 0:0936e2c7769b 106 // Display gend Get menu ********************************
techand 0:0936e2c7769b 107 menu();
techand 0:0936e2c7769b 108 while ( rss_no == 0)
techand 0:0936e2c7769b 109 {
techand 0:0936e2c7769b 110 if (sw1 == 0 ) {rss_no = 1; select(1);}
techand 0:0936e2c7769b 111 if (sw2 == 0 ) {rss_no = 2; select(2);}
techand 0:0936e2c7769b 112 if (sw3 == 0 ) {rss_no = 3; select(3);}
techand 0:0936e2c7769b 113 if (sw4 == 0 ) {rss_no = 4; select(4);}
techand 0:0936e2c7769b 114 }
techand 0:0936e2c7769b 115
techand 0:0936e2c7769b 116
techand 0:0936e2c7769b 117 rss_img.attach(&animate, 0.3); // the address of the function to be attached (flip) and the interval (2 seconds)
techand 0:0936e2c7769b 118 EthernetErr ethErr = eth.setup();
techand 0:0936e2c7769b 119 if(ethErr)
techand 0:0936e2c7769b 120 {
techand 0:0936e2c7769b 121 lcd.printf("Error %d in setup.\n\r", ethErr);
techand 0:0936e2c7769b 122 return -1;
techand 0:0936e2c7769b 123 }
techand 0:0936e2c7769b 124
techand 0:0936e2c7769b 125 rss_img.detach();
techand 0:0936e2c7769b 126
techand 0:0936e2c7769b 127 lcd.printf("\x1b[1;19;4;m"); //Set char to Blue
techand 0:0936e2c7769b 128 lcd.printf("\x1b@21;0I"); // Dsiaplay backGround
techand 0:0936e2c7769b 129 lcd.printf("\x1b[*"); // Claer all char and set curser to HP
techand 0:0936e2c7769b 130
techand 0:0936e2c7769b 131 // Set UTF8
techand 0:0936e2c7769b 132 lcd.printf("\x1b@21Z"); // Not CR/LF
techand 0:0936e2c7769b 133 lcd.printf("\x1b[4;0H"); // Set Cuser to LINE 4
techand 0:0936e2c7769b 134 lcd.printf("\x1b@3Z"); // Set Char code to UTF-8
techand 0:0936e2c7769b 135
techand 0:0936e2c7769b 136 HTTPStream stream;
techand 0:0936e2c7769b 137
techand 0:0936e2c7769b 138 completed=false;
techand 0:0936e2c7769b 139
techand 0:0936e2c7769b 140 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
techand 0:0936e2c7769b 141
techand 0:0936e2c7769b 142 if (rss_no == 1)
techand 0:0936e2c7769b 143 {
techand 0:0936e2c7769b 144 // lcd.printf("%s ...\n\r", RSS1);
techand 0:0936e2c7769b 145 HTTPResult r = http.get(RSS1, &stream, request_callback);
techand 0:0936e2c7769b 146 }
techand 0:0936e2c7769b 147 if (rss_no == 2)
techand 0:0936e2c7769b 148 {
techand 0:0936e2c7769b 149 // lcd.printf("%s ...\n\r", RSS2);
techand 0:0936e2c7769b 150 HTTPResult r = http.get(RSS2, &stream, request_callback);
techand 0:0936e2c7769b 151 }
techand 0:0936e2c7769b 152 if (rss_no == 3)
techand 0:0936e2c7769b 153 {
techand 0:0936e2c7769b 154 // lcd.printf("%s ...\n\r", RSS3);
techand 0:0936e2c7769b 155 HTTPResult r = http.get(RSS3, &stream, request_callback);
techand 0:0936e2c7769b 156 }
techand 0:0936e2c7769b 157 if (rss_no == 4)
techand 0:0936e2c7769b 158 {
techand 0:0936e2c7769b 159 // lcd.printf("%s ...\n\r", RSS4);
techand 0:0936e2c7769b 160 HTTPResult r = http.get(RSS4, &stream, request_callback);
techand 0:0936e2c7769b 161 }
techand 0:0936e2c7769b 162 rss_no = 0;
techand 0:0936e2c7769b 163
techand 0:0936e2c7769b 164 FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
techand 0:0936e2c7769b 165 while(!completed)
techand 0:0936e2c7769b 166 {
techand 0:0936e2c7769b 167 Net::poll(); //Polls the Networking stack
techand 0:0936e2c7769b 168 if(stream.readable())
techand 0:0936e2c7769b 169 {
techand 0:0936e2c7769b 170 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
techand 0:0936e2c7769b 171 fprintf(fp,BigBuf);
techand 0:0936e2c7769b 172 //Note: some servers do not like if you throttle them too much, so printf'ing during a request is generally bad practice
techand 0:0936e2c7769b 173 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
techand 0:0936e2c7769b 174 }
techand 0:0936e2c7769b 175 }
techand 0:0936e2c7769b 176 fclose(fp);
techand 0:0936e2c7769b 177
techand 0:0936e2c7769b 178 fp = fopen( "/local/out.txt", "r");
techand 0:0936e2c7769b 179
techand 0:0936e2c7769b 180 if (fp==NULL) printf ("Error opening file");
techand 0:0936e2c7769b 181 else
techand 0:0936e2c7769b 182 {
techand 0:0936e2c7769b 183 do
techand 0:0936e2c7769b 184 {
techand 0:0936e2c7769b 185 c = fgetc (fp);
techand 0:0936e2c7769b 186 if (c == '<')
techand 0:0936e2c7769b 187 {
techand 0:0936e2c7769b 188 c = fgetc(fp);
techand 0:0936e2c7769b 189 if(c == 't')
techand 0:0936e2c7769b 190 {
techand 0:0936e2c7769b 191 c = fgetc(fp);
techand 0:0936e2c7769b 192 if(c == 'i')
techand 0:0936e2c7769b 193 {
techand 0:0936e2c7769b 194 c = fgetc(fp);
techand 0:0936e2c7769b 195 if(c == 't')
techand 0:0936e2c7769b 196 {
techand 0:0936e2c7769b 197 c = fgetc(fp);
techand 0:0936e2c7769b 198 if( c == 'l')
techand 0:0936e2c7769b 199 {
techand 0:0936e2c7769b 200 c = fgetc(fp);
techand 0:0936e2c7769b 201 if( c == 'e')
techand 0:0936e2c7769b 202 {
techand 0:0936e2c7769b 203 c = fgetc(fp);
techand 0:0936e2c7769b 204 if(c == '>')
techand 0:0936e2c7769b 205 {
techand 0:0936e2c7769b 206 count++;
techand 0:0936e2c7769b 207 if((count >=1) && (count <=10))
techand 0:0936e2c7769b 208 {
techand 0:0936e2c7769b 209 if(count == 11) break;
techand 0:0936e2c7769b 210 int i = 0;
techand 0:0936e2c7769b 211 c = fgetc(fp);
techand 0:0936e2c7769b 212 while(c != '<')
techand 0:0936e2c7769b 213 {
techand 0:0936e2c7769b 214 buffer[i] = c;
techand 0:0936e2c7769b 215 i++;
techand 0:0936e2c7769b 216 c = fgetc(fp);
techand 0:0936e2c7769b 217 }
techand 0:0936e2c7769b 218 buffer[i] = '\0';
techand 0:0936e2c7769b 219 lcd.printf("%s\n\r",buffer);
techand 0:0936e2c7769b 220 if (color == 0)
techand 0:0936e2c7769b 221 { lcd.printf("\x1b[29;23;24m"); //Set char to BLUE
techand 0:0936e2c7769b 222 color = 1;
techand 0:0936e2c7769b 223 } else {
techand 0:0936e2c7769b 224 lcd.printf("\x1b[29;18;24m"); //Set char to RED
techand 0:0936e2c7769b 225 color = 0;
techand 0:0936e2c7769b 226 }
techand 0:0936e2c7769b 227
techand 0:0936e2c7769b 228 }
techand 0:0936e2c7769b 229 }
techand 0:0936e2c7769b 230 }
techand 0:0936e2c7769b 231 }
techand 0:0936e2c7769b 232 }
techand 0:0936e2c7769b 233 }
techand 0:0936e2c7769b 234 }
techand 0:0936e2c7769b 235 }
techand 0:0936e2c7769b 236 }
techand 0:0936e2c7769b 237 while (count <= 11);
techand 0:0936e2c7769b 238 fclose (fp);
techand 0:0936e2c7769b 239 } // END OF else
techand 0:0936e2c7769b 240
techand 0:0936e2c7769b 241 } // END OF main