Ver Fonte

Severe codecleaning and bugfixes.

Fix for volume-bug
Changed IPaddr to u_long
Jordy Sipkema há 9 anos atrás
pai
commit
06e834169e
9 ficheiros alterados com 73 adições e 141 exclusões
  1. 8 28
      alarm.c
  2. 16 20
      alarm.h
  3. 3 2
      displayHandler.c
  4. 3 7
      eeprom.h
  5. 3 3
      httpstream.c
  6. 1 1
      httpstream.h
  7. 21 42
      main.c
  8. 1 1
      network.c
  9. 17 37
      ntp.c

+ 8 - 28
alarm.c

@@ -5,9 +5,11 @@
 #include <time.h>
 #include <assert.h>
 
+#include "alarm.h"
 #include "log.h"
+#include "ntp.h"
+#include "typedefs.h"
 #include "rtc.h"
-#include "alarm.h"
 
 #define n 2
 
@@ -64,18 +66,18 @@ void setState(int idx){
 	struct _tm ct;
 	X12RtcGetClock(&ct);
 	
-	if (compareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != 0){
+	if (NtpCompareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != false){
 		alarm[idx].state = 1;
 	} else {
 		alarm[idx].state = 0;
 	}
 	
-	/*if (compareTime(alarm[idx].time,snooze[idx].snoozeStart)){
+	/*if (NtpCompareTime(alarm[idx].time,snooze[idx].snoozeStart)){
 		alarm[idx].state = 2;
 	}
 	
 	if (alarm[idx].state == 2){
-		if (compareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
+		if (NtpCompareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
 			snooze[idx].snoozeStart.tm_min += alarm[idx].snooze + 1;
 			snooze[idx].snoozeEnd.tm_min += alarm[idx].snooze + 1;
 		}
@@ -89,11 +91,11 @@ void setState(int idx){
 	}
 }*/
 
-void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
+void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx){
 	alarm[idx].time = time;
 	
 	strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
-	strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
+	alarm[idx].ip = ip;
 	alarm[idx].port = port;
 	strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
 
@@ -123,26 +125,4 @@ void handleAlarm(int idx){
 	alarm[idx].state = 0;
 }
 
-int compareTime(tm t1,tm t2){
-    if (t1.tm_year > t2.tm_year){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
-        return 1;
-	}
-
-    return 0;
-}
 

+ 16 - 20
alarm.h

@@ -1,30 +1,26 @@
-/* Alarm get/set status values */
-#define ALARM_1 	5
-#define ALARM_2		6
-
-#define AFLGS		0b11111111
 
 #ifndef _ALARM_DEFINED
-struct _alarm
+typedef struct _alarm
 {
-	struct _tm time;
-	char ip[24];
-	u_short port;
-	char url[24];
-	char name[16];
-	int snooze;
-	int id;
-	int state;
-};
-#define _ALARM_DEFINED
-#endif
+    struct _tm time;   // Alarm time                          ?
+    u_long ip;         // IP of the stream to be played       4
+	u_short port;      // Port of the stream to be played     2
+	char url[24];      // Get url                            24
+	char name[16];     // Name of the alarm                  16
+	char snooze;       // Snooze time in minutes              1
+	int id;            // Database identifier                 2
+	signed char state; // Enum: [-1 ... 2]                    1
+} AAlarm;              // Total size:                        50 + ?
+
 
 void handleAlarm(int idx);
 int checkAlarms(void);
-void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx);
+void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx); // TODO: Linked list?
 int alarmExist(int id);
 void deleteAlarm(int idx);
-int compareTime(tm t1, tm t2);
 void setState(int idx);
 int getState(int idx);
-struct _alarm getAlarm(int idx);
+struct _alarm getAlarm(int idx);
+
+#define _ALARM_DEFINED
+#endif

+ 3 - 2
displayHandler.c

@@ -85,7 +85,7 @@ void displayVolume(int pos)
 {
     ClearLcd();
     int i;
-    LcdArrayLineOne("     Volume     ", 16);
+    (*write_display_ptr[0])("     Volume     ", 16);
 
     char characters[17];
 
@@ -93,7 +93,8 @@ void displayVolume(int pos)
     {
         characters[i] = 0xFF;
     }
-        LcdArrayLineTwo(characters,pos);
+
+    (*write_display_ptr[1])(characters, pos);
 }
 
 

