blob: 497747bd7b5b0356cab148f3fc64b7de2579b315 [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
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000023#include <stdbool.h>
24
Uwe Freese86352cc2003-01-22 12:50:34 +000025#include "lcd.h"
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +000026#include "action.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000027#include "kernel.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000028#include <string.h>
29#include "settings.h"
30#include "power.h"
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000031#include "icons.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000032#include "rtc.h"
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000033#include "misc.h"
34#include "screens.h"
Jonathan Gordond0ed3712008-05-29 08:20:07 +000035#include "talk.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000036#include "lang.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000037#include "alarm_menu.h"
Kevin Ferraree991bee2005-11-16 15:12:15 +000038#include "splash.h"
Jonathan Gordond0ed3712008-05-29 08:20:07 +000039#include "viewport.h"
Uwe Freese86352cc2003-01-22 12:50:34 +000040
Nils Wallméniusf34a8412009-10-17 18:02:48 +000041static void speak_time(int hours, int minutes, bool speak_hours, bool enqueue)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000042{
Steve Bavin32a95752007-10-19 15:31:42 +000043 if (global_settings.talk_menu){
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000044 if(speak_hours) {
Nils Wallméniusf34a8412009-10-17 18:02:48 +000045 talk_value(hours, UNIT_HOUR, enqueue);
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000046 talk_value(minutes, UNIT_MIN, true);
47 } else {
Nils Wallméniusf34a8412009-10-17 18:02:48 +000048 talk_value(minutes, UNIT_MIN, enqueue);
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000049 }
50 }
51}
52
William Wilgusdd40c462018-10-15 23:04:04 -040053int alarm_screen(void)
Uwe Freese86352cc2003-01-22 12:50:34 +000054{
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000055 int h, m;
Nils Wallméniusf34a8412009-10-17 18:02:48 +000056 bool done = false;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000057 struct tm *tm;
58 int togo;
59 int button;
60 bool update = true;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000061 bool hour_wrapped = false;
Jonathan Gordond0ed3712008-05-29 08:20:07 +000062 struct viewport vp[NB_SCREENS];
Uwe Freese5bfa9b72003-03-03 14:23:39 +000063
Uwe Freese86352cc2003-01-22 12:50:34 +000064 rtc_get_alarm(&h, &m);
Uwe Freese5bfa9b72003-03-03 14:23:39 +000065
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000066 /* After a battery change the RTC values are out of range */
67 if (m > 60 || h > 24) {
Uwe Freese5bfa9b72003-03-03 14:23:39 +000068 m = 0;
69 h = 12;
70 } else {
71 m = m / 5 * 5; /* 5 min accuracy should be enough */
72 }
Jonathan Gordond0ed3712008-05-29 08:20:07 +000073 FOR_NB_SCREENS(i)
74 {
75 viewport_set_defaults(&vp[i], i);
76 }
Uwe Freese5bfa9b72003-03-03 14:23:39 +000077
Uwe Freese86352cc2003-01-22 12:50:34 +000078 while(!done) {
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000079 if(update)
80 {
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000081 FOR_NB_SCREENS(i)
82 {
Jonathan Gordond0ed3712008-05-29 08:20:07 +000083 screens[i].set_viewport(&vp[i]);
84 screens[i].clear_viewport();
Rafaël Carré8302c5f2010-05-22 00:28:10 +000085 screens[i].puts(0, 4, str(LANG_ALARM_MOD_KEYS));
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000086 }
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +000087 /* Talk when entering the wakeup screen */
Nils Wallméniusf34a8412009-10-17 18:02:48 +000088 speak_time(h, m, true, true);
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000089 update = false;
90 }
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +000091
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000092 FOR_NB_SCREENS(i)
93 {
Jonathan Gordond0ed3712008-05-29 08:20:07 +000094 screens[i].set_viewport(&vp[i]);
Rafaël Carré8302c5f2010-05-22 00:28:10 +000095 screens[i].putsf(0, 1, str(LANG_ALARM_MOD_TIME));
96 screens[i].putsf(0, 2, "%02d:%02d", h, m);
Jonathan Gordond0ed3712008-05-29 08:20:07 +000097 screens[i].update_viewport();
98 screens[i].set_viewport(NULL);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +000099 }
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000100 button = get_action(CONTEXT_SETTINGS,HZ);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000101
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000102 switch(button) {
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000103 case ACTION_STD_OK:
Uwe Freese86352cc2003-01-22 12:50:34 +0000104 /* prevent that an alarm occurs in the shutdown procedure */
105 /* accept alarms only if they are in 2 minutes or more */
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000106 tm = get_time();
107 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000108
Uwe Freese86352cc2003-01-22 12:50:34 +0000109 if (togo > 1) {
Uwe Freese86352cc2003-01-22 12:50:34 +0000110 rtc_init();
111 rtc_set_alarm(h,m);
Christi Scarborougha83ffb22005-02-06 17:21:42 +0000112 rtc_enable_alarm(true);
Steve Bavin32a95752007-10-19 15:31:42 +0000113 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000114 {
115 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
116 talk_value(togo / 60, UNIT_HOUR, true);
117 talk_value(togo % 60, UNIT_MIN, true);
118 talk_force_enqueue_next();
119 }
Nils Wallménius01729e72008-08-15 08:27:39 +0000120 splashf(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000121 togo / 60, togo % 60);
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000122 done = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000123 } else {
Nils Wallménius01729e72008-08-15 08:27:39 +0000124 splash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000125 update = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000126 }
127 break;
128
129 /* inc(m) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000130 case ACTION_SETTINGS_INC:
131 case ACTION_SETTINGS_INCREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000132 m += 5;
133 if (m == 60) {
134 h += 1;
135 m = 0;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000136 hour_wrapped = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000137 }
138 if (h == 24)
139 h = 0;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000140
Nils Wallméniusf34a8412009-10-17 18:02:48 +0000141 speak_time(h, m, hour_wrapped, false);
Uwe Freese86352cc2003-01-22 12:50:34 +0000142 break;
143
144 /* dec(m) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000145 case ACTION_SETTINGS_DEC:
146 case ACTION_SETTINGS_DECREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000147 m -= 5;
148 if (m == -5) {
149 h -= 1;
150 m = 55;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000151 hour_wrapped = true;
Uwe Freese86352cc2003-01-22 12:50:34 +0000152 }
153 if (h == -1)
154 h = 23;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000155
Nils Wallméniusf34a8412009-10-17 18:02:48 +0000156 speak_time(h, m, hour_wrapped, false);
Uwe Freese86352cc2003-01-22 12:50:34 +0000157 break;
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000158
Kevin Ferrare1a1abf22005-11-20 01:02:14 +0000159 /* inc(h) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000160 case ACTION_STD_NEXT:
161 case ACTION_STD_NEXTREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000162 h = (h+1) % 24;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000163
Steve Bavin32a95752007-10-19 15:31:42 +0000164 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000165 talk_value(h, UNIT_HOUR, false);
Uwe Freese86352cc2003-01-22 12:50:34 +0000166 break;
167
168 /* dec(h) */
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000169 case ACTION_STD_PREV:
Linus Nielsen Feltzingd723e6e2006-08-15 12:49:22 +0000170 case ACTION_STD_PREVREPEAT:
Uwe Freese86352cc2003-01-22 12:50:34 +0000171 h = (h+23) % 24;
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000172
Steve Bavin32a95752007-10-19 15:31:42 +0000173 if (global_settings.talk_menu)
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000174 talk_value(h, UNIT_HOUR, false);
Uwe Freese86352cc2003-01-22 12:50:34 +0000175 break;
Uwe Freese86352cc2003-01-22 12:50:34 +0000176
Linus Nielsen Feltzingd723e6e2006-08-15 12:49:22 +0000177 case ACTION_STD_CANCEL:
Christi Scarborougha83ffb22005-02-06 17:21:42 +0000178 rtc_enable_alarm(false);
Nils Wallménius01729e72008-08-15 08:27:39 +0000179 splash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
Uwe Freese86352cc2003-01-22 12:50:34 +0000180 done = true;
181 break;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000182
Linus Nielsen Feltzing224c0a12006-08-15 12:27:07 +0000183 case ACTION_NONE:
Linus Nielsen Feltzingb0a37362007-10-04 08:56:49 +0000184 hour_wrapped = false;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000185 break;
186
187 default:
188 if(default_event_handler(button) == SYS_USB_CONNECTED)
189 {
190 rtc_enable_alarm(false);
William Wilgusdd40c462018-10-15 23:04:04 -0400191 return 1;
Linus Nielsen Feltzing50d9e3f2005-07-06 08:24:24 +0000192 }
193 break;
Uwe Freese86352cc2003-01-22 12:50:34 +0000194 }
195 }
William Wilgusdd40c462018-10-15 23:04:04 -0400196 return 0;
Uwe Freese86352cc2003-01-22 12:50:34 +0000197}