///---------------------------------------------------------------------------- /// Function Break ///---------------------------------------------------------------------------- void PowerOffTimerCallback(void) { debug("Power Off Timer callback: activated.\r\n"); // Handle and finish any processing StopMonitoring(g_triggerRecord.opMode, FINISH_PROCESSING); if (g_timerModeLastRun == YES) { debug("Timer Mode: Ending last session, now disabling...\r\n"); g_unitConfig.timerMode = DISABLED; // Save Unit Config (also covers LCD contrast change case) SaveRecordData(&g_unitConfig, DEFAULT_RECORD, REC_UNIT_CONFIG_TYPE); } else if (g_lcdContrastChanged == YES) { // Save Unit Config here to prevent constant saving on LCD contrast adjustment SaveRecordData(&g_unitConfig, DEFAULT_RECORD, REC_UNIT_CONFIG_TYPE); } OverlayMessage(getLangText(TIMER_MODE_TEXT), getLangText(POWERING_UNIT_OFF_NOW_TEXT), 3 * SOFT_SECS); // Power the unit off debug("Timer mode: Finished for the day, sleep time.\r\n"); PowerUnitOff(SHUTDOWN_UNIT); }
///---------------------------------------------------------------------------- /// Function Break ///---------------------------------------------------------------------------- void HandleUserPowerOffDuringTimerMode(void) { INPUT_MSG_STRUCT mn_msg; uint8 choice; // Simulate a keypress if the user pressed the off key, which doesn't register as a keypess KeypressEventMgr(); MessageBox(getLangText(STATUS_TEXT), getLangText(UNIT_IS_IN_TIMER_MODE_TEXT), MB_OK); choice = MessageBox(getLangText(WARNING_TEXT), getLangText(CANCEL_TIMER_MODE_Q_TEXT), MB_YESNO); // User decided to cancel Timer mode if (choice == MB_FIRST_CHOICE) { g_unitConfig.timerMode = DISABLED; // Disable the Power Off timer ClearSoftTimer(POWER_OFF_TIMER_NUM); // Save Unit Config SaveRecordData(&g_unitConfig, DEFAULT_RECORD, REC_UNIT_CONFIG_TYPE); OverlayMessage(getLangText(STATUS_TEXT), getLangText(TIMER_MODE_DISABLED_TEXT), 2 * SOFT_SECS); } else // User decided to stay in Timer mode { choice = MessageBox(getLangText(STATUS_TEXT), getLangText(POWER_UNIT_OFF_EARLY_Q_TEXT), MB_YESNO); if (choice == MB_FIRST_CHOICE) { MessageBox(getLangText(STATUS_TEXT), getLangText(POWERING_UNIT_OFF_NOW_TEXT), MB_OK); MessageBox(getLangText(STATUS_TEXT), getLangText(PLEASE_PRESS_ENTER_TEXT), MB_OK); // Turn unit off/sleep debug("Timer mode: Shutting down unit early due to user request. Powering off now...\r\n"); PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } } SETUP_MENU_MSG(MAIN_MENU); JUMP_TO_ACTIVE_MENU(); }
///---------------------------------------------------------------------------- /// Function Break ///---------------------------------------------------------------------------- void ProcessTimerModeSettings(uint8 mode) { //uint8 dayOfWeek = 0; uint8 startDay = 0; uint8 startHour = 0; uint16 minutesLeft = 0; DATE_TIME_STRUCT currentTime = GetCurrentTime(); uint8 status = ValidateTimerModeSettings(); // Check if the timer mode settings check failed or if the timer mode setting has been disabled if ((status == FAILED) || (g_unitConfig.timerMode == DISABLED)) { g_unitConfig.timerMode = DISABLED; SaveRecordData(&g_unitConfig, DEFAULT_RECORD, REC_UNIT_CONFIG_TYPE); // Disable the Power Off timer in case it's set ClearSoftTimer(POWER_OFF_TIMER_NUM); if (mode == PROMPT) { sprintf((char*)g_spareBuffer, "%s %s", getLangText(TIMER_SETTINGS_INVALID_TEXT), getLangText(TIMER_MODE_DISABLED_TEXT)); MessageBox(getLangText(ERROR_TEXT), (char*)g_spareBuffer, MB_OK); } } else // status == PASSED || status == IN_PROGRESS { // Calculate timer mode active run time in minutes TimerModeActiveMinutes(); // Init start day based on the start date provided by the user startDay = g_unitConfig.timerStartDate.day; // Check if in progress, requiring extra logic to determine alarm settings if (status == IN_PROGRESS) { // Check if the stop time is greater than the start time if ((g_unitConfig.timerStopTime.hour > g_unitConfig.timerStartTime.hour) || ((g_unitConfig.timerStopTime.hour == g_unitConfig.timerStartTime.hour) && (g_unitConfig.timerStopTime.min > g_unitConfig.timerStartTime.min))) { // Advance the start day startDay++; // Check if the start day is beyond the total days in the current month if (startDay > g_monthTable[(uint8)(g_unitConfig.timerStartDate.month)].days) { // Set the start day to the first day of next month startDay = 1; } } } // Check for specialty case hourly mode and in progress, requiring extra logic to determine alarm settings if ((g_unitConfig.timerModeFrequency == TIMER_MODE_HOURLY) && (status == IN_PROGRESS)) { // Check if another hour time slot to run again today if (currentTime.hour != g_unitConfig.timerStopTime.hour) { // Start day remains the same startDay = g_unitConfig.timerStartDate.day; // Check if current hour time slot has not started if (currentTime.min < g_unitConfig.timerStartTime.min) { // Set alarm for the same hour startHour = currentTime.hour; } else { // Set alarm for the next hour startHour = currentTime.hour + 1; // Account for end of day boundary if (startHour > 23) { startHour = 0; // Advance the start day startDay++; // Check if the start day is beyond the total days in the current month if (startDay > g_monthTable[(uint8)(g_unitConfig.timerStartDate.month)].days) { // Set the start day to the first day of next month startDay = 1; } } } EnableExternalRtcAlarm(startDay, startHour, g_unitConfig.timerStartTime.min, 0); } else // This is the last hour time slot to run today, set alarm for next day { // startDay calculated correctly in above previous status == IN_PROGRESS logic EnableExternalRtcAlarm(startDay, g_unitConfig.timerStartTime.hour, g_unitConfig.timerStartTime.min, 0); } } else // All other timer modes { EnableExternalRtcAlarm(startDay, g_unitConfig.timerStartTime.hour, g_unitConfig.timerStartTime.min, 0); } if (status == PASSED) { if (mode == PROMPT) { sprintf((char*)g_spareBuffer, "%s %s", getLangText(TIMER_MODE_NOW_ACTIVE_TEXT), getLangText(PLEASE_POWER_OFF_UNIT_TEXT)); MessageBox(getLangText(STATUS_TEXT), (char*)g_spareBuffer, MB_OK); } // Check if start time is greater than the current time if (((g_unitConfig.timerStartTime.hour * 60) + g_unitConfig.timerStartTime.min) > ((currentTime.hour * 60) + currentTime.min)) { // Take the difference between start time and current time minutesLeft = (uint16)(((g_unitConfig.timerStartTime.hour * 60) + g_unitConfig.timerStartTime.min) - ((currentTime.hour * 60) + currentTime.min)); } else // Current time is after the start time, meaning the start time is the next day { // Take the difference between 24 hours and the current time plus the start time minutesLeft = (uint16)((24 * 60) - ((currentTime.hour * 60) + currentTime.min) + ((g_unitConfig.timerStartTime.hour * 60) + g_unitConfig.timerStartTime.min)); } // Check if the start time is within the next minute if (minutesLeft <= 1) { OverlayMessage(getLangText(WARNING_TEXT), getLangText(POWERING_UNIT_OFF_NOW_TEXT), 2 * SOFT_SECS); // Need to shutdown the unit now, otherwise the start time window will be missed PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } else // More than 1 minute left before the start time { // Make sure the unit turns off one minute before the start time if the user forgets to turn the unit off minutesLeft -= 1; // Need to handle state where timer mode is going active but unit hasn't power cycled into timer mode yet g_allowQuickPowerOffForTimerModeSetup = YES; // Set the Power off soft timer to prevent the unit from staying on past the Timer mode start time AssignSoftTimer(POWER_OFF_TIMER_NUM, (uint32)(minutesLeft * 60 * 2), PowerOffTimerCallback); } } else // status == IN_PROGRESS { if (mode == PROMPT) { sprintf((char*)g_spareBuffer, "%s", getLangText(TIMER_MODE_NOW_ACTIVE_TEXT)); MessageBox(getLangText(STATUS_TEXT), (char*)g_spareBuffer, MB_OK); } // Check if specialty mode hourly if (g_unitConfig.timerModeFrequency == TIMER_MODE_HOURLY) { if (currentTime.min < g_unitConfig.timerStopTime.min) { minutesLeft = (g_unitConfig.timerStopTime.min - currentTime.min); } else { minutesLeft = (60 + g_unitConfig.timerStopTime.min - currentTime.min); } if (minutesLeft > 58) minutesLeft = 58; } // Check if the current time is greater than the stop time, indicating that midnight boundary was crossed else if (((currentTime.hour * 60) + currentTime.min) > ((g_unitConfig.timerStopTime.hour * 60) + g_unitConfig.timerStopTime.min)) { // Calculate the time left before powering off to be 24 + the stop time minus the current time minutesLeft = (uint16)(((24 * 60) + (g_unitConfig.timerStopTime.hour * 60) + g_unitConfig.timerStopTime.min) - ((currentTime.hour * 60) + currentTime.min)); } else // Current time is less than start time, operating within the same day { // Calculate the time left before powering off to be the stop time minus the current time minutesLeft = (uint16)(((g_unitConfig.timerStopTime.hour * 60) + g_unitConfig.timerStopTime.min) - ((currentTime.hour * 60) + currentTime.min)); } // Make sure timeout value is not zero if (minutesLeft == 0) minutesLeft = 1; debug("Timer Mode: In progress, minutes left before power off: %d (Expired secs this min: %d)\r\n", minutesLeft, currentTime.sec); // Setup soft timer to turn system off when timer mode is finished for the day AssignSoftTimer(POWER_OFF_TIMER_NUM, (uint32)((minutesLeft * 60 * 2) - (currentTime.sec * 2)), PowerOffTimerCallback); } } }
///---------------------------------------------------------------------------- /// Function Break ///---------------------------------------------------------------------------- BOOLEAN TimerModeActiveCheck(void) { BOOLEAN status = FALSE; DATE_TIME_STRUCT time = GetExternalRtcTime(); uint8 choice; if (g_unitConfig.validationKey == 0xA5A5) { // Check if timer mode is enabled if (g_unitConfig.timerMode == ENABLED) { debug("Timer Mode active\r\n"); // Check if the timer mode settings match the current hour and minute meaning the unit powered itself on if ((g_unitConfig.timerStartTime.hour == time.hour) && (g_unitConfig.timerStartTime.min == time.min)) { debug("Timer Mode Check: Matched Timer settings...\r\n"); status = TRUE; } // Check again if settings match current hour and near minute meaning unit powered itself on but suffered from long startup else if ((g_unitConfig.timerStartTime.hour == time.hour) && ((((g_unitConfig.timerStartTime.min + 1) == 60) && (time.min == 0)) || ((g_unitConfig.timerStartTime.min + 1) == time.min))) { debug("Timer Mode Check: Matched Timer settings (long startup)...\r\n"); status = TRUE; } // Check specialty hourly mode and if current minute matches and the current hour is within range else if ((g_unitConfig.timerModeFrequency == TIMER_MODE_HOURLY) && (g_unitConfig.timerStartTime.min == time.min) && // Check if the start and stop hours match meaning hourly mode runs every hour ((g_unitConfig.timerStartTime.hour == g_unitConfig.timerStopTime.hour) || // OR Check if hourly mode does not cross a 24 hour boundary and the current hour is within range ((g_unitConfig.timerStartTime.hour < g_unitConfig.timerStopTime.hour) && (time.hour >= g_unitConfig.timerStartTime.hour) && (time.hour <= g_unitConfig.timerStopTime.hour)) || // OR Check if the current hour is within range with an hourly mode that does cross a 24 hour boundary ((time.hour >= g_unitConfig.timerStartTime.hour) || (time.hour <= g_unitConfig.timerStopTime.hour)))) { debug("Timer Mode Check: Matched Timer settings (hourly)...\r\n"); status = TRUE; } else { MessageBox(getLangText(STATUS_TEXT), getLangText(UNIT_IS_IN_TIMER_MODE_TEXT), MB_OK); choice = MessageBox(getLangText(WARNING_TEXT), getLangText(CANCEL_TIMER_MODE_Q_TEXT), MB_YESNO); if (choice == MB_FIRST_CHOICE) { g_unitConfig.timerMode = DISABLED; // Save Unit Config SaveRecordData(&g_unitConfig, DEFAULT_RECORD, REC_UNIT_CONFIG_TYPE); OverlayMessage(getLangText(STATUS_TEXT), getLangText(TIMER_MODE_DISABLED_TEXT), 2 * SOFT_SECS); } else // User decided to stay in Timer mode { // TOD Alarm registers still set from previous run, TOD Alarm Mask re-enabled in rtc init // Clear TOD Alarm Mask to allow TOD Alarm interrupt to be generated // ADD CODE TO CLEAR ALARM OverlayMessage(getLangText(WARNING_TEXT), getLangText(POWERING_UNIT_OFF_NOW_TEXT), 2 * SOFT_SECS); // Turn unit off/sleep debug("Timer mode: Staying in Timer mode. Powering off now...\r\n"); PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } } } } return (status); }
///---------------------------------------------------------------------------- /// Function Break ///---------------------------------------------------------------------------- void ProcessTimerMode(void) { DATE_TIME_STRUCT currTime = GetExternalRtcTime(); // Check if the Timer mode activated after stop date if (// First Check for past year (currTime.year > g_unitConfig.timerStopDate.year) || // Second check for equal year but past month ((currTime.year == g_unitConfig.timerStopDate.year) && (currTime.month > g_unitConfig.timerStopDate.month)) || // Third check for equal year, equal month, but past day ((currTime.year == g_unitConfig.timerStopDate.year) && (currTime.month == g_unitConfig.timerStopDate.month) && (currTime.day > g_unitConfig.timerStopDate.day))) { // Disable alarm output generation debug("Timer Mode: Activated after date...\r\n"); debug("Timer Mode: Disabling...\r\n"); g_unitConfig.timerMode = DISABLED; // Save Unit Config SaveRecordData(&g_unitConfig, DEFAULT_RECORD, REC_UNIT_CONFIG_TYPE); // Deactivate alarm interrupts DisableExternalRtcAlarm(); // Turn unit off/sleep debug("Timer mode: Powering unit off...\r\n"); PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } // Check if the Timer mode activated before start date else if (// First Check for before year (currTime.year < g_unitConfig.timerStartDate.year) || // Second check for equal year but before month ((currTime.year == g_unitConfig.timerStartDate.year) && (currTime.month < g_unitConfig.timerStartDate.month)) || // Third check for equal year, equal month, but before day ((currTime.year == g_unitConfig.timerStartDate.year) && (currTime.month == g_unitConfig.timerStartDate.month) && (currTime.day < g_unitConfig.timerStartDate.day))) { debug("Timer Mode: Activated before date...\r\n"); ResetTimeOfDayAlarm(); // Turn unit off/sleep debug("Timer mode: Powering unit off...\r\n"); PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } // Check if the Timer mode activated during active dates but on an off day else if ((g_unitConfig.timerModeFrequency == TIMER_MODE_WEEKDAYS) && ((currTime.weekday == SAT) || (currTime.weekday == SUN))) { debug("Timer Mode: Activated on an off day (weekday freq)...\r\n"); ResetTimeOfDayAlarm(); // Turn unit off/sleep debug("Timer mode: Powering unit off...\r\n"); PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } // Check if the Timer mode activated during active dates but on an off day else if ((g_unitConfig.timerModeFrequency == TIMER_MODE_MONTHLY) && (currTime.day != g_unitConfig.timerStartDate.day)) { debug("Timer Mode: Activated on off day (monthly freq)...\r\n"); ResetTimeOfDayAlarm(); // Turn unit off/sleep debug("Timer mode: Powering unit off...\r\n"); PowerUnitOff(SHUTDOWN_UNIT); // Return unnecessary } // Check if the Timer mode activated on end date (and not hourly mode) else if ((g_unitConfig.timerModeFrequency != TIMER_MODE_HOURLY) && (currTime.year == g_unitConfig.timerStopDate.year) && (currTime.month == g_unitConfig.timerStopDate.month) && (currTime.day == g_unitConfig.timerStopDate.day)) { // Disable alarm output generation debug("Timer Mode: Activated on end date...\r\n"); // Signal the timer mode end of session timer to stop timer mode due to this being the last run g_timerModeLastRun = YES; // Deactivate alarm interrupts DisableExternalRtcAlarm(); } // Check if the Timer mode activated on end date and end hour (hourly mode) else if ((g_unitConfig.timerModeFrequency == TIMER_MODE_HOURLY) && (currTime.year == g_unitConfig.timerStopDate.year) && (currTime.month == g_unitConfig.timerStopDate.month) && (currTime.day == g_unitConfig.timerStopDate.day) && (currTime.hour == g_unitConfig.timerStopTime.hour)) { // Disable alarm output generation debug("Timer Mode: Activated on end date...\r\n"); // Signal the timer mode end of session timer to stop timer mode due to this being the last run g_timerModeLastRun = YES; // Deactivate alarm interrupts DisableExternalRtcAlarm(); } else // Timer mode started during the active dates on a day it's supposed to run { if (g_unitConfig.timerModeFrequency == TIMER_MODE_ONE_TIME) { // Signal the timer mode end of session timer to stop timer mode due to this being the last run g_timerModeLastRun = YES; // Deactivate alarm interrupts DisableExternalRtcAlarm(); } else // All other timer modes { // Reset the Time of Day Alarm to wake the unit up again ResetTimeOfDayAlarm(); } } OverlayMessage(getLangText(STATUS_TEXT), getLangText(TIMER_MODE_NOW_ACTIVE_TEXT), (2 * SOFT_SECS)); raiseTimerEventFlag(TIMER_MODE_TIMER_EVENT); // Setup soft timer to turn system off when timer mode is finished for the day (minus the expired secs in the current minute AssignSoftTimer(POWER_OFF_TIMER_NUM, ((g_unitConfig.TimerModeActiveMinutes * 60 * 2) - (currTime.sec * 2)), PowerOffTimerCallback); debug("Timer mode: running...\r\n"); }