Bläddra i källkod

Meerdere Alarmen toegevoegd, gaan ook uit

guusvdongen 9 år sedan
förälder
incheckning
704bbd3466
7 ändrade filer med 163 tillägg och 94 borttagningar
  1. 103 62
      alarm.c
  2. 22 5
      alarm.h
  3. 6 13
      displayHandler.c
  4. 3 1
      displayHandler.h
  5. 1 1
      keyboard.c
  6. 1 1
      keyboard.h
  7. 27 11
      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_min = alarm[idx].time.tm_min + 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);

+ 6 - 13
displayHandler.c

@@ -59,30 +59,23 @@ void displayDate(int line_number) {
     }
 }
 
-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);
     }
 }
 

+ 3 - 1
displayHandler.h

@@ -7,5 +7,7 @@
 
 void displayTime(int);
 void displayDate(int);
-void displayAlarm(int line_number, int line_numberTwo);
+//void displayAlarm(int line_number, int line_numberTwo, struct _alarm am);
+void displayAlarm(int line_number, int line_numberTwo, int idx);
+
 #endif //MUTLI_OS_BUILD_DISPLAYHANDLER_H

+ 1 - 1
keyboard.c

@@ -103,7 +103,7 @@ static void KbClearEvent(HANDLE *pEvent)
  * After each keyboard-scan, check for a valid MMCard
  */
 /* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
-int KbScan()
+u_short KbScan()
 {
     u_char KeyNibble0, KeyNibble1, KeyNibble2, KeyNibble3;
 

+ 1 - 1
keyboard.h

@@ -90,7 +90,7 @@
 /* export global routines (interface)                                      */
 /*-------------------------------------------------------------------------*/
 void    KbInit(void);
-int    KbScan(void);
+u_short    KbScan(void);
 int 	CheckKey();
 int     KbWaitForKeyEvent(u_long);
 u_char  KbGetKey(void);

+ 27 - 11
main.c

@@ -221,6 +221,7 @@ int checkOffPressed(){
 
 int main(void)
 {
+	int idx = 0;
 	time_t start;
 	int running = 0;
 
@@ -267,6 +268,13 @@ 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);
     for (;;)
     {
 		//Check if a button is pressed
@@ -283,19 +291,27 @@ int main(void)
 				LcdBackLight(LCD_BACKLIGHT_OFF);
 			}
 		}
-
-        if(!isAlarmSyncing && X12RtcGetStatus(5) > 0)
+	
+		u_short n = KbScan();
+        if(checkAlarms() == 1)
         {
-			displayAlarm(0,1);
-			if (KbScan() < -1 || checkTime() == 1){
-				handleAlarm();
-				LcdBackLight(LCD_BACKLIGHT_OFF);
+			for (idx = 0; idx < 3; idx++){
+				if (getState(idx) == 1){
+					displayAlarm(0,1,idx);
+					if (n == -17){
+						NutDelay(50);
+						handleAlarm(idx);
+						NutDelay(50);
+						LcdBackLight(LCD_BACKLIGHT_OFF);
+					}
+				}
 			}
-        }
-        else {
-            displayTime(0);
-            displayDate(1);
-        }
+		}
+		else {
+			displayTime(0);				
+			displayDate(1);
+		}
+	
         WatchDogRestart();
     }