display recentest video in niconico.
Dependencies: EthernetInterface GraphicOLED NokiaLCD mbed-rtos mbed picojson
main.cpp@1:19589c30cc74, 2014-12-17 (annotated)
- Committer:
- mia_0032
- Date:
- Wed Dec 17 13:19:28 2014 +0000
- Revision:
- 1:19589c30cc74
- Parent:
- 0:1e0d84ef27ec
fix comment.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mia_0032 | 0:1e0d84ef27ec | 1 | #include "mbed.h" |
mia_0032 | 0:1e0d84ef27ec | 2 | #include "EthernetInterface.h" |
mia_0032 | 0:1e0d84ef27ec | 3 | #include "GraphicOLED.h" |
mia_0032 | 0:1e0d84ef27ec | 4 | #include "MARMEX_OB_oled.h" |
mia_0032 | 0:1e0d84ef27ec | 5 | #include "picojson.h" |
mia_0032 | 0:1e0d84ef27ec | 6 | #include <string> |
mia_0032 | 0:1e0d84ef27ec | 7 | |
mia_0032 | 0:1e0d84ef27ec | 8 | #define TEMP_FILE "/local/thumbtmp.bmp" |
mia_0032 | 0:1e0d84ef27ec | 9 | |
mia_0032 | 0:1e0d84ef27ec | 10 | // for debug |
mia_0032 | 0:1e0d84ef27ec | 11 | Serial pc(USBTX, USBRX); |
mia_0032 | 0:1e0d84ef27ec | 12 | DigitalOut led1(LED1); |
mia_0032 | 0:1e0d84ef27ec | 13 | DigitalOut led2(LED2); |
mia_0032 | 0:1e0d84ef27ec | 14 | DigitalOut led3(LED3); |
mia_0032 | 0:1e0d84ef27ec | 15 | DigitalOut led4(LED4); |
mia_0032 | 0:1e0d84ef27ec | 16 | |
mia_0032 | 0:1e0d84ef27ec | 17 | // network |
mia_0032 | 0:1e0d84ef27ec | 18 | EthernetInterface eth; |
mia_0032 | 0:1e0d84ef27ec | 19 | DigitalIn lnk(P1_25); |
mia_0032 | 0:1e0d84ef27ec | 20 | DigitalIn spd(P1_26); |
mia_0032 | 0:1e0d84ef27ec | 21 | DigitalOut speed(p29); |
mia_0032 | 0:1e0d84ef27ec | 22 | DigitalOut link(p30); |
mia_0032 | 0:1e0d84ef27ec | 23 | |
mia_0032 | 0:1e0d84ef27ec | 24 | void flip(void const *args) { |
mia_0032 | 0:1e0d84ef27ec | 25 | speed = !spd; |
mia_0032 | 0:1e0d84ef27ec | 26 | link = !lnk; |
mia_0032 | 0:1e0d84ef27ec | 27 | } |
mia_0032 | 0:1e0d84ef27ec | 28 | |
mia_0032 | 0:1e0d84ef27ec | 29 | // display |
mia_0032 | 0:1e0d84ef27ec | 30 | GraphicOLED oled(p24, p26, p27, p28, p21, p22); |
mia_0032 | 0:1e0d84ef27ec | 31 | MARMEX_OB_oled oled_s(p5, p7, p8, p12, p11); // mosi, sclk, cs, rst, power_control |
mia_0032 | 0:1e0d84ef27ec | 32 | |
mia_0032 | 0:1e0d84ef27ec | 33 | uint8_t c[3]; |
mia_0032 | 0:1e0d84ef27ec | 34 | |
mia_0032 | 0:1e0d84ef27ec | 35 | LocalFileSystem local("local"); |
mia_0032 | 0:1e0d84ef27ec | 36 | |
mia_0032 | 0:1e0d84ef27ec | 37 | const string SERVER_HOST = "XXXXX.herokuapp.com"; |
mia_0032 | 0:1e0d84ef27ec | 38 | const int SERVER_PORT = 80; |
mia_0032 | 0:1e0d84ef27ec | 39 | |
mia_0032 | 0:1e0d84ef27ec | 40 | const int CATEGORY_NUM = 27; |
mia_0032 | 0:1e0d84ef27ec | 41 | const string NICO_CATEGORIES[CATEGORY_NUM] = { |
mia_0032 | 0:1e0d84ef27ec | 42 | "ent", "music", "sing", "play", "dance", |
mia_0032 | 0:1e0d84ef27ec | 43 | "vocaloid", "nicoindies", "animal", "cooking", "nature", |
mia_0032 | 0:1e0d84ef27ec | 44 | "travel", "sport", "lecture", "drive", "history", |
mia_0032 | 0:1e0d84ef27ec | 45 | "science", "tech", "handcraft", "make", "politics", |
mia_0032 | 0:1e0d84ef27ec | 46 | "anime", "game", "toho", "imas", "radio", |
mia_0032 | 0:1e0d84ef27ec | 47 | "draw", "diary" |
mia_0032 | 0:1e0d84ef27ec | 48 | }; |
mia_0032 | 0:1e0d84ef27ec | 49 | |
mia_0032 | 0:1e0d84ef27ec | 50 | string access_recent_api(const string host, const int port, string category) |
mia_0032 | 0:1e0d84ef27ec | 51 | { |
mia_0032 | 0:1e0d84ef27ec | 52 | TCPSocketConnection sock; |
mia_0032 | 0:1e0d84ef27ec | 53 | sock.connect(host.c_str(), port); |
mia_0032 | 0:1e0d84ef27ec | 54 | |
mia_0032 | 0:1e0d84ef27ec | 55 | char http_cmd[256]; |
mia_0032 | 0:1e0d84ef27ec | 56 | 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()); |
mia_0032 | 0:1e0d84ef27ec | 57 | pc.printf("Request:\r\n%s\r\n", http_cmd); |
mia_0032 | 0:1e0d84ef27ec | 58 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
mia_0032 | 0:1e0d84ef27ec | 59 | |
mia_0032 | 0:1e0d84ef27ec | 60 | char buffer[1024]; |
mia_0032 | 0:1e0d84ef27ec | 61 | int ret; |
mia_0032 | 0:1e0d84ef27ec | 62 | while (true) { |
mia_0032 | 0:1e0d84ef27ec | 63 | ret = sock.receive(buffer, sizeof(buffer)-1); |
mia_0032 | 0:1e0d84ef27ec | 64 | if (ret <= 0) |
mia_0032 | 0:1e0d84ef27ec | 65 | break; |
mia_0032 | 0:1e0d84ef27ec | 66 | buffer[ret] = '\0'; |
mia_0032 | 0:1e0d84ef27ec | 67 | pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer); |
mia_0032 | 0:1e0d84ef27ec | 68 | } |
mia_0032 | 0:1e0d84ef27ec | 69 | |
mia_0032 | 0:1e0d84ef27ec | 70 | sock.close(); |
mia_0032 | 0:1e0d84ef27ec | 71 | return string(buffer); |
mia_0032 | 0:1e0d84ef27ec | 72 | } |
mia_0032 | 0:1e0d84ef27ec | 73 | |
mia_0032 | 0:1e0d84ef27ec | 74 | void access_thumb_api(const string host, const int port, string cmsid) { |
mia_0032 | 0:1e0d84ef27ec | 75 | TCPSocketConnection sock; |
mia_0032 | 0:1e0d84ef27ec | 76 | sock.connect(host.c_str(), port); |
mia_0032 | 0:1e0d84ef27ec | 77 | |
mia_0032 | 0:1e0d84ef27ec | 78 | char http_cmd[256]; |
mia_0032 | 0:1e0d84ef27ec | 79 | 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()); |
mia_0032 | 0:1e0d84ef27ec | 80 | pc.printf("Request:\r\n%s\r\n", http_cmd); |
mia_0032 | 0:1e0d84ef27ec | 81 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
mia_0032 | 0:1e0d84ef27ec | 82 | |
mia_0032 | 0:1e0d84ef27ec | 83 | int ret = 0; |
mia_0032 | 0:1e0d84ef27ec | 84 | int total_bytes = 0; |
mia_0032 | 0:1e0d84ef27ec | 85 | char buffer[1024]; |
mia_0032 | 0:1e0d84ef27ec | 86 | FILE* pFile; |
mia_0032 | 0:1e0d84ef27ec | 87 | |
mia_0032 | 0:1e0d84ef27ec | 88 | pFile = fopen(TEMP_FILE, "w"); |
mia_0032 | 0:1e0d84ef27ec | 89 | pc.printf("Opening File\n\r"); |
mia_0032 | 0:1e0d84ef27ec | 90 | |
mia_0032 | 0:1e0d84ef27ec | 91 | bool found_start = false; |
mia_0032 | 0:1e0d84ef27ec | 92 | |
mia_0032 | 0:1e0d84ef27ec | 93 | while (true) { |
mia_0032 | 0:1e0d84ef27ec | 94 | ret = sock.receive(buffer, sizeof(buffer)-1); |
mia_0032 | 0:1e0d84ef27ec | 95 | if (ret <= 0) { |
mia_0032 | 0:1e0d84ef27ec | 96 | break; |
mia_0032 | 0:1e0d84ef27ec | 97 | } |
mia_0032 | 0:1e0d84ef27ec | 98 | buffer[ret] = '\0'; |
mia_0032 | 0:1e0d84ef27ec | 99 | total_bytes += ret; |
mia_0032 | 0:1e0d84ef27ec | 100 | //pc.printf("Received %d chars from server, total %d: %s\r\n", ret,total_bytes, buffer); |
mia_0032 | 0:1e0d84ef27ec | 101 | if(found_start) { |
mia_0032 | 0:1e0d84ef27ec | 102 | fwrite(buffer,1,ret,pFile); |
mia_0032 | 0:1e0d84ef27ec | 103 | } else { |
mia_0032 | 0:1e0d84ef27ec | 104 | for(int i=1; i<ret; i++) { |
mia_0032 | 1:19589c30cc74 | 105 | if(buffer[i-1]==0x42 && buffer[i]==0x4D) { //bmp file starts from 424D |
mia_0032 | 0:1e0d84ef27ec | 106 | pc.printf("found start.\n\r"); |
mia_0032 | 0:1e0d84ef27ec | 107 | fwrite(&buffer[i-1],1,ret-i+1,pFile); |
mia_0032 | 0:1e0d84ef27ec | 108 | found_start = true; |
mia_0032 | 0:1e0d84ef27ec | 109 | break; |
mia_0032 | 0:1e0d84ef27ec | 110 | } |
mia_0032 | 0:1e0d84ef27ec | 111 | } |
mia_0032 | 0:1e0d84ef27ec | 112 | } |
mia_0032 | 0:1e0d84ef27ec | 113 | } |
mia_0032 | 0:1e0d84ef27ec | 114 | |
mia_0032 | 0:1e0d84ef27ec | 115 | fclose (pFile); |
mia_0032 | 0:1e0d84ef27ec | 116 | pc.printf("Closing File\n\r"); |
mia_0032 | 0:1e0d84ef27ec | 117 | sock.close(); |
mia_0032 | 0:1e0d84ef27ec | 118 | } |
mia_0032 | 0:1e0d84ef27ec | 119 | |
mia_0032 | 0:1e0d84ef27ec | 120 | void display_bmp() { |
mia_0032 | 0:1e0d84ef27ec | 121 | FILE *f = fopen(TEMP_FILE, "rb"); |
mia_0032 | 0:1e0d84ef27ec | 122 | fseek(f, 54, 1); |
mia_0032 | 0:1e0d84ef27ec | 123 | int color; |
mia_0032 | 0:1e0d84ef27ec | 124 | for(int y=127; y >= 0; y--){ |
mia_0032 | 0:1e0d84ef27ec | 125 | for(int x=0; x < 128; x++){ |
mia_0032 | 0:1e0d84ef27ec | 126 | for(int i=0;i < 3;i++){ |
mia_0032 | 0:1e0d84ef27ec | 127 | c[i] = fgetc(f); |
mia_0032 | 0:1e0d84ef27ec | 128 | } |
mia_0032 | 0:1e0d84ef27ec | 129 | color = c[0] | (c[1] << 8) | (c[2] << 16); |
mia_0032 | 0:1e0d84ef27ec | 130 | oled_s.pixel(x, y, color); |
mia_0032 | 0:1e0d84ef27ec | 131 | } |
mia_0032 | 0:1e0d84ef27ec | 132 | } |
mia_0032 | 0:1e0d84ef27ec | 133 | fclose(f); |
mia_0032 | 0:1e0d84ef27ec | 134 | } |
mia_0032 | 0:1e0d84ef27ec | 135 | |
mia_0032 | 0:1e0d84ef27ec | 136 | int main() |
mia_0032 | 0:1e0d84ef27ec | 137 | { |
mia_0032 | 0:1e0d84ef27ec | 138 | Thread::wait(2000); |
mia_0032 | 0:1e0d84ef27ec | 139 | |
mia_0032 | 0:1e0d84ef27ec | 140 | RtosTimer flipper(flip, osTimerPeriodic, NULL); |
mia_0032 | 0:1e0d84ef27ec | 141 | flipper.start(50); |
mia_0032 | 0:1e0d84ef27ec | 142 | |
mia_0032 | 0:1e0d84ef27ec | 143 | oled.cls(); |
mia_0032 | 0:1e0d84ef27ec | 144 | oled.printf("Hello World!\r\n"); |
mia_0032 | 0:1e0d84ef27ec | 145 | oled_s.cls(); |
mia_0032 | 0:1e0d84ef27ec | 146 | oled_s.background(0xFFFFFF); |
mia_0032 | 0:1e0d84ef27ec | 147 | |
mia_0032 | 0:1e0d84ef27ec | 148 | eth.init(); |
mia_0032 | 0:1e0d84ef27ec | 149 | eth.connect(); |
mia_0032 | 0:1e0d84ef27ec | 150 | oled.cls(); |
mia_0032 | 0:1e0d84ef27ec | 151 | oled.printf("IP Address: %s\r\n", eth.getIPAddress()); |
mia_0032 | 0:1e0d84ef27ec | 152 | |
mia_0032 | 0:1e0d84ef27ec | 153 | string response, response_body; |
mia_0032 | 0:1e0d84ef27ec | 154 | |
mia_0032 | 0:1e0d84ef27ec | 155 | while(1){ |
mia_0032 | 0:1e0d84ef27ec | 156 | for(int i=0;i<CATEGORY_NUM;i++){ |
mia_0032 | 0:1e0d84ef27ec | 157 | response = access_recent_api(SERVER_HOST, SERVER_PORT, NICO_CATEGORIES[i]); |
mia_0032 | 0:1e0d84ef27ec | 158 | response_body = response.substr((int)response.find("\r\n\r\n") + 1); |
mia_0032 | 0:1e0d84ef27ec | 159 | |
mia_0032 | 0:1e0d84ef27ec | 160 | picojson::value v; |
mia_0032 | 0:1e0d84ef27ec | 161 | const char *json = response_body.c_str(); |
mia_0032 | 0:1e0d84ef27ec | 162 | string err = picojson::parse(v, json, json + strlen(json)); |
mia_0032 | 0:1e0d84ef27ec | 163 | string cmsid = v.get("cmsid").get<string>(); |
mia_0032 | 0:1e0d84ef27ec | 164 | string category = v.get("category").get<string>(); |
mia_0032 | 0:1e0d84ef27ec | 165 | string title = v.get("title").get<string>(); |
mia_0032 | 0:1e0d84ef27ec | 166 | |
mia_0032 | 0:1e0d84ef27ec | 167 | access_thumb_api(SERVER_HOST, SERVER_PORT, cmsid); |
mia_0032 | 0:1e0d84ef27ec | 168 | oled.cls(); |
mia_0032 | 0:1e0d84ef27ec | 169 | oled.printf("%s|%s", category.c_str(), title.c_str()); |
mia_0032 | 0:1e0d84ef27ec | 170 | display_bmp(); |
mia_0032 | 0:1e0d84ef27ec | 171 | Thread::wait(5000); |
mia_0032 | 0:1e0d84ef27ec | 172 | } |
mia_0032 | 0:1e0d84ef27ec | 173 | } |
mia_0032 | 0:1e0d84ef27ec | 174 | |
mia_0032 | 0:1e0d84ef27ec | 175 | //eth.disconnect(); |
mia_0032 | 0:1e0d84ef27ec | 176 | } |