Переглянути джерело

Merge pull request #36 from jancoow/snooze-Old-dev

Snooze old dev
Janco Kock 9 роки тому
батько
коміт
91de7e9358
4 змінених файлів з 128 додано та 70 видалено
  1. 82 22
      alarm.c
  2. 2 3
      displayHandler.c
  3. 44 28
      main.c
  4. 0 17
      ntp.c

+ 82 - 22
alarm.c

@@ -8,14 +8,15 @@
 #include "log.h"
 #include "rtc.h"
 #include "alarm.h"
+#include "display.h"
+#include "httpstream.h"
 
 #define n 5
 
 
 struct _snooze
 {
-	struct _tm snoozeStart;
-	struct _tm snoozeEnd;
+	struct _tm snoozeTime;
 };
 
 struct _alarm alarm[n];
@@ -27,6 +28,9 @@ int checkAlarms(){
 	int check = 0;
 	for (i = 0; i < n; i++){
 		setState(i);
+		if (alarm[i].time.tm_year == 0){
+			alarm[i].state = 0;
+		}
 		if (alarm[i].state == 1){
 			check = 1;
 		}
@@ -60,26 +64,86 @@ int maxAlarms(){
 	return n;
 }
 
+void setSnooze(int idx){
+	struct _tm ct;
+	X12RtcGetClock(&ct);
+	
+	alarm[idx].state = 2;
+	snooze[idx].snoozeTime = ct;
+	snooze[idx].snoozeTime.tm_min += alarm[idx].snooze;
+	stopStream();
+}
+
+int daysInMonth(int m, int y) {
+    if(m == 2 && isLeapYear(y))
+        return 29 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
+    return 28 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
+}
+
+int daysInYear(int y){
+    if(isLeapYear(y))
+        return 366;
+    return 365;
+}
+
+int isLeapYear(int y){
+    return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
+}
+
+
+void AddSnoozeMinutes(int idx, int minutes){
+	if (snooze[idx].snoozeTime.tm_min + minutes >= 60){ //Checks if minutes is >= 60 else minute
+		snooze[idx].snoozeTime.tm_hour += 1;
+		snooze[idx].snoozeTime.tm_min = ((snooze[idx].snoozeTime.tm_min + minutes) % 60);
+		if (snooze[idx].snoozeTime.tm_hour >= 24){ //Checks if hours is >= 24
+			snooze[idx].snoozeTime.tm_hour = 0;
+			if ((snooze[idx].snoozeTime.tm_mday + 1) <= daysInMonth((snooze[idx].snoozeTime.tm_mon+1), (snooze[idx].snoozeTime.tm_year+1900))){ //Checks if day+1 smaller or even is to the amount of days in the month
+				snooze[idx].snoozeTime.tm_mday += 1;
+			} else { //If the days+1 is bigger than the amount of days in the month, day = 1 & month is + 1
+				snooze[idx].snoozeTime.tm_mday = 1;
+				if (snooze[idx].snoozeTime.tm_mon + 1 > 11){//If month+1 is bigger than 11 (month is 0-11) then month = 0 & year + 1
+					snooze[idx].snoozeTime.tm_mon = 0;
+					snooze[idx].snoozeTime.tm_year += 1;
+				} else {
+					snooze[idx].snoozeTime.tm_mon += 1;
+				}
+			}
+		}
+	} else {
+		snooze[idx].snoozeTime.tm_min += minutes;
+	}
+}
+
 void setState(int idx){
 	struct _tm ct;
 	X12RtcGetClock(&ct);
 	
-	if (compareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != 0){
+	if (alarm[idx].state == 0){
+		snooze[idx].snoozeTime = alarm[idx].time;
+		AddSnoozeMinutes(idx,1);
+	}
+	
+	if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state != 2){
 		alarm[idx].state = 1;
-	} else {
+	} else if (alarm[idx].state != 2){
 		alarm[idx].state = 0;
 	}
 	
-	/*if (compareTime(alarm[idx].time,snooze[idx].snoozeStart)){
+	if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1){
 		alarm[idx].state = 2;
 	}
 	
-	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;
-		}
-	}*/
+	if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
+		alarm[idx].state = 2;
+		snooze[idx].snoozeTime = ct;
+		AddSnoozeMinutes(idx, alarm[idx].snooze);
+		LcdBackLight(LCD_BACKLIGHT_OFF);
+	}
+	
+	if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
+		alarm[idx].state = 1;
+		AddSnoozeMinutes(idx, 1);
+	}
 }
 
 /*void getAlarm(struct _alarm *am){
@@ -100,11 +164,6 @@ void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, in
 	alarm[idx].snooze = snooze;
 	alarm[idx].id = id;
 	alarm[idx].state = 0;
-
-	//snooze[idx].snoozeStart = time;
-	//snooze[idx].snoozeEnd = time;
-	//snooze[idx].snoozeStart += 1;
-	//snooze[idx].snoozeEnd += (snooze +1);
 }
 
 
@@ -119,8 +178,9 @@ void deleteAlarm(int idx){
 }
 
 void handleAlarm(int idx){
-	alarm[idx].time.tm_mday = alarm[idx].time.tm_mday + 1;
 	alarm[idx].state = 0;
+	alarm[idx].time.tm_mday += 1;
+	printf("state is %d \n",alarm[idx].state);
 }
 
 int compareTime(tm t1,tm t2){
@@ -128,19 +188,19 @@ int compareTime(tm t1,tm t2){
         return 1;
 	}
     if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
-        return 1;
+        return 2;
 	}
     if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
-        return 1;
+        return 3;
 	}
     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;
+        return 4;
 	}
     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;
+        return 5;
 	}
     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 6;
 	}
 
     return 0;

+ 2 - 3
displayHandler.c

@@ -26,7 +26,7 @@ void displayTime(int line_number){
     X12RtcGetClock(&time);
 
     char str[16];
-    if (NtpTimeIsValid()){
+    if (1){
         sprintf(str, "    %02d:%02d:%02d    ", time.tm_hour, time.tm_min, time.tm_sec);
     }else {
         sprintf(str, "    ??:??:??    ");
@@ -43,7 +43,7 @@ void displayDate(int line_number) {
 
     char str[16];
 
-    if (NtpTimeIsValid()) {
+    if (1) {
         sprintf(str, "   %02d-%02d-%04d      ", time->tm_mday, time->tm_mon + MONTH_OFFSET, time->tm_year + YEAR_OFFSET);
     } else {
         sprintf(str, "   ??-??-????      ");
@@ -85,7 +85,6 @@ void displayAlarm(int line_number, int line_numberTwo, int idx)
 	if (j != 16){
 		startidx = (8-(j/2));
 	}
-	printf("startidx: %d, %d",startidx, j);
 	j = 0;
 	for(i = 0; i < 16; i++){
 		if (i >= startidx){

+ 44 - 28
main.c

@@ -183,6 +183,7 @@ static void SysControlMainBeat(u_char OnOff)
 /*-------------------------------------------------------------------------*/
 int isAlarmSyncing;
 int initialized;
