blob: 3866a99bc192d9044865a84a48d90cb71cd44aa7 [file] [log] [blame]
Uwe Freese86352cc2003-01-22 12:50:34 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2003 Uwe Freese
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Uwe Freese86352cc2003-01-22 12:50:34 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "config.h"
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000022
Thom Johansen8fd6d652007-02-28 21:55:11 +000023#ifdef HAVE_RTC_ALARM
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000024
25#include <stdbool.h>
26
Uwe Freese86352cc2003-01-22 12:50:34 +000027#include "lcd.h"
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +000028#include "action.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000029#include "kernel.h"
30#include "sprintf.h"
31#include <string.h>
32#include "settings.h"
33#include "power.h"
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000034#include "icons.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000035#include "rtc.h"
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000036#include "misc.h"
37#include "screens.h"
Jonathan Gordond0ed3712008-05-29 08:20:07 +000038#include "talk.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000039#include "lang.h"
40#include "power.h"
41#include "alarm_menu.h"
42#include "backlight.h"
Kevin Ferraree991bee2005-11-16 15:12:15 +000043#include "splash.h"
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000044#include "statusbar.h"
Jonathan Gordond0ed3712008-05-29 08:20:07 +000045#include "viewport.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000046
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000047static void speak_time(int hours, int minutes, bool speak_hours)
48{
Steve Bavin32a95752007-10-19 15:31:42 +000049 if (global_settings.talk_menu){
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000050 if(speak_hours) {
51 talk_value(hours, UNIT_HOUR, false);
52 talk_value(minutes, UNIT_MIN, true);
53 } else {
54 talk_value(minutes, UNIT_MIN, false);
55 }
56 }
57}
58
Uwe Freese86352cc2003-01-22 12:50:34 +000059bool alarm_screen(void)
60{
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000061 int h, m;
62 bool done=false;
63 char buf[32];
64 struct tm *tm;
65 int togo;
66 int button;
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000067 int i;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000068 bool update = true;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000069 bool hour_wrapped = false;
Jonathan Gordond0ed3712008-05-29 08:20:07 +000070 struct viewport vp[NB_SCREENS];
Uwe Freese5bfa9b72003-03-03 14:23:39 +000071
Uwe Freese86352cc2003-01-22 12:50:34 +000072 rtc_get_alarm(&h, &m);
Uwe Freese5bfa9b72003-03-03 14:23:39 +000073
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000074 /* After a battery change the RTC values are out of range */
75 if (m > 60 || h > 24) {
Uwe Freese5bfa9b72003-03-03 14:23:39 +000076 m = 0;
77 h = 12;
78 } else {
79 m = m / 5 * 5; /* 5 min accuracy should be enough */
80 }
Jonathan Gordond0ed3712008-05-29 08:20:07 +000081 FOR_NB_SCREENS(i)
82 {
83 viewport_set_defaults(&vp[i], i);
84 }
Uwe Freese5bfa9b72003-03-03 14:23:39 +000085
Uwe Freese86352cc2003-01-22 12:50:34 +000086 while(!done) {
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000087 if(update)
88 {
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000089 FOR_NB_SCREENS(i)
90 {
Jonathan Gordond0ed3712008-05-29 08:20:07 +000091 screens[i].set_viewport(&vp[i]);
92 screens[i].clear_viewport();
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000093 screens[i].puts(0, 3, str(LANG_ALARM_MOD_KEYS));
94 }
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000095 /* Talk when entering the wakeup screen */
Steve Bavin32a95752007-10-19 15:31:42 +000096 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000097 {
98 talk_value(h, UNIT_HOUR, true);
99 talk_value(m, UNIT_MIN, true);
100 }
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000101 update = false;
102 }
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000103
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000104 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME), h, m);
105 FOR_NB_SCREENS(i)
106 {
Jonathan Gordond0ed3712008-05-29 08:20:07 +0000107 screens[i].set_viewport(&vp[i]);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000108 screens[i].puts(0, 1, buf);
Jonathan Gordond0ed3712008-05-29 08:20:07 +0000109 screens[i].update_viewport();
110 screens[i].set_viewport(NULL);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000111 }
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000112 button = get_action(CONTEXT_SETTINGS,HZ);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000113
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000114 switch(button) {
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000115 case ACTION_STD_OK:
Uwe Freese86352cc2003-01-22 12:50:34 +0000116 /* prevent that an alarm occurs in the shutdown procedure */
117 /* accept alarms only if they are in 2 minutes or more */
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000118 tm = get_time();
119 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000120
Uwe Freese86352cc2003-01-22 12:50:34 +0000121 if (togo > 1) {
Uwe Freese86352cc2003-01-22 12:50:34 +0000122 rtc_init();
123 rtc_set_alarm(h,m);
Christi Scarborougha83ffb22005-02-06 17:21:42 +0000124 rtc_enable_alarm(true);
Steve Bavin32a95752007-10-19 15:31:42 +0000125 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000126 {
127 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
128 talk_value(togo / 60, UNIT_HOUR, true);
129 talk_value(togo % 60, UNIT_MIN, true);
130 talk_force_enqueue_next();
131 }
Jens Arnold4d6374c2007-03-16 21:56:08 +0000132 gui_syncsplash(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000133 togo / 60, togo % 60);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000134 done = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000135 } else {
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000136 gui_syncsplash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000137 update = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000138 }
139 break;
140
141 /* inc(m) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000142 case ACTION_SETTINGS_INC:
143 case ACTION_SETTINGS_INCREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000144 m += 5;
145 if (m == 60) {
146 h += 1;
147 m = 0;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000148 hour_wrapped = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000149 }
150 if (h == 24)
151 h = 0;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000152
153 speak_time(h, m, hour_wrapped);
Uwe Freese86352cc2003-01-22 12:50:34 +0000154 break;
155
156 /* dec(m) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000157 case ACTION_SETTINGS_DEC:
158 case ACTION_SETTINGS_DECREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000159 m -= 5;
160 if (m == -5) {
161 h -= 1;
162 m = 55;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000163 hour_wrapped = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000164 }
165 if (h == -1)
166 h = 23;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000167
168 speak_time(h, m, hour_wrapped);
Uwe Freese86352cc2003-01-22 12:50:34 +0000169 break;
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000170
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000171 /* inc(h) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000172 case ACTION_STD_NEXT:
173 case ACTION_STD_NEXTREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000174 h = (h+1) % 24;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000175
Steve Bavin32a95752007-10-19 15:31:42 +0000176 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000177 talk_value(h, UNIT_HOUR, false);
Uwe Freese86352cc2003-01-22 12:50:34 +0000178 break;
179
180 /* dec(h) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000181 case ACTION_STD_PREV:
Linus Nielsen Feltzingd723e6e2006-08-15 12:49:22 +0000182 case ACTION_STD_PREVREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000183 h = (h+23) % 24;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000184
Steve Bavin32a95752007-10-19 15:31:42 +0000185 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000186 talk_value(h, UNIT_HOUR, false);
Uwe Freese86352cc2003-01-22 12:50:34 +0000187 break;
Uwe Freese86352cc2003-01-22 12:50:34 +0000188
Linus Nielsen Feltzingd723e6e2006-08-15 12:49:22 +0000189 case ACTION_STD_CANCEL:
Christi Scarborougha83ffb22005-02-06 17:21:42 +0000190 rtc_enable_alarm(false);
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000191 gui_syncsplash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
Uwe Freese86352cc2003-01-22 12:50:34 +0000192 done = true;
193 break;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000194
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000195 case ACTION_NONE:
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000196 gui_syncstatusbar_draw(&statusbars, false);
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000197 hour_wrapped = false;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000198 break;
199
200 default:
201 if(default_event_handler(button) == SYS_USB_CONNECTED)
202 {
203 rtc_enable_alarm(false);
204 return true;
205 }
206 break;
Uwe Freese86352cc2003-01-22 12:50:34 +0000207 }
208 }
Uwe Freese86352cc2003-01-22 12:50:34 +0000209 return false;
210}
211
Thom Johansen8fd6d652007-02-28 21:55:11 +0000212#endif /* HAVE_RTC_ALARM */