Split 8-bit-to-native conversion in bmp.c into a function, add support for plugging...
[kugel-rb.git] / apps / alarm_menu.c
blob319127840e5d82314a57b08ed199a487eb7c4f97
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 "statusbar.h"
44 #include "viewport.h"
46 static void speak_time(int hours, int minutes, bool speak_hours)
48 if (global_settings.talk_menu){
49 if(speak_hours) {
50 talk_value(hours, UNIT_HOUR, false);
51 talk_value(minutes, UNIT_MIN, true);
52 } else {
53 talk_value(minutes, UNIT_MIN, false);
58 bool alarm_screen(void)
60 int h, m;
61 bool done=false;
62 char buf[32];
63 struct tm *tm;
64 int togo;
65 int button;
66 int i;
67 bool update = true;
68 bool hour_wrapped = false;
69 struct viewport vp[NB_SCREENS];
71 rtc_get_alarm(&h, &m);
73 /* After a battery change the RTC values are out of range */
74 if (m > 60 || h > 24) {
75 m = 0;
76 h = 12;
77 } else {
78 m = m / 5 * 5; /* 5 min accuracy should be enough */
80 FOR_NB_SCREENS(i)
82 viewport_set_defaults(&vp[i], i);
85 while(!done) {
86 if(update)
88 FOR_NB_SCREENS(i)
90 screens[i].set_viewport(&vp[i]);
91 screens[i].clear_viewport();
92 screens[i].puts(0, 3, str(LANG_ALARM_MOD_KEYS));
94 /* Talk when entering the wakeup screen */
95 if (global_settings.talk_menu)
97 talk_value(h, UNIT_HOUR, true);
98 talk_value(m, UNIT_MIN, true);
100 update = false;
103 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME), h, m);
104 FOR_NB_SCREENS(i)
106 screens[i].set_viewport(&vp[i]);
107 screens[i].puts(0, 1, buf);
108 screens[i].update_viewport();
109 screens[i].set_viewport(NULL);
111 button = get_action(CONTEXT_SETTINGS,HZ);
113 switch(button) {
114 case ACTION_STD_OK:
115 /* prevent that an alarm occurs in the shutdown procedure */
116 /* accept alarms only if they are in 2 minutes or more */
117 tm = get_time();
118 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
120 if (togo > 1) {
121 rtc_init();
122 rtc_set_alarm(h,m);
123 rtc_enable_alarm(true);
124 if (global_settings.talk_menu)
126 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
127 talk_value(togo / 60, UNIT_HOUR, true);
128 talk_value(togo % 60, UNIT_MIN, true);
129 talk_force_enqueue_next();
131 splashf(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
132 togo / 60, togo % 60);
133 done = true;
134 } else {
135 splash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
136 update = true;
138 break;
140 /* inc(m) */
141 case ACTION_SETTINGS_INC:
142 case ACTION_SETTINGS_INCREPEAT:
143 m += 5;
144 if (m == 60) {
145 h += 1;
146 m = 0;
147 hour_wrapped = true;
149 if (h == 24)
150 h = 0;
152 speak_time(h, m, hour_wrapped);
153 break;
155 /* dec(m) */
156 case ACTION_SETTINGS_DEC:
157 case ACTION_SETTINGS_DECREPEAT:
158 m -= 5;
159 if (m == -5) {
160 h -= 1;
161 m = 55;
162 hour_wrapped = true;
164 if (h == -1)
165 h = 23;
167 speak_time(h, m, hour_wrapped);
168 break;
170 /* inc(h) */
171 case ACTION_STD_NEXT:
172 case ACTION_STD_NEXTREPEAT:
173 h = (h+1) % 24;
175 if (global_settings.talk_menu)
176 talk_value(h, UNIT_HOUR, false);
177 break;
179 /* dec(h) */
180 case ACTION_STD_PREV:
181 case ACTION_STD_PREVREPEAT:
182 h = (h+23) % 24;
184 if (global_settings.talk_menu)
185 talk_value(h, UNIT_HOUR, false);
186 break;
188 case ACTION_STD_CANCEL:
189 rtc_enable_alarm(false);
190 splash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
191 done = true;
192 break;
194 case ACTION_NONE:
195 hour_wrapped = false;
196 break;
198 default:
199 if(default_event_handler(button) == SYS_USB_CONNECTED)
201 rtc_enable_alarm(false);
202 return true;
204 break;
207 return false;
210 #endif /* HAVE_RTC_ALARM */