/*! * \brief Set alarm of an X12xx hardware clock. * * \deprecated New applications must use NutRtcSetAlarm(). * * \param idx Zero based index. Two alarms are supported. * \param tm Points to a structure which contains the date and time * information. May be NULL to clear the alarm. * \param aflgs Each bit enables a specific comparision. * - Bit 0: Seconds * - Bit 1: Minutes * - Bit 2: Hours * - Bit 3: Day of month * - Bit 4: Month * - Bit 7: Day of week (Sunday is zero) * * \return 0 on success or -1 in case of an error. */ int X12RtcSetAlarm(int idx, CONST struct _tm *tm, int aflgs) { u_char data[10]; int flags; memset(data, 0, sizeof(data)); data[1] = idx * 8; if (tm) { if (aflgs & RTC_ALARM_SECOND) { data[2] = BIN2BCD(tm->tm_sec) | X12RTC_SCA_ESC; } if (aflgs & RTC_ALARM_MINUTE) { data[3] = BIN2BCD(tm->tm_min) | X12RTC_MNA_EMN; } if (aflgs & RTC_ALARM_HOUR) { data[4] = BIN2BCD(tm->tm_hour) | X12RTC_HRA_EHR; } if (aflgs & RTC_ALARM_MDAY) { data[5] = BIN2BCD(tm->tm_mday) | X12RTC_DTA_EDT; } if (aflgs & RTC_ALARM_MONTH) { data[6] = BIN2BCD(tm->tm_mon + 1) | X12RTC_MOA_EMO; } if (aflgs & RTC_ALARM_WDAY) { data[8] = BIN2BCD(tm->tm_wday) | X12RTC_DWA_EDW; } } X12RtcGetAlarm(1, &test, &flags); //printf("\n set Alarm: date: %d-%d-%d Time: %d:%d:%d \n", test.tm_year, test.tm_mon, test.tm_mday, test.tm_hour, test.tm_min, test.tm_sec); return(X12RtcWrite(1, data, 10)); }
/** * * @param gmt * @return returns time set for alarm B */ void get_alarm_b(tm* gmt) { int flags = 0; X12RtcGetAlarm(1, gmt, &flags); }
tm get_alarm(int alarmid){ tm time; int* flags; X12RtcGetAlarm(alarmid, &time, &flags); return time; }
/*! * \brief thread that checks the alarms every 10 seconds * * \author Matthijs */ THREAD(AlarmThread, args) { //time struct datetime can't be found when placed in main.h tm datetime; u_long flags; X12RtcGetClock(&datetime); for(;;) { int succes = X12RtcGetStatus(&flags); int i; for(i = 0; i <=10; i++) { NutSleep(1000); } //power fail if(flags == 0) { //printf("\n ========== Power Fail ========== \n"); X12RtcClearStatus(0); } //alarm A if(flags == 32) { printf("\n ========== Alarm 0 =========== \n"); //get current alarm time to compare to the weekendtime, if weekendtime is //equal to currentAlarm and the day of the week is friday. The alarm should //not go off. tm currentAlarm; int iets = 0; X12RtcGetAlarm(0, ¤tAlarm, &iets); int alarmASeconds = 0; alarmASeconds = (currentAlarm.tm_hour * 360) + (currentAlarm.tm_min * 6) + (currentAlarm.tm_sec / 10); int weekendtimeSeconds = 0; weekendtimeSeconds = (weekendtime.hour * 360) + (weekendtime.minute * 6) + (weekendtime.second / 10); printf("\nalarmASeconds: %d, weekendtimeSeconds: %d", alarmASeconds, weekendtimeSeconds); if((alarmASeconds != weekendtimeSeconds) && checkWeekend() == 1) { printf("\nHet is VRIJDAG!!! alarm is niet gelijk aan de weekendtijd en moet dus nog geset worden\n SOUND IS PLAYING"); //SoundA(); startSnoozeThreadA(); } else if(((alarmASeconds != weekendtimeSeconds) && (checkWeekend() == 2)) || ((alarmASeconds != weekendtimeSeconds) && (checkWeekend() == 3))) { printf("\nalarm is niet gelijk aan de weekendtijd en het is weekend, dus set"); } else if(((alarmASeconds == weekendtimeSeconds) && (checkWeekend() == 2)) || ((alarmASeconds == weekendtimeSeconds) && (checkWeekend() == 3))) { printf("\nalarm is gelijk aan de weekendtijd en het is weekend,\n SOUND IS PLAYING"); //SoundA(); startSnoozeThreadA(); //if the alarm goes off and it is sunday set the alarm back to its //original settings if(checkWeekend() == 3) { printf("het is blijkbaar zondag dus terug naar doordeweekse tijd"); setAlarmA(alarmA.tm_hour, alarmA.tm_min, alarmA.tm_sec); } } else if(checkWeekend() == 0) { printf("\nhet is doordeweeks,\nSOUND IS PLAYING"); startSnoozeThreadA(); //SoundA(); } //else //{ // printf("weekendAlarm gaat af!!!"); //} //when checkWeekend returns 1(Friday) or 2(Saturday) it means that it //will be weekend the next day. Therefore we set the alarm to the weekend //settings. Problem is that most likely the alarm in the weekends will go //off later then it would during the week. This problem is solved underneath //the if(checkWeekend() == 3){}. int currentTime = (datetime.tm_hour * 360) + (datetime.tm_min * 6) + (datetime.tm_sec / 10); if(checkWeekend() > 0 && checkWeekend() < 3) { printf("alarmA set to weekend time"); setAlarmA(weekendtime.hour,weekendtime.minute,weekendtime.second); } else if(checkWeekend() == 3 && (currentTime <= weekendtimeSeconds)) { setAlarmA(weekendtime.hour,weekendtime.minute,weekendtime.second); } X12RtcClearStatus(32); } //alarm B if(flags == 64) { printf("\n ============ Alarm 1================== \n"); startSnoozeThreadB(); ClearAlarm('b'); X12RtcClearStatus(64); alarmBArray[currentAlarm.index].set = 0; alarmBStruct newSet = checkFirst(); X12RtcSetAlarm(1, &newSet.timeSet, 31); currentAlarm = newSet; } //both alarms if(flags == 96) { //kijkt of alarm a of b nog niet heeft geluid, en speelt die dan printf("\n ========== Alarm 0 en 1 ========= \n"); //kijkt welke van de 2 nog niet is afgegaan en speelt die dan nog af SoundA(); X12RtcClearStatus(32); SoundB(); X12RtcClearStatus(64); } if(succes == 0) { //succes } else { //error } } }