Sample web camera program for GR-PEACH

Dependencies:   GR-PEACH_video GraphicsFramework HttpServer_snapshot_mbed-os LWIPBP3595Interface_STA_for_mbed-os R_BSP RomRamBlockDevice mbed-rpc

Committer:
tuthai
Date:
Mon Aug 07 06:43:34 2017 +0000
Revision:
2:82e09e910229
Parent:
1:bd1c4dd0eccd
Update to mbed-os 5.4.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tuthai 0:4edfb8401555 1 #include "mbed.h"
tuthai 0:4edfb8401555 2 #include "DisplayBace.h"
tuthai 0:4edfb8401555 3 #include "rtos.h"
tuthai 0:4edfb8401555 4 #include "JPEG_Converter.h"
tuthai 0:4edfb8401555 5 #include "HTTPServer.h"
tuthai 0:4edfb8401555 6 #include "mbed_rpc.h"
tuthai 0:4edfb8401555 7 #include "FATFileSystem.h"
tuthai 0:4edfb8401555 8 #include "RomRamBlockDevice.h"
tuthai 0:4edfb8401555 9 #include "file_table.h" //Binary data of web pages
tuthai 0:4edfb8401555 10 #include "i2c_setting.h"
tuthai 0:4edfb8401555 11
tuthai 0:4edfb8401555 12 #define VIDEO_CVBS (0) /* Analog Video Signal */
tuthai 0:4edfb8401555 13 #define VIDEO_CMOS_CAMERA (1) /* Digital Video Signal */
tuthai 0:4edfb8401555 14 #define VIDEO_YCBCR422 (0)
tuthai 0:4edfb8401555 15 #define VIDEO_RGB888 (1)
tuthai 0:4edfb8401555 16 #define VIDEO_RGB565 (2)
tuthai 0:4edfb8401555 17
tuthai 0:4edfb8401555 18 /**** User Selection *********/
tuthai 0:4edfb8401555 19 /** Network setting **/
tuthai 1:bd1c4dd0eccd 20 #define USE_DHCP (0) /* Select 0(static configuration) or 1(use DHCP) */
tuthai 0:4edfb8401555 21 #if (USE_DHCP == 0)
tuthai 0:4edfb8401555 22 #define IP_ADDRESS ("192.168.0.2") /* IP address */
tuthai 0:4edfb8401555 23 #define SUBNET_MASK ("255.255.255.0") /* Subnet mask */
tuthai 0:4edfb8401555 24 #define DEFAULT_GATEWAY ("192.168.0.3") /* Default gateway */
tuthai 0:4edfb8401555 25 #endif
tuthai 0:4edfb8401555 26 #define NETWORK_TYPE (0) /* Select 0(EthernetInterface) or 1(GR_PEACH_WlanBP3595) */
tuthai 0:4edfb8401555 27 #if (NETWORK_TYPE == 1)
tuthai 0:4edfb8401555 28 #define SCAN_NETWORK (1) /* Select 0(Use WLAN_SSID, WLAN_PSK, WLAN_SECURITY) or 1(To select a network using the terminal.) */
tuthai 0:4edfb8401555 29 #define WLAN_SSID ("SSIDofYourAP") /* SSID */
tuthai 0:4edfb8401555 30 #define WLAN_PSK ("PSKofYourAP") /* PSK(Pre-Shared Key) */
tuthai 0:4edfb8401555 31 #define WLAN_SECURITY NSAPI_SECURITY_WPA_WPA2 /* NSAPI_SECURITY_NONE, NSAPI_SECURITY_WEP, NSAPI_SECURITY_WPA, NSAPI_SECURITY_WPA2 or NSAPI_SECURITY_WPA_WPA2 */
tuthai 0:4edfb8401555 32 #endif
tuthai 0:4edfb8401555 33 /** Camera setting **/
tuthai 0:4edfb8401555 34 #define VIDEO_INPUT_METHOD (VIDEO_CMOS_CAMERA) /* Select VIDEO_CVBS or VIDEO_CMOS_CAMERA */
tuthai 0:4edfb8401555 35 #define VIDEO_INPUT_FORMAT (VIDEO_YCBCR422) /* Select VIDEO_YCBCR422 or VIDEO_RGB888 or VIDEO_RGB565 */
tuthai 0:4edfb8401555 36 #define USE_VIDEO_CH (0) /* Select 0 or 1 If selecting VIDEO_CMOS_CAMERA, should be 0.) */
tuthai 0:4edfb8401555 37 #define VIDEO_PAL (0) /* Select 0(NTSC) or 1(PAL) If selecting VIDEO_CVBS, this parameter is not referenced.) */
tuthai 0:4edfb8401555 38 /*****************************/
tuthai 0:4edfb8401555 39
tuthai 0:4edfb8401555 40 #if USE_VIDEO_CH == (0)
tuthai 0:4edfb8401555 41 #define VIDEO_INPUT_CH (DisplayBase::VIDEO_INPUT_CHANNEL_0)
tuthai 0:4edfb8401555 42 #define VIDEO_INT_TYPE (DisplayBase::INT_TYPE_S0_VFIELD)
tuthai 0:4edfb8401555 43 #else
tuthai 0:4edfb8401555 44 #define VIDEO_INPUT_CH (DisplayBase::VIDEO_INPUT_CHANNEL_1)
tuthai 0:4edfb8401555 45 #define VIDEO_INT_TYPE (DisplayBase::INT_TYPE_S1_VFIELD)
tuthai 0:4edfb8401555 46 #endif
tuthai 0:4edfb8401555 47
tuthai 0:4edfb8401555 48 #if ( VIDEO_INPUT_FORMAT == VIDEO_YCBCR422 || VIDEO_INPUT_FORMAT == VIDEO_RGB565 )
tuthai 0:4edfb8401555 49 #define DATA_SIZE_PER_PIC (2u)
tuthai 0:4edfb8401555 50 #else
tuthai 0:4edfb8401555 51 #define DATA_SIZE_PER_PIC (4u)
tuthai 0:4edfb8401555 52 #endif
tuthai 0:4edfb8401555 53
tuthai 0:4edfb8401555 54 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128
tuthai 0:4edfb8401555 55 in accordance with the frame buffer burst transfer mode. */
tuthai 0:4edfb8401555 56 #define PIXEL_HW (320u) /* QVGA */
tuthai 0:4edfb8401555 57 #define PIXEL_VW (240u) /* QVGA */
tuthai 0:4edfb8401555 58
tuthai 0:4edfb8401555 59 #define VIDEO_BUFFER_STRIDE (((PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u)
tuthai 0:4edfb8401555 60 #define VIDEO_BUFFER_HEIGHT (PIXEL_VW)
tuthai 0:4edfb8401555 61
tuthai 0:4edfb8401555 62 #if (NETWORK_TYPE == 0)
tuthai 0:4edfb8401555 63 #include "EthernetInterface.h"
tuthai 0:4edfb8401555 64 EthernetInterface network;
tuthai 0:4edfb8401555 65 #elif (NETWORK_TYPE == 1)
tuthai 0:4edfb8401555 66 #include "LWIPBP3595Interface.h"
tuthai 0:4edfb8401555 67 LWIPBP3595Interface network;
tuthai 0:4edfb8401555 68 DigitalOut usb1en(P3_8);
tuthai 0:4edfb8401555 69 #else
tuthai 0:4edfb8401555 70 #error NETWORK_TYPE error
tuthai 0:4edfb8401555 71 #endif /* NETWORK_TYPE */
tuthai 0:4edfb8401555 72 FATFileSystem fs("storage");
tuthai 0:4edfb8401555 73 RomRamBlockDevice romram_bd(512000, 512);
tuthai 0:4edfb8401555 74 Serial pc(USBTX, USBRX);
tuthai 0:4edfb8401555 75
tuthai 0:4edfb8401555 76 #if defined(__ICCARM__)
tuthai 0:4edfb8401555 77 #pragma data_alignment=16
tuthai 0:4edfb8401555 78 static uint8_t FrameBuffer_Video[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]@ ".mirrorram"; //16 bytes aligned!;
tuthai 0:4edfb8401555 79 #pragma data_alignment=4
tuthai 0:4edfb8401555 80 #else
tuthai 0:4edfb8401555 81 static uint8_t FrameBuffer_Video[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(16))); //16 bytes aligned!;
tuthai 0:4edfb8401555 82 #endif
tuthai 0:4edfb8401555 83 static volatile int32_t vsync_count = 0;
tuthai 0:4edfb8401555 84 #if VIDEO_INPUT_METHOD == VIDEO_CVBS
tuthai 0:4edfb8401555 85 static volatile int32_t vfield_count = 1;
tuthai 0:4edfb8401555 86 #endif
tuthai 0:4edfb8401555 87 #if defined(__ICCARM__)
tuthai 0:4edfb8401555 88 #pragma data_alignment=8
tuthai 0:4edfb8401555 89 static uint8_t JpegBuffer[2][1024 * 50]@ ".mirrorram"; //8 bytes aligned!;
tuthai 0:4edfb8401555 90 #pragma data_alignment=4
tuthai 0:4edfb8401555 91 #else
tuthai 0:4edfb8401555 92 static uint8_t JpegBuffer[2][1024 * 50]__attribute((section("NC_BSS"),aligned(8))); //8 bytes aligned!;
tuthai 0:4edfb8401555 93 #endif
tuthai 0:4edfb8401555 94 static size_t jcu_encode_size[2];
tuthai 0:4edfb8401555 95 static int image_change = 0;
tuthai 0:4edfb8401555 96 JPEG_Converter Jcu;
tuthai 0:4edfb8401555 97 static int jcu_buf_index_write = 0;
tuthai 0:4edfb8401555 98 static int jcu_buf_index_write_done = 0;
tuthai 0:4edfb8401555 99 static int jcu_buf_index_read = 0;
tuthai 0:4edfb8401555 100 static int jcu_encoding = 0;
tuthai 0:4edfb8401555 101 static char i2c_setting_str_buf[I2C_SETTING_STR_BUF_SIZE];
tuthai 0:4edfb8401555 102
tuthai 0:4edfb8401555 103 static void JcuEncodeCallBackFunc(JPEG_Converter::jpeg_conv_error_t err_code) {
tuthai 0:4edfb8401555 104 jcu_buf_index_write_done = jcu_buf_index_write;
tuthai 0:4edfb8401555 105 image_change = 1;
tuthai 0:4edfb8401555 106 jcu_encoding = 0;
tuthai 0:4edfb8401555 107 }
tuthai 0:4edfb8401555 108
tuthai 0:4edfb8401555 109 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type) {
tuthai 0:4edfb8401555 110 //Interrupt callback function
tuthai 0:4edfb8401555 111 #if VIDEO_INPUT_METHOD == VIDEO_CVBS
tuthai 0:4edfb8401555 112 if (vfield_count != 0) {
tuthai 0:4edfb8401555 113 vfield_count = 0;
tuthai 0:4edfb8401555 114 } else {
tuthai 0:4edfb8401555 115 vfield_count = 1;
tuthai 0:4edfb8401555 116 #else
tuthai 0:4edfb8401555 117 {
tuthai 0:4edfb8401555 118 #endif
tuthai 0:4edfb8401555 119 JPEG_Converter::bitmap_buff_info_t bitmap_buff_info;
tuthai 0:4edfb8401555 120 JPEG_Converter::encode_options_t encode_options;
tuthai 0:4edfb8401555 121
tuthai 0:4edfb8401555 122 bitmap_buff_info.width = PIXEL_HW;
tuthai 0:4edfb8401555 123 bitmap_buff_info.height = PIXEL_VW;
tuthai 0:4edfb8401555 124 bitmap_buff_info.format = JPEG_Converter::WR_RD_YCbCr422;
tuthai 0:4edfb8401555 125 bitmap_buff_info.buffer_address = (void *)FrameBuffer_Video;
tuthai 0:4edfb8401555 126
tuthai 0:4edfb8401555 127 encode_options.encode_buff_size = sizeof(JpegBuffer[0]);
tuthai 0:4edfb8401555 128 encode_options.p_EncodeCallBackFunc = &JcuEncodeCallBackFunc;
tuthai 0:4edfb8401555 129
tuthai 0:4edfb8401555 130 jcu_encoding = 1;
tuthai 0:4edfb8401555 131 if (jcu_buf_index_read == jcu_buf_index_write) {
tuthai 0:4edfb8401555 132 if (jcu_buf_index_write != 0) {
tuthai 0:4edfb8401555 133 jcu_buf_index_write = 0;
tuthai 0:4edfb8401555 134 } else {
tuthai 0:4edfb8401555 135 jcu_buf_index_write = 1;
tuthai 0:4edfb8401555 136 }
tuthai 0:4edfb8401555 137 }
tuthai 0:4edfb8401555 138 jcu_encode_size[jcu_buf_index_write] = 0;
tuthai 0:4edfb8401555 139 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) {
tuthai 0:4edfb8401555 140 jcu_encode_size[jcu_buf_index_write] = 0;
tuthai 0:4edfb8401555 141 jcu_encoding = 0;
tuthai 0:4edfb8401555 142 }
tuthai 0:4edfb8401555 143 }
tuthai 0:4edfb8401555 144 }
tuthai 0:4edfb8401555 145
tuthai 0:4edfb8401555 146 static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type) {
tuthai 0:4edfb8401555 147 //Interrupt callback function for Vsync interruption
tuthai 0:4edfb8401555 148 if (vsync_count > 0) {
tuthai 0:4edfb8401555 149 vsync_count--;
tuthai 0:4edfb8401555 150 }
tuthai 0:4edfb8401555 151 }
tuthai 0:4edfb8401555 152
tuthai 0:4edfb8401555 153 static void WaitVsync(const int32_t wait_count) {
tuthai 0:4edfb8401555 154 //Wait for the specified number of times Vsync occurs
tuthai 0:4edfb8401555 155 vsync_count = wait_count;
tuthai 0:4edfb8401555 156 while (vsync_count > 0) {
tuthai 0:4edfb8401555 157 /* Do nothing */
tuthai 0:4edfb8401555 158 }
tuthai 0:4edfb8401555 159 }
tuthai 0:4edfb8401555 160
tuthai 0:4edfb8401555 161 static void camera_start(void) {
tuthai 0:4edfb8401555 162 DisplayBase::graphics_error_t error;
tuthai 0:4edfb8401555 163
tuthai 0:4edfb8401555 164 #if VIDEO_INPUT_METHOD == VIDEO_CMOS_CAMERA
tuthai 0:4edfb8401555 165 DisplayBase::video_ext_in_config_t ext_in_config;
tuthai 0:4edfb8401555 166 PinName cmos_camera_pin[11] = {
tuthai 0:4edfb8401555 167 /* data pin */
tuthai 0:4edfb8401555 168 P2_7, P2_6, P2_5, P2_4, P2_3, P2_2, P2_1, P2_0,
tuthai 0:4edfb8401555 169 /* control pin */
tuthai 0:4edfb8401555 170 P10_0, /* DV0_CLK */
tuthai 0:4edfb8401555 171 P1_0, /* DV0_Vsync */
tuthai 0:4edfb8401555 172 P1_1 /* DV0_Hsync */
tuthai 0:4edfb8401555 173 };
tuthai 0:4edfb8401555 174 #endif
tuthai 0:4edfb8401555 175
tuthai 0:4edfb8401555 176 /* Create DisplayBase object */
tuthai 0:4edfb8401555 177 DisplayBase Display;
tuthai 0:4edfb8401555 178
tuthai 0:4edfb8401555 179 /* Graphics initialization process */
tuthai 0:4edfb8401555 180 error = Display.Graphics_init(NULL);
tuthai 0:4edfb8401555 181 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 182 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 183 while (1);
tuthai 0:4edfb8401555 184 }
tuthai 0:4edfb8401555 185
tuthai 0:4edfb8401555 186 #if VIDEO_INPUT_METHOD == VIDEO_CVBS
tuthai 0:4edfb8401555 187 error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_VDEC, NULL);
tuthai 0:4edfb8401555 188 if( error != DisplayBase::GRAPHICS_OK ) {
tuthai 0:4edfb8401555 189 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 190 while(1);
tuthai 0:4edfb8401555 191 }
tuthai 0:4edfb8401555 192
tuthai 0:4edfb8401555 193 #elif VIDEO_INPUT_METHOD == VIDEO_CMOS_CAMERA
tuthai 0:4edfb8401555 194 /* MT9V111 camera input config */
tuthai 0:4edfb8401555 195 ext_in_config.inp_format = DisplayBase::VIDEO_EXTIN_FORMAT_BT601; /* BT601 8bit YCbCr format */
tuthai 0:4edfb8401555 196 ext_in_config.inp_pxd_edge = DisplayBase::EDGE_RISING; /* Clock edge select for capturing data */
tuthai 0:4edfb8401555 197 ext_in_config.inp_vs_edge = DisplayBase::EDGE_RISING; /* Clock edge select for capturing Vsync signals */
tuthai 0:4edfb8401555 198 ext_in_config.inp_hs_edge = DisplayBase::EDGE_RISING; /* Clock edge select for capturing Hsync signals */
tuthai 0:4edfb8401555 199 ext_in_config.inp_endian_on = DisplayBase::OFF; /* External input bit endian change on/off */
tuthai 0:4edfb8401555 200 ext_in_config.inp_swap_on = DisplayBase::OFF; /* External input B/R signal swap on/off */
tuthai 0:4edfb8401555 201 ext_in_config.inp_vs_inv = DisplayBase::SIG_POL_NOT_INVERTED; /* External input DV_VSYNC inversion control */
tuthai 0:4edfb8401555 202 ext_in_config.inp_hs_inv = DisplayBase::SIG_POL_INVERTED; /* External input DV_HSYNC inversion control */
tuthai 0:4edfb8401555 203 ext_in_config.inp_f525_625 = DisplayBase::EXTIN_LINE_525; /* Number of lines for BT.656 external input */
tuthai 0:4edfb8401555 204 ext_in_config.inp_h_pos = DisplayBase::EXTIN_H_POS_CRYCBY; /* Y/Cb/Y/Cr data string start timing to Hsync reference */
tuthai 0:4edfb8401555 205 ext_in_config.cap_vs_pos = 6; /* Capture start position from Vsync */
tuthai 0:4edfb8401555 206 ext_in_config.cap_hs_pos = 150; /* Capture start position form Hsync */
tuthai 0:4edfb8401555 207 ext_in_config.cap_width = 640; /* Capture width */
tuthai 0:4edfb8401555 208 ext_in_config.cap_height = 468u; /* Capture height Max 468[line]
tuthai 0:4edfb8401555 209 Due to CMOS(MT9V111) output signal timing and VDC5 specification */
tuthai 0:4edfb8401555 210 error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_EXT, &ext_in_config);
tuthai 0:4edfb8401555 211 if( error != DisplayBase::GRAPHICS_OK ) {
tuthai 0:4edfb8401555 212 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 213 while(1);
tuthai 0:4edfb8401555 214 }
tuthai 0:4edfb8401555 215
tuthai 0:4edfb8401555 216 /* MT9V111 camera input port setting */
tuthai 0:4edfb8401555 217 error = Display.Graphics_Dvinput_Port_Init(cmos_camera_pin, 11);
tuthai 0:4edfb8401555 218 if( error != DisplayBase::GRAPHICS_OK ) {
tuthai 0:4edfb8401555 219 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 220 while (1);
tuthai 0:4edfb8401555 221 }
tuthai 0:4edfb8401555 222 #endif
tuthai 0:4edfb8401555 223
tuthai 0:4edfb8401555 224 /* Interrupt callback function setting (Vsync signal input to scaler 0) */
tuthai 0:4edfb8401555 225 error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VI_VSYNC, 0, IntCallbackFunc_Vsync);
tuthai 0:4edfb8401555 226 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 227 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 228 while (1);
tuthai 0:4edfb8401555 229 }
tuthai 0:4edfb8401555 230 /* Video capture setting (progressive form fixed) */
tuthai 0:4edfb8401555 231 error = Display.Video_Write_Setting(
tuthai 0:4edfb8401555 232 VIDEO_INPUT_CH,
tuthai 0:4edfb8401555 233 #if VIDEO_PAL == 0
tuthai 0:4edfb8401555 234 DisplayBase::COL_SYS_NTSC_358,
tuthai 0:4edfb8401555 235 #else
tuthai 0:4edfb8401555 236 DisplayBase::COL_SYS_PAL_443,
tuthai 0:4edfb8401555 237 #endif
tuthai 0:4edfb8401555 238 FrameBuffer_Video,
tuthai 0:4edfb8401555 239 VIDEO_BUFFER_STRIDE,
tuthai 0:4edfb8401555 240 #if VIDEO_INPUT_FORMAT == VIDEO_YCBCR422
tuthai 0:4edfb8401555 241 DisplayBase::VIDEO_FORMAT_YCBCR422,
tuthai 0:4edfb8401555 242 DisplayBase::WR_RD_WRSWA_NON,
tuthai 0:4edfb8401555 243 #elif VIDEO_INPUT_FORMAT == VIDEO_RGB565
tuthai 0:4edfb8401555 244 DisplayBase::VIDEO_FORMAT_RGB565,
tuthai 0:4edfb8401555 245 DisplayBase::WR_RD_WRSWA_32_16BIT,
tuthai 0:4edfb8401555 246 #else
tuthai 0:4edfb8401555 247 DisplayBase::VIDEO_FORMAT_RGB888,
tuthai 0:4edfb8401555 248 DisplayBase::WR_RD_WRSWA_32BIT,
tuthai 0:4edfb8401555 249 #endif
tuthai 0:4edfb8401555 250 PIXEL_VW,
tuthai 0:4edfb8401555 251 PIXEL_HW
tuthai 0:4edfb8401555 252 );
tuthai 0:4edfb8401555 253 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 254 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 255 while (1);
tuthai 0:4edfb8401555 256 }
tuthai 0:4edfb8401555 257
tuthai 0:4edfb8401555 258 /* Interrupt callback function setting (Field end signal for recording function in scaler 0) */
tuthai 0:4edfb8401555 259 error = Display.Graphics_Irq_Handler_Set(VIDEO_INT_TYPE, 0, IntCallbackFunc_Vfield);
tuthai 0:4edfb8401555 260 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 261 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 262 while (1);
tuthai 0:4edfb8401555 263 }
tuthai 0:4edfb8401555 264
tuthai 0:4edfb8401555 265 /* Video write process start */
tuthai 0:4edfb8401555 266 error = Display.Video_Start (VIDEO_INPUT_CH);
tuthai 0:4edfb8401555 267 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 268 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 269 while (1);
tuthai 0:4edfb8401555 270 }
tuthai 0:4edfb8401555 271
tuthai 0:4edfb8401555 272 /* Video write process stop */
tuthai 0:4edfb8401555 273 error = Display.Video_Stop (VIDEO_INPUT_CH);
tuthai 0:4edfb8401555 274 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 275 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 276 while (1);
tuthai 0:4edfb8401555 277 }
tuthai 0:4edfb8401555 278
tuthai 0:4edfb8401555 279 /* Video write process start */
tuthai 0:4edfb8401555 280 error = Display.Video_Start (VIDEO_INPUT_CH);
tuthai 0:4edfb8401555 281 if (error != DisplayBase::GRAPHICS_OK) {
tuthai 0:4edfb8401555 282 printf("Line %d, error %d\n", __LINE__, error);
tuthai 0:4edfb8401555 283 while (1);
tuthai 0:4edfb8401555 284 }
tuthai 0:4edfb8401555 285
tuthai 0:4edfb8401555 286 /* Wait vsync to update resister */
tuthai 0:4edfb8401555 287 WaitVsync(1);
tuthai 0:4edfb8401555 288 }
tuthai 0:4edfb8401555 289
tuthai 0:4edfb8401555 290 static int snapshot_req(const char ** pp_data) {
tuthai 0:4edfb8401555 291 int encode_size;
tuthai 0:4edfb8401555 292
tuthai 0:4edfb8401555 293 while ((jcu_encoding == 1) || (image_change == 0)) {
tuthai 0:4edfb8401555 294 Thread::wait(1);
tuthai 0:4edfb8401555 295 }
tuthai 0:4edfb8401555 296 jcu_buf_index_read = jcu_buf_index_write_done;
tuthai 0:4edfb8401555 297 image_change = 0;
tuthai 0:4edfb8401555 298
tuthai 0:4edfb8401555 299 *pp_data = (const char *)JpegBuffer[jcu_buf_index_read];
tuthai 0:4edfb8401555 300 encode_size = (int)jcu_encode_size[jcu_buf_index_read];
tuthai 0:4edfb8401555 301
tuthai 0:4edfb8401555 302 return encode_size;
tuthai 0:4edfb8401555 303 }
tuthai 0:4edfb8401555 304
tuthai 0:4edfb8401555 305 static void TerminalWrite(Arguments* arg, Reply* r) {
tuthai 0:4edfb8401555 306 if ((arg != NULL) && (r != NULL)) {
tuthai 0:4edfb8401555 307 for (int i = 0; i < arg->argc; i++) {
tuthai 0:4edfb8401555 308 if (arg->argv[i] != NULL) {
tuthai 0:4edfb8401555 309 printf("%s", arg->argv[i]);
tuthai 0:4edfb8401555 310 }
tuthai 0:4edfb8401555 311 }
tuthai 0:4edfb8401555 312 printf("\n");
tuthai 0:4edfb8401555 313 r->putData<const char*>("ok");
tuthai 0:4edfb8401555 314 }
tuthai 0:4edfb8401555 315 }
tuthai 0:4edfb8401555 316
tuthai 0:4edfb8401555 317 static void mount_romramfs(void) {
tuthai 0:4edfb8401555 318 FILE * fp;
tuthai 0:4edfb8401555 319
tuthai 0:4edfb8401555 320 romram_bd.SetRomAddr(0x18000000, 0x1FFFFFFF);
tuthai 0:4edfb8401555 321 fs.format(&romram_bd, 512);
tuthai 0:4edfb8401555 322 fs.mount(&romram_bd);
tuthai 0:4edfb8401555 323
tuthai 0:4edfb8401555 324 //index.htm
tuthai 0:4edfb8401555 325 fp = fopen("/storage/index.htm", "w");
tuthai 0:4edfb8401555 326 fwrite(index_htm_tbl, sizeof(char), sizeof(index_htm_tbl), fp);
tuthai 0:4edfb8401555 327 fclose(fp);
tuthai 0:4edfb8401555 328
tuthai 0:4edfb8401555 329 //camera.js
tuthai 0:4edfb8401555 330 fp = fopen("/storage/camera.js", "w");
tuthai 0:4edfb8401555 331 fwrite(camaera_js_tbl, sizeof(char), sizeof(camaera_js_tbl), fp);
tuthai 0:4edfb8401555 332 fclose(fp);
tuthai 0:4edfb8401555 333
tuthai 0:4edfb8401555 334 //camera.htm
tuthai 0:4edfb8401555 335 fp = fopen("/storage/camera.htm", "w");
tuthai 0:4edfb8401555 336 fwrite(camera_htm_tbl, sizeof(char), sizeof(camera_htm_tbl), fp);
tuthai 0:4edfb8401555 337 fclose(fp);
tuthai 0:4edfb8401555 338
tuthai 0:4edfb8401555 339 //mbedrpc.js
tuthai 0:4edfb8401555 340 fp = fopen("/storage/mbedrpc.js", "w");
tuthai 0:4edfb8401555 341 fwrite(mbedrpc_js_tbl, sizeof(char), sizeof(mbedrpc_js_tbl), fp);
tuthai 0:4edfb8401555 342 fclose(fp);
tuthai 0:4edfb8401555 343
tuthai 0:4edfb8401555 344 //led.htm
tuthai 0:4edfb8401555 345 fp = fopen("/storage/led.htm", "w");
tuthai 0:4edfb8401555 346 fwrite(led_htm_tbl, sizeof(char), sizeof(led_htm_tbl), fp);
tuthai 0:4edfb8401555 347 fclose(fp);
tuthai 0:4edfb8401555 348
tuthai 0:4edfb8401555 349 //i2c_set.htm
tuthai 0:4edfb8401555 350 fp = fopen("/storage/i2c_set.htm", "w");
tuthai 0:4edfb8401555 351 fwrite(i2c_set_htm_tbl, sizeof(char), sizeof(i2c_set_htm_tbl), fp);
tuthai 0:4edfb8401555 352 fclose(fp);
tuthai 0:4edfb8401555 353
tuthai 0:4edfb8401555 354 //web_top.htm
tuthai 0:4edfb8401555 355 fp = fopen("/storage/web_top.htm", "w");
tuthai 0:4edfb8401555 356 fwrite(web_top_htm_tbl, sizeof(char), sizeof(web_top_htm_tbl), fp);
tuthai 0:4edfb8401555 357 fclose(fp);
tuthai 0:4edfb8401555 358
tuthai 0:4edfb8401555 359 //menu.htm
tuthai 0:4edfb8401555 360 fp = fopen("/storage/menu.htm", "w");
tuthai 0:4edfb8401555 361 fwrite(menu_htm_tbl, sizeof(char), sizeof(menu_htm_tbl), fp);
tuthai 0:4edfb8401555 362 fclose(fp);
tuthai 0:4edfb8401555 363
tuthai 0:4edfb8401555 364 //window.htm
tuthai 0:4edfb8401555 365 fp = fopen("/storage/window.htm", "w");
tuthai 0:4edfb8401555 366 fwrite(window_htm_tbl, sizeof(char), sizeof(window_htm_tbl), fp);
tuthai 0:4edfb8401555 367 fclose(fp);
tuthai 0:4edfb8401555 368 }
tuthai 0:4edfb8401555 369
tuthai 0:4edfb8401555 370 static void SetI2CfromWeb(Arguments* arg, Reply* r) {
tuthai 0:4edfb8401555 371 int result = 0;
tuthai 0:4edfb8401555 372
tuthai 0:4edfb8401555 373 if (arg != NULL) {
tuthai 0:4edfb8401555 374 if (arg->argc >= 2) {
tuthai 0:4edfb8401555 375 if ((arg->argv[0] != NULL) && (arg->argv[1] != NULL)) {
tuthai 0:4edfb8401555 376 sprintf(i2c_setting_str_buf, "%s,%s", arg->argv[0], arg->argv[1]);
tuthai 0:4edfb8401555 377 result = 1;
tuthai 0:4edfb8401555 378 }
tuthai 0:4edfb8401555 379 } else if (arg->argc == 1) {
tuthai 0:4edfb8401555 380 if (arg->argv[0] != NULL) {
tuthai 0:4edfb8401555 381 sprintf(i2c_setting_str_buf, "%s", arg->argv[0]);
tuthai 0:4edfb8401555 382 result = 1;
tuthai 0:4edfb8401555 383 }
tuthai 0:4edfb8401555 384 } else {
tuthai 0:4edfb8401555 385 /* Do nothing */
tuthai 0:4edfb8401555 386 }
tuthai 0:4edfb8401555 387 /* command analysis and execute */
tuthai 0:4edfb8401555 388 if (result != 0) {
tuthai 0:4edfb8401555 389 if (i2c_setting_exe(i2c_setting_str_buf) != false) {
tuthai 0:4edfb8401555 390 r->putData<const char*>(i2c_setting_str_buf);
tuthai 0:4edfb8401555 391 }
tuthai 0:4edfb8401555 392 }
tuthai 0:4edfb8401555 393 }
tuthai 0:4edfb8401555 394 }
tuthai 0:4edfb8401555 395
tuthai 0:4edfb8401555 396 #if (SCAN_NETWORK == 1)
tuthai 0:4edfb8401555 397 static const char *sec2str(nsapi_security_t sec) {
tuthai 0:4edfb8401555 398 switch (sec) {
tuthai 0:4edfb8401555 399 case NSAPI_SECURITY_NONE:
tuthai 0:4edfb8401555 400 return "None";
tuthai 0:4edfb8401555 401 case NSAPI_SECURITY_WEP:
tuthai 0:4edfb8401555 402 return "WEP";
tuthai 0:4edfb8401555 403 case NSAPI_SECURITY_WPA:
tuthai 0:4edfb8401555 404 return "WPA";
tuthai 0:4edfb8401555 405 case NSAPI_SECURITY_WPA2:
tuthai 0:4edfb8401555 406 return "WPA2";
tuthai 0:4edfb8401555 407 case NSAPI_SECURITY_WPA_WPA2:
tuthai 0:4edfb8401555 408 return "WPA/WPA2";
tuthai 0:4edfb8401555 409 case NSAPI_SECURITY_UNKNOWN:
tuthai 0:4edfb8401555 410 default:
tuthai 0:4edfb8401555 411 return "Unknown";
tuthai 0:4edfb8401555 412 }
tuthai 0:4edfb8401555 413 }
tuthai 0:4edfb8401555 414
tuthai 0:4edfb8401555 415 static bool scan_network(WiFiInterface *wifi) {
tuthai 0:4edfb8401555 416 WiFiAccessPoint *ap;
tuthai 0:4edfb8401555 417 bool ret = false;
tuthai 0:4edfb8401555 418 int i;
tuthai 0:4edfb8401555 419 int count = 8; /* Limit number of network arbitrary to 8 */
tuthai 0:4edfb8401555 420
tuthai 0:4edfb8401555 421 printf("Scan:\r\n");
tuthai 0:4edfb8401555 422 ap = new WiFiAccessPoint[count];
tuthai 0:4edfb8401555 423 count = wifi->scan(ap, count);
tuthai 0:4edfb8401555 424 for (i = 0; i < count; i++) {
tuthai 0:4edfb8401555 425 printf("No.%d Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", i, ap[i].get_ssid(),
tuthai 0:4edfb8401555 426 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
tuthai 0:4edfb8401555 427 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
tuthai 0:4edfb8401555 428 }
tuthai 0:4edfb8401555 429 printf("%d networks available.\r\n", count);
tuthai 0:4edfb8401555 430
tuthai 0:4edfb8401555 431 if (count > 0) {
tuthai 0:4edfb8401555 432 char c;
tuthai 0:4edfb8401555 433 char pass[64];
tuthai 0:4edfb8401555 434 int select_no;
tuthai 0:4edfb8401555 435 bool loop_break = false;;
tuthai 0:4edfb8401555 436
tuthai 0:4edfb8401555 437 printf("\nPlease enter the number of the network you want to connect.\r\n");
tuthai 0:4edfb8401555 438 printf("Enter key:[0]-[%d], (If inputting the other key, it's scanned again.)\r\n", count - 1);
tuthai 0:4edfb8401555 439 c = (uint8_t)pc.getc();
tuthai 0:4edfb8401555 440 select_no = c - 0x30;
tuthai 0:4edfb8401555 441 if ((select_no >= 0) && (select_no < count)) {
tuthai 0:4edfb8401555 442 printf("[%s] is selected.\r\n", ap[select_no].get_ssid());
tuthai 0:4edfb8401555 443 printf("Please enter the PSK.\r\n");
tuthai 0:4edfb8401555 444 i = 0;
tuthai 0:4edfb8401555 445 while (loop_break == false) {
tuthai 0:4edfb8401555 446 c = (uint8_t)pc.getc();
tuthai 0:4edfb8401555 447 switch (c) {
tuthai 0:4edfb8401555 448 case 0x0D:
tuthai 0:4edfb8401555 449 pass[i] = '\0';
tuthai 0:4edfb8401555 450 pc.puts("\r\n");
tuthai 0:4edfb8401555 451 loop_break = true;
tuthai 0:4edfb8401555 452 break;
tuthai 0:4edfb8401555 453 case 0x08:
tuthai 0:4edfb8401555 454 if (i > 0) {
tuthai 0:4edfb8401555 455 pc.puts("\b \b");
tuthai 0:4edfb8401555 456 i--;
tuthai 0:4edfb8401555 457 }
tuthai 0:4edfb8401555 458 break;
tuthai 0:4edfb8401555 459 case 0x0A:
tuthai 0:4edfb8401555 460 break;
tuthai 0:4edfb8401555 461 default:
tuthai 0:4edfb8401555 462 if ((i + 1) < sizeof(pass)) {
tuthai 0:4edfb8401555 463 pass[i] = c;
tuthai 0:4edfb8401555 464 i++;
tuthai 0:4edfb8401555 465 pc.putc(c);
tuthai 0:4edfb8401555 466 }
tuthai 0:4edfb8401555 467 break;
tuthai 0:4edfb8401555 468 }
tuthai 0:4edfb8401555 469 }
tuthai 0:4edfb8401555 470 printf("connecting...\r\n");
tuthai 0:4edfb8401555 471 wifi->set_credentials(ap[select_no].get_ssid(), pass, ap[select_no].get_security());
tuthai 0:4edfb8401555 472 ret = true;
tuthai 0:4edfb8401555 473 }
tuthai 0:4edfb8401555 474 }
tuthai 0:4edfb8401555 475
tuthai 0:4edfb8401555 476 delete[] ap;
tuthai 0:4edfb8401555 477
tuthai 0:4edfb8401555 478 return ret;
tuthai 0:4edfb8401555 479 }
tuthai 0:4edfb8401555 480 #endif
tuthai 0:4edfb8401555 481
tuthai 0:4edfb8401555 482 int main(void) {
tuthai 0:4edfb8401555 483 printf("********* PROGRAM START ***********\r\n");
tuthai 0:4edfb8401555 484
tuthai 0:4edfb8401555 485 /* Please enable this line when performing the setting from the Terminal side. */
tuthai 0:4edfb8401555 486 // Thread thread(SetI2CfromTerm, NULL, osPriorityBelowNormal, DEFAULT_STACK_SIZE);
tuthai 0:4edfb8401555 487
tuthai 0:4edfb8401555 488 mount_romramfs(); //RomRamFileSystem Mount
tuthai 0:4edfb8401555 489 camera_start(); //Camera Start
tuthai 0:4edfb8401555 490
tuthai 0:4edfb8401555 491 RPC::add_rpc_class<RpcDigitalOut>();
tuthai 0:4edfb8401555 492 RPC::construct<RpcDigitalOut, PinName, const char*>(LED1, "led1");
tuthai 0:4edfb8401555 493 RPC::construct<RpcDigitalOut, PinName, const char*>(LED2, "led2");
tuthai 0:4edfb8401555 494 RPC::construct<RpcDigitalOut, PinName, const char*>(LED3, "led3");
tuthai 0:4edfb8401555 495 RPCFunction rpcFunc(TerminalWrite, "TerminalWrite");
tuthai 0:4edfb8401555 496 RPCFunction rpcSetI2C(SetI2CfromWeb, "SetI2CfromWeb");
tuthai 0:4edfb8401555 497
tuthai 0:4edfb8401555 498 #if (NETWORK_TYPE == 1)
tuthai 0:4edfb8401555 499 //Audio Camera Shield USB1 enable for WlanBP3595
tuthai 0:4edfb8401555 500 usb1en = 1; //Outputs high level
tuthai 0:4edfb8401555 501 Thread::wait(5);
tuthai 0:4edfb8401555 502 usb1en = 0; //Outputs low level
tuthai 0:4edfb8401555 503 Thread::wait(5);
tuthai 0:4edfb8401555 504 #endif
tuthai 0:4edfb8401555 505
tuthai 0:4edfb8401555 506 printf("Network Setting up...\r\n");
tuthai 0:4edfb8401555 507 #if (USE_DHCP == 0)
tuthai 0:4edfb8401555 508 network.set_dhcp(false);
tuthai 0:4edfb8401555 509 if (network.set_network(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY) != 0) { //for Static IP Address (IPAddress, NetMasks, Gateway)
tuthai 0:4edfb8401555 510 printf("Network Set Network Error \r\n");
tuthai 0:4edfb8401555 511 return -1;
tuthai 0:4edfb8401555 512 }
tuthai 0:4edfb8401555 513 #endif
tuthai 0:4edfb8401555 514 #if (NETWORK_TYPE == 1)
tuthai 0:4edfb8401555 515 #if (SCAN_NETWORK == 1)
tuthai 0:4edfb8401555 516 while (!scan_network(&network));
tuthai 0:4edfb8401555 517 #else
tuthai 0:4edfb8401555 518 network.set_credentials(WLAN_SSID, WLAN_PSK, WLAN_SECURITY);
tuthai 0:4edfb8401555 519 #endif
tuthai 0:4edfb8401555 520 #endif
tuthai 0:4edfb8401555 521 if (network.connect() != 0) {
tuthai 0:4edfb8401555 522 printf("Network Connect Error \r\n");
tuthai 0:4edfb8401555 523 return -1;
tuthai 0:4edfb8401555 524 }
tuthai 0:4edfb8401555 525 printf("MAC Address is %s\r\n", network.get_mac_address());
tuthai 0:4edfb8401555 526 printf("IP Address is %s\r\n", network.get_ip_address());
tuthai 0:4edfb8401555 527 printf("NetMask is %s\r\n", network.get_netmask());
tuthai 0:4edfb8401555 528 printf("Gateway Address is %s\r\n", network.get_gateway());
tuthai 0:4edfb8401555 529 printf("Network Setup OK\r\n");
tuthai 0:4edfb8401555 530
tuthai 0:4edfb8401555 531 SnapshotHandler::attach_req(&snapshot_req);
tuthai 0:4edfb8401555 532 HTTPServerAddHandler<SnapshotHandler>("/camera"); //Camera
tuthai 0:4edfb8401555 533 FSHandler::mount("/storage", "/");
tuthai 0:4edfb8401555 534 HTTPServerAddHandler<FSHandler>("/");
tuthai 0:4edfb8401555 535 HTTPServerAddHandler<RPCHandler>("/rpc");
tuthai 0:4edfb8401555 536 HTTPServerStart(&network, 80);
tuthai 0:4edfb8401555 537 }