Эх сурвалжийг харах

Merge pull request #53 from jancoow/Eenmalig-alarm

Eenmalig alarm
Janco Kock 9 жил өмнө
parent
commit
c88b35bbad
5 өөрчлөгдсөн 99 нэмэгдсэн , 39 устгасан
  1. 57 14
      alarm.c
  2. 2 0
      alarm.h
  3. 36 2
      contentparser.c
  4. 3 22
      displayHandler.c
  5. 1 1
      main.c

+ 57 - 14
alarm.c

@@ -32,8 +32,9 @@ int checkAlarms(){
 			alarm[i].state = 0;
 		}else{
 			setState(i);
+			eenmaligAlarmCheck(i);
 		}
-		if (alarm[i].state == 1){
+		if (alarm[i].state == 1 || alarm[i].state == 4){
 			check = 1;
 		}
 	}
@@ -61,7 +62,7 @@ struct _alarm* getAlarm(int idx){
 char getRunningAlarmID(){
 	char idx;
 	for (idx = 0; idx < 5; idx++) {
-		if (getState(idx) == 1) {
+		if (getState(idx) == 1 || getState(idx) == 4) {
 			return idx;
 		}
 	}
@@ -80,9 +81,11 @@ 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;
+	if (alarm[idx].state < 3){
+		alarm[idx].state = 2;
+		snooze[idx].snoozeTime = ct;
+		snooze[idx].snoozeTime.tm_min += alarm[idx].snooze;
+	}
 }
 
 int daysInMonth(int m, int y) {
@@ -129,12 +132,13 @@ void setState(int idx){
 	struct _tm ct;
 	X12RtcGetClock(&ct);
 	
-	if (alarm[idx].state == 0){
+	//Set snooze time for snooze alarm
+	if (alarm[idx].state == 0  && alarm[idx].state < 3){
 		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){
+	if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state != 2 && alarm[idx].state < 3){
 		if(alarm[idx].state != 1) {
 			alarm[idx].state = 1;
 			printf("\n\nAlarm gaat nu af!\n\n");
@@ -145,15 +149,17 @@ void setState(int idx){
 				printf("ConnectToStream failed. Aborting.\n\n");
 			}
 		}
-	} else if (alarm[idx].state != 2){
+	} else if (alarm[idx].state != 2 && alarm[idx].state < 3 ){
 		alarm[idx].state = 0;
 	}
 	
-	if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1){
+	//Check if alarm has to snooze
+	if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
 		alarm[idx].state = 2;
 	}
 	
-	if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
+	//Check if alarm has to snooze
+	if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
 		alarm[idx].state = 2;
 		snooze[idx].snoozeTime = ct;
 		AddSnoozeMinutes(idx, alarm[idx].snooze);
@@ -161,7 +167,7 @@ void setState(int idx){
 		killPlayerThread();
 	}
 	
-	if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
+	if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
 		if(alarm[idx].state != 1){
 			printf("Alarm komt nu uit snooze!!");
 			bool success = connectToStream(alarm[idx].ip, alarm[idx].port, alarm[idx].url);
@@ -174,6 +180,7 @@ void setState(int idx){
 		}
 		AddSnoozeMinutes(idx, 1);
 	}
+	
 }
 
 
@@ -191,6 +198,39 @@ void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, in
 	alarm[idx].state = 0;
 }
 
+void eenmaligAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int 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].port = port;
+	strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
+
+	alarm[idx].id = id;
+	alarm[idx].state = 3;
+}
+
+
+//Checks if there is an alarm that has to go off
+void eenmaligAlarmCheck(int idx){
+	struct _tm ct;
+	X12RtcGetClock(&ct);
+	
+	//Check if alarm goes off, compares the RTC time to the alarm time
+	if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state == 3){
+		alarm[idx].state = 4;
+		snooze[idx].snoozeTime = ct;
+		AddSnoozeMinutes(idx,2);
+	}
+	
+	//Delete alarm after 30 minutes, compares the RTC time to the snooze
+	if (compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state == 4){
+		deleteAlarm(idx);
+		LcdBackLight(LCD_BACKLIGHT_OFF);
+		killPlayerThread();
+	}
+}
+
 
 void deleteAlarm(int idx){
 	struct _tm tm;
@@ -203,10 +243,13 @@ void deleteAlarm(int idx){
 }
 
 void handleAlarm(int idx){
-	alarm[idx].state = 0;
-	alarm[idx].time.tm_mday += 1;
+	if (alarm[idx].state < 3){
+		alarm[idx].state = 0;
+		alarm[idx].time.tm_mday += 1;
+	} else if (alarm[idx].state == 4){
+		deleteAlarm(idx);
+	}
 	killPlayerThread();
-	printf("state is %d \n",alarm[idx].state);
 }
 
 int compareTime(tm t1,tm t2){

+ 2 - 0
alarm.h

@@ -19,6 +19,8 @@ int checkAlarms(void);
 void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx);
 int alarmExist(int id);
 void deleteAlarm(int idx);
+void eenmaligAlarmCheck(int idx);
+void eenmaligAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx);
 int compareTime(tm t1, tm t2);
 void setState(int idx);
 int getState(int idx);

