Web Camera for mbed-os. This sample works on GR-LYCHEE besides GR-PEACH. When you use this program, we judge you have agreed to the following contents. https://developer.mbed.org/teams/Renesas/wiki/About-LICENSE
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 //favicon.ico 00233 fp = fopen("/storage/favicon.ico", "w"); 00234 fwrite(favicon_ico_tbl, sizeof(char), sizeof(favicon_ico_tbl), fp); 00235 fclose(fp); 00236 } 00237 00238 static void SetI2CfromWeb(Arguments* arg, Reply* r) { 00239 int result = 0; 00240 00241 if (arg != NULL) { 00242 if (arg->argc >= 2) { 00243 if ((arg->argv[0] != NULL) && (arg->argv[1] != NULL)) { 00244 sprintf(i2c_setting_str_buf, "%s,%s", arg->argv[0], arg->argv[1]); 00245 result = 1; 00246 } 00247 } else if (arg->argc == 1) { 00248 if (arg->argv[0] != NULL) { 00249 sprintf(i2c_setting_str_buf, "%s", arg->argv[0]); 00250 result = 1; 00251 } 00252 } else { 00253 /* Do nothing */ 00254 } 00255 /* command analysis and execute */ 00256 if (result != 0) { 00257 if (i2c_setting_exe(i2c_setting_str_buf) != false) { 00258 r->putData<const char*>(i2c_setting_str_buf); 00259 } 00260 } 00261 } 00262 } 00263 00264 #if (SCAN_NETWORK == 1) && (NETWORK_TYPE != 3) 00265 static const char *sec2str(nsapi_security_t sec) { 00266 switch (sec) { 00267 case NSAPI_SECURITY_NONE: 00268 return "None"; 00269 case NSAPI_SECURITY_WEP: 00270 return "WEP"; 00271 case NSAPI_SECURITY_WPA: 00272 return "WPA"; 00273 case NSAPI_SECURITY_WPA2: 00274 return "WPA2"; 00275 case NSAPI_SECURITY_WPA_WPA2: 00276 return "WPA/WPA2"; 00277 case NSAPI_SECURITY_UNKNOWN: 00278 default: 00279 return "Unknown"; 00280 } 00281 } 00282 00283 static bool scan_network(WiFiInterface *wifi) { 00284 WiFiAccessPoint *ap; 00285 bool ret = false; 00286 int i; 00287 int count = 10; /* Limit number of network arbitrary to 10 */ 00288 00289 printf("Scan:\r\n"); 00290 ap = new WiFiAccessPoint[count]; 00291 if (ap == NULL) { 00292 printf("memory error\r\n"); 00293 return 0; 00294 } 00295 count = wifi->scan(ap, count); 00296 for (i = 0; i < count; i++) { 00297 printf("No.%d Network: %s secured: %s BSSID: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx RSSI: %hhd Ch: %hhd\r\n", 00298 i, ap[i].get_ssid(), sec2str(ap[i].get_security()), 00299 ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], ap[i].get_bssid()[3], 00300 ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); 00301 } 00302 printf("%d networks available.\r\n", count); 00303 00304 if (count > 0) { 00305 char c; 00306 char pass[64]; 00307 int select_no; 00308 bool loop_break = false;; 00309 00310 printf("\nPlease enter the number of the network you want to connect.\r\n"); 00311 printf("Enter key:[0]-[%d], (If inputting the other key, it's scanned again.)\r\n", count - 1); 00312 c = (uint8_t)pc.getc(); 00313 select_no = c - 0x30; 00314 if ((select_no >= 0) && (select_no < count)) { 00315 printf("[%s] is selected.\r\n", ap[select_no].get_ssid()); 00316 printf("Please enter the PSK.\r\n"); 00317 i = 0; 00318 while (loop_break == false) { 00319 c = (uint8_t)pc.getc(); 00320 switch (c) { 00321 case 0x0D: 00322 pass[i] = '\0'; 00323 pc.puts("\r\n"); 00324 loop_break = true; 00325 break; 00326 case 0x08: 00327 if (i > 0) { 00328 pc.puts("\b \b"); 00329 i--; 00330 } 00331 break; 00332 case 0x0A: 00333 break; 00334 default: 00335 if ((i + 1) < sizeof(pass)) { 00336 pass[i] = c; 00337 i++; 00338 pc.putc(c); 00339 } 00340 break; 00341 } 00342 } 00343 wifi->set_credentials(ap[select_no].get_ssid(), pass, ap[select_no].get_security()); 00344 ret = true; 00345 } 00346 } 00347 00348 delete[] ap; 00349 00350 return ret; 00351 } 00352 #endif 00353 00354 static void sd_connect_task(void) { 00355 int storage_type = 0; 00356 00357 while (1) { 00358 if (storage_type == 0) { 00359 if (sd.connect()) { 00360 fs.unmount(); 00361 fs.mount(&sd); 00362 storage_type = 1; 00363 printf("SDBlockDevice\r\n"); 00364 } 00365 } else { 00366 if (sd.connected() == false) { 00367 fs.unmount(); 00368 fs.mount(&romram_bd); 00369 storage_type = 0; 00370 printf("RomRamBlockDevice\r\n"); 00371 } 00372 } 00373 ThisThread::sleep_for(250); 00374 } 00375 } 00376 00377 int main(void) { 00378 printf("********* PROGRAM START ***********\r\n"); 00379 00380 mount_romramfs(); //RomRamFileSystem Mount 00381 00382 sdConnectTask.start(&sd_connect_task); 00383 00384 EasyAttach_Init(Display); 00385 Jcu.SetQuality(JPEG_ENCODE_QUALITY); 00386 // Interrupt callback function setting (Field end signal for recording function in scaler 0) 00387 Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VFIELD, 0, IntCallbackFunc_Vfield); 00388 Start_Video_Camera(); 00389 00390 RPC::add_rpc_class<RpcDigitalOut>(); 00391 RPCFunction rpcFunc(TerminalWrite, "TerminalWrite"); 00392 RPCFunction rpcSetI2C(SetI2CfromWeb, "SetI2CfromWeb"); 00393 00394 printf("Network Setting up...\r\n"); 00395 #if (USE_DHCP == 0) 00396 network.set_dhcp(false); 00397 if (network.set_network(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY) != 0) { //for Static IP Address (IPAddress, NetMasks, Gateway) 00398 printf("Network Set Network Error \r\n"); 00399 } 00400 #endif 00401 00402 #if (NETWORK_TYPE >= 2) 00403 #if (SCAN_NETWORK == 1) && (NETWORK_TYPE != 3) 00404 while (!scan_network(&network)); 00405 #else 00406 network.set_credentials(WLAN_SSID, WLAN_PSK, WLAN_SECURITY); 00407 #endif 00408 #endif 00409 00410 printf("\r\nConnecting...\r\n"); 00411 if (network.connect() != 0) { 00412 printf("Network Connect Error \r\n"); 00413 return -1; 00414 } 00415 printf("MAC Address is %s\r\n", network.get_mac_address()); 00416 printf("IP Address is %s\r\n", network.get_ip_address()); 00417 printf("NetMask is %s\r\n", network.get_netmask()); 00418 printf("Gateway Address is %s\r\n", network.get_gateway()); 00419 printf("Network Setup OK\r\n"); 00420 00421 SnapshotHandler::attach_req(&snapshot_req); 00422 HTTPServerAddHandler<SnapshotHandler>("/camera"); //Camera 00423 FSHandler::mount("/storage", "/"); 00424 HTTPServerAddHandler<FSHandler>("/"); 00425 HTTPServerAddHandler<RPCHandler>("/rpc"); 00426 HTTPServerStart(&network, 80); 00427 }
Generated on Wed Jul 13 2022 18:48:00 by 1.7.2