summaryrefslogtreecommitdiff
path: root/apps/status.c
blob: 03badc5b75c5f112c15b835aa8a7d3850409cdf8 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by 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 "string.h"
#include "lcd.h"
#include "debug.h"
#include "kernel.h"
#include "power.h"
#include "thread.h"
#include "settings.h"
#include "status.h"
#include "mp3_playback.h"
#include "mpeg.h"
#include "wps.h"
#ifdef HAVE_RTC
#include "timefuncs.h"
#endif
#ifdef HAVE_LCD_BITMAP
#include "icons.h"
#include "font.h"
#endif
#include "powermgmt.h"

static enum playmode ff_mode;

static long switch_tick;
static int  battery_charge_step = 0;
static bool plug_state;
static bool battery_state = true;

struct status_info {
    int battlevel;
    int volume;
    int hour;
    int minute;
    int playmode;
    int repeat;
    bool inserted;
    bool shuffle;
    bool keylock;
    bool battery_safe;
    bool redraw_volume; /* true if the volume gauge needs updating */
};

void status_init(void)
{
    ff_mode = 0;
}

void status_set_ffmode(enum playmode mode)
{
    ff_mode = mode; /* Either STATUS_FASTFORWARD or STATUS_FASTBACKWARD */
    status_draw(false);
}

int current_playmode(void)
{
    int mpeg_stat = mpeg_status();

    /* ff_mode can be either STATUS_FASTFORWARD or STATUS_FASTBACKWARD
       and that supercedes the other modes */
    if(ff_mode)
        return ff_mode;
    
    if(mpeg_stat & MPEG_STATUS_PLAY)
    {
        if(mpeg_stat & MPEG_STATUS_PAUSE)
            return STATUS_PAUSE;
        else
            return STATUS_PLAY;
    }
#if CONFIG_HWCODEC == MAS3587F
    else
    {
        if(mpeg_stat & MPEG_STATUS_RECORD)
        {
            if(mpeg_stat & MPEG_STATUS_PAUSE)
                return STATUS_RECORD_PAUSE;
            else
                return STATUS_RECORD;
        }
    }
#endif
    
    return STATUS_STOP;
}

#if defined(HAVE_LCD_CHARCELLS)
static bool record = false;
static bool audio = false;
static bool param = false;
static bool usb = false;

void status_set_record(bool b)
{
    record = b;
}

void status_set_audio(bool b)
{
    audio = b;
}

void status_set_param(bool b)
{
    param = b;
}

void status_set_usb(bool b)
{
    usb = b;
}

#endif /* HAVE_LCD_CHARCELLS */