+ 36 - 2
contentparser.c

@@ -16,9 +16,14 @@ int streamid;
 
 void parseAlarmJson(char* content){
     int r;
-    int i;
+    int i = 2;
+	
+	int startidx = 0;
+	int charAmount = 0;
+
     int usedAlarms[maxAlarms()];
     int j;
+    
     jsmn_parser p;
     jsmntok_t token[160]; /* We expect no more than 128 tokens */
 
@@ -43,6 +48,7 @@ void parseAlarmJson(char* content){
         char url[24];
         char ip[24];
         char name[16];
+		char str2[16];
         char st = -1;
         char oo = -1;
         memset(url, 0, 24);
@@ -86,11 +92,39 @@ void parseAlarmJson(char* content){
             printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
             printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
             printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
+			
+			charAmount = 0;
+			for (i = 0; i < 16;i++){
+				if (name[i] != 0){
+					charAmount = charAmount + 1;
+				}
+			}
+			
+			startidx = (8-(charAmount/2));
+			
+			charAmount = 0;
+			for(i = 0; i < 16; i++){
+				if (i >= startidx){
+					if (name[charAmount] != 0){
+						str2[i] = name[charAmount];
+					} else {
+						str2[i] = ' ';
+					}	
+					charAmount++;
+				} else {
+					str2[i] = ' ';
+				}
+			}
+			
 
             //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, st, id, j);
+					if (oo == 1){
+						eenmaligAlarm(time,str2,ip,port,url,st,id,j);
+					} else {
+						setAlarm(time, str2, ip, port, url, st, id, j);
+					}
                     usedAlarms[j] = 1;
                     j = 10;             //Uit de for loop
                 }

+ 3 - 22
displayHandler.c

@@ -91,38 +91,19 @@ void displayAlarm(char idx)
         currentViewDisplay = DISPLAY_DateTime;
     }
 	int i;
-	int j;
-	int startidx;
+
     char str[16];
     struct _alarm *am = getAlarm(idx);
 
     sprintf(str, "    %02d:%02d:%02d    ", am->time.tm_hour, am->time.tm_min, am->time.tm_sec);
     (*write_display_ptr[0])(str, 16);
 
-	j = 0;
     char str2[16];
 	for (i = 0; i < 16;i++){
-		if (am->name[i] != 0){
-			j = j + 1;
-		}
+		str2[i] = am->name[i];
 	}
+		
 
-	if (j != 16){
-		startidx = (8-(j/2));
-	}
-	j = 0;
-	for(i = 0; i < 16; i++){
-		if (i >= startidx){
-			if (am->name[j] != 0){
-				str2[i] = am->name[j];
-			} else {
-				str2[i] = ' ';
-			}
-			j++;
-		} else {
-			str2[i] = ' ';
-		}
-	}
     (*write_display_ptr[1])(str2, 16);
 }
 

+ 1 - 1
main.c

@@ -307,7 +307,7 @@ int main(void)
 
     X12RtcGetClock(&timeCheck);
 
-    for (;;)
+ 	for (;;)
     {
         //Key detecten
         if(KbGetKey() != KEY_UNDEFINED){