+ 3 - 7
eeprom.h

@@ -36,15 +36,11 @@ typedef struct _TCache
     tm last_sync;                      // TODO: figure out the size
 } TCache;
 
-typedef struct _TAlarmGeneral
-{
-    char snooze_time;                 // size = ?, desc: snooze_time in minutes.
-} TAlarmGeneral;
-
-typedef struct _TAlarm
+typedef struct _TAlarm // Max 5
 {
     tm alarm_time;
     char name[16];
+    char snooze_time;                 // size = ?, desc: snooze_time in minutes.
 } TAlarm;
 
 /*!\brief Settings to write on Eeprom */
@@ -52,7 +48,7 @@ typedef struct _TSettings
 {
     size_t              Checksum;      // Checksum for validation TSettings-struct
     TSettingsSystem     System;        // System settings
-    TAlarmGeneral       Alarm_general; // General alarms settings
+    TAlarm              Alarm_general; // Alarms settings
     TCache              Cache;         // Cache
 } TSettings;
 

+ 3 - 3
httpstream.c

@@ -48,10 +48,10 @@ THREAD(Stream, args)
     NutThreadExit();
 }
 
-void playStream(char *ipaddr, u_short port, char *radiourl){
+void playStream(u_long ipaddr, u_short port, char *radiourl){
     if(isStreaming != true){
         isStreaming = true;
-        ConnectStation(sock, inet_addr(ipaddr), port, radiourl, &metaint);
+        ConnectStation(sock, ipaddr, port, radiourl, &metaint);
         NutThreadCreate("Stream", Stream, NULL, 1024);
     }
 }
@@ -282,4 +282,4 @@ void PlayMp3Stream(FILE *stream, u_long metaint)
             break;
         }
     }
-}
+}

+ 1 - 1
httpstream.h

@@ -6,7 +6,7 @@
 #define _Httpstream_H
 
 extern bool HttpIsStreaming();
-extern void playStream(char *ipaddr, u_short port, char *radiourl);
+extern void playStream(u_long ipaddr, u_short port, char *radiourl);
 extern void stopStream();
 
 

+ 21 - 42
main.c

@@ -181,8 +181,9 @@ static void SysControlMainBeat(u_char OnOff)
 /*-------------------------------------------------------------------------*/
 /* global variable definitions                                             */
 /*-------------------------------------------------------------------------*/
-int isAlarmSyncing;
-int initialized;
+bool isAlarmSyncing;
+bool initialized = false;
+unsigned char VS_volume = 7; //[0-15];
 
 
 /*-------------------------------------------------------------------------*/
@@ -198,37 +199,27 @@ THREAD(StartupInit, arg)
 
     NtpSync();
 
-    initialized = 1;
+    initialized = true;
     NutThreadExit();
 }
 
