/* clock_set_alarm: set alarm time to hours, minutes and seconds */ void clock_set_alarm(int hours, int minutes, int seconds) { pthread_mutex_lock(&Clock.mutex); Clock.alarm_time.hours = hours; Clock.alarm_time.minutes = minutes; Clock.alarm_time.seconds = seconds; Clock.alarm_enabled = 1; pthread_mutex_unlock(&Clock.mutex); display_alarm_time(hours, minutes, seconds); }
/* clock_set_alarm: set current time to hours, minutes and seconds */ void clock_set_alarm(int hours, int minutes, int seconds) { si_sem_wait(&Clock.mutex); Clock.alarm_time.hours = hours; Clock.alarm_time.minutes = minutes; Clock.alarm_time.seconds = seconds; Clock.alarm_enabled = 1; display_alarm_time(hours, minutes, seconds); si_sem_signal(&Clock.mutex); }
//Checks buttons for system settings void check_buttons(void) { uint8_t i; uint8_t sling_shot = 0; uint8_t minute_change = 1; uint8_t previous_button = 0; //If the user hits snooze while alarm is going off, record time so that we can set off alarm again in 9 minutes if ( (PIND & (1<<BUT_SNOOZE)) == 0 && alarm_going == TRUE) { alarm_going = FALSE; //Turn off alarm snooze = TRUE; //But remember that we are in snooze mode, alarm needs to go off again in a few minutes seconds_alarm_snooze = 0; minutes_alarm_snooze = minutes + 9; //Snooze to 9 minutes from now hours_alarm_snooze = hours; ampm_alarm_snooze = ampm; if(minutes_alarm_snooze > 59) { minutes_alarm_snooze -= 60; hours_alarm_snooze++; if(hours_alarm_snooze == 12) { if(ampm_alarm_snooze == AM) ampm_alarm_snooze = PM; else ampm_alarm_snooze = AM; } if(hours_alarm_snooze == 13) hours_alarm_snooze = 1; } } //Check for set time if ( (PINB & ((1<<BUT_UP)|(1<<BUT_DOWN))) == 0) { delay_ms(1000); if ( (PINB & ((1<<BUT_UP)|(1<<BUT_DOWN))) == 0) { //You've been holding up and down for 2 seconds //Set time! //siren(500); //Make some noise to show that you're setting the time while( (PINB & ((1<<BUT_UP)|(1<<BUT_DOWN))) == 0) //Wait for you to stop pressing the buttons display_time(1000); //Display current time for 1000ms while(1) { if ( (PIND & (1<<BUT_SNOOZE)) == 0) //All done! { for(i = 0 ; i < 3 ; i++) { display_time(250); //Display current time for 100ms clear_display(); delay_ms(250); } while((PIND & (1<<BUT_SNOOZE)) == 0) ; //Wait for you to release button break; } if ( (PINB & (1<<BUT_UP)) == 0) { //Ramp minutes faster if we are holding the button //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if(previous_button == BUT_UP) sling_shot++; else { sling_shot = 0; minute_change = 1; } previous_button = BUT_UP; if (sling_shot > 5) { minute_change++; if(minute_change > 30) minute_change = 30; sling_shot = 0; } //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= minutes += minute_change; if (minutes > 59) { minutes -= 60; hours++; if(hours == 13) hours = 1; if(hours == 12) { if(ampm == AM) ampm = PM; else ampm = AM; } } delay_ms(100); } if ( (PINB & (1<<BUT_DOWN)) == 0) { //Ramp minutes faster if we are holding the button //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if(previous_button == BUT_DOWN) sling_shot++; else { sling_shot = 0; minute_change = 1; } previous_button = BUT_DOWN; if (sling_shot > 5) { minute_change++; if(minute_change > 30) minute_change = 30; sling_shot = 0; } //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= minutes -= minute_change; if(minutes > 60) { minutes = 59; hours--; if(hours == 0) hours = 12; if(hours == 11) { if(ampm == AM) ampm = PM; else ampm = AM; } } delay_ms(100); } //clear_display(); //Blink display //delay_ms(100); } } } //Check for set alarm if ( (PIND & (1<<BUT_SNOOZE)) == 0) { TIMSK2 = 0; display_alarm_time(1000); if ( (PIND & (1<<BUT_SNOOZE)) == 0) { //You've been holding snooze for 2 seconds //Set alarm time! //Disable the regular display clock interrupt TIMSK2 = 0; while( (PIND & (1<<BUT_SNOOZE)) == 0) //Wait for you to stop pressing the buttons { clear_display(); delay_ms(250); display_alarm_time(250); //Display current time for 1000ms } while(1) { display_alarm_time(100); //Display current time for 100ms if ( (PIND & (1<<BUT_SNOOZE)) == 0) //All done! { for(i = 0 ; i < 4 ; i++) { display_alarm_time(250); //Display current time for 100ms clear_display(); delay_ms(250); } while((PIND & (1<<BUT_SNOOZE)) == 0) ; //Wait for you to release button TIMSK2 = (1<<TOIE2); //Re-enable the timer 2 interrupt break; } if ( (PINB & (1<<BUT_UP)) == 0) { //Ramp minutes faster if we are holding the button //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if(previous_button == BUT_UP) sling_shot++; else { sling_shot = 0; minute_change = 1; } previous_button = BUT_UP; if (sling_shot > 5) { minute_change++; if(minute_change > 30) minute_change = 30; sling_shot = 0; } //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= minutes_alarm += minute_change; if (minutes_alarm > 59) { minutes_alarm -= 60; hours_alarm++; if(hours_alarm == 13) hours_alarm = 1; if(hours_alarm == 12) { if(ampm_alarm == AM) ampm_alarm = PM; else ampm_alarm = AM; } } //delay_ms(100); } if ( (PINB & (1<<BUT_DOWN)) == 0) { //Ramp minutes faster if we are holding the button //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if(previous_button == BUT_DOWN) sling_shot++; else { sling_shot = 0; minute_change = 1; } previous_button = BUT_DOWN; if (sling_shot > 5) { minute_change++; if(minute_change > 30) minute_change = 30; sling_shot = 0; } //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= minutes_alarm -= minute_change; if(minutes_alarm > 60) { minutes_alarm = 59; hours_alarm--; if(hours_alarm == 0) hours_alarm = 12; if(hours_alarm == 11) { if(ampm_alarm == AM) ampm_alarm = PM; else ampm_alarm = AM; } } //delay_ms(100); } //clear_display(); //Blink display //delay_ms(100); } } else TIMSK2 = (1<<TOIE2); //Re-enable the timer 2 interrupt } }
/* set_task: reads messages from the user interface... */ void set_task(void) { /* message array */ char message[SI_UI_MAX_MESSAGE_SIZE]; /* time read from user interface */ int hours = 0, minutes = 0, seconds = 0; /* set GUI size */ si_ui_set_size(400, 200); while(1) { /* read a message */ si_ui_receive(message); /* check if it is a set message */ if (strncmp(message, "set:", 4) == 0) { time_from_set_message(message, &hours, &minutes, &seconds); if (time_ok(hours, minutes, seconds)) { set_time(hours, minutes, seconds); } else { si_ui_show_error("Illegal value for hours, minutes or seconds"); } } /* check if it is an alarm message */ else if (strncmp(message, "alarm:", 6) == 0) { time_from_alarm_message(message, &hours, &minutes, &seconds); if (time_ok(hours, minutes, seconds)) { set_alarm(hours, minutes, seconds); display_alarm_time(hours,minutes,seconds); } else { si_ui_show_error("Illegal value for hours, minutes or seconds"); } } /* check if it is an reset message */ else if (strncmp(message, "reset", 5) == 0) { erase_alarm_time(); erase_alarm_text(); reset_alarm(); } /* check if it is an exit message */ else if (strcmp(message, "exit") == 0) { exit(0); } /* not a legal message */ else { si_ui_show_error("unexpected message type"); } } }