Web Camera for mbed-os. 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 LWIPBP3595Interface_STA_for_mbed-os RomRamBlockDevice mbed-rpc

Fork of GR-Boards_WebCamera by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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