test public
Dependencies: HttpServer_snapshot_mbed-os
main.cpp
00001 #include "mbed.h" 00002 #include "EasyAttach_CameraAndLCD.h" 00003 #include "dcache-control.h" 00004 #include "JPEG_Converter.h" 00005 #include "HTTPServer.h" 00006 #include "mbed_rpc.h" 00007 #include "FATFileSystem.h" 00008 #include "RomRamBlockDevice.h" 00009 #include "SDBlockDevice_GRBoard.h" 00010 #if defined(TARGET_RZ_A1H) 00011 #include "file_table_peach.h" //Binary data of web pages 00012 #elif defined(TARGET_GR_LYCHEE) 00013 #include "file_table_lychee.h" //Binary data of web pages 00014 #endif 00015 #include "i2c_setting.h" 00016 00017 /**** User Selection *********/ 00018 /** Network setting **/ 00019 #define USE_DHCP (1) /* Select 0(static configuration) or 1(use DHCP) */ 00020 #if (USE_DHCP == 0) 00021 #define IP_ADDRESS ("192.168.0.2") /* IP address */ 00022 #define SUBNET_MASK ("255.255.255.0") /* Subnet mask */ 00023 #define DEFAULT_GATEWAY ("192.168.0.3") /* Default gateway */ 00024 #endif 00025 #define NETWORK_TYPE (2) /* Select 0(Ethernet), 1(reserved), 2(ESP32 STA) ,3(ESP32 AP) */ 00026 #if (NETWORK_TYPE >= 2) 00027 #define SCAN_NETWORK (1) /* Select 0(Use WLAN_SSID, WLAN_PSK, WLAN_SECURITY) or 1(To select a network using the terminal.) */ 00028 #define WLAN_SSID ("SSIDofYourAP") /* SSID */ 00029 #define WLAN_PSK ("PSKofYourAP") /* PSK(Pre-Shared Key) */ 00030 #define WLAN_SECURITY NSAPI_SECURITY_WPA_WPA2 /* NSAPI_SECURITY_NONE, NSAPI_SECURITY_WEP, NSAPI_SECURITY_WPA, NSAPI_SECURITY_WPA2 or NSAPI_SECURITY_WPA_WPA2 */ 00031 #endif 00032 /** JPEG out setting **/ 00033 #define JPEG_ENCODE_QUALITY (75) /* JPEG encode quality (min:1, max:75 (Considering the size of JpegBuffer, about 75 is the upper limit.)) */ 00034 #define VFIELD_INT_SKIP_CNT (0) /* A guide for GR-LYCHEE. 0:60fps, 1:30fps, 2:20fps, 3:15fps, 4:12fps, 5:10fps */ 00035 /*****************************/ 00036 00037 #if (NETWORK_TYPE == 0) 00038 #include "EthernetInterface.h" 00039 EthernetInterface network; 00040 #elif (NETWORK_TYPE == 2) 00041 #include "ESP32Interface.h" 00042 ESP32Interface network; 00043 #elif (NETWORK_TYPE == 3) 00044 #include "ESP32InterfaceAP.h" 00045 ESP32InterfaceAP network; 00046 #else 00047 #error NETWORK_TYPE error 00048 #endif /* NETWORK_TYPE */ 00049 00050 /* Video input and LCD layer 0 output */ 00051 #define VIDEO_FORMAT (DisplayBase::VIDEO_FORMAT_YCBCR422) 00052 #define GRAPHICS_FORMAT (DisplayBase::GRAPHICS_FORMAT_YCBCR422) 00053 #define WR_RD_WRSWA (DisplayBase::WR_RD_WRSWA_32_16BIT) 00054 #define DATA_SIZE_PER_PIC (2u) 00055 00056 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128 00057 in accordance with the frame buffer burst transfer mode. */ 00058 #define VIDEO_PIXEL_HW (320u) /* QVGA */ 00059 #define VIDEO_PIXEL_VW (240u) /* QVGA */ 00060 00061 #define FRAME_BUFFER_STRIDE (((VIDEO_PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u) 00062 #define FRAME_BUFFER_HEIGHT (VIDEO_PIXEL_VW) 00063 00064 DisplayBase Display; 00065 00066 #if defined(__ICCARM__) 00067 #pragma data_alignment=32 00068 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]@ ".mirrorram"; 00069 #else 00070 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(32))); 00071 #endif 00072 00073 FATFileSystem fs("storage"); 00074 RomRamBlockDevice romram_bd(512000, 512); 00075 SDBlockDevice_GRBoard sd; 00076 Serial pc(USBTX, USBRX); 00077 Thread sdConnectTask; 00078 00079 #if defined(__ICCARM__) 00080 #pragma data_alignment=32 00081 static uint8_t JpegBuffer[2][1024 * 64]; 00082 #else 00083 static uint8_t JpegBuffer[2][1024 * 64]__attribute((aligned(32))); 00084 #endif 00085 static size_t jcu_encode_size[2]; 00086 static int image_change = 0; 00087 JPEG_Converter Jcu; 00088 static int jcu_buf_index_write = 0; 00089 static int jcu_buf_index_write_done = 0; 00090 static int jcu_buf_index_read = 0; 00091 static int jcu_encoding = 0; 00092 static int Vfield_Int_Cnt = 0; 00093 static char i2c_setting_str_buf[I2C_SETTING_STR_BUF_SIZE]; 00094 00095 static void JcuEncodeCallBackFunc(JPEG_Converter::jpeg_conv_error_t err_code) { 00096 if (err_code == JPEG_Converter::JPEG_CONV_OK ) { 00097 jcu_buf_index_write_done = jcu_buf_index_write; 00098 image_change = 1; 00099 } 00100 jcu_encoding = 0; 00101 } 00102 00103 static int snapshot_req(const char ** pp_data) { 00104 int encode_size; 00105 00106 while ((jcu_encoding == 1) || (image_change == 0)) { 00107 ThisThread::sleep_for(1); 00108 } 00109 jcu_buf_index_read = jcu_buf_index_write_done; 00110 image_change = 0; 00111 00112 *pp_data = (const char *)JpegBuffer[jcu_buf_index_read]; 00113 encode_size = (int)jcu_encode_size[jcu_buf_index_read]; 00114 00115 return encode_size; 00116 } 00117 00118 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type) { 00119 if (Vfield_Int_Cnt < VFIELD_INT_SKIP_CNT) { 00120 Vfield_Int_Cnt++; 00121 return; 00122 } 00123 Vfield_Int_Cnt = 0; 00124 00125 //Interrupt callback function 00126 if (jcu_encoding == 0) { 00127 JPEG_Converter::bitmap_buff_info_t bitmap_buff_info; 00128 JPEG_Converter::encode_options_t encode_options; 00129 00130 bitmap_buff_info.width = VIDEO_PIXEL_HW; 00131 bitmap_buff_info.height = VIDEO_PIXEL_VW; 00132 bitmap_buff_info.format = JPEG_Converter::WR_RD_YCbCr422 ; 00133 bitmap_buff_info.buffer_address = (void *)user_frame_buffer0; 00134 00135 encode_options.encode_buff_size = sizeof(JpegBuffer[0]); 00136 encode_options.p_EncodeCallBackFunc = &JcuEncodeCallBackFunc; 00137 encode_options.input_swapsetting = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT ; 00138 00139 jcu_encoding = 1; 00140 if (jcu_buf_index_read == jcu_buf_index_write) { 00141 jcu_buf_index_write ^= 1; // toggle 00142 } 00143 jcu_encode_size[jcu_buf_index_write] = 0; 00144 dcache_invalid(JpegBuffer[jcu_buf_index_write], sizeof(JpegBuffer[0])); 00145 if (Jcu.encode(&bitmap_buff_info, JpegBuffer[jcu_buf_index_write], 00146 &jcu_encode_size[jcu_buf_index_write], &encode_options) != JPEG_Converter::JPEG_CONV_OK ) { 00147 jcu_encode_size[jcu_buf_index_write] = 0; 00148 jcu_encoding = 0; 00149 } 00150 } 00151 } 00152 00153 static void Start_Video_Camera(void) { 00154 // Video capture setting (progressive form fixed) 00155 Display.Video_Write_Setting( 00156 DisplayBase::VIDEO_INPUT_CHANNEL_0 , 00157 DisplayBase::COL_SYS_NTSC_358 , 00158 (void *)user_frame_buffer0, 00159 FRAME_BUFFER_STRIDE, 00160 VIDEO_FORMAT, 00161 WR_RD_WRSWA, 00162 VIDEO_PIXEL_VW, 00163 VIDEO_PIXEL_HW 00164 ); 00165 EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0 ); 00166 } 00167 00168 static void TerminalWrite(Arguments* arg, Reply* r) { 00169 if ((arg != NULL) && (r != NULL)) { 00170 for (int i = 0; i < arg->argc; i++) { 00171 if (arg->argv[i] != NULL) { 00172 printf("%s", arg->argv[i]); 00173 } 00174 } 00175 printf("\n"); 00176 r->putData<const char*>("ok"); 00177 } 00178 } 00179 00180 static void mount_romramfs(void) { 00181 FILE * fp; 00182 00183 romram_bd.SetRomAddr(0x18000000, 0x1FFFFFFF); 00184 fs.format(&romram_bd, 512); 00185 fs.mount(&romram_bd); 00186 00187 //index.htm 00188 fp = fopen("/storage/index.htm", "w"); 00189 fwrite(index_htm_tbl, sizeof(char), sizeof(index_htm_tbl), fp); 00190 fclose(fp); 00191 00192 //camera.js 00193 fp = fopen("/storage/camera.js", "w"); 00194 fwrite(camaera_js_tbl, sizeof(char), sizeof(camaera_js_tbl), fp); 00195 fclose(fp); 00196 00197 //camera.htm 00198 fp = fopen("/storage/camera.htm", "w"); 00199 fwrite(camera_htm_tbl, sizeof(char), sizeof(camera_htm_tbl), fp); 00200 fclose(fp); 00201 00202 //mbedrpc.js 00203 fp = fopen("/storage/mbedrpc.js", "w"); 00204 fwrite(mbedrpc_js_tbl, sizeof(char), sizeof(mbedrpc_js_tbl), fp); 00205 fclose(fp); 00206 00207 //led.htm 00208 fp = fopen("/storage/led.htm", "w"); 00209 fwrite(led_htm_tbl, sizeof(char), sizeof(led_htm_tbl), fp); 00210 fclose(fp); 00211 00212 //i2c_set.htm 00213 fp = fopen("/storage/i2c_set.htm", "w"); 00214 fwrite(i2c_set_htm_tbl, sizeof(char), sizeof(i2c_set_htm_tbl), fp); 00215 fclose(fp); 00216 00217 //web_top.htm 00218 fp = fopen("/storage/web_top.htm", "w"); 00219 fwrite(web_top_htm_tbl, sizeof(char), sizeof(web_top_htm_tbl), fp); 00220 fclose(fp); 00221 00222 //menu.htm 00223 fp = fopen("/storage/menu.htm", "w"); 00224 fwrite(menu_htm_tbl, sizeof(char), sizeof(menu_htm_tbl), fp); 00225 fclose(fp); 00226 00227 //window.htm 00228 fp = fopen("/storage/window.htm", "w"); 00229 fwrite(window_htm_tbl, sizeof(char), sizeof(window_htm_tbl), fp); 00230 fclose(fp); 00231 } 00232 00233 static void SetI2CfromWeb(Arguments* arg, Reply* r) { 00234 int result = 0; 00235 00236 if (arg != NULL) { 00237 if (arg->argc >= 2) { 00238 if ((arg->argv[0] != NULL) && (arg->argv[1] != NULL)) { 00239 sprintf(i2c_setting_str_buf, "%s,%s", arg->argv[0], arg->argv[1]); 00240 result = 1; 00241 } 00242 } else if (arg->argc == 1) { 00243 if (arg->argv[0] != NULL) { 00244 sprintf(i2c_setting_str_buf, "%s", arg->argv[0]); 00245 result = 1; 00246 } 00247 } else { 00248 /* Do nothing */ 00249 } 00250 /* command analysis and execute */ 00251 if (result != 0) { 00252 if (i2c_setting_exe(i2c_setting_str_buf) != false) { 00253 r->putData<const char*>(i2c_setting_str_buf); 00254 } 00255 } 00256 } 00257 } 00258 00259 #if (SCAN_NETWORK == 1) && (NETWORK_TYPE != 3) 00260 static const char *sec2str(nsapi_security_t sec) { 00261 switch (sec) { 00262 case NSAPI_SECURITY_NONE: 00263 return "None"; 00264 case NSAPI_SECURITY_WEP: 00265 return "WEP"; 00266 case NSAPI_SECURITY_WPA: 00267 return "WPA"; 00268 case NSAPI_SECURITY_WPA2: 00269 return "WPA2"; 00270 case NSAPI_SECURITY_WPA_WPA2: 00271 return "WPA/WPA2"; 00272 case NSAPI_SECURITY_UNKNOWN: 00273 default: 00274 return "Unknown"; 00275 } 00276 } 00277 00278 static bool scan_network(WiFiInterface *wifi) { 00279 WiFiAccessPoint *ap; 00280 bool ret = false; 00281 int i; 00282 int count = 10; /* Limit number of network arbitrary to 10 */ 00283 00284 printf("Scan:\r\n"); 00285 ap = new WiFiAccessPoint[count]; 00286 if (ap == NULL) { 00287 printf("memory error\r\n"); 00288 return 0; 00289 } 00290 count = wifi->scan(ap, count); 00291 for (i = 0; i < count; i++) { 00292 printf("No.%d Network: %s secured: %s BSSID: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx RSSI: %hhd Ch: %hhd\r\n", 00293 i, ap[i].get_ssid(), sec2str(ap[i].get_security()), 00294 ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], ap[i].get_bssid()[3], 00295 ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); 00296 } 00297 printf("%d networks available.\r\n", count); 00298 00299 if (count > 0) { 00300 char c; 00301 char pass[64]; 00302 int select_no; 00303 bool loop_break = false;; 00304 00305 printf("\nPlease enter the number of the network you want to connect.\r\n"); 00306 printf("Enter key:[0]-[%d], (If inputting the other key, it's scanned again.)\r\n", count - 1); 00307 c = (uint8_t)pc.getc(); 00308 select_no = c - 0x30; 00309 if ((select_no >= 0) && (select_no < count)) { 00310 printf("[%s] is selected.\r\n", ap[select_no].get_ssid()); 00311 printf("Please enter the PSK.\r\n"); 00312 i = 0; 00313 while (loop_break == false) { 00314 c = (uint8_t)pc.getc(); 00315 switch (c) { 00316 case 0x0D: 00317 pass[i] = '\0'; 00318 pc.puts("\r\n"); 00319 loop_break = true; 00320 break; 00321 case 0x08: 00322 if (i > 0) { 00323 pc.puts("\b \b"); 00324 i--; 00325 } 00326 break; 00327 case 0x0A: 00328 break; 00329 default: 00330 if ((i + 1) < sizeof(pass)) { 00331 pass[i] = c; 00332 i++; 00333 pc.putc(c); 00334 } 00335 break; 00336 } 00337 } 00338 wifi->set_credentials(ap[select_no].get_ssid(), pass, ap[select_no].get_security()); 00339 ret = true; 00340 } 00341 } 00342 00343 delete[] ap; 00344 00345 return ret; 00346 } 00347 #endif 00348 00349 static void sd_connect_task(void) { 00350 int storage_type = 0; 00351 00352 while (1) { 00353 if (storage_type == 0) { 00354 if (sd.connect()) { 00355 fs.unmount(); 00356 fs.mount(&sd); 00357 storage_type = 1; 00358 printf("SDBlockDevice\r\n"); 00359 } 00360 } else { 00361 if (sd.connected() == false) { 00362 fs.unmount(); 00363 fs.mount(&romram_bd); 00364 storage_type = 0; 00365 printf("RomRamBlockDevice\r\n"); 00366 } 00367 } 00368 ThisThread::sleep_for(250); 00369 } 00370 } 00371 00372 int main(void) { 00373 printf("********* PROGRAM START ***********\r\n"); 00374 00375 mount_romramfs(); //RomRamFileSystem Mount 00376 00377 sdConnectTask.start(&sd_connect_task); 00378 00379 EasyAttach_Init(Display); 00380 Jcu.SetQuality(JPEG_ENCODE_QUALITY); 00381 // Interrupt callback function setting (Field end signal for recording function in scaler 0) 00382 Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VFIELD , 0, IntCallbackFunc_Vfield); 00383 Start_Video_Camera(); 00384 00385 RPC::add_rpc_class<RpcDigitalOut>(); 00386 RPCFunction rpcFunc(TerminalWrite, "TerminalWrite"); 00387 RPCFunction rpcSetI2C(SetI2CfromWeb, "SetI2CfromWeb"); 00388 00389 printf("Network Setting up...\r\n"); 00390 #if (USE_DHCP == 0) 00391 network.set_dhcp(false); 00392 if (network.set_network(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY) != 0) { //for Static IP Address (IPAddress, NetMasks, Gateway) 00393 printf("Network Set Network Error \r\n"); 00394 } 00395 #endif 00396 00397 #if (NETWORK_TYPE >= 2) 00398 #if (SCAN_NETWORK == 1) && (NETWORK_TYPE != 3) 00399 while (!scan_network(&network)); 00400 #else 00401 network.set_credentials(WLAN_SSID, WLAN_PSK, WLAN_SECURITY); 00402 #endif 00403 #endif 00404 00405 printf("\r\nConnecting...\r\n"); 00406 if (network.connect() != 0) { 00407 printf("Network Connect Error \r\n"); 00408 return -1; 00409 } 00410 printf("MAC Address is %s\r\n", network.get_mac_address()); 00411 printf("IP Address is %s\r\n", network.get_ip_address()); 00412 printf("NetMask is %s\r\n", network.get_netmask()); 00413 printf("Gateway Address is %s\r\n", network.get_gateway()); 00414 printf("Network Setup OK\r\n"); 00415 00416 SnapshotHandler::attach_req(&snapshot_req); 00417 HTTPServerAddHandler<SnapshotHandler>("/camera"); //Camera 00418 FSHandler::mount("/storage", "/"); 00419 HTTPServerAddHandler<FSHandler>("/"); 00420 HTTPServerAddHandler<RPCHandler>("/rpc"); 00421 HTTPServerStart(&network, 80); 00422 }
Generated on Wed Jul 13 2022 05:33:36 by 1.7.2