Digital Photo Frame using FTP Client

Dependencies:   FTPClient SDFileSystem SeeedStudioTFTv2 TFT_fonts WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "SDFileSystem.h"
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 #include "FTPClient.h"
00008 #include "MySeeedStudioTFTv2.h"
00009 #include "Arial12x12.h"
00010 #include "Arial24x23.h"
00011 #include "Arial28x28.h"
00012 #include "font_big.h"
00013 
00014 #define PIN_XP          A3
00015 #define PIN_XM          A1
00016 #define PIN_YP          A2
00017 #define PIN_YM          A0
00018 #define PIN_MOSI        D11
00019 #define PIN_MISO        D12
00020 #define PIN_SCLK        D13
00021 #define PIN_CS_TFT      D5
00022 #define PIN_DC_TFT      D6
00023 #define PIN_BL_TFT      D7
00024 #define PIN_CS_SD       D4
00025 
00026 #define MAC     "\x00\x08\xDC\x11\x34\x78"
00027 #define IP      "192.168.1.100"
00028 #define MASK    "255.255.255.0"
00029 #define GATEWAY "192.168.1.1"
00030 
00031 #define FTP_SERVER_IP "192.168.1.200"
00032 
00033 #define _MAX_FNAME_LEN_   127
00034 #define _FTP_UPDATE_TIME_  20
00035 
00036 
00037 Serial uart(USBTX, USBRX);
00038 
00039 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
00040 //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
00041 //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
00042 //SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 
00043 
00044 EthernetInterface eth;
00045 
00046 DigitalOut led1(LED1); //server listning status
00047 DigitalOut led2(LED2); //socket connecting status
00048 
00049 
00050 MySeeedStudioTFTv2 TFT(PIN_XP, PIN_XM, PIN_YP, PIN_YM, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_DC_TFT, PIN_BL_TFT, PIN_CS_SD);
00051 
00052 
00053 FTPClient myFTP("/sdc");  // mountname in MySeeedStudioTFTv2
00054 
00055 Ticker ledTick;
00056 
00057 uint32_t ftp_time_1s = 0;
00058 
00059 char myfilelist[MAX_SS] = {0,};
00060 
00061 void ledTickfunc()
00062 {
00063     led1 = !led1;
00064     if(ftp_time_1s)
00065     {
00066         if(ftp_time_1s++ > _FTP_UPDATE_TIME_) ftp_time_1s = 0;
00067     }
00068 }
00069 
00070 
00071 int main (void)
00072 {
00073     int ret;
00074     char* tok = NULL;
00075     char* lasts = NULL;
00076     char filename[_MAX_FNAME_LEN_];
00077     FILE* fp;
00078    
00079 //    Serial Interface eth;
00080     uart.baud(115200);
00081     uart.printf("Initializing\n");
00082 
00083 //    EthernetInterface eth;
00084     uart.printf("Initializing Ethernet\n");
00085 
00086     //eth.init(); //Use DHCP
00087     eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
00088     uart.printf("Connecting\n");
00089     eth.connect();
00090     uart.printf("IP Address is %s\n", eth.getIPAddress());
00091 
00092 //    Check File System
00093     uart.printf("Checking File System\n");
00094     DIR *d = opendir("/sdc/");
00095     if (d != NULL) {
00096         uart.printf("SD Card Present\n");
00097         closedir(d);
00098     } else {
00099         uart.printf("SD Card Root Directory Not Found\n");
00100     }
00101 
00102     ledTick.attach(&ledTickfunc,1);
00103 
00104     while(1)
00105     {
00106         if(ftp_time_1s == 0)
00107         {
00108             //Configure the display driver
00109             ftp_time_1s = 1;
00110             TFT.background(Black);
00111             TFT.foreground(White);
00112             TFT.set_font((unsigned char*) Arial12x12);    
00113             TFT.cls();
00114 
00115             TFT.locate(0,0);
00116             TFT.printf(" UPDATING PHOTO\n");
00117             TFT.printf("==================\n\n");
00118         
00119             if(myFTP.open("192.168.77.210", 21, "user", "pass"))
00120             {
00121                 printf("Connect Success to FTPServer\r\n");
00122                 TFT.printf("Connected to FTP Server\n");
00123 
00124                 myFTP.ls(myfilelist);
00125             
00126                 if(*myfilelist !=0)
00127                 {
00128                     tok = myfilelist;
00129                     while(tok)
00130                     {
00131                         tok = strtok_r(tok,"\r\n",&lasts);
00132                         if(tok != NULL)
00133                         {
00134                             printf("tok=%s\r\n",tok);
00135                             if(strstr(tok,"bmp"))
00136                             {
00137                                 sprintf(filename,"/sdc/%s",tok);
00138                                 fp = fopen(filename, "r");
00139                                 printf("fp=%d\r\n",fp);
00140                                 if(fp==NULL)
00141                                 {
00142                                     myFTP.getfile(tok);
00143                                     printf("Get File : %s\r\n",tok);
00144                                     TFT.printf("   New file : %s\n", tok);
00145                                 }
00146                                 else fclose(fp);
00147                             }
00148                             tok = lasts; 
00149                         }
00150                     }
00151                 }
00152                 else TFT.printf(" Empty FTP Server\n");
00153                 TFT.printf("\n UPDATE DONE\n");
00154                 myFTP.quit();
00155             }
00156             else
00157             {
00158                 TFT.printf(" Can't connect to FTP Server\n\n");
00159                 TFT.printf(" UPDATE FAIL\n");
00160             }
00161             TFT.printf("==================\n");
00162         }
00163      
00164         d = opendir("/sdc/");
00165         if(d != NULL)
00166         {
00167             struct dirent *p;
00168             while((p = readdir(d)) != NULL) 
00169             {
00170                 sprintf(filename, "/sdc/%s", p->d_name);
00171                 uart.printf("%s\n", filename);
00172                 DIR *subDir = opendir(filename);
00173                 if (subDir != NULL) uart.printf("Skip a sub-directory\r\n");
00174                 else 
00175                 {
00176                     fp = fopen(filename, "r");
00177                     if(fp)
00178                     {
00179                         TFT.DrawBitmapFile(fp);
00180                         TFT.locate(1,1);
00181                         TFT.printf("%s", filename);
00182                         fclose(fp);
00183                     }
00184                     else uart.printf("Can't open file %s\r\n", filename);
00185                 }
00186             }
00187         }
00188         else
00189         {
00190             TFT.cls();
00191             TFT.locate(0,0);
00192             TFT.printf(" No SD Card !!!\n");
00193             TFT.printf(" Insert a SD card\n");
00194         }
00195     }
00196     
00197 }