+int running = 0;
 
 
 /*-------------------------------------------------------------------------*/
@@ -246,8 +247,18 @@ int timer(time_t start){
     return diff;
 }
 
+long timerStruct(struct _tm s){
+	struct _tm ct;
+	X12RtcGetClock(&ct);
+	
+	long stime = (s.tm_hour * 3600) + (s.tm_min * 60) + s.tm_sec;
+	long ctime = (ct.tm_hour * 3600) + (ct.tm_min * 60) + ct.tm_sec;
+	
+	return ctime - stime;
+}
+
 int checkOffPressed(){
-    if (KbGetKey() == KEY_POWER){
+    if (KbGetKey() != KEY_UNDEFINED){
         LcdBackLight(LCD_BACKLIGHT_ON);
         return 1;
     } else {
@@ -261,11 +272,10 @@ int main(void)
 {
 	initialized = 0;
     int VOL2;
-    time_t start;
+    struct _tm timeCheck;
+	struct _tm start;
 	int idx = 0;
 
-	int running;
-
     WatchDogDisable();
 
     NutDelay(100);
@@ -288,7 +298,7 @@ int main(void)
 
     VsPlayerInit();
 
-    LcdBackLight(LCD_BACKLIGHT_ON);
+ 
     NtpInit();
 
     NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
@@ -310,33 +320,35 @@ 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);*/
-
-/*    if(hasNetworkConnection() == true){
-        playStream("145.58.53.152", 80, "/3fm-bb-mp3");
-    }*/
-    start = time(0) - 10;
-    unsigned char VOL = 64;
 
-    running = 1;
+    unsigned char VOL = 64;
+	
+	LcdBackLight(LCD_BACKLIGHT_OFF);
+	X12RtcGetClock(&timeCheck);
+	X12RtcGetClock(&start);
 
     for (;;)
     {
+		//printf("running = %d, time = %d\n", running, timerStruct(start));
+		
+		if (timerStruct(start) < 0){
+			X12RtcGetClock(&start);
+		}
+		
+		if (timerStruct(timeCheck) < 0){
+			X12RtcGetClock(&timeCheck);
+		}
+		
 		//Check if a button is pressed
 		if (checkOffPressed() == 1){
-			start = time(0);
+			X12RtcGetClock(&start);
 			running = 1;
-            LcdBacklightKnipperen(startLCD);
+            LcdBackLight(LCD_BACKLIGHT_ON);
 		}
 
 		//Check if background LED is on, and compare to timer
 		if (running == 1){
-			if (timer(start) >= 10){
+			if (timerStruct(start) >= 10 || running > 1){
 				running = 0;
 				LcdBackLight(LCD_BACKLIGHT_OFF);
 			}
@@ -346,7 +358,7 @@ int main(void)
         if(KbGetKey() == KEY_DOWN)
         {
             NutSleep(150);
-            start = time(0);
+             X12RtcGetClock(&timeCheck);
             if(VOL > 8){
                 VOL -= 8;
                 VsSetVolume (128-VOL, 128-VOL);
@@ -356,29 +368,33 @@ int main(void)
         else if(KbGetKey() == KEY_UP)
         {
             NutSleep(150);
-            start = time(0);
+             X12RtcGetClock(&timeCheck);
             if(VOL < 128) {
                 VOL += 8;
                 VsSetVolume(128-VOL, 128-VOL);
                 displayVolume(VOL/8);
             }
         }
-        else if(timer(start) >= 5 && checkAlarms() == 1)
+        else if(timerStruct(timeCheck) >= 5 && checkAlarms() == 1)
         {
-			for (idx = 0; idx < 2; idx++){
+			for (idx = 0; idx < 5; idx++){
 				if (getState(idx) == 1){
 					displayAlarm(0,1,idx);
 					if (KbGetKey() == KEY_ESC){
-						NutDelay(50);
+						//NutDelay(50);
 						handleAlarm(idx);
-						NutDelay(50);
+						//NutDelay(50);
+						LcdBackLight(LCD_BACKLIGHT_OFF);
+                        stopStream();
+					} else if (KbGetKey() == KEY_01 || KbGetKey() == KEY_02 || KbGetKey() == KEY_03 || KbGetKey() == KEY_04 || KbGetKey() == KEY_05 || KbGetKey() == KEY_ALT){
+						setSnooze(idx);
 						LcdBackLight(LCD_BACKLIGHT_OFF);
                         stopStream();
 					}
 				}
 			}
 		}
-		else if (timer(start) >= 5){
+		else if (timerStruct(timeCheck) >= 5){
             displayTime(0);
             displayDate(1);
 		}

+ 0 - 17
ntp.c

@@ -77,23 +77,6 @@ void NtpCheckValidTime(void){
 
 //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 \n",
-            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;