Network clock using 7 segment.

Dependencies:   SNTPClient WIZnetInterface mbed

Fork of mbed_blinky by Mbed

Files at this revision

API Documentation at this revision

Comitter:
nanjsk
Date:
Wed Mar 02 01:30:10 2016 +0000
Parent:
11:8a1ab8deea2b
Commit message:
Add Time Update

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
segment.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8a1ab8deea2b -r e60d3d6f24e8 main.cpp
--- a/main.cpp	Tue Mar 01 17:26:46 2016 +0000
+++ b/main.cpp	Wed Mar 02 01:30:10 2016 +0000
@@ -15,45 +15,9 @@
 struct tm timeinfo;
 
 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0xab};
-//char ip_addr[4] = {222,98,173.222};
-//char mask_addr[4] = {255,255,255,0};
-//char gw_addr[4] = {222,98,173,254};
 uint8_t display_mode = 0;
-
-void time_set(){
-    timeinfo.tm_mon = ntptime.mo-1;
-    timeinfo.tm_mday = ntptime.dd;
-    timeinfo.tm_hour = ntptime.hh;
-    timeinfo.tm_min = ntptime.mm;
-    timeinfo.tm_sec = ntptime.ss;
-    timeinfo.tm_year = ntptime.yy-1900;
-    //printf("%d-%d-%d, %02d:%02d:%02d\r\n", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
-    time_t t =mktime(&timeinfo);
-    set_time(t);
-    t = time(NULL);
-}
+uint8_t update_mode = 0;
 
-uint8_t SNTP_Connect(){
-    printf("Getting time information by using NTP...\r\n");
-    
-    SNTPClient sntp("time.nist.gov", 40);   // timezone: Korea, Republic of
-    sntp.connect();
-    if(sntp.getTime(&ntptime) == true)
-    {
-          printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
-          printf("Completed Get and Set Time\r\n\r\n");
-          ledR = 0;
-          return 0;
-    }
-    else
-    {
-        while(sntp.getTime(&ntptime) == true)
-        {
-            break;
-        }
-        return 1;
-    }
-}
 
 void SW1_Interrupt(){
     ledG=!ledG;
@@ -62,17 +26,69 @@
     pc.printf("Display Mode : %d\n\r",display_mode);
 }
 
+void SNTP_Connect() {
+    printf("Getting time information by using NTP...\r\n");
+    
+    SNTPClient sntp("time.nist.gov", 40);   // timezone: Korea, Republic of
+    sntp.connect();
+    if(sntp.getTime(&ntptime) == true) {
+          printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
+          printf("Completed Get and Set Time\r\n\r\n");
+          ledR = 0;
+    }
+    else {
+        while(sntp.getTime(&ntptime) == true) {
+            printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
+            printf("Completed Get and Set Time\r\n\r\n");
+            ledR = 0;
+            break;
+        }
+    }
+}
+void time_set() {
+    timeinfo.tm_mon = ntptime.mo-1;
+    timeinfo.tm_mday = ntptime.dd;
+    timeinfo.tm_hour = ntptime.hh;
+    timeinfo.tm_min = ntptime.mm;
+    timeinfo.tm_sec = ntptime.ss;
+    timeinfo.tm_year = ntptime.yy-1900;
+    time_t t =mktime(&timeinfo);
+    set_time(t);
+    t = time(NULL);
+}
+
+void time_update(char buffer[]) {
+    uint8_t h_buffer = ((buffer[0]-48)*10) + (buffer[1]-48);
+    uint8_t m_buffer = ((buffer[2]-48)*10) + (buffer[3]-48);
+    uint8_t s_buffer = ((buffer[4]-48)*10) + (buffer[5]-48);
+    uint8_t time_update[3] = {10, 17, 00};   //Hour,Minute,Second
+
+    if(update_mode == 0){
+        if(h_buffer == time_update[0] && m_buffer == time_update[1] && s_buffer == time_update[2]){
+            SNTP_Connect();
+            time_set();
+            update_mode = 1;
+            printf("Time Update Completed.\n\r\n\r");
+        }
+    }
+    else if(update_mode == 1){
+        if(!(h_buffer == time_update[0] && m_buffer == time_update[1] && s_buffer == time_update[2])){
+            update_mode = 0;
+        }
+    }
+}
+
 int main() {    
     pc.baud(115200);
-    wait(0.5f);
-    printf("Hello WIZwiki-W7500!\n\r");
+    wait(0.1);
+    printf("\n\rHello WIZwiki-W7500!\n\r");
     printf("===========================================\n\r");
     ledR = ledG = ledB = 1;
     
     Seg_Reset();
+    SW1.fall(&SW1_Interrupt);
     
     eth.init(mac_addr); //Use DHCP
-    //eth.init(mac_addr, ip_addr, mask_addr, gw_addr);
     printf("Check Ethernet Link\r\n");
     while(1) //Wait link up
     {
@@ -83,27 +99,36 @@
     eth.connect();
     printf("My IP Address is %s\r\n", eth.getIPAddress());
     
-    //printf("SNTP_Connect : %d\r\n",SNTP_Connect());
-    while(SNTP_Connect());
+    SNTP_Connect();
     time_set();
-    
+    //ymd_buffer[0]~[3] : Year
+    //ymd_buffer[4]~[5] : Month
+    //ymd_buffer[6]~[7] : Day
     char ymd_buffer[8];
+    //hms_buffer[0]~[1] : Hour
+    //hms_buffer[2]~[3] : Minute
+    //hms_buffer[4]~[5] : Second
     char hms_buffer[6];
     
-    SW1.fall(&SW1_Interrupt);
     while(1) {
         time_t seconds = time(NULL);
-        //ymd_buffer[0]~[3] : Year
-        //ymd_buffer[4]~[5] : Month
-        //ymd_buffer[6]~[7] : Day
+        
+        strftime(hms_buffer, 6, "%H%M%S\n\r", localtime(&seconds));
         strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds));
-        //hms_buffer[0]~[1] : Hour
-        //hms_buffer[2]~[3] : Minute
-        //hms_buffer[4]~[5] : Second
-        strftime(hms_buffer, 6, "%H%M%S\n\r", localtime(&seconds));
+        
+        if(hms_buffer[4] == '0' && hms_buffer[5] == '0'){
+            time_update(hms_buffer);
+        }
         
-        if(display_mode == 0)   display_hms(hms_buffer);
-        else if(display_mode == 1)   display_ymd(ymd_buffer);
-        else    display_hms(hms_buffer);
+        if(display_mode == 0) {
+            display_hms(hms_buffer);
+        }
+        else if(display_mode == 1) {
+            
+            display_ymd(ymd_buffer);
+        }
+        else {
+            display_hms(hms_buffer);
+        }
     }
 }