-THREAD(NTPSync, arg)
+THREAD(Sync, arg)
 {
     for(;;)
     {
-        if(initialized && (hasNetworkConnection() == true))
+        if((initialized == true) && (hasNetworkConnection() == true))
         {
-            while(isAlarmSyncing)
-            {
-                NutSleep(2000);
+            // NTP
+            if (0){ // TODO: Do a normal check
+                NtpSync();
             }
-            NtpSync();
-        }
-        NutSleep(86400000);
-    }
-}
 
-THREAD(AlarmSync, arg)
-{
-    for(;;)
-    {
-        if(initialized && (hasNetworkConnection() == true))
-        {
-            isAlarmSyncing = 1;
+            // Regular alarms
+            isAlarmSyncing = true;
             char* content = httpGet("/getAlarmen.php?radioid=DE370");
             parseAlarmJson(content);
             free(content);
-            isAlarmSyncing = 0;
+            isAlarmSyncing = false;
         }
         NutSleep(3000);
     }
@@ -257,7 +248,6 @@ int checkOffPressed(){
 
 int main(void)
 {
-	initialized = 0;
     int VOL2;
     time_t start;
 	int idx = 0;
@@ -290,8 +280,7 @@ int main(void)
     NtpInit();
 
     NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
-    NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
-    NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
+    NutThreadCreate("BackgroundThread", Sync, NULL, 2500);
     /** Quick fix for turning off the display after 10 seconds boot */
 
     RcInit();
@@ -308,15 +297,7 @@ int main(void)
 	/* Enable global interrupts */
 	sei();
 
-   /* struct _tm tm;
-	tm = GetRTCTime();
-	tm.tm_sec += 10;
-    setAlarm(tm,"    test1234      ", "0.0.0.0", 8001,1,0,0);
-	tm.tm_sec +=20;
-	setAlarm(tm,"    test5678      ", "0.0.0.0", 8001,1,0,1);*/
-
     start = time(0) - 10;
-    unsigned char VOL = 64;
 
     running = 1;
 
@@ -341,25 +322,24 @@ int main(void)
             playStream("145.58.53.152", 80, "/3fm-bb-mp3");
         }
 
-        VOL = VOL2;
         if(KbGetKey() == KEY_DOWN)
         {
             NutSleep(150);
             start = time(0);
-            if(VOL > 8){
-                VOL -= 8;
-                VsSetVolume (128-VOL, 128-VOL);
-                displayVolume(VOL/8);
+            if (VS_volume >= 1){
+                VS_volume = (--VS_volume) % 17;
+                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
+                displayVolume(VS_volume);
             }
         }
         else if(KbGetKey() == KEY_UP)
         {
             NutSleep(150);
             start = time(0);
-            if(VOL < 128) {
-                VOL += 8;
-                VsSetVolume(128-VOL, 128-VOL);
-                displayVolume(VOL/8);
+            if (VS_volume <= 15){
+                VS_volume = (++VS_volume) % 17;
+                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
+                displayVolume(VS_volume);
             }
         }
         else if(timer(start) >= 5 && checkAlarms() == 1)
@@ -382,7 +362,6 @@ int main(void)
             displayDate(1);
 		}
 
-        VOL2 = VOL;
         WatchDogRestart();
     }
     return(0);

+ 1 - 1
network.c

@@ -190,7 +190,7 @@ void parseAlarmJson(char* content){
             //zoek naar een vrije plaats in de alarm array
             for(j = 0; j < maxAlarms(); j++){
                 if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
-                    setAlarm(time, name, ip, port, url, 5, id, j);
+                    setAlarm(time, name, inet_addr(ip), port, url, 5, id, j);
                     usedAlarms[j] = 1;
                     j = 10;
                 }

+ 17 - 37
ntp.c

@@ -23,14 +23,13 @@
 #include "eeprom.h"
 #include "typedefs.h"
 
-int TIME_ZONE = 1;
 #define LOG_MODULE  LOG_NTP_MODULE
 
-bool isSyncing;
+bool isSyncing = false;
 bool validTime = false;
 time_t ntp_time = 0;
 tm *ntp_datetime;
-uint32_t timeserver = 0;
+int TIME_ZONE = 1;
 
 void NtpInit(void) {
     puts("Func: NtpInit(void)");
@@ -59,38 +58,14 @@ void NtpCheckValidTime(void){
     tm current_tm;
     X12RtcGetClock(&current_tm);
 
+    tm stored_tm = cache->last_sync;
+
     validTime = NtpCompareTime(current_tm, stored_tm);
     if (validTime){
         puts("NtpCheckValidTime(): Time was valid \n");
     }else {
         puts("NtpCheckValidTime(): Invalid time! \n");
     }
-
-//    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.
@@ -113,20 +88,25 @@ bool NtpCompareTime(tm t1, tm t2){
     );
     puts(debug);
 
-    if (t1.tm_year > t2.tm_year)
+    if (t1.tm_year > t2.tm_year){
         return true;
-    if (t1.tm_mon > t2.tm_mon)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
         return true;
-    if (t1.tm_mday > t2.tm_mday)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
         return true;
-    if (t1.tm_hour > t2.tm_hour)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
         return true;
-    if (t1.tm_min > t2.tm_min)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
         return true;
-    if (t1.tm_sec > t2.tm_sec)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
         return true;
+    }
 
-    //else
     return false;
 }
 
@@ -142,7 +122,7 @@ void NtpSync(void){
     printf(TIME_ZONE);
     NutDelay(100);
     //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
-    timeserver = inet_addr("213.154.229.24");
+    uint32_t timeserver = inet_addr("213.154.229.24");
 
     for (;;) {
         if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {