When we read the year from the RTC, it can be so totally messed up so that
[kugel-rb.git] / apps / alarm_menu.c
blob9ccdcfc3c29b0f367b01916b11360c92af576d97
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Uwe Freese
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "options.h"
22 #include "lcd.h"
23 #include "font.h"
24 #include "button.h"
25 #include "kernel.h"
26 #include "sprintf.h"
27 #include <string.h>
28 #include "settings.h"
29 #include "power.h"
30 #include "status.h"
31 #include "rtc.h"
32 #include <stdbool.h>
34 #include "lang.h"
35 #include "power.h"
36 #include "alarm_menu.h"
37 #include "backlight.h"
39 #ifdef HAVE_ALARM_MOD
41 bool alarm_screen(void)
43 /* get alarm time from RTC */
45 int h, m, hour, minute;
47 rtc_get_alarm(&h, &m);
49 if (m > 60 || h > 24) { /* after battery-change RTC-values are out of range */
50 m = 0;
51 h = 12;
52 } else {
53 m = m / 5 * 5; /* 5 min accuracy should be enough */
56 bool done=false;
57 char buf[32];
59 lcd_clear_display();
60 lcd_puts(0,1, str(LANG_ALARM_MOD_KEYS));
62 while(!done) {
63 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME), h, m);
64 lcd_puts(0,0, buf);
65 lcd_update();
67 switch(button_get(true)) {
68 case BUTTON_PLAY:
69 /* prevent that an alarm occurs in the shutdown procedure */
70 /* accept alarms only if they are in 2 minutes or more */
71 hour = rtc_read(0x03);
72 hour = ((hour & 0x30) >> 4) * 10 + (hour & 0x0f);
73 minute = rtc_read(0x02);
74 minute = ((minute & 0x70) >> 4) * 10 + (minute & 0x0f);
75 int togo = (m + h * 60 - minute - hour * 60 + 1440) % 1440;
76 if (togo > 1) {
77 lcd_clear_display();
78 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME_TO_GO), togo / 60, togo % 60);
79 lcd_puts(0,0, buf);
80 lcd_update();
81 rtc_init();
82 rtc_set_alarm(h,m);
83 /* in some cases enabling the alarm results in an activated AF flag */
84 /* this should not happen, but it does */
85 /* if you know why, tell me! */
86 /* for now, we try again forever in this case */
87 while (rtc_enable_alarm(true)) { /* error occured */
88 sleep(HZ / 10);
89 rtc_init();
90 rtc_set_alarm(h,m);
92 sleep(HZ);
93 lcd_puts(0,1,str(LANG_ALARM_MOD_SHUTDOWN));
94 lcd_update();
95 sleep(HZ);
96 power_off();
97 } else {
98 lcd_clear_display();
99 lcd_puts(0,0,str(LANG_ALARM_MOD_ERROR));
100 lcd_update();
101 sleep(HZ);
102 lcd_clear_display();
103 lcd_puts(0,1,str(LANG_ALARM_MOD_KEYS));
105 break;
107 /* inc(m) */
108 case BUTTON_RIGHT:
109 case BUTTON_RIGHT | BUTTON_REPEAT:
110 m += 5;
111 if (m == 60) {
112 h += 1;
113 m = 0;
115 if (h == 24)
116 h = 0;
117 break;
119 /* dec(m) */
120 case BUTTON_LEFT:
121 case BUTTON_LEFT | BUTTON_REPEAT:
122 m -= 5;
123 if (m == -5) {
124 h -= 1;
125 m = 55;
127 if (h == -1)
128 h = 23;
129 break;
131 #ifdef HAVE_RECORDER_KEYPAD
132 /* inc(h) */
133 case BUTTON_UP:
134 case BUTTON_UP | BUTTON_REPEAT:
135 h = (h+1) % 24;
136 break;
138 /* dec(h) */
139 case BUTTON_DOWN:
140 case BUTTON_DOWN | BUTTON_REPEAT:
141 h = (h+23) % 24;
142 break;
143 #endif
145 #ifdef HAVE_RECORDER_KEYPAD
146 case BUTTON_OFF:
147 #else
148 case BUTTON_STOP:
149 case BUTTON_MENU:
150 #endif
151 done = true;
152 break;
156 return false;
159 #endif /* HAVE_ALARM_MOD */