\ No newline at end of file
diff -r 8a1ab8deea2b -r e60d3d6f24e8 segment.cpp
--- a/segment.cpp	Tue Mar 01 17:26:46 2016 +0000
+++ b/segment.cpp	Wed Mar 02 01:30:10 2016 +0000
@@ -53,22 +53,28 @@
 
 uint8_t SEGMENT_SEL[9] = {0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
 
-void Seg_Drive(int seg, int num, int dot) {
+void Seg_Drive(int seg, int num, int dot)
+{
     Seg_num = 0xff;
     Seg_Sel = SEGMENT_SEL[seg];
-    if(dot == 1){
+    if(dot == 1)
+    {
         Seg_num = SEGMENTS[num] & 0x7F;
     }
-    else {
+    else
+    {
         Seg_num = SEGMENTS[num];
     }
     wait_ms(1);
 }
 
-void Seg_Test(){
-    for(int j = 1 ; j <9 ; j++){            
+void Seg_Test()
+{
+    for(int j = 1 ; j <9 ; j++)
+    {            
         pc1.printf("SEGMENT_SEL : %x\r\n",SEGMENT_SEL[j]);           
-        for(int i = 0; i<10 ; i++){
+        for(int i = 0; i<10 ; i++)
+        {
             Seg_Drive(j,i,1);
             pc1.printf("Seg Num : %d\r\n",i);
             wait(0.05);
@@ -77,14 +83,17 @@
     Seg_num = 0xff;
 }
 
-void Seg_Reset(){
+void Seg_Reset()
+{
     Seg_num = 0xff;
     Seg_Sel = 0x00;
 }
 
-void display_ymd(char buffer[]){
+void display_ymd(char buffer[])
+{
     int dot = 0;
-    for(int i = 0 ; i <= 7 ; i++){
+    for(int i = 0 ; i <= 7 ; i++)
+    {
         if(i == 3)      dot = 1;
         else if(i == 5) dot = 1;
         else if(i == 7) dot = 1;
@@ -95,9 +104,11 @@
     
 }
 
-void display_hms(char buffer[]){
+void display_hms(char buffer[])
+{
     int dot = 0;
-    for(int i = 0 ; i < 6 ; i++){
+    for(int i = 0 ; i < 6 ; i++)
+    {
         if(i == 1)      dot = 1;
         else if(i == 3) dot = 1;
         else if(i == 5) dot = 1;