Change the hardcoded radio screen almost to how it was before - better use of screen...
[kugel-rb.git] / apps / alarm_menu.c
blobb87ae2b53ac29cbd58cd14bdb8460f4413e956a7
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 #include <stdbool.h>
25 #include "lcd.h"
26 #include "action.h"
27 #include "kernel.h"
28 #include <string.h>
29 #include "settings.h"
30 #include "power.h"
31 #include "icons.h"
32 #include "rtc.h"
33 #include "misc.h"
34 #include "screens.h"
35 #include "talk.h"
36 #include "lang.h"
37 #include "alarm_menu.h"
38 #include "splash.h"
39 #include "viewport.h"
41 static void speak_time(int hours, int minutes, bool speak_hours, bool enqueue)
43 if (global_settings.talk_menu){
44 if(speak_hours) {
45 talk_value(hours, UNIT_HOUR, enqueue);
46 talk_value(minutes, UNIT_MIN, true);
47 } else {
48 talk_value(minutes, UNIT_MIN, enqueue);
53 bool alarm_screen(void)
55 int h, m;
56 bool done = false;
57 struct tm *tm;
58 int togo;
59 int button;
60 int i;
61 bool update = true;
62 bool hour_wrapped = false;
63 struct viewport vp[NB_SCREENS];
65 rtc_get_alarm(&h, &m);
67 /* After a battery change the RTC values are out of range */
68 if (m > 60 || h > 24) {
69 m = 0;
70 h = 12;
71 } else {
72 m = m / 5 * 5; /* 5 min accuracy should be enough */
74 FOR_NB_SCREENS(i)
76 viewport_set_defaults(&vp[i], i);
79 while(!done) {
80 if(update)
82 FOR_NB_SCREENS(i)
84 screens[i].set_viewport(&vp[i]);
85 screens[i].clear_viewport();
86 screens[i].puts(0, 4, str(LANG_ALARM_MOD_KEYS));
88 /* Talk when entering the wakeup screen */
89 speak_time(h, m, true, true);
90 update = false;
93 FOR_NB_SCREENS(i)
95 screens[i].set_viewport(&vp[i]);
96 screens[i].putsf(0, 1, str(LANG_ALARM_MOD_TIME));
97 screens[i].putsf(0, 2, "%02d:%02d", h, m);
98 screens[i].update_viewport();
99 screens[i].set_viewport(NULL);
101 button = get_action(CONTEXT_SETTINGS,HZ);
103 switch(button) {
104 case ACTION_STD_OK:
105 /* prevent that an alarm occurs in the shutdown procedure */
106 /* accept alarms only if they are in 2 minutes or more */
107 tm = get_time();
108 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
110 if (togo > 1) {
111 rtc_init();
112 rtc_set_alarm(h,m);
113 rtc_enable_alarm(true);
114 if (global_settings.talk_menu)
116 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
117 talk_value(togo / 60, UNIT_HOUR, true);
118 talk_value(togo % 60, UNIT_MIN, true);
119 talk_force_enqueue_next();
121 splashf(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
122 togo / 60, togo % 60);
123 done = true;
124 } else {
125 splash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
126 update = true;
128 break;
130 /* inc(m) */
131 case ACTION_SETTINGS_INC:
132 case ACTION_SETTINGS_INCREPEAT:
133 m += 5;
134 if (m == 60) {
135 h += 1;
136 m = 0;
137 hour_wrapped = true;
139 if (h == 24)
140 h = 0;
142 speak_time(h, m, hour_wrapped, false);
143 break;
145 /* dec(m) */
146 case ACTION_SETTINGS_DEC:
147 case ACTION_SETTINGS_DECREPEAT:
148 m -= 5;
149 if (m == -5) {
150 h -= 1;
151 m = 55;
152 hour_wrapped = true;
154 if (h == -1)
155 h = 23;
157 speak_time(h, m, hour_wrapped, false);
158 break;
160 /* inc(h) */
161 case ACTION_STD_NEXT:
162 case ACTION_STD_NEXTREPEAT:
163 h = (h+1) % 24;
165 if (global_settings.talk_menu)
166 talk_value(h, UNIT_HOUR, false);
167 break;
169 /* dec(h) */
170 case ACTION_STD_PREV:
171 case ACTION_STD_PREVREPEAT:
172 h = (h+23) % 24;
174 if (global_settings.talk_menu)
175 talk_value(h, UNIT_HOUR, false);
176 break;
178 case ACTION_STD_CANCEL:
179 rtc_enable_alarm(false);
180 splash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
181 done = true;
182 break;
184 case ACTION_NONE:
185 hour_wrapped = false;
186 break;
188 default:
189 if(default_event_handler(button) == SYS_USB_CONNECTED)
191 rtc_enable_alarm(false);
192 return true;
194 break;
197 return false;