Set the font to FONT_UI after exiting a plugin (FS#10132). This makes the core Rockbo...
[kugel-rb/myfork.git] / apps / alarm_menu.c
blobad8e1d755cfde79f4dc7015fc99c96012992ba91
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Uwe Freese
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #ifdef HAVE_RTC_ALARM
25 #include <stdbool.h>
27 #include "lcd.h"
28 #include "action.h"
29 #include "kernel.h"
30 #include "sprintf.h"
31 #include <string.h>
32 #include "settings.h"
33 #include "power.h"
34 #include "icons.h"
35 #include "rtc.h"
36 #include "misc.h"
37 #include "screens.h"
38 #include "talk.h"
39 #include "lang.h"
40 #include "power.h"
41 #include "alarm_menu.h"
42 #include "splash.h"
43 #include "viewport.h"
45 static void speak_time(int hours, int minutes, bool speak_hours)
47 if (global_settings.talk_menu){
48 if(speak_hours) {
49 talk_value(hours, UNIT_HOUR, false);
50 talk_value(minutes, UNIT_MIN, true);
51 } else {
52 talk_value(minutes, UNIT_MIN, false);
57 bool alarm_screen(void)
59 int h, m;
60 bool done=false;
61 char buf[32];
62 struct tm *tm;
63 int togo;
64 int button;
65 int i;
66 bool update = true;
67 bool hour_wrapped = false;
68 struct viewport vp[NB_SCREENS];
70 rtc_get_alarm(&h, &m);
72 /* After a battery change the RTC values are out of range */
73 if (m > 60 || h > 24) {
74 m = 0;
75 h = 12;
76 } else {
77 m = m / 5 * 5; /* 5 min accuracy should be enough */
79 FOR_NB_SCREENS(i)
81 viewport_set_defaults(&vp[i], i);
84 while(!done) {
85 if(update)
87 FOR_NB_SCREENS(i)
89 screens[i].set_viewport(&vp[i]);
90 screens[i].clear_viewport();
91 screens[i].puts(0, 3, str(LANG_ALARM_MOD_KEYS));
93 /* Talk when entering the wakeup screen */
94 if (global_settings.talk_menu)
96 talk_value(h, UNIT_HOUR, true);
97 talk_value(m, UNIT_MIN, true);
99 update = false;
102 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME), h, m);
103 FOR_NB_SCREENS(i)
105 screens[i].set_viewport(&vp[i]);
106 screens[i].puts(0, 1, buf);
107 screens[i].update_viewport();
108 screens[i].set_viewport(NULL);
110 button = get_action(CONTEXT_SETTINGS,HZ);
112 switch(button) {
113 case ACTION_STD_OK:
114 /* prevent that an alarm occurs in the shutdown procedure */
115 /* accept alarms only if they are in 2 minutes or more */
116 tm = get_time();
117 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
119 if (togo > 1) {
120 rtc_init();
121 rtc_set_alarm(h,m);
122 rtc_enable_alarm(true);
123 if (global_settings.talk_menu)
125 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
126 talk_value(togo / 60, UNIT_HOUR, true);
127 talk_value(togo % 60, UNIT_MIN, true);
128 talk_force_enqueue_next();
130 splashf(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
131 togo / 60, togo % 60);
132 done = true;
133 } else {
134 splash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
135 update = true;
137 break;
139 /* inc(m) */
140 case ACTION_SETTINGS_INC:
141 case ACTION_SETTINGS_INCREPEAT:
142 m += 5;
143 if (m == 60) {
144 h += 1;
145 m = 0;
146 hour_wrapped = true;
148 if (h == 24)
149 h = 0;
151 speak_time(h, m, hour_wrapped);
152 break;
154 /* dec(m) */
155 case ACTION_SETTINGS_DEC:
156 case ACTION_SETTINGS_DECREPEAT:
157 m -= 5;
158 if (m == -5) {
159 h -= 1;
160 m = 55;
161 hour_wrapped = true;
163 if (h == -1)
164 h = 23;
166 speak_time(h, m, hour_wrapped);
167 break;
169 /* inc(h) */
170 case ACTION_STD_NEXT:
171 case ACTION_STD_NEXTREPEAT:
172 h = (h+1) % 24;
174 if (global_settings.talk_menu)
175 talk_value(h, UNIT_HOUR, false);
176 break;
178 /* dec(h) */
179 case ACTION_STD_PREV:
180 case ACTION_STD_PREVREPEAT:
181 h = (h+23) % 24;
183 if (global_settings.talk_menu)
184 talk_value(h, UNIT_HOUR, false);
185 break;
187 case ACTION_STD_CANCEL:
188 rtc_enable_alarm(false);
189 splash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
190 done = true;
191 break;
193 case ACTION_NONE:
194 hour_wrapped = false;
195 break;
197 default:
198 if(default_event_handler(button) == SYS_USB_CONNECTED)
200 rtc_enable_alarm(false);
201 return true;
203 break;
206 return false;
209 #endif /* HAVE_RTC_ALARM */