smart_watch

Dependencies:   SNTPClinet WIZnetInterface mbed-src

Fork of smart_watch by WIZnet

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 "SNTPClient.h"
00004 #include "SSD1306.h"
00005 
00006 #if defined(TARGET_WIZwiki_W7500)
00007     EthernetInterface eth;
00008  /*   DigitalOut led1(LED1);
00009     DigitalOut led2(LED2);
00010     DigitalOut led3(LED3);
00011     DigitalOut led4(LED4); */
00012     DigitalIn  sw(PC_0);
00013 
00014 
00015 #endif
00016 TCPSocketConnection sock;
00017 
00018 DigitalOut myled(PA_6);
00019 
00020 Serial pc(USBTX, USBRX); // tx, rx
00021 
00022 datetime ntptime;
00023 struct tm timeinfo;
00024 
00025 
00026 int main()
00027 {
00028     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
00029     uint8_t count=0;
00030     uint8_t time_x_offset =0;
00031     uint8_t old_offset =0;
00032     char buffer[32];
00033     
00034     pc.baud(9600);
00035     init();
00036     cls();
00037     OLED_DrawBMP(0,0,128,8,(unsigned char *)wiznet);
00038     wait(1);
00039     cls();
00040     eth.init(mac_addr); //Use DHCP
00041     eth.connect();
00042   
00043     pc.printf("IP Address is %s\n\r", eth.getIPAddress());
00044     SNTPClient sntp("time.nist.gov", 40);   // timezone: Korea, Republic of
00045     sntp.connect();
00046     if(sntp.getTime(&ntptime) == true)
00047     {
00048         pc.printf("%d-%d-%d, %d:%d:%d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
00049         wait(1.0);
00050     }
00051     else
00052     {
00053         while(sntp.getTime(&ntptime) != true)
00054         {
00055             //break;
00056         }
00057     }
00058 
00059     timeinfo.tm_mon = ntptime.mo-1;
00060     timeinfo.tm_mday = ntptime.dd;
00061     timeinfo.tm_hour = ntptime.hh;
00062     timeinfo.tm_min = ntptime.mm;
00063     timeinfo.tm_sec = ntptime.ss;
00064     timeinfo.tm_year = ntptime.yy-1900;
00065     // printf("%d-%d, %d:%d:%d\r\n", timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
00066     time_t t =mktime(&timeinfo);
00067             
00068     set_time(t);
00069     t = time(NULL);
00070      
00071     while (1) {
00072         time_t seconds = time(NULL);
00073               
00074        // printf("Time as a basic string = %s\r\n", ctime(&seconds));
00075         if(count == 50)
00076         {
00077             cls();
00078             count++;
00079         }
00080         if(count > 50)
00081         {
00082            time_x_offset =0;              
00083            if(old_offset != time_x_offset){
00084                old_offset = time_x_offset;
00085             }
00086             //strftime (buffer,32,"%I:%M:%S ",localtime(&seconds));
00087             strftime (buffer,32,"%I:%M",localtime(&seconds));
00088             LED_P23x32Str(time_x_offset,3,buffer);
00089 
00090             //intBigTime(buffer);
00091             if(sw == 0)
00092             {
00093                 count = 0;   
00094                 cls();            
00095             }
00096         
00097         }
00098         else
00099         {   
00100             strftime(buffer, 32,"%a %d %b %Y", localtime(&seconds));
00101             OLED_ShowStr(10, 3, buffer,1 );
00102             strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
00103             OLED_ShowStr(10, 4, buffer,2 );
00104             count ++;
00105         }
00106     }
00107 }