summaryrefslogtreecommitdiff
path: root/apps/sleeptimer.c
blob: a236ed9ba7b731909166607c0ab281e9cc4e987f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "options.h"

#include "lcd.h"
#include "font.h"
#include "button.h"
#include "kernel.h"
#include "sprintf.h"
#include <string.h>
#include "settings.h"
#include "power.h"
#include "powermgmt.h"
#include "status.h"
#include "debug.h"
#include "talk.h"

#include "lang.h"

#define SMALL_STEP_SIZE 15*60    /* Seconds */
#define LARGE_STEP_SIZE 30*60    /* Seconds */
#define THRESHOLD       60       /* Minutes */
#define MAX_TIME        5*60*60  /* Hours */

bool sleeptimer_screen(void)
{
#ifdef HAVE_LCD_BITMAP
    int w, h;
#endif
    unsigned long seconds;
    int hours, minutes;
    int button;
    bool done = false;
    char buf[32];
    int oldtime, newtime;
    int amount = 0;
    int org_timer=get_sleep_timer();
    bool changed=false;
    bool sayit = true;

#ifdef HAVE_LCD_BITMAP
    lcd_setfont(FONT_UI);
    lcd_getstringsize("M", &w, &h);
    lcd_setmargins(w, 8);
#endif
    
    lcd_clear_display();
    lcd_puts_scroll(0, 0, str(LANG_SLEEP_TIMER));

    while(!done)
    {
        button = button_get_w_tmo(HZ);
        switch(button)
        {
#ifdef HAVE_RECORDER_KEYPAD
        case BUTTON_LEFT:
        case BUTTON_PLAY:
#else
        case BUTTON_PLAY:
#endif
          done = true;
          break;

#ifdef HAVE_RECORDER_KEYPAD
        case BUTTON_OFF:
#else
        case BUTTON_STOP:
        case BUTTON_MENU:
#endif
          if (changed) {
            lcd_stop_scroll();
            lcd_puts(0, 0, str(LANG_MENU_SETTING_CANCEL));
            lcd_update();
            set_sleep_timer(org_timer);
            sleep(HZ/2);
          }
          done = true;
          break;

#if defined(HAVE_PLAYER_KEYPAD) || defined(HAVE_NEO_KEYPAD)
        case BUTTON_RIGHT:
#else
        case BUTTON_UP:
#endif
            oldtime = (get_sleep_timer()+59) / 60;
            if(oldtime < THRESHOLD)
                amount = SMALL_STEP_SIZE;
            else
                amount = LARGE_STEP_SIZE;
            
            newtime = oldtime * 60 + amount;
            if(newtime > MAX_TIME)
                newtime = MAX_TIME;

            changed = sayit = true;
            set_sleep_timer(newtime);
            break;
                
#ifdef HAVE_PLAYER_KEYPAD
        case BUTTON_LEFT:
#else
        case BUTTON_DOWN:
#endif
            oldtime = (get_sleep_timer()+59) / 60;
            if(oldtime <= THRESHOLD)
                amount = SMALL_STEP_SIZE;
            else
                amount = LARGE_STEP_SIZE;
            
            newtime = oldtime*60 - amount;
            if(newtime < 0)
                newtime = 0;
            
            changed = sayit = true;
            set_sleep_timer(newtime);
            break;
        }

        seconds = get_sleep_timer();

        if(seconds)
        {
            seconds += 59; /* Round up for a "friendlier" display */
            hours = seconds / 3600;
            minutes = (seconds - (hours * 3600)) / 60;
            snprintf(buf, 32, "%d:%02d",
                     hours, minutes);
            lcd_puts(0, 1, buf);

            if (sayit && global_settings.talk_menu)
            {
                bool enqueue = false; /* first one should not ne queued */

                if (hours)
                {   
                    talk_value(hours, UNIT_HOUR, enqueue);
                    enqueue = true; /* queue any following */
                }
                if (minutes)
                {
                    talk_value(minutes, UNIT_MIN, enqueue);
                }

                sayit = false;
            }
        }
        else
        {
            lcd_puts(0, 1, str(LANG_OFF));
            if (sayit)
            {
                talk_id(LANG_OFF, false);
                sayit = false;
            }
        }

        status_draw(true);

        lcd_update();
    }
    lcd_stop_scroll();
    return false;
}