Minor tweaks to compile on win32.
[kugel-rb.git] / apps / alarm_menu.c
blob20b2f518212163607ecb88a62dd21d197723a65c
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_setfont(FONT_SYSFIXED);
61 lcd_setmargins(0, 0);
62 lcd_puts(0,1, str(LANG_ALARM_MOD_KEYS));
64 while(!done) {
65 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME), h, m);
66 lcd_puts(0,0, buf);
67 lcd_update();
69 switch(button_get(true)) {
70 case BUTTON_PLAY:
71 /* prevent that an alarm occurs in the shutdown procedure */
72 /* accept alarms only if they are in 2 minutes or more */
73 hour = rtc_read(0x03);
74 hour = ((hour & 0x30) >> 4) * 10 + (hour & 0x0f);
75 minute = rtc_read(0x02);
76 minute = ((minute & 0x70) >> 4) * 10 + (minute & 0x0f);
77 int togo = (m + h * 60 - minute - hour * 60 + 1440) % 1440;
78 if (togo > 1) {
79 lcd_clear_display();
80 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME_TO_GO), togo / 60, togo % 60);
81 lcd_puts(0,0, buf);
82 lcd_update();
83 rtc_init();
84 rtc_set_alarm(h,m);
85 /* in some cases enabling the alarm results in an activated AF flag */
86 /* this should not happen, but it does */
87 /* if you know why, tell me! */
88 /* for now, we try again forever in this case */
89 while (rtc_enable_alarm(true)) { /* error occured */
90 sleep(HZ / 10);
91 rtc_init();
92 rtc_set_alarm(h,m);
94 sleep(HZ);
95 lcd_puts(0,1,str(LANG_ALARM_MOD_SHUTDOWN));
96 lcd_update();
97 sleep(HZ);
98 power_off();
99 } else {
100 lcd_clear_display();
101 lcd_puts(0,0,str(LANG_ALARM_MOD_ERROR));
102 lcd_update();
103 sleep(HZ);
104 lcd_clear_display();
105 lcd_puts(0,1,str(LANG_ALARM_MOD_KEYS));
107 break;
109 /* inc(m) */
110 case BUTTON_RIGHT:
111 case BUTTON_RIGHT | BUTTON_REPEAT:
112 m += 5;
113 if (m == 60) {
114 h += 1;
115 m = 0;
117 if (h == 24)
118 h = 0;
119 break;
121 /* dec(m) */
122 case BUTTON_LEFT:
123 case BUTTON_LEFT | BUTTON_REPEAT:
124 m -= 5;
125 if (m == -5) {
126 h -= 1;
127 m = 55;
129 if (h == -1)
130 h = 23;
131 break;
133 #if CONFIG_KEYPAD == RECORDER_PAD
134 /* inc(h) */
135 case BUTTON_UP:
136 case BUTTON_UP | BUTTON_REPEAT:
137 h = (h+1) % 24;
138 break;
140 /* dec(h) */
141 case BUTTON_DOWN:
142 case BUTTON_DOWN | BUTTON_REPEAT:
143 h = (h+23) % 24;
144 break;
145 #endif
147 #if CONFIG_KEYPAD == RECORDER_PAD
148 case BUTTON_OFF:
149 #else
150 case BUTTON_STOP:
151 case BUTTON_MENU:
152 #endif
153 done = true;
154 break;
158 return false;
161 #endif /* HAVE_ALARM_MOD */