I made Digital Camera using Arducam & WIZwiki-W7500
Dependencies: Arduino Arducam_OV5642 SDFileSystem Arducam_UTFT_SPI WIZnetInterface_Ricky mbed
main.cpp
00001 #include "mbed.h" 00002 #include "UTFT_SPI.h" 00003 #include "OV5642.h" 00004 #include "OV5642_regs.h" 00005 #include "SDFileSystem.h" 00006 #include "Arduino.h" 00007 #include "EthernetInterface.h" 00008 #include <stdio.h> 00009 #include <string.h> 00010 00011 ArduCAM myCAM(D11, D12, D13, D10, D14, D15); 00012 ArduLCD myGLCD(D11, D12, D13, D10); 00013 SDFileSystem sd(D11, D12, D13, D9, "sd"); 00014 00015 Serial pc(USBTX, USBRX); 00016 bool isShowFlag = true; 00017 char fnamecamera[32]; 00018 char fnamecntcamera=0; 00019 00020 /* FTP */ 00021 #define USER "user " 00022 #define PASS "pass " 00023 #define PASV "pasv " 00024 #define PORT "port " 00025 #define LIST "list " 00026 #define STOR "stor " 00027 #define RETR "retr " 00028 #define END "\r\n" 00029 #define FTP_SERVER_PORT 21 00030 #define MAX_SS 256 00031 bool gDataSockReady = false; 00032 bool gDataPutGetStart = false; 00033 bool ftpclientrun = false; 00034 int remote_port; 00035 char ftpServer_data_ip_addr[4]={0,}; 00036 char ftpServer_data_ip_addr_str[20]={0,}; 00037 static char buf[256]; 00038 static char ID[]={"abc"}; //Set FTPServer Login ID 00039 static char PASSWORD[]={"123"}; //Set FTPServer Login Password 00040 enum CommandFirst { 00041 f_nocmd, 00042 f_put, 00043 }; 00044 enum CommandSecond { 00045 s_nocmd, 00046 s_put, 00047 }; 00048 enum ftpc_datasock_state{ 00049 DATASOCK_IDLE, 00050 DATASOCK_READY, 00051 DATASOCK_START 00052 }; 00053 struct Command { 00054 enum CommandFirst First; 00055 enum CommandSecond Second; 00056 }; 00057 struct ftpc { 00058 enum ftpc_datasock_state dsock_state; 00059 }; 00060 struct ftpc FTPClient; 00061 struct Command FTPCommand; 00062 00063 /* SD Card filesystem */ 00064 static FILE *fp_jpeg; 00065 char fname[32]; 00066 char fname_server[16]; 00067 char fnamecnt=0; 00068 char gMsgBuf[10]; 00069 char User_Keyboard_MSG_Cnt; 00070 /* Function*/ 00071 char* User_Keyboard_MSG(void); 00072 int pportc(char * arg); 00073 void FTP_server_uploader(); 00074 void setup(); 00075 void loop(); 00076 void itoa( unsigned long long int value, char *str); 00077 00078 int main() 00079 { 00080 *(volatile uint32_t *)(0x41001014) = 0x0060200; //clock 24MHz 00081 00082 setup(); 00083 00084 //FTP_server_uploader(); 00085 while(1) 00086 { 00087 loop(); 00088 } 00089 } 00090 00091 void setup() 00092 { 00093 uint8_t vid,pid; 00094 uint8_t temp; 00095 00096 pc.baud(115200); 00097 00098 pc.printf("ArduCAM Start!\r\n"); 00099 00100 uint8_t temp1,temp2; 00101 myCAM.write_reg(ARDUCHIP_TEST1, 0x55); //Write to test1 register by 0x55 00102 myCAM.write_reg(ARDUCHIP_TEST2, 0xAA); //Write to test1 register by 0xaa 00103 wait_ms(1000); 00104 temp1 = myCAM.read_reg(ARDUCHIP_TEST1); //Read from test1 register 00105 temp2 = myCAM.read_reg(ARDUCHIP_TEST2); //Read from test1 register 00106 pc.printf("temp1 : %d\r\n",temp1); 00107 pc.printf("temp2 : %d\r\n",temp2); 00108 wait_ms(1000); 00109 00110 myCAM.write_reg(ARDUCHIP_TEST1, 0x55); 00111 temp = myCAM.read_reg(ARDUCHIP_TEST1); 00112 00113 if(temp != 0x55) 00114 { 00115 pc.printf("SPI interface Error!\r\n"); 00116 while(1); 00117 } 00118 00119 //Change MCU mode 00120 myCAM.set_mode(MCU2LCD_MODE); 00121 00122 //Initialize the LCD Module 00123 myGLCD.InitLCD(); 00124 00125 //Check if the camera module type is OV5642 00126 myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid); 00127 myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid); 00128 if((vid != 0x56) || (pid != 0x42)) 00129 pc.printf("Can't find OV5642 module!\r\n"); 00130 else 00131 pc.printf("OV5642 detected\r\n"); 00132 00133 //Change to BMP capture mode and initialize the OV5642 module 00134 myCAM.set_format(BMP); 00135 00136 myCAM.InitCAM(); 00137 } 00138 00139 void loop() 00140 { 00141 FILE *outFile; 00142 uint8_t buf[256]; 00143 static int i = 0; 00144 uint8_t temp,temp_last; 00145 uint8_t start_capture = 0; 00146 00147 //Wait trigger from shutter buttom 00148 if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK)) 00149 { 00150 isShowFlag = false; 00151 myCAM.set_mode(MCU2LCD_MODE); 00152 myCAM.set_format(JPEG); 00153 myCAM.InitCAM(); 00154 myCAM.OV5642_set_JPEG_size(OV5642_1920x1080); 00155 myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH 00156 00157 //Wait until buttom released 00158 while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK)); 00159 wait_ms(1000); 00160 start_capture = 1; 00161 } 00162 else 00163 { 00164 if(isShowFlag ) 00165 { 00166 00167 if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming 00168 { 00169 myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU 00170 myGLCD.resetXY(); 00171 myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM 00172 while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone 00173 } 00174 } 00175 } 00176 if(start_capture) 00177 { 00178 //Flush the FIFO 00179 myCAM.flush_fifo(); 00180 //Clear the capture done flag 00181 myCAM.clear_fifo_flag(); 00182 //Start capture 00183 myCAM.start_capture(); 00184 pc.printf("Start Capture\r\n"); 00185 } 00186 00187 if(myCAM.get_bit(ARDUCHIP_TRIG ,CAP_DONE_MASK)) 00188 { 00189 00190 pc.printf("Capture Done!\r\n"); 00191 //Construct a file name 00192 snprintf(fnamecamera, sizeof(fnamecamera), "/sd/jpss%04d.jpg", fnamecntcamera); 00193 fnamecntcamera++; 00194 //Open the new file 00195 outFile = fopen(fnamecamera,"w"); 00196 if (! outFile) 00197 { 00198 pc.printf("open file failed\r\n"); 00199 return; 00200 } 00201 //Read first dummy byte 00202 //myCAM.read_fifo(); 00203 00204 i = 0; 00205 temp = myCAM.read_fifo(); 00206 //Write first image data to buffer 00207 buf[i++] = temp; 00208 00209 //Read JPEG data from FIFO 00210 while( (temp != 0xD9) | (temp_last != 0xFF) ) 00211 { 00212 temp_last = temp; 00213 temp = myCAM.read_fifo(); 00214 //Write image data to buffer if not full 00215 if(i < 256) 00216 buf[i++] = temp; 00217 else 00218 { 00219 //Write 256 bytes image data to file 00220 fwrite(buf,256,1,outFile); 00221 i = 0; 00222 buf[i++] = temp; 00223 } 00224 } 00225 //Write the remain bytes in the buffer 00226 if(i > 0) 00227 fwrite(buf,i,1,outFile); 00228 00229 //Close the file 00230 fclose(outFile); 00231 //Clear the capture done flag 00232 myCAM.clear_fifo_flag(); 00233 //Clear the start capture flag 00234 start_capture = 0; 00235 00236 myCAM.set_format(BMP); 00237 myCAM.InitCAM(); 00238 isShowFlag = true; 00239 } 00240 } 00241 00242 /* itoa: convert n to characters in s */ 00243 void itoa( unsigned long long int value, char *str) 00244 { 00245 int i,j; 00246 char temp[30]; 00247 for(i=0; value > 0; i++){ 00248 str[i] = value%10+'0'; 00249 value=value/10; 00250 } 00251 for(j=0;i>=0;j++,i--){ 00252 temp[j]=str[i-1]; 00253 } 00254 for(i=0;i<j;i++){ 00255 str[i]=temp[i]; 00256 } 00257 } 00258 00259 void FTP_server_uploader(){ 00260 00261 char Msg_c; 00262 int remain_filesize; 00263 int send_byte; 00264 00265 pc.printf("Hello FTP Camera!\r\n"); 00266 00267 uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45}; 00268 const char ip_addr[] = "192.168.0.123"; // WIZwiki-W7500 IP-address 00269 const char mask_addr[] = "255.255.255.0"; 00270 const char gateway_addr[] = "192.168.0.1"; 00271 const char ftpServer_control_ip_addr[] = "192.168.0.209"; // my IP address 00272 00273 00274 EthernetInterface eth; 00275 eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static 00276 eth.connect(); 00277 00278 TCPSocketConnection FTP_CONTROL_SOCK; 00279 TCPSocketConnection FTP_DATA_SOCK; 00280 00281 00282 pc.printf("\r\n----------------------------------------\r\n"); 00283 pc.printf("Press menu key\r\n"); 00284 pc.printf("----------------------------------------\r\n"); 00285 pc.printf("1> Snapshot Picture and Send to FTPServer\r\n"); 00286 pc.printf("----------------------------------------\r\n"); 00287 Msg_c = pc.getc(); 00288 if(Msg_c==0x31){ 00289 00290 ftpclientrun = true; 00291 while(ftpclientrun){ 00292 //while 2 00293 while(!FTP_CONTROL_SOCK.is_connected()){ 00294 pc.printf("Connecting...FTPServer\r\nIP:%s, PORT:%d\r\n", ftpServer_control_ip_addr, FTP_SERVER_PORT); 00295 FTP_CONTROL_SOCK.connect(ftpServer_control_ip_addr, FTP_SERVER_PORT); 00296 FTP_CONTROL_SOCK.set_blocking(false, 15000); // Timeout after (1.5)s 00297 }//end of while 2 00298 00299 //while 3 00300 while(true) 00301 { 00302 //gDataSockReady if 00303 if(gDataSockReady){ 00304 gDataSockReady = false; 00305 //FTPCommand.First switch case 00306 switch(FTPCommand.First){ 00307 case f_put: 00308 FTP_CONTROL_SOCK.send(STOR, sizeof(STOR)-1); 00309 FTP_CONTROL_SOCK.send(fname_server, sizeof(fname_server)); 00310 FTP_CONTROL_SOCK.send(END, sizeof(END)-1); 00311 break; 00312 }//end of FTPCommand.First switch case 00313 }//end of gDataSockReady if 00314 /* received from FTPServer */ 00315 int n = FTP_CONTROL_SOCK.receive(buf, sizeof(buf)); 00316 if (n <= 0) break; 00317 buf[n] = '\0'; 00318 pc.printf("\r\nReceived message from server: '%s'\r\n", buf); 00319 00320 //buf if 00321 if (!strncmp(buf, "220", 3)){ 00322 FTP_CONTROL_SOCK.send(USER, sizeof(USER)-1); 00323 FTP_CONTROL_SOCK.send(ID, sizeof(ID)); 00324 FTP_CONTROL_SOCK.send(END, sizeof(END)-1); 00325 } 00326 else if(!strncmp(buf, "331", 3)){ 00327 FTP_CONTROL_SOCK.send(PASS, sizeof(PASS)-1); 00328 FTP_CONTROL_SOCK.send(PASSWORD, sizeof(PASSWORD)); 00329 FTP_CONTROL_SOCK.send(END, sizeof(END)-1); 00330 } 00331 else if(!strncmp(buf, "230", 3)){ 00332 FTP_CONTROL_SOCK.send(PASV, sizeof(PASV)-1); 00333 FTP_CONTROL_SOCK.send(END, sizeof(END)-1); 00334 FTPCommand.First = f_put; 00335 } 00336 else if(!strncmp(buf, "227", 3)){ 00337 if (pportc(buf) == -1){ 00338 pc.printf("Bad port syntax\r\n"); 00339 } 00340 else{ 00341 pc.printf("Go Open Data Sock...\r\n "); 00342 FTPClient.dsock_state = DATASOCK_READY; 00343 } 00344 } 00345 else if(!strncmp(buf, "150", 3)){ 00346 switch(FTPCommand.First){ 00347 case f_put: 00348 FTPCommand.First = f_nocmd; 00349 FTPCommand.Second = s_put; 00350 gDataPutGetStart = true; 00351 break; 00352 } 00353 } 00354 else if(!strncmp(buf, "226", 3)){ 00355 ftpclientrun = false; 00356 FTP_CONTROL_SOCK.close(); 00357 }//end of buf if 00358 00359 00360 //DATASOCK_READY if 00361 if(FTPClient.dsock_state == DATASOCK_READY){ 00362 while(!FTP_DATA_SOCK.is_connected()){ 00363 pc.printf("Connecting...FTPServer Data sock\r\nIP:%s, PORT:%d\r\n", ftpServer_data_ip_addr_str, remote_port); 00364 FTP_DATA_SOCK.connect(ftpServer_control_ip_addr, remote_port); 00365 FTP_DATA_SOCK.set_blocking(false, 15000); // Timeout after (1.5)s 00366 } 00367 FTPClient.dsock_state = DATASOCK_IDLE; 00368 gDataSockReady = true; 00369 }//end of DATASOCK_READY if 00370 00371 //gDataPutGetStart if 00372 if(gDataPutGetStart){ 00373 //FTPCommand.Second switch case 00374 switch(FTPCommand.Second){ 00375 case s_put: 00376 pc.printf("put waiting...\r\n"); 00377 snprintf(fname, sizeof(fname), "/sd/jpss0000.jpg"); 00378 fp_jpeg = fopen(fname, "r"); 00379 fseek(fp_jpeg, 0, SEEK_END); // seek to end of file 00380 remain_filesize = ftell(fp_jpeg); // get current file pointer 00381 fseek(fp_jpeg, 0, SEEK_SET); // seek back to beginning of file 00382 do{ 00383 memset(buf, 0, sizeof(buf)); 00384 if(remain_filesize > MAX_SS) 00385 send_byte = MAX_SS; 00386 else 00387 send_byte = remain_filesize; 00388 fread (buf, 1, send_byte, fp_jpeg); 00389 FTP_DATA_SOCK.send(buf, send_byte); 00390 remain_filesize -= send_byte; 00391 pc.printf("#"); 00392 }while(remain_filesize!=0); 00393 fclose(fp_jpeg); 00394 gDataPutGetStart = false; 00395 FTPCommand.Second = s_nocmd; 00396 FTP_DATA_SOCK.close(); 00397 break; 00398 00399 }//end of FTPCommand.Second switch case 00400 }//end of gDataPutGetStart if 00401 }//end of while 3 00402 } 00403 } 00404 } 00405 00406 char* User_Keyboard_MSG(void) 00407 { 00408 User_Keyboard_MSG_Cnt = 0; 00409 memset(gMsgBuf, 0, sizeof(gMsgBuf)); 00410 do{ 00411 gMsgBuf[User_Keyboard_MSG_Cnt] = pc.getc(); 00412 if(gMsgBuf[User_Keyboard_MSG_Cnt]==0x08){ 00413 User_Keyboard_MSG_Cnt--; 00414 } 00415 else{ 00416 User_Keyboard_MSG_Cnt++; 00417 } 00418 }while(gMsgBuf[User_Keyboard_MSG_Cnt-1]!=0x0d); 00419 return gMsgBuf; 00420 } 00421 00422 int pportc(char * arg) 00423 { 00424 int i; 00425 char* tok=0; 00426 strtok(arg,"("); 00427 for (i = 0; i < 4; i++) 00428 { 00429 if(i==0) tok = strtok(NULL,",\r\n"); 00430 else tok = strtok(NULL,","); 00431 ftpServer_data_ip_addr[i] = (uint8_t)atoi(tok); 00432 if (!tok){ 00433 pc.printf("bad pport : %s\r\n", arg); 00434 return -1; 00435 } 00436 } 00437 remote_port = 0; 00438 for (i = 0; i < 2; i++){ 00439 tok = strtok(NULL,",\r\n"); 00440 remote_port <<= 8; 00441 remote_port += atoi(tok); 00442 if (!tok){ 00443 pc.printf("bad pport : %s\r\n", arg); 00444 return -1; 00445 } 00446 } 00447 pc.printf("ip : %d.%d.%d.%d, port : %d\r\n", ftpServer_data_ip_addr[0], ftpServer_data_ip_addr[1], ftpServer_data_ip_addr[2], ftpServer_data_ip_addr[3], remote_port); 00448 sprintf(ftpServer_data_ip_addr_str, "%d.%d.%d.%d", ftpServer_data_ip_addr[0], ftpServer_data_ip_addr[1], ftpServer_data_ip_addr[2], ftpServer_data_ip_addr[3]); 00449 return 0; 00450 } 00451
Generated on Mon Jul 25 2022 04:01:51 by 1.7.2