Sfoglia il codice sorgente

Merge pull request #23 from jancoow/pulls/14

Pulls/14
Janco Kock 9 anni fa
parent
commit
c33d54c6a5
5 ha cambiato i file con 188 aggiunte e 119 eliminazioni
  1. 103 62
      alarm.c
  2. 22 5
      alarm.h
  3. 14 21
      displayHandler.c
  4. 2 1
      displayHandler.h
  5. 47 30
      main.c

+ 103 - 62
alarm.c

@@ -8,83 +8,124 @@
 #include "rtc.h"
 #include "alarm.h"
 
-struct _alarm{
-	int seconds;
-	char name[16];
+#define n 3
+
+
+struct _snooze
+{
+	struct _tm snoozeStart;
+	struct _tm snoozeEnd;
 };
 
-void setAlarm(struct _tm time, char *name, int sec){
-	struct _alarm al;
-	
-	X12RtcSetAlarm(0, &time, AFLGS);
-	NutDelay(100);
-	
-	al.seconds = sec;
-	strncpy(al.name, name, sizeof(al.name));
+struct _alarm alarm[n];
+struct _snooze snooze[n];
+
+
+int checkAlarms(){
+	int i = 0;
+	int check = 0;
+	for (i = 0; i < n; i++){
+		setState(i);
+		if (alarm[i].state == 1){
+			check = 1;
+		}
+	}
+	if (check == 1){
+		return 1;
+	}
 	
-	//Schrijf struct naar eeprom
+	return 0;
 }
 
-int getDuration(){
-	//Haal duration op uit eeprom
-	return 10;
+struct _alarm getAlarm(int idx){
+	return alarm[idx];
 }
 
-char* getName(){
-	//haal naam op uit eeprom en geef de pointer mee
-	char str[17];
-	//int x = 561;
-	//sprintf(str,"test123456789%d", x);
-	sprintf(str, "     Wekker     ");
-	return str;
+int getState(int idx){
+	return alarm[idx].state;
 }
 
-void handleAlarm(){
-	struct _tm alarmtime;
+void setState(int idx){
+	struct _tm ct;
+	X12RtcGetClock(&ct);
 	
-    alarmtime = GetRTCTime();
+	if (compareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != 0){
+		alarm[idx].state = 1;
+	} else {
+		alarm[idx].state = 0;
+	}
 	
-    X12RtcGetAlarm(0,&alarmtime,AFLGS);
-	alarmtime.tm_min = (alarmtime.tm_min-79);
-	alarmtime.tm_mday = (alarmtime.tm_mday - 80);
-	alarmtime.tm_mon = (alarmtime.tm_mon - 80);
+	/*if (compareTime(alarm[idx].time,snooze[idx].snoozeStart)){
+		alarm[idx].state = 2;
+	}
 	
-		
-	X12RtcSetAlarm(0,&alarmtime, AFLGS);
-	NutDelay(100);
-	X12RtcClearStatus(ALARM_1);
+	if (alarm[idx].state == 2){
+		if (compareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
+			snooze[idx].snoozeStart.tm_min += alarm[idx].snooze + 1;
+			snooze[idx].snoozeEnd.tm_min += alarm[idx].snooze + 1;
+		}
+	}*/
 }
 
-int checkTime(){
-	/*struct _tm ctime;
-	struct _tm atime;
-	time_t at;
-	time_t ct;
-	time_t diff;
-	
-	atime = GetRTCTime();
-	ctime = GetRTCTime();
-	
-	X12RtcGetAlarm(0,&atime,0b11111111);
-	atime.tm_min = atime.tm_min - 80;
-	atime.tm_mday = (atime.tm_mday - 80);
-	atime.tm_mon = (atime.tm_mon - 80);
-	atime.tm_year = 116;
-	
-	LogMsg_P(LOG_INFO, PSTR("at %02d-%02d-%04d || %02d-%02d-%02d"), atime.tm_mday, atime.tm_mon+1, atime.tm_year+1900, atime.tm_hour, atime.tm_min, atime.tm_sec);
-	
-	ct = mktime(&ctime);
-	at = mktime(&atime);
-	
-	at += getDuration();
-	
-	LogMsg_P(LOG_INFO, PSTR("at = %d, ct = %d"), at, ct);
+/*void getAlarm(struct _alarm *am){
+	int i = 0;
+	for (i = 0; i < n; i++){
+		am[i] = alarm[i]; 
+	}
+}*/
+
+void setAlarm(struct _tm time, char* name, char* ip, u_short port, int snooze, int type, int idx){
+	alarm[idx].time = time;
 	
-	diff = ct - at;
+	strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
+	strncpy(alarm[idx].ip, name, sizeof(alarm[idx].ip));
+	alarm[idx].port = port;
 	
-	if (diff > 0){
-		return 1;
-	}*/
-	return 0;
+	alarm[idx].snooze = snooze;
+	alarm[idx].type = type;
+	alarm[idx].state = 0;
+
+	//snooze[idx].snoozeStart = time;
+	//snooze[idx].snoozeEnd = time;
+	//snooze[idx].snoozeStart += 1;
+	//snooze[idx].snoozeEnd += (snooze +1);
+}
+
+
+void deleteAlarm(int idx){
+	struct _tm tm;
+	alarm[idx].time = tm;
+	alarm[idx].port = 0;
+	alarm[idx].snooze = 5;
+	alarm[idx].type = -1;
+	alarm[idx].state = -1;
+}
+
+void handleAlarm(int idx){
+	alarm[idx].time.tm_mday = alarm[idx].time.tm_mday + 1;
+	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;
 }
 

+ 22 - 5
alarm.h

@@ -4,8 +4,25 @@
 
 #define AFLGS		0b11111111
 
-void handleAlaram(void);
-void setAlarm(struct _tm time, char *name, int sec);
-int getDuration(void);
-char* getName(void);
-int checkTime(void);
+#ifndef _ALARM_DEFINED
+struct _alarm
+{
+	struct _tm time;
+	char ip[24];
+	u_short port;
+	char name[16];
+	int snooze;
+	int type;
+	int state;
+};
+#define _ALARM_DEFINED
+#endif
+
+void handleAlarm(int idx);
+int checkAlarms(void);
+void setAlarm(struct _tm time, char* name, char* ip, u_short port, int snooze, int type, int idx);
+void deleteAlarm(int idx);
+int compareTime(tm t1, tm t2);
+void setState(int idx);
+int getState(int idx);
+struct _alarm getAlarm(int idx);

+ 14 - 21
displayHandler.c

@@ -24,15 +24,15 @@ void displayTime(int line_number){
     tm time;
     X12RtcGetClock(&time);
 
-    char str[12];
+    char str[16];
     if (NtpTimeIsValid()){
-        sprintf(str, "    %02d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
+        sprintf(str, "    %02d:%02d:%02d    ", time.tm_hour, time.tm_min, time.tm_sec);
     }else {
-        sprintf(str, "    ??:??:??");
+        sprintf(str, "    ??:??:??    ");
     }
 
     if (line_number > -1 && line_number < 2){
-        (*write_display_ptr[line_number])(str, 12);
+        (*write_display_ptr[line_number])(str, 16);
     }
 }
 
@@ -40,12 +40,12 @@ void displayDate(int line_number) {
     tm *time;
     X12RtcGetClock(time);
 
-    char str[13];
+    char str[16];
 
     if (NtpTimeIsValid()) {
-        sprintf(str, "   %02d-%02d-%04d   ", time->tm_mday, time->tm_mon + MONTH_OFFSET, time->tm_year + YEAR_OFFSET);
+        sprintf(str, "   %02d-%02d-%04d      ", time->tm_mday, time->tm_mon + MONTH_OFFSET, time->tm_year + YEAR_OFFSET);
     } else {
-        sprintf(str, "   ??-??-????   ");
+        sprintf(str, "   ??-??-????      ");
     }
 
     if (NtpIsSyncing()) {
@@ -55,34 +55,27 @@ void displayDate(int line_number) {
     }
 
     if (line_number > -1 && line_number < 2){
-        (*write_display_ptr[line_number])(str, 13);
+        (*write_display_ptr[line_number])(str, 16);
     }
 }
 
-void displayAlarm(int line_number, int line_numberTwo)
+void displayAlarm(int line_number, int line_numberTwo, int idx)
 {
-    struct _tm alarmtime;
 	int i;
-    alarmtime = GetRTCTime();
-	
-    X12RtcGetAlarm(0,&alarmtime,0b11111111);
     char str[12];
-    sprintf(str, "    %02d:%02d:%02d    ", alarmtime.tm_hour, alarmtime.tm_min - 80, alarmtime.tm_sec);
+	struct _alarm am = getAlarm(idx);
+    sprintf(str, "    %02d:%02d:%02d    ", am.time.tm_hour, am.time.tm_min, am.time.tm_sec);
     if (line_number > -1 && line_number < 2){
         (*write_display_ptr[line_number])(str, 12);
     }
 
-    char str2[17];
-	char *data = getName();
+    char str2[16];
 	for(i = 0; i < 17; i++){
-		str2[i] = data[i];
+		str2[i] = am.name[i];
 	}
-	//LogMsg_P(LOG_INFO, PSTR("%d"), str2);
-    sprintf(str2,"     Wekker     ");
-	//LogMsg_P(LOG_INFO, PSTR("%d"), str2);
     if (line_numberTwo > -1 && line_numberTwo < 2){
         (*write_display_ptr[line_numberTwo])(str2, 16);
-        LcdBacklightKnipperen(startLCD);
+        LcdBackLight(LCD_BACKLIGHT_ON);
     }
 }
 

+ 2 - 1
displayHandler.h

@@ -7,6 +7,7 @@
 
 void displayTime(int);
 void displayDate(int);
-void displayAlarm(int line_number, int line_numberTwo);
+void displayAlarm(int line_number, int line_numberTwo, int idx);
 void displayVolume(int pos);
+
 #endif //MUTLI_OS_BUILD_DISPLAYHANDLER_H

+ 47 - 30
main.c

@@ -180,6 +180,8 @@ static void SysControlMainBeat(u_char OnOff)
 /*-------------------------------------------------------------------------*/
 int isAlarmSyncing;
 int initialized;
+
+
 /*-------------------------------------------------------------------------*/
 /* local variable definitions                                              */
 /*-------------------------------------------------------------------------*/
@@ -240,20 +242,25 @@ int timer(time_t start){
 }
 
 int checkOffPressed(){
-    if (KbGetKey() > 1){
+    if (KbGetKey() == KEY_POWER){
         LcdBackLight(LCD_BACKLIGHT_ON);
         return 1;
     } else {
         return 0;
     }
 }
-int VOL2;
+
+
+
 int main(void)
 {
 	initialized = 0;
-	time_t start;
+    int VOL2;
+    time_t start;
     time_t startVolumeTime;
-	int running = 0;
+	int idx = 0;
+
+	int running;
 
     WatchDogDisable();
 
@@ -282,9 +289,6 @@ int main(void)
     NutThreadCreate("BackgroundThread", AlarmSync, NULL, 1024);
     NutThreadCreate("BackgroundThread", NTPSync, NULL, 1024);
     /** Quick fix for turning off the display after 10 seconds boot */
-    start = time(0);
-    startVolumeTime = time(0);
-    running = 1;
 
     RcInit();
 
@@ -299,9 +303,20 @@ 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;
-    displayDate(1);
-    displayTime(0);
+    startVolumeTime = time(0) - 5;
+
+    running = 1;
+
     for (;;)
     {
 		//Check if a button is pressed
@@ -319,33 +334,16 @@ int main(void)
 			}
 		}
 
-        if(!isAlarmSyncing && X12RtcGetStatus(5) > 0)
-        {
-			displayAlarm(0,1);
-			if (KbGetKey() < -1 || checkTime() == 1){
-				handleAlarm();
-				LcdBackLight(LCD_BACKLIGHT_OFF);
-			}
-        }
-        else {
-                if (timer(startVolumeTime) >= 10) {
-                    startVolumeTime = time(0);
-                    ClearLcd();
-                    displayTime(0);
-                    displayDate(1);
-                }
-            }
-
         VOL = VOL2;
         if(KbGetKey() == KEY_DOWN)
         {
             NutSleep(150);
             startVolumeTime = time(0);
             if(VOL > 1){
-            VOL -= 8;
-            VsSetVolume (128-VOL, 128-VOL);
-            displayVolume(VOL/8);
-                }
+                VOL -= 8;
+                VsSetVolume (128-VOL, 128-VOL);
+                displayVolume(VOL/8);
+            }
         }
         else if(KbGetKey() == KEY_UP)
         {
@@ -358,6 +356,25 @@ int main(void)
 
             }
         }
+        else if(timer(startVolumeTime) >= 5 && checkAlarms() == 1)
+        {
+			for (idx = 0; idx < 3; idx++){
+				if (getState(idx) == 1){
+					displayAlarm(0,1,idx);
+					if (KbGetKey() == KEY_ESC){
+						NutDelay(50);
+						handleAlarm(idx);
+						NutDelay(50);
+						LcdBackLight(LCD_BACKLIGHT_OFF);
+					}
+				}
+			}
+		}
+		else if (timer(startVolumeTime) >= 5){
+            displayTime(0);
+            displayDate(1);
+		}
+
         VOL2 = VOL;
         WatchDogRestart();
     }