Browse Source

Merge remote-tracking branch 'origin/developer' into Alarm_weergeven

Conflicts:
	displayHandler.c
guusvdongen 9 years ago
parent
commit
df71ab0a25
4 changed files with 135 additions and 16 deletions
  1. 13 2
      displayHandler.c
  2. 10 3
      main.c
  3. 103 7
      ntp.c
  4. 9 4
      ntp.h

+ 13 - 2
displayHandler.c

@@ -24,7 +24,11 @@ void displayTime(int line_number){
     X12RtcGetClock(&time);
     X12RtcGetClock(&time);
 
 
     char str[12];
     char str[12];
-    sprintf(str, "    %02d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
+    if (NtpTimeIsValid()){
+        sprintf(str, "    %02d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
+    }else {
+        sprintf(str, "    ??:??:??");
+    }
 
 
     if (line_number > -1 && line_number < 2){
     if (line_number > -1 && line_number < 2){
         (*write_display_ptr[line_number])(str, 12);
         (*write_display_ptr[line_number])(str, 12);
@@ -36,10 +40,17 @@ void displayDate(int line_number){
     X12RtcGetClock(time);
     X12RtcGetClock(time);
 
 
     char str[13];
     char str[13];
-    sprintf(str, "   %02d-%02d-%04d   ", time->tm_mday, time->tm_mon+MONTH_OFFSET, time->tm_year+YEAR_OFFSET);
+
+    if (NtpTimeIsValid()){
+        sprintf(str, "   %02d-%02d-%04d   ", time->tm_mday, time->tm_mon+MONTH_OFFSET, time->tm_year+YEAR_OFFSET);
+    }else {
+        sprintf(str, "   ??-??-????   ");
+    }
+>>>>>>> origin/developer
 
 
     if(NtpIsSyncing())
     if(NtpIsSyncing())
         str[1] = 'S';
         str[1] = 'S';
+
     if (line_number > -1 && line_number < 2){
     if (line_number > -1 && line_number < 2){
         (*write_display_ptr[line_number])(str, 13);
         (*write_display_ptr[line_number])(str, 13);
     }
     }

+ 10 - 3
main.c

@@ -252,7 +252,7 @@ int checkOffPressed(){
 THREAD(StartupInit, arg)
 THREAD(StartupInit, arg)
 {
 {
     NetworkInit();
     NetworkInit();
-    NtpInit();
+    NtpSync();
     NutThreadExit();
     NutThreadExit();
 }
 }
 
 
@@ -291,7 +291,14 @@ int main(void)
 
 
     X12Init();
     X12Init();
 
 
-    NutThreadCreate("Bg", StartupInit, NULL, 512);
+    LcdBackLight(LCD_BACKLIGHT_ON);
+    NtpInit();
+
+    NutThreadCreate("BackgroundThread", StartupInit, NULL, 512);
+    
+    /** Quick fix for turning off the display after 10 seconds boot */
+    start = time(0);
+    running = 1;
 
 
 	/*
 	/*
 	 * Kroeske: sources in rtc.c en rtc.h
 	 * Kroeske: sources in rtc.c en rtc.h
@@ -359,4 +366,4 @@ int main(void)
     }
     }
 
 
     return(0);      // never reached, but 'main()' returns a non-void, so.....
     return(0);      // never reached, but 'main()' returns a non-void, so.....
-}
+}

+ 103 - 7
ntp.c

@@ -3,6 +3,7 @@
 //
 //
 #include <dev/board.h>
 #include <dev/board.h>
 #include <dev/debug.h>
 #include <dev/debug.h>
+#include <dev/nvmem.h>
 
 
 #include <sys/timer.h>
 #include <sys/timer.h>
 
 
@@ -23,25 +24,102 @@
 #define TIME_ZONE 1
 #define TIME_ZONE 1
 #define LOG_MODULE  LOG_NTP_MODULE
 #define LOG_MODULE  LOG_NTP_MODULE
 
 
+typedef struct _Eeprom_tm {
+    size_t len;
+    tm tm_struct;
+} Eeprom_tm;
+
 bool isSyncing;
 bool isSyncing;
+bool validTime = false;
 time_t ntp_time = 0;
 time_t ntp_time = 0;
 tm *ntp_datetime;
 tm *ntp_datetime;
 uint32_t timeserver = 0;
 uint32_t timeserver = 0;
 
 
-void NtpInit() {
+void NtpInit(void) {
+    puts("Func: NtpInit(void)");
     /* Timezone van nederland (gmt 1) */
     /* Timezone van nederland (gmt 1) */
     _timezone = -TIME_ZONE * 3600;
     _timezone = -TIME_ZONE * 3600;
-    GetTime();
+    NtpCheckValidTime();
 }
 }
 
 
-bool NtpIsSyncing(){
+bool NtpIsSyncing(void){
     return isSyncing;
     return isSyncing;
 }
 }
 
 
-void GetTime(){
+void NtpCheckValidTime(void){
+
+    Eeprom_tm eeprom_tm_struct;
+
+    NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
+
+    if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
+        // Size mismatch: There is no valid configuration present.
+        puts("NtpCheckValidTime(): Size mismatch");
+        validTime = false;
+        return;
+    }
+
+    // Valid configuration available.
+    puts("NtpCheckValidTime(): Valid config available");
+    tm stored_tm = eeprom_tm_struct.tm_struct;
+
+    // Check time is valid;
+    tm current_tm;
+    X12RtcGetClock(&current_tm);
+
+    validTime = NtpCompareTime(current_tm, stored_tm);
+    if (validTime){
+        puts("NtpCheckValidTime(): Time was valid");
+    }else {
+        puts("NtpCheckValidTime(): Invalid time!");
+    }
+}
+
+//Tests if t1 is after t2.
+bool NtpCompareTime(tm t1, tm t2){
+    char debug[120];
+    sprintf(&debug, "Comparing two times\nt1=%04d-%02d-%02d+%02d:%02d:%02d\nt2=%04d-%02d-%02d+%02d:%02d:%02d",
+            t1.tm_year+1900,
+            t1.tm_mon+1,
+            t1.tm_mday,
+            t1.tm_hour,
+            t1.tm_min,
+            t1.tm_sec,
+
+            t2.tm_year+1900,
+            t2.tm_mon+1,
+            t2.tm_mday,
+            t2.tm_hour,
+            t2.tm_min,
+            t2.tm_sec
+    );
+    puts(debug);
+
+    if (t1.tm_year > t2.tm_year)
+        return true;
+    if (t1.tm_mon > t2.tm_mon)
+        return true;
+    if (t1.tm_mday > t2.tm_mday)
+        return true;
+    if (t1.tm_hour > t2.tm_hour)
+        return true;
+    if (t1.tm_min > t2.tm_min)
+        return true;
+    if (t1.tm_sec > t2.tm_sec)
+        return true;
+
+    //else
+    return false;
+}
+
+bool NtpTimeIsValid(void){
+    return validTime;
+}
+
+void NtpSync(void){
     /* Ophalen van pool.ntp.org */
     /* Ophalen van pool.ntp.org */
     isSyncing = true;
     isSyncing = true;
-    puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
+    //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
     timeserver = inet_addr("213.154.229.24");
     timeserver = inet_addr("213.154.229.24");
 
 
     for (;;) {
     for (;;) {
@@ -52,7 +130,7 @@ void GetTime(){
             puts("Fout bij het ontvangen van de tijd");
             puts("Fout bij het ontvangen van de tijd");
         }
         }
     }
     }
-    puts("Opgehaald.\n");
+    //puts("Opgehaald.\n");
 
 
     ntp_datetime = localtime(&ntp_time);
     ntp_datetime = localtime(&ntp_time);
 
 
@@ -60,6 +138,24 @@ void GetTime(){
     printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
     printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
 
 
     X12RtcSetClock(ntp_datetime);
     X12RtcSetClock(ntp_datetime);
+    NtpWriteTimeToEeprom(*ntp_datetime);
 
 
     isSyncing = false;
     isSyncing = false;
-}
+    validTime = true;
+}
+
+void NtpWriteTimeToEeprom(tm time_struct){
+    Eeprom_tm eeprom_tm_struct;
+
+    eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
+    eeprom_tm_struct.tm_struct = time_struct;
+
+    int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
+    if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom"); }
+
+    NutDelay(100);
+}
+
+//unsigned long TmStructToEpoch(tm tm_struct){
+//
+//}

+ 9 - 4
ntp.h

@@ -2,12 +2,17 @@
 // Created by janco on 25-2-16.
 // Created by janco on 25-2-16.
 //
 //
 
 
-#ifndef _Network_H
-#define _Network_H
+#ifndef _Ntp_H
+#define _Ntp_H
 
 
 typedef enum {false, true} bool;
 typedef enum {false, true} bool;
 
 
 extern bool NtpIsSyncing(void);
 extern bool NtpIsSyncing(void);
 extern void NtpInit(void);
 extern void NtpInit(void);
-extern void GetTime(void);
-#endif /* _Network_H */
+extern void NtpSync(void);
+extern bool NtpTimeIsValid(void);
+
+void NtpCheckValidTime(void);
+void NtpWriteTimeToEeprom(tm);
+bool NtpCompareTime(tm, tm);
+#endif /* _Ntp_H */