display recentest video in niconico.
Dependencies: EthernetInterface GraphicOLED NokiaLCD mbed-rtos mbed picojson
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "GraphicOLED.h" 00004 #include "MARMEX_OB_oled.h" 00005 #include "picojson.h" 00006 #include <string> 00007 00008 #define TEMP_FILE "/local/thumbtmp.bmp" 00009 00010 // for debug 00011 Serial pc(USBTX, USBRX); 00012 DigitalOut led1(LED1); 00013 DigitalOut led2(LED2); 00014 DigitalOut led3(LED3); 00015 DigitalOut led4(LED4); 00016 00017 // network 00018 EthernetInterface eth; 00019 DigitalIn lnk(P1_25); 00020 DigitalIn spd(P1_26); 00021 DigitalOut speed(p29); 00022 DigitalOut link(p30); 00023 00024 void flip(void const *args) { 00025 speed = !spd; 00026 link = !lnk; 00027 } 00028 00029 // display 00030 GraphicOLED oled(p24, p26, p27, p28, p21, p22); 00031 MARMEX_OB_oled oled_s(p5, p7, p8, p12, p11); // mosi, sclk, cs, rst, power_control 00032 00033 uint8_t c[3]; 00034 00035 LocalFileSystem local("local"); 00036 00037 const string SERVER_HOST = "XXXXX.herokuapp.com"; 00038 const int SERVER_PORT = 80; 00039 00040 const int CATEGORY_NUM = 27; 00041 const string NICO_CATEGORIES[CATEGORY_NUM] = { 00042 "ent", "music", "sing", "play", "dance", 00043 "vocaloid", "nicoindies", "animal", "cooking", "nature", 00044 "travel", "sport", "lecture", "drive", "history", 00045 "science", "tech", "handcraft", "make", "politics", 00046 "anime", "game", "toho", "imas", "radio", 00047 "draw", "diary" 00048 }; 00049 00050 string access_recent_api(const string host, const int port, string category) 00051 { 00052 TCPSocketConnection sock; 00053 sock.connect(host.c_str(), port); 00054 00055 char http_cmd[256]; 00056 sprintf(http_cmd, "GET /recent/%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", category.c_str(), host.c_str()); 00057 pc.printf("Request:\r\n%s\r\n", http_cmd); 00058 sock.send_all(http_cmd, sizeof(http_cmd)-1); 00059 00060 char buffer[1024]; 00061 int ret; 00062 while (true) { 00063 ret = sock.receive(buffer, sizeof(buffer)-1); 00064 if (ret <= 0) 00065 break; 00066 buffer[ret] = '\0'; 00067 pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); 00068 } 00069 00070 sock.close(); 00071 return string(buffer); 00072 } 00073 00074 void access_thumb_api(const string host, const int port, string cmsid) { 00075 TCPSocketConnection sock; 00076 sock.connect(host.c_str(), port); 00077 00078 char http_cmd[256]; 00079 sprintf(http_cmd, "GET /thumbnail/%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", cmsid.c_str(), host.c_str()); 00080 pc.printf("Request:\r\n%s\r\n", http_cmd); 00081 sock.send_all(http_cmd, sizeof(http_cmd)-1); 00082 00083 int ret = 0; 00084 int total_bytes = 0; 00085 char buffer[1024]; 00086 FILE* pFile; 00087 00088 pFile = fopen(TEMP_FILE, "w"); 00089 pc.printf("Opening File\n\r"); 00090 00091 bool found_start = false; 00092 00093 while (true) { 00094 ret = sock.receive(buffer, sizeof(buffer)-1); 00095 if (ret <= 0) { 00096 break; 00097 } 00098 buffer[ret] = '\0'; 00099 total_bytes += ret; 00100 //pc.printf("Received %d chars from server, total %d: %s\r\n", ret,total_bytes, buffer); 00101 if(found_start) { 00102 fwrite(buffer,1,ret,pFile); 00103 } else { 00104 for(int i=1; i<ret; i++) { 00105 if(buffer[i-1]==0x42 && buffer[i]==0x4D) { //bmp file starts from 424D 00106 pc.printf("found start.\n\r"); 00107 fwrite(&buffer[i-1],1,ret-i+1,pFile); 00108 found_start = true; 00109 break; 00110 } 00111 } 00112 } 00113 } 00114 00115 fclose (pFile); 00116 pc.printf("Closing File\n\r"); 00117 sock.close(); 00118 } 00119 00120 void display_bmp() { 00121 FILE *f = fopen(TEMP_FILE, "rb"); 00122 fseek(f, 54, 1); 00123 int color; 00124 for(int y=127; y >= 0; y--){ 00125 for(int x=0; x < 128; x++){ 00126 for(int i=0;i < 3;i++){ 00127 c[i] = fgetc(f); 00128 } 00129 color = c[0] | (c[1] << 8) | (c[2] << 16); 00130 oled_s.pixel(x, y, color); 00131 } 00132 } 00133 fclose(f); 00134 } 00135 00136 int main() 00137 { 00138 Thread::wait(2000); 00139 00140 RtosTimer flipper(flip, osTimerPeriodic, NULL); 00141 flipper.start(50); 00142 00143 oled.cls(); 00144 oled.printf("Hello World!\r\n"); 00145 oled_s.cls(); 00146 oled_s.background(0xFFFFFF); 00147 00148 eth.init(); 00149 eth.connect(); 00150 oled.cls(); 00151 oled.printf("IP Address: %s\r\n", eth.getIPAddress()); 00152 00153 string response, response_body; 00154 00155 while(1){ 00156 for(int i=0;i<CATEGORY_NUM;i++){ 00157 response = access_recent_api(SERVER_HOST, SERVER_PORT, NICO_CATEGORIES[i]); 00158 response_body = response.substr((int)response.find("\r\n\r\n") + 1); 00159 00160 picojson::value v; 00161 const char *json = response_body.c_str(); 00162 string err = picojson::parse(v, json, json + strlen(json)); 00163 string cmsid = v.get("cmsid").get<string>(); 00164 string category = v.get("category").get<string>(); 00165 string title = v.get("title").get<string>(); 00166 00167 access_thumb_api(SERVER_HOST, SERVER_PORT, cmsid); 00168 oled.cls(); 00169 oled.printf("%s|%s", category.c_str(), title.c_str()); 00170 display_bmp(); 00171 Thread::wait(5000); 00172 } 00173 } 00174 00175 //eth.disconnect(); 00176 }
Generated on Thu Jul 14 2022 00:37:45 by 1.7.2