void status_draw(bool force_redraw)
{
    struct status_info info;

#ifdef HAVE_LCD_BITMAP
    static struct status_info lastinfo;
    struct tm* tm;

    if ( !global_settings.statusbar )
        return;
#else
    (void)force_redraw; /* players always "redraw" */
#endif

    info.volume = mpeg_val2phys(SOUND_VOLUME, global_settings.volume);
    info.inserted = charger_inserted();
    info.battlevel = battery_level();
    info.battery_safe = battery_level_safe();

#ifdef HAVE_LCD_BITMAP
    tm = get_time();
    info.hour = tm->tm_hour;
    info.minute = tm->tm_min;
    info.shuffle = global_settings.playlist_shuffle;
    info.keylock = keys_locked;
    info.repeat = global_settings.repeat_mode;
    info.playmode = current_playmode();

    /* only redraw if forced to, or info has changed */
    if (force_redraw ||
        info.inserted ||
        !info.battery_safe ||
        info.redraw_volume ||
        memcmp(&info, &lastinfo, sizeof(struct status_info)))
    {
        lcd_clearrect(0,0,LCD_WIDTH,8);
#else
    /* players always "redraw" */
    {
#endif
        
        if (info.inserted) {
            battery_state = true;
            plug_state = true;
#if defined(HAVE_CHARGE_CTRL) || CONFIG_BATTERY == BATT_LIION2200
            /* zero battery run time if charging */
            if (charge_state > 0) {
                global_settings.runtime = 0;
                lasttime = current_tick;
            }
            
            /* animate battery if charging */
            if ((charge_state == 1) ||
                (charge_state == 2)) {
#else
            global_settings.runtime = 0;
            lasttime = current_tick;
            {
#endif
                /* animate in three steps (34% per step for a better look) */
                info.battlevel = battery_charge_step * 34;
                if (info.battlevel > 100)
                    info.battlevel = 100;
                if(TIME_AFTER(current_tick, switch_tick)) {
                    battery_charge_step=(battery_charge_step+1)%4;
                    switch_tick = current_tick + HZ;
                }
            }
        }
        else {
            plug_state=false;
            if (info.battery_safe)
                battery_state = true;
            else {
                /* blink battery if level is low */
                if(TIME_AFTER(current_tick, switch_tick) &&
                   (info.battlevel > -1)) {
                    switch_tick = current_tick+HZ;
                    battery_state =! battery_state;
                }
            }
        }

#ifdef HAVE_LCD_BITMAP
        if (battery_state)
            statusbar_icon_battery(info.battlevel, plug_state);
            
        info.redraw_volume = statusbar_icon_volume(info.volume);
        statusbar_icon_play_state(current_playmode() + Icon_Play);
        switch (info.repeat) {
            case REPEAT_ONE:
                statusbar_icon_play_mode(Icon_RepeatOne);
                break;

            case REPEAT_ALL:
                statusbar_icon_play_mode(Icon_Repeat);
                break;
        }
        if (info.shuffle)
            statusbar_icon_shuffle();
        if (info.keylock)
            statusbar_icon_lock();
#ifdef HAVE_RTC
        statusbar_time(info.hour, info.minute);
#endif
        lcd_update_rect(0, 0, LCD_WIDTH, STATUSBAR_HEIGHT);
        lastinfo = info;
#endif
    }


#if defined(HAVE_LCD_CHARCELLS)
    if (info.battlevel > -1)
    lcd_icon(ICON_BATTERY, battery_state);
    lcd_icon(ICON_BATTERY_1, info.battlevel > 25);
    lcd_icon(ICON_BATTERY_2, info.battlevel > 50);
    lcd_icon(ICON_BATTERY_3, info.battlevel > 75);

    lcd_icon(ICON_VOLUME, true);
    lcd_icon(ICON_VOLUME_1, info.volume > 10);
    lcd_icon(ICON_VOLUME_2, info.volume > 30);
    lcd_icon(ICON_VOLUME_3, info.volume > 50);
    lcd_icon(ICON_VOLUME_4, info.volume > 70);
    lcd_icon(ICON_VOLUME_5, info.volume > 90);

    lcd_icon(ICON_PLAY, current_playmode() == STATUS_PLAY);
    lcd_icon(ICON_PAUSE, current_playmode() == STATUS_PAUSE);

    lcd_icon(ICON_REPEAT, global_settings.repeat_mode != REPEAT_OFF);
    lcd_icon(ICON_1, global_settings.repeat_mode == REPEAT_ONE);
    
    lcd_icon(ICON_RECORD, record);
    lcd_icon(ICON_AUDIO, audio);
    lcd_icon(ICON_PARAM, param);
    lcd_icon(ICON_USB, usb);
#endif

}

#if defined(HAVE_LCD_BITMAP) && (CONFIG_KEYPAD == RECORDER_PAD)
static void draw_buttonbar_btn(int num, const char* caption)
{
    int xpos, ypos, button_width, text_width;
    int fw, fh;

    lcd_setfont(FONT_SYSFIXED);
    lcd_getstringsize("M", &fw, &fh);

    button_width = LCD_WIDTH/3;
    xpos = num * button_width;
    ypos = LCD_HEIGHT - fh;
    
    if(caption)
    {
        /* center the text */
        text_width = fw * strlen(caption);
        lcd_putsxy(xpos + (button_width - text_width)/2, ypos, caption);
    }
    
    lcd_invertrect(xpos, ypos, button_width - 1, fh);
}

static char stored_caption1[8];
static char stored_caption2[8];
static char stored_caption3[8];

void buttonbar_set(const char* caption1, const char *caption2,
                   const char *caption3)
{
    buttonbar_unset();
    if(caption1)
    {
        strncpy(stored_caption1, caption1, 7);
        stored_caption1[7] = 0;
    }
    if(caption2)
    {
        strncpy(stored_caption2, caption2, 7);
        stored_caption2[7] = 0;
    }
    if(caption3)
    {
        strncpy(stored_caption3, caption3, 7);
        stored_caption3[7] = 0;
    }
}

void buttonbar_unset(void)
{
    stored_caption1[0] = 0;
    stored_caption2[0] = 0;
    stored_caption3[0] = 0;
}

void buttonbar_draw(void)
{
    lcd_clearrect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
    draw_buttonbar_btn(0, stored_caption1);
    draw_buttonbar_btn(1, stored_caption2);
    draw_buttonbar_btn(2, stored_caption3);
}

bool buttonbar_isset(void)
{
    /* If all buttons are unset, the button bar is considered disabled */
    return (global_settings.buttonbar &&
            ((stored_caption1[0] != 0) ||
             (stored_caption2[0] != 0) ||
             (stored_caption3[0] != 0)));
}

#endif