Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DhcpServer EthernetInterface FATFileSystem GR-PEACH_WlanBP3595AP GR-PEACH_video GraphicsFramework HttpServer_snapshot R_BSP mbed-rpc mbed-rtos mbed
Fork of GR-Boards_WebCamera by
main.cpp
00001 #include "mbed.h" 00002 #include "DisplayBace.h" 00003 #include "rtos.h" 00004 #include "JPEG_Converter.h" 00005 #include "HTTPServer.h" 00006 #include "mbed_rpc.h" 00007 #include "RomRamFileSystem.h" 00008 #include "file_table.h" //Binary data of web pages 00009 #include "i2c_setting.h" 00010 #include "DhcpServer.h" 00011 00012 #define VIDEO_CVBS (0) /* Analog Video Signal */ 00013 #define VIDEO_CMOS_CAMERA (1) /* Digital Video Signal */ 00014 #define VIDEO_YCBCR422 (0) 00015 #define VIDEO_RGB888 (1) 00016 #define VIDEO_RGB565 (2) 00017 00018 /**** User Selection *********/ 00019 /** Network setting **/ 00020 #define USE_DHCP (0) /* Select 0(static configuration) or 1(use DHCP) */ 00021 #if (USE_DHCP == 0) 00022 #define IP_ADDRESS ("192.168.0.2") /* IP address */ 00023 #define SUBNET_MASK ("255.255.255.0") /* Subnet mask */ 00024 #define DEFAULT_GATEWAY ("192.168.0.3") /* Default gateway */ 00025 #endif 00026 #define NETWORK_TYPE (1) /* Select 0(EthernetInterface) or 1(GR_PEACH_WlanBP3595) */ 00027 #if (NETWORK_TYPE == 1) 00028 #define WLAN_SSID ("GR-PEACH_1") /* SSID */ 00029 #define WLAN_PSK ("12345") /* PSK(Pre-Shared Key) */ 00030 #define WLAN_SECURITY NSAPI_SECURITY_WEP /* NSAPI_SECURITY_NONE, NSAPI_SECURITY_WEP, NSAPI_SECURITY_WPA or NSAPI_SECURITY_WPA2 */ 00031 #endif 00032 /** Camera setting **/ 00033 #define VIDEO_INPUT_METHOD (VIDEO_CMOS_CAMERA) /* Select VIDEO_CVBS or VIDEO_CMOS_CAMERA */ 00034 #define VIDEO_INPUT_FORMAT (VIDEO_YCBCR422) /* Select VIDEO_YCBCR422 or VIDEO_RGB888 or VIDEO_RGB565 */ 00035 #define USE_VIDEO_CH (0) /* Select 0 or 1 If selecting VIDEO_CMOS_CAMERA, should be 0.) */ 00036 #define VIDEO_PAL (0) /* Select 0(NTSC) or 1(PAL) If selecting VIDEO_CVBS, this parameter is not referenced.) */ 00037 /*****************************/ 00038 00039 #if USE_VIDEO_CH == (0) 00040 #define VIDEO_INPUT_CH (DisplayBase::VIDEO_INPUT_CHANNEL_0) 00041 #define VIDEO_INT_TYPE (DisplayBase::INT_TYPE_S0_VFIELD) 00042 #else 00043 #define VIDEO_INPUT_CH (DisplayBase::VIDEO_INPUT_CHANNEL_1) 00044 #define VIDEO_INT_TYPE (DisplayBase::INT_TYPE_S1_VFIELD) 00045 #endif 00046 00047 #if ( VIDEO_INPUT_FORMAT == VIDEO_YCBCR422 || VIDEO_INPUT_FORMAT == VIDEO_RGB565 ) 00048 #define DATA_SIZE_PER_PIC (2u) 00049 #else 00050 #define DATA_SIZE_PER_PIC (4u) 00051 #endif 00052 00053 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128 00054 in accordance with the frame buffer burst transfer mode. */ 00055 #define PIXEL_HW (320u) /* QVGA */ 00056 #define PIXEL_VW (240u) /* QVGA */ 00057 00058 #define VIDEO_BUFFER_STRIDE (((PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u) 00059 #define VIDEO_BUFFER_HEIGHT (PIXEL_VW) 00060 00061 #if (NETWORK_TYPE == 0) 00062 #include "EthernetInterface.h" 00063 EthernetInterface network; 00064 #elif (NETWORK_TYPE == 1) 00065 #include "GR_PEACH_WlanBP3595.h" 00066 GR_PEACH_WlanBP3595 network; 00067 DigitalOut usb1en(P3_8); 00068 #else 00069 #error NETWORK_TYPE error 00070 #endif /* NETWORK_TYPE */ 00071 RomRamFileSystem romramfs("romram"); 00072 00073 #if defined(__ICCARM__) 00074 #pragma data_alignment=16 00075 static uint8_t FrameBuffer_Video[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]@ ".mirrorram"; //16 bytes aligned!; 00076 #pragma data_alignment=4 00077 #else 00078 static uint8_t FrameBuffer_Video[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(16))); //16 bytes aligned!; 00079 #endif 00080 static volatile int32_t vsync_count = 0; 00081 static volatile int32_t vfield_count = 1; 00082 #if defined(__ICCARM__) 00083 #pragma data_alignment=8 00084 static uint8_t JpegBuffer[2][1024 * 50]@ ".mirrorram"; //8 bytes aligned!; 00085 #pragma data_alignment=4 00086 #else 00087 static uint8_t JpegBuffer[2][1024 * 50]__attribute((section("NC_BSS"),aligned(8))); //8 bytes aligned!; 00088 #endif 00089 static size_t jcu_encode_size[2]; 00090 static int image_change = 0; 00091 JPEG_Converter Jcu; 00092 static int jcu_buf_index_write = 0; 00093 static int jcu_buf_index_write_done = 0; 00094 static int jcu_buf_index_read = 0; 00095 static int jcu_encoding = 0; 00096 static char i2c_setting_str_buf[I2C_SETTING_STR_BUF_SIZE]; 00097 00098 static void JcuEncodeCallBackFunc(JPEG_Converter::jpeg_conv_error_t err_code) { 00099 jcu_buf_index_write_done = jcu_buf_index_write; 00100 image_change = 1; 00101 jcu_encoding = 0; 00102 } 00103 00104 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type) { 00105 //Interrupt callback function 00106 if (vfield_count != 0) { 00107 vfield_count = 0; 00108 } else { 00109 vfield_count = 1; 00110 00111 JPEG_Converter::bitmap_buff_info_t bitmap_buff_info; 00112 JPEG_Converter::encode_options_t encode_options; 00113 00114 bitmap_buff_info.width = PIXEL_HW; 00115 bitmap_buff_info.height = PIXEL_VW; 00116 bitmap_buff_info.format = JPEG_Converter::WR_RD_YCbCr422; 00117 bitmap_buff_info.buffer_address = (void *)FrameBuffer_Video; 00118 00119 encode_options.encode_buff_size = sizeof(JpegBuffer[0]); 00120 encode_options.p_EncodeCallBackFunc = &JcuEncodeCallBackFunc; 00121 00122 jcu_encoding = 1; 00123 if (jcu_buf_index_read == jcu_buf_index_write) { 00124 if (jcu_buf_index_write != 0) { 00125 jcu_buf_index_write = 0; 00126 } else { 00127 jcu_buf_index_write = 1; 00128 } 00129 } 00130 jcu_encode_size[jcu_buf_index_write] = 0; 00131 if (Jcu.encode(&bitmap_buff_info, JpegBuffer[jcu_buf_index_write], &jcu_encode_size[jcu_buf_index_write], &encode_options) != JPEG_Converter::JPEG_CONV_OK) { 00132 jcu_encode_size[jcu_buf_index_write] = 0; 00133 jcu_encoding = 0; 00134 } 00135 } 00136 } 00137 00138 static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type) { 00139 //Interrupt callback function for Vsync interruption 00140 if (vsync_count > 0) { 00141 vsync_count--; 00142 } 00143 } 00144 00145 static void WaitVsync(const int32_t wait_count) { 00146 //Wait for the specified number of times Vsync occurs 00147 vsync_count = wait_count; 00148 while (vsync_count > 0) { 00149 /* Do nothing */ 00150 } 00151 } 00152 00153 static void camera_start(void) { 00154 DisplayBase::graphics_error_t error; 00155 00156 #if VIDEO_INPUT_METHOD == VIDEO_CMOS_CAMERA 00157 DisplayBase::video_ext_in_config_t ext_in_config; 00158 PinName cmos_camera_pin[11] = { 00159 /* data pin */ 00160 P2_7, P2_6, P2_5, P2_4, P2_3, P2_2, P2_1, P2_0, 00161 /* control pin */ 00162 P10_0, /* DV0_CLK */ 00163 P1_0, /* DV0_Vsync */ 00164 P1_1 /* DV0_Hsync */ 00165 }; 00166 #endif 00167 00168 /* Create DisplayBase object */ 00169 DisplayBase Display; 00170 00171 /* Graphics initialization process */ 00172 error = Display.Graphics_init(NULL); 00173 if (error != DisplayBase::GRAPHICS_OK) { 00174 printf("Line %d, error %d\n", __LINE__, error); 00175 while (1); 00176 } 00177 00178 #if VIDEO_INPUT_METHOD == VIDEO_CVBS 00179 error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_VDEC, NULL); 00180 if( error != DisplayBase::GRAPHICS_OK ) { 00181 printf("Line %d, error %d\n", __LINE__, error); 00182 while(1); 00183 } 00184 00185 #elif VIDEO_INPUT_METHOD == VIDEO_CMOS_CAMERA 00186 /* MT9V111 camera input config */ 00187 ext_in_config.inp_format = DisplayBase::VIDEO_EXTIN_FORMAT_BT601; /* BT601 8bit YCbCr format */ 00188 ext_in_config.inp_pxd_edge = DisplayBase::EDGE_RISING; /* Clock edge select for capturing data */ 00189 ext_in_config.inp_vs_edge = DisplayBase::EDGE_RISING; /* Clock edge select for capturing Vsync signals */ 00190 ext_in_config.inp_hs_edge = DisplayBase::EDGE_RISING; /* Clock edge select for capturing Hsync signals */ 00191 ext_in_config.inp_endian_on = DisplayBase::OFF; /* External input bit endian change on/off */ 00192 ext_in_config.inp_swap_on = DisplayBase::OFF; /* External input B/R signal swap on/off */ 00193 ext_in_config.inp_vs_inv = DisplayBase::SIG_POL_NOT_INVERTED; /* External input DV_VSYNC inversion control */ 00194 ext_in_config.inp_hs_inv = DisplayBase::SIG_POL_INVERTED; /* External input DV_HSYNC inversion control */ 00195 ext_in_config.inp_f525_625 = DisplayBase::EXTIN_LINE_525; /* Number of lines for BT.656 external input */ 00196 ext_in_config.inp_h_pos = DisplayBase::EXTIN_H_POS_CRYCBY; /* Y/Cb/Y/Cr data string start timing to Hsync reference */ 00197 ext_in_config.cap_vs_pos = 6; /* Capture start position from Vsync */ 00198 ext_in_config.cap_hs_pos = 150; /* Capture start position form Hsync */ 00199 ext_in_config.cap_width = 640; /* Capture width */ 00200 ext_in_config.cap_height = 468u; /* Capture height Max 468[line] 00201 Due to CMOS(MT9V111) output signal timing and VDC5 specification */ 00202 error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_EXT, &ext_in_config); 00203 if( error != DisplayBase::GRAPHICS_OK ) { 00204 printf("Line %d, error %d\n", __LINE__, error); 00205 while(1); 00206 } 00207 00208 /* MT9V111 camera input port setting */ 00209 error = Display.Graphics_Dvinput_Port_Init(cmos_camera_pin, 11); 00210 if( error != DisplayBase::GRAPHICS_OK ) { 00211 printf("Line %d, error %d\n", __LINE__, error); 00212 while (1); 00213 } 00214 #endif 00215 00216 /* Interrupt callback function setting (Vsync signal input to scaler 0) */ 00217 error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VI_VSYNC, 0, IntCallbackFunc_Vsync); 00218 if (error != DisplayBase::GRAPHICS_OK) { 00219 printf("Line %d, error %d\n", __LINE__, error); 00220 while (1); 00221 } 00222 /* Video capture setting (progressive form fixed) */ 00223 error = Display.Video_Write_Setting( 00224 VIDEO_INPUT_CH, 00225 #if VIDEO_PAL == 0 00226 DisplayBase::COL_SYS_NTSC_358, 00227 #else 00228 DisplayBase::COL_SYS_PAL_443, 00229 #endif 00230 FrameBuffer_Video, 00231 VIDEO_BUFFER_STRIDE, 00232 #if VIDEO_INPUT_FORMAT == VIDEO_YCBCR422 00233 DisplayBase::VIDEO_FORMAT_YCBCR422, 00234 DisplayBase::WR_RD_WRSWA_NON, 00235 #elif VIDEO_INPUT_FORMAT == VIDEO_RGB565 00236 DisplayBase::VIDEO_FORMAT_RGB565, 00237 DisplayBase::WR_RD_WRSWA_32_16BIT, 00238 #else 00239 DisplayBase::VIDEO_FORMAT_RGB888, 00240 DisplayBase::WR_RD_WRSWA_32BIT, 00241 #endif 00242 PIXEL_VW, 00243 PIXEL_HW 00244 ); 00245 if (error != DisplayBase::GRAPHICS_OK) { 00246 printf("Line %d, error %d\n", __LINE__, error); 00247 while (1); 00248 } 00249 00250 /* Interrupt callback function setting (Field end signal for recording function in scaler 0) */ 00251 error = Display.Graphics_Irq_Handler_Set(VIDEO_INT_TYPE, 0, IntCallbackFunc_Vfield); 00252 if (error != DisplayBase::GRAPHICS_OK) { 00253 printf("Line %d, error %d\n", __LINE__, error); 00254 while (1); 00255 } 00256 00257 /* Video write process start */ 00258 error = Display.Video_Start (VIDEO_INPUT_CH); 00259 if (error != DisplayBase::GRAPHICS_OK) { 00260 printf("Line %d, error %d\n", __LINE__, error); 00261 while (1); 00262 } 00263 00264 /* Video write process stop */ 00265 error = Display.Video_Stop (VIDEO_INPUT_CH); 00266 if (error != DisplayBase::GRAPHICS_OK) { 00267 printf("Line %d, error %d\n", __LINE__, error); 00268 while (1); 00269 } 00270 00271 /* Video write process start */ 00272 error = Display.Video_Start (VIDEO_INPUT_CH); 00273 if (error != DisplayBase::GRAPHICS_OK) { 00274 printf("Line %d, error %d\n", __LINE__, error); 00275 while (1); 00276 } 00277 00278 /* Wait vsync to update resister */ 00279 WaitVsync(1); 00280 } 00281 00282 static int snapshot_req(const char ** pp_data) { 00283 int encode_size; 00284 00285 while ((jcu_encoding == 1) || (image_change == 0)) { 00286 Thread::wait(1); 00287 } 00288 jcu_buf_index_read = jcu_buf_index_write_done; 00289 image_change = 0; 00290 00291 *pp_data = (const char *)JpegBuffer[jcu_buf_index_read]; 00292 encode_size = (int)jcu_encode_size[jcu_buf_index_read]; 00293 00294 return encode_size; 00295 } 00296 00297 static void TerminalWrite(Arguments* arg, Reply* r) { 00298 if ((arg != NULL) && (r != NULL)) { 00299 for (int i = 0; i < arg->argc; i++) { 00300 if (arg->argv[i] != NULL) { 00301 printf("%s", arg->argv[i]); 00302 } 00303 } 00304 printf("\n"); 00305 r->putData<const char*>("ok"); 00306 } 00307 } 00308 00309 static void mount_romramfs(void) { 00310 FILE * fp; 00311 00312 romramfs.format(); 00313 00314 //index.htm 00315 fp = fopen("/romram/index.htm", "w"); 00316 fwrite(index_htm_tbl, sizeof(char), sizeof(index_htm_tbl), fp); 00317 fclose(fp); 00318 00319 //camera.js 00320 fp = fopen("/romram/camera.js", "w"); 00321 fwrite(camaera_js_tbl, sizeof(char), sizeof(camaera_js_tbl), fp); 00322 fclose(fp); 00323 00324 //camera.htm 00325 fp = fopen("/romram/camera.htm", "w"); 00326 fwrite(camera_htm_tbl, sizeof(char), sizeof(camera_htm_tbl), fp); 00327 fclose(fp); 00328 00329 //mbedrpc.js 00330 fp = fopen("/romram/mbedrpc.js", "w"); 00331 fwrite(mbedrpc_js_tbl, sizeof(char), sizeof(mbedrpc_js_tbl), fp); 00332 fclose(fp); 00333 00334 //led.htm 00335 fp = fopen("/romram/led.htm", "w"); 00336 fwrite(led_htm_tbl, sizeof(char), sizeof(led_htm_tbl), fp); 00337 fclose(fp); 00338 00339 //i2c_set.htm 00340 fp = fopen("/romram/i2c_set.htm", "w"); 00341 fwrite(i2c_set_htm_tbl, sizeof(char), sizeof(i2c_set_htm_tbl), fp); 00342 fclose(fp); 00343 00344 //web_top.htm 00345 fp = fopen("/romram/web_top.htm", "w"); 00346 fwrite(web_top_htm_tbl, sizeof(char), sizeof(web_top_htm_tbl), fp); 00347 fclose(fp); 00348 00349 //menu.htm 00350 fp = fopen("/romram/menu.htm", "w"); 00351 fwrite(menu_htm_tbl, sizeof(char), sizeof(menu_htm_tbl), fp); 00352 fclose(fp); 00353 00354 //window.htm 00355 fp = fopen("/romram/window.htm", "w"); 00356 fwrite(window_htm_tbl, sizeof(char), sizeof(window_htm_tbl), fp); 00357 fclose(fp); 00358 } 00359 00360 static void SetI2CfromWeb(Arguments* arg, Reply* r) { 00361 int result = 0; 00362 00363 if (arg != NULL) { 00364 if (arg->argc >= 2) { 00365 if ((arg->argv[0] != NULL) && (arg->argv[1] != NULL)) { 00366 sprintf(i2c_setting_str_buf, "%s,%s", arg->argv[0], arg->argv[1]); 00367 result = 1; 00368 } 00369 } else if (arg->argc == 1) { 00370 if (arg->argv[0] != NULL) { 00371 sprintf(i2c_setting_str_buf, "%s", arg->argv[0]); 00372 result = 1; 00373 } 00374 } else { 00375 /* Do nothing */ 00376 } 00377 /* command analysis and execute */ 00378 if (result != 0) { 00379 if (i2c_setting_exe(i2c_setting_str_buf) != false) { 00380 r->putData<const char*>(i2c_setting_str_buf); 00381 } 00382 } 00383 } 00384 } 00385 00386 int main(void) { 00387 printf("********* PROGRAM START ***********\r\n"); 00388 00389 /* Please enable this line when performing the setting from the Terminal side. */ 00390 // Thread thread(SetI2CfromTerm, NULL, osPriorityBelowNormal, DEFAULT_STACK_SIZE); 00391 00392 mount_romramfs(); //RomRamFileSystem Mount 00393 camera_start(); //Camera Start 00394 00395 RPC::add_rpc_class<RpcDigitalOut>(); 00396 RPC::construct<RpcDigitalOut, PinName, const char*>(LED1, "led1"); 00397 RPC::construct<RpcDigitalOut, PinName, const char*>(LED2, "led2"); 00398 RPC::construct<RpcDigitalOut, PinName, const char*>(LED3, "led3"); 00399 RPCFunction rpcFunc(TerminalWrite, "TerminalWrite"); 00400 RPCFunction rpcSetI2C(SetI2CfromWeb, "SetI2CfromWeb"); 00401 00402 #if (NETWORK_TYPE == 1) 00403 //Audio Camera Shield USB1 enable for WlanBP3595 00404 usb1en = 1; //Outputs high level 00405 Thread::wait(5); 00406 usb1en = 0; //Outputs low level 00407 Thread::wait(5); 00408 #endif 00409 00410 printf("Network Setting up...\r\n"); 00411 #if (USE_DHCP == 1) 00412 if (network.init() != 0) { //for DHCP Server 00413 #else 00414 if (network.init(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY) != 0) { //for Static IP Address (IPAddress, NetMasks, Gateway) 00415 #endif 00416 printf("Network Initialize Error \r\n"); 00417 return -1; 00418 } 00419 #if (NETWORK_TYPE == 0) 00420 if (network.connect() != 0) { 00421 #else 00422 if (network.connect(WLAN_SSID, WLAN_PSK, WLAN_SECURITY) != 0) { 00423 #endif 00424 printf("Network Connect Error \r\n"); 00425 return -1; 00426 } 00427 printf("MAC Address is %s\r\n", network.getMACAddress()); 00428 printf("IP Address is %s\r\n", network.getIPAddress()); 00429 printf("NetMask is %s\r\n", network.getNetworkMask()); 00430 printf("Gateway Address is %s\r\n", network.getGateway()); 00431 printf("Network Setup OK\r\n"); 00432 DhcpServer dhcp_server("GR-PEACH_1", network.getIPAddress()); 00433 00434 SnapshotHandler::attach_req(&snapshot_req); 00435 HTTPServerAddHandler<SnapshotHandler>("/camera"); //Camera 00436 FSHandler::mount("/romram", "/"); 00437 HTTPServerAddHandler<FSHandler>("/"); 00438 HTTPServerAddHandler<RPCHandler>("/rpc"); 00439 HTTPServerStart(80); 00440 }
Generated on Fri Jul 15 2022 01:45:12 by
1.7.2
