Browse Source

Eenmalig alarm added

guusvdongen 9 years ago
parent
commit
d0351df2a9
5 changed files with 106 additions and 37 deletions
  1. 61 14
      alarm.c
  2. 2 0
      alarm.h
  3. 37 2
      contentparser.c
  4. 0 16
      displayHandler.c
  5. 6 5
      main.c

+ 61 - 14
alarm.c

@@ -28,10 +28,11 @@ int checkAlarms(){
 	int check = 0;
 	for (i = 0; i < n; i++){
 		setState(i);
+		eenmaligAlarmCheck(i);
 		if (alarm[i].time.tm_year == 0){
 			alarm[i].state = 0;
 		}
-		if (alarm[i].state == 1){
+		if (alarm[i].state == 1 || alarm[i].state == 4){
 			check = 1;
 		}
 	}
@@ -68,10 +69,14 @@ 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();
+	if (alarm[idx].state < 3){
+		alarm[idx].state = 2;
+		snooze[idx].snoozeTime = ct;
+		snooze[idx].snoozeTime.tm_min += alarm[idx].snooze;
+		stopStream();
+		LcdBackLight(LCD_BACKLIGHT_OFF);
+		stopStream();
+	}
 }
 
 int daysInMonth(int m, int y) {
@@ -118,22 +123,26 @@ 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){
+	//Check if alarm has to go off for snooze alarm
+	if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state != 2 && alarm[idx].state < 3){
 		alarm[idx].state = 1;
-	} 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);
@@ -141,10 +150,12 @@ void setState(int idx){
 		stopStream();
 	}
 	
-	if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
+	//Check if snooze is done, and alarm goes off again
+	if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
 		alarm[idx].state = 1;
 		AddSnoozeMinutes(idx, 1);
 	}
+	
 }
 
 /*void getAlarm(struct _alarm *am){
@@ -167,6 +178,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);
+		stopStream();
+	}
+}
+
 
 void deleteAlarm(int idx){
 	struct _tm tm;
@@ -179,9 +223,12 @@ void deleteAlarm(int idx){
 }
 
 void handleAlarm(int idx){
-	alarm[idx].state = 0;
-	alarm[idx].time.tm_mday += 1;
-	printf("state is %d \n",alarm[idx].state);
+	if (alarm[idx].state < 3){
+		alarm[idx].state = 0;
+		alarm[idx].time.tm_mday += 1;
+	} else if (alarm[idx].state == 4){
+		deleteAlarm(idx);
+	}
 }
 
 int compareTime(tm t1,tm t2){

+ 2 - 0
alarm.h

@@ -24,6 +24,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);

+ 37 - 2
contentparser.c

@@ -11,6 +11,10 @@
 void parseAlarmJson(char* content){
     int r;
     int i = 2;
+	
+	int startidx = 0;
+	int charAmount = 0;
+	
     jsmn_parser p;
     jsmntok_t token[150]; /* We expect no more than 128 tokens */
 
@@ -36,7 +40,9 @@ 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);
         memset(ip, 0, 24);
         memset(name, 0, 17);
@@ -64,7 +70,9 @@ void parseAlarmJson(char* content){
                 getStringToken(content, &token[i + 1], url);
             }else if (jsoneq(content, &token[i], "name") == 0) {
                 getStringToken(content, &token[i + 1], name);
-            }else if (jsoneq(content, &token[i], "st") == 0) {
+            }else if (jsoneq(content, &token[i], "oo") == 0) {
+                oo = getIntegerToken(content, &token[i + 1]);
+            } if (jsoneq(content, &token[i], "st") == 0) {
                 st = getIntegerToken(content, &token[i + 1]);
                 i+=2;
             }
@@ -77,11 +85,38 @@ 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);
+			
+			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] = ' ';
+				}
+			}
+			printf("Str2 = %s", str2);
+			
 
             //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
                 }

+ 0 - 16
displayHandler.c

@@ -82,22 +82,6 @@ void displayAlarm(int line_number, int line_numberTwo, int idx)
 		}
 	}
 	
-	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] = ' ';
-		}
-	}
     if (line_numberTwo > -1 && line_numberTwo < 2){
         (*write_display_ptr[line_numberTwo])(str2, 16);
         LcdBackLight(LCD_BACKLIGHT_ON);

+ 6 - 5
main.c

@@ -319,6 +319,11 @@ int main(void)
 	LcdBackLight(LCD_BACKLIGHT_OFF);
 	X12RtcGetClock(&timeCheck);
 	X12RtcGetClock(&start);
+	
+	/*struct _tm time;
+	X12RtcGetClock(&time);
+	time.tm_sec += 10;
+	eenmaligAlarm(time, "eenmalig alarm12", 0, 8000, "", 0, 0, 0);*/
 
     for (;;)
     {
@@ -374,18 +379,14 @@ int main(void)
         else if(timerStruct(timeCheck) >= 5 && checkAlarms() == 1)
         {
 			for (idx = 0; idx < 5; idx++){
-				if (getState(idx) == 1){
+				if (getState(idx) == 1 || getState(idx) == 4){
 					displayAlarm(0,1,idx);
 					if (KbGetKey() == KEY_ESC){
-						//NutDelay(50);
 						handleAlarm(idx);
-						//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();
 					}
 				}
 			}