summaryrefslogtreecommitdiff
path: root/apps/alarm_menu.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/alarm_menu.c')
-rw-r--r--apps/alarm_menu.c161
1 files changed, 28 insertions, 133 deletions
diff --git a/apps/alarm_menu.c b/apps/alarm_menu.c
index 62b54a84bb..67f8d1e8dd 100644
--- a/apps/alarm_menu.c
+++ b/apps/alarm_menu.c
@@ -38,161 +38,56 @@
38#include "splash.h" 38#include "splash.h"
39#include "viewport.h" 39#include "viewport.h"
40 40
41static void speak_time(int hours, int minutes, bool speak_hours, bool enqueue)
42{
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);
49 }
50 }
51}
52
53int alarm_screen(void) 41int alarm_screen(void)
54{ 42{
55 int h, m; 43 bool usb, update;
56 bool done = false; 44 struct tm *now = get_time();
57 struct tm *tm; 45 struct tm atm;
58 int togo; 46 memcpy(&atm, now, sizeof(struct tm));
59 int button; 47 rtc_get_alarm(&atm.tm_hour, &atm.tm_min);
60 bool update = true;
61 bool hour_wrapped = false;
62 struct viewport vp[NB_SCREENS];
63 struct viewport * last_vp;
64
65 rtc_get_alarm(&h, &m);
66 48
67 /* After a battery change the RTC values are out of range */ 49 /* After a battery change the RTC values are out of range */
68 if (m > 60 || h > 24) { 50 if (!valid_time(&atm))
69 m = 0; 51 memcpy(&atm, now, sizeof(struct tm));
70 h = 12; 52 atm.tm_sec = 0;
71 } else {
72 m = m / 5 * 5; /* 5 min accuracy should be enough */
73 }
74 FOR_NB_SCREENS(i)
75 {
76 viewport_set_defaults(&vp[i], i);
77 }
78 53
79 while(!done) { 54 usb = set_time_screen(str(LANG_ALARM_MOD_TIME), &atm, false);
80 if(update) 55 update = valid_time(&atm); /* set_time returns invalid time if canceled */
81 {
82 FOR_NB_SCREENS(i)
83 {
84 screens[i].set_viewport(&vp[i]);
85 screens[i].clear_viewport();
86 screens[i].puts(0, 4, str(LANG_ALARM_MOD_KEYS));
87 }
88 /* Talk when entering the wakeup screen */
89 speak_time(h, m, true, true);
90 update = false;
91 }
92 56
93 FOR_NB_SCREENS(i) 57 if (!usb && update)
94 { 58 {
95 last_vp = 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(last_vp);
100 }
101 button = get_action(CONTEXT_SETTINGS,HZ);
102 59
103 switch(button) { 60 now = get_time();
104 case ACTION_STD_OK: 61 int nmins = now->tm_min + (now->tm_hour * 60);
62 int amins = atm.tm_min + (atm.tm_hour * 60);
63 int mins_togo = (amins - nmins + 1440) % 1440;
105 /* prevent that an alarm occurs in the shutdown procedure */ 64 /* prevent that an alarm occurs in the shutdown procedure */
106 /* accept alarms only if they are in 2 minutes or more */ 65 /* accept alarms only if they are in 2 minutes or more */
107 tm = get_time(); 66 if (mins_togo > 1) {
108 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
109
110 if (togo > 1) {
111 rtc_init(); 67 rtc_init();
112 rtc_set_alarm(h,m); 68 rtc_set_alarm(atm.tm_hour,atm.tm_min);
113 rtc_enable_alarm(true); 69 rtc_enable_alarm(true);
114 if (global_settings.talk_menu) 70 if (global_settings.talk_menu)
115 { 71 {
116 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true); 72 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
117 talk_value(togo / 60, UNIT_HOUR, true); 73 talk_value(mins_togo / 60, UNIT_HOUR, true);
118 talk_value(togo % 60, UNIT_MIN, true); 74 talk_value(mins_togo % 60, UNIT_MIN, true);
119 talk_force_enqueue_next(); 75 talk_force_enqueue_next();
120 } 76 }
121 splashf(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO), 77 splashf(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
122 togo / 60, togo % 60); 78 mins_togo / 60, mins_togo % 60);
123 done = true;
124 } else { 79 } else {
125 splash(HZ, ID2P(LANG_ALARM_MOD_ERROR)); 80 splash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
126 update = true; 81 update = false;
127 }
128 break;
129
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;
138 } 82 }
139 if (h == 24) 83 }
140 h = 0;
141
142 speak_time(h, m, hour_wrapped, false);
143 break;
144
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;
153 }
154 if (h == -1)
155 h = 23;
156
157 speak_time(h, m, hour_wrapped, false);
158 break;
159
160 /* inc(h) */
161 case ACTION_STD_NEXT:
162 case ACTION_STD_NEXTREPEAT:
163 h = (h+1) % 24;
164
165 if (global_settings.talk_menu)
166 talk_value(h, UNIT_HOUR, false);
167 break;
168
169 /* dec(h) */
170 case ACTION_STD_PREV:
171 case ACTION_STD_PREVREPEAT:
172 h = (h+23) % 24;
173
174 if (global_settings.talk_menu)
175 talk_value(h, UNIT_HOUR, false);
176 break;
177 84
178 case ACTION_STD_CANCEL: 85 if (usb || !update)
179 rtc_enable_alarm(false); 86 {
87 if (!usb)
180 splash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE)); 88 splash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
181 done = true; 89 rtc_enable_alarm(false);
182 break; 90 return 1;
183
184 case ACTION_NONE:
185 hour_wrapped = false;
186 break;
187
188 default:
189 if(default_event_handler(button) == SYS_USB_CONNECTED)
190 {
191 rtc_enable_alarm(false);
192 return 1;
193 }
194 break;
195 }
196 } 91 }
197 return 0; 92 return 0;
198} 93}