THREAD(AlarmPollingThread, arg) { static u_int button_cooldown_counter = 0; static bool button_cooldown = true; u_long alarm_a; u_long alarm_b; short snooze_time; tm snooze; tm alarm_a_backup; for(;;) { X12RtcGetStatus(&alarm_a); alarm_b = alarm_a; alarm_a &= 0b00100000; alarm_b &= 0b01000000; if(alarm_a!=0 && alarm_a_on) { VsBeep((u_char)500, 1000); //BeepBoop, beeps with (frequency, duration) VsSetVolume(0,0); puts("Volume on"); } if(alarm_b!=0 && alarm_b_on) { VsBeep((u_char)1000, 1000); //BeepBoop, beeps with (frequency, duration) VsSetVolume(0,0); puts("Volume on"); } if(button_cooldown_counter >= 2) { button_cooldown = false; button_cooldown_counter = 0; } if(kb_button_is_pressed(KEY_ALT) && alarm_a!=0) { At45dbPageRead(2, &snooze_time, 2); X12RtcClearStatus(0b00100000); get_alarm_a(&snooze); snooze.tm_min+=snooze_time; set_alarm_a(&snooze); } if(!button_cooldown) { switch(kb_button_pressed()) { case KEY_ESC: puts("escape pressed"); if(VsGetVolume() != 0) { puts("volume on"); VsSetVolume(0, 0); } else if(VsGetVolume() == 0) { puts("volume off"); VsSetVolume(100, 100); } if(alarm_a != 0) { X12RtcClearStatus(0b00100000); At45dbPageRead(3, &alarm_a_backup, sizeof(tm)); set_alarm_a(&alarm_a_backup); } if(alarm_b != 0) { X12RtcClearStatus(0b01000000); disable_alarm_b(); alarm_b_on = false; } break; case KEY_01: alarm_a_on = !alarm_a_on; alarmstatus_changed = true; break; case KEY_02: alarm_b_on = !alarm_b_on; alarmstatus_changed = true; break; default: break; } button_cooldown = true; } if(button_cooldown) button_cooldown_counter++; NutSleep(100); } }
/*! * \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 } } }