summaryrefslogtreecommitdiffstats
path: root/apps/plugins/battery_bench.c
blob: 44e7ad7bec85e174cf8af4fe474d11efae8677b3 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 *
 * Copyright (C) 2006 Alexander Spyridakis, Hristo Kovachev
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef SIMULATOR /* not for the simulator */

#include "plugin.h"
PLUGIN_HEADER

#define BATTERY_LOG "/battery_bench.txt"
#define BUF_SIZE 16000

#define EV_EXIT 1337

/* seems to work with 1300, but who knows... */ 
#define THREAD_STACK_SIZE DEFAULT_STACK_SIZE + 0x200

#if CONFIG_KEYPAD == RECORDER_PAD

#define BATTERY_ON BUTTON_PLAY
#define BATTERY_OFF BUTTON_OFF
#define BATTERY_ON_TXT  "PLAY - start"
#define BATTERY_OFF_TXT "OFF  - quit"

#if BUTTON_REMOTE != 0
#define BATTERY_RC_ON BUTTON_RC_PLAY
#define BATTERY_RC_OFF BUTTON_RC_STOP
#endif

#elif CONFIG_KEYPAD == ONDIO_PAD

#define BATTERY_ON BUTTON_RIGHT
#define BATTERY_OFF BUTTON_OFF
#define BATTERY_ON_TXT  "RIGHT - start"
#define BATTERY_OFF_TXT "OFF   - quit"

#elif CONFIG_KEYPAD == PLAYER_PAD

#define BATTERY_ON BUTTON_PLAY
#define BATTERY_OFF BUTTON_STOP

#define BATTERY_RC_ON BUTTON_RC_PLAY
#define BATTERY_RC_OFF BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
      
#define BATTERY_ON BUTTON_ON
#define BATTERY_RC_ON BUTTON_RC_ON

#define BATTERY_OFF BUTTON_OFF
#define BATTERY_RC_OFF BUTTON_RC_STOP

#define BATTERY_ON_TXT  "PLAY - start"
#define BATTERY_OFF_TXT "STOP - quit"

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)

#define BATTERY_ON  BUTTON_PLAY
#define BATTERY_OFF BUTTON_MENU
#define BATTERY_ON_TXT  "PLAY - start"
#define BATTERY_OFF_TXT "MENU - quit"

#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD

#define BATTERY_ON  BUTTON_SELECT
#define BATTERY_OFF BUTTON_POWER
#define BATTERY_ON_TXT  "SELECT - start"
#define BATTERY_OFF_TXT "POWER  - quit"

#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD

#define BATTERY_ON  BUTTON_SELECT
#define BATTERY_OFF BUTTON_PLAY
#define BATTERY_ON_TXT  "SELECT - start"
#define BATTERY_OFF_TXT "PLAY   - quit"

#elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
(CONFIG_KEYPAD == SANSA_C200_PAD)
#define BATTERY_ON BUTTON_SELECT
#define BATTERY_OFF BUTTON_POWER
#define BATTERY_ON_TXT  "SELECT - start"
#define BATTERY_OFF_TXT "POWER  - quit"

#elif CONFIG_KEYPAD == IRIVER_H10_PAD

#define BATTERY_ON  BUTTON_PLAY
#define BATTERY_OFF BUTTON_POWER
#define BATTERY_ON_TXT  "PLAY  - start"
#define BATTERY_OFF_TXT "POWER - quit"

#elif CONFIG_KEYPAD == GIGABEAT_PAD

#define BATTERY_ON  BUTTON_SELECT
#define BATTERY_OFF BUTTON_POWER
#define BATTERY_ON_TXT  "SELECT - start"
#define BATTERY_OFF_TXT "POWER  - quit"

#elif CONFIG_KEYPAD == GIGABEAT_S_PAD

#define BATTERY_ON  BUTTON_SELECT
#define BATTERY_OFF BUTTON_BACK
#define BATTERY_ON_TXT  "SELECT - start"
#define BATTERY_OFF_TXT "BACK  - quit"

#elif CONFIG_KEYPAD == MROBE500_PAD

#define BATTERY_ON  BUTTON_RC_PLAY
#define BATTERY_OFF BUTTON_POWER
#define BATTERY_ON_TXT  "PLAY - start"
#define BATTERY_OFF_TXT "POWER  - quit"

#elif CONFIG_KEYPAD == MROBE100_PAD

#define BATTERY_ON  BUTTON_SELECT
#define BATTERY_OFF BUTTON_POWER
#define BATTERY_ON_TXT  "SELECT - start"
#define BATTERY_OFF_TXT "POWER  - quit"

#elif CONFIG_KEYPAD == IAUDIO_M3_PAD

#define BATTERY_ON  BUTTON_PLAY
#define BATTERY_OFF BUTTON_REC
#define BATTERY_RC_ON  BUTTON_RC_PLAY
#define BATTERY_RC_OFF BUTTON_RC_REC
#define BATTERY_ON_TXT  "PLAY - start"
#define BATTERY_OFF_TXT "REC  - quit"

#elif CONFIG_KEYPAD == COWOND2_PAD

#define BATTERY_OFF BUTTON_POWER
#define BATTERY_OFF_TXT "POWER  - quit"

#elif CONFIG_KEYPAD == IAUDIO67_PAD

#define BATTERY_OFF BUTTON_POWER
#define BATTERY_OFF_TXT "POWER  - quit"
#define BATTERY_ON BUTTON_PLAY
#define BATTERY_ON_TXT  "PLAY - start"
#else
#error No keymap defined!
#endif

#ifdef HAVE_TOUCHSCREEN
#ifndef BATTERY_ON
#define BATTERY_ON       BUTTON_CENTER
#endif
#ifndef BATTERY_OFF
#define BATTERY_OFF      BUTTON_TOPLEFT
#endif
#ifndef BATTERY_ON_TXT
#define BATTERY_ON_TXT  "CENTRE - start"
#endif
#ifndef BATTERY_OFF_TXT
#define BATTERY_OFF_TXT "TOPLEFT  - quit"
#endif
#endif

/****************************** Plugin Entry Point ****************************/
static const struct plugin_api* rb;
MEM_FUNCTION_WRAPPERS(rb);
int main(void);
bool exit_tsr(bool);
void thread(void);


enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
{
    (void)parameter;
    rb = api;
    
    return main();
}

/* Struct for battery information */
struct batt_info
{
    /* the size of the struct elements is optimised to make the struct size
     * a power of 2 */
    long ticks;
    int eta;
    unsigned int voltage;
    short level;
    unsigned short flags;
} bat[BUF_SIZE/sizeof(struct batt_info)];

#define BUF_ELEMENTS    (sizeof(bat)/sizeof(struct batt_info))

static struct thread_entry *thread_id;
static struct event_queue thread_q;
static bool in_usb_mode;
static unsigned int buf_idx;

bool exit_tsr(bool reenter)
{
    (void)reenter;
    rb->lcd_clear_display();
    rb->lcd_puts_scroll(0, 0, "Batt.Bench is currently running.");
    rb->lcd_puts_scroll(0, 1, "Press OFF to cancel the test");
#ifdef HAVE_LCD_BITMAP
    rb->lcd_puts_scroll(0, 2, "Anything else will resume");
#endif
    rb->lcd_update();

    if (rb->button_get(true) == BATTERY_OFF)
    {
        rb->queue_post(&thread_q, EV_EXIT, 0);
        rb->thread_wait(thread_id);
        /* remove the thread's queue from the broadcast list */
        rb->queue_delete(&thread_q);
        return true;
    }
    else return false;
}

#define BIT_CHARGER     0x1
#define BIT_CHARGING    0x2
#define BIT_USB_POWER   0x4

#define HMS(x) (x)/3600,((x)%3600)/60,((x)%3600)%60 

/* use long for aligning */
unsigned long thread_stack[THREAD_STACK_SIZE/sizeof(long)];

#if CONFIG_CHARGING || defined(HAVE_USB_POWER)
static unsigned int charge_state(void)
{
    unsigned int ret = 0;
#if CONFIG_CHARGING
    if (rb->charger_inserted())
        ret = BIT_CHARGER;
#if CONFIG_CHARGING == CHARGING_MONITOR
    if (rb->charging_state())
        ret |= BIT_CHARGING;
#endif
#endif
#ifdef HAVE_USB_POWER
    if (rb->usb_powered())
        ret |= BIT_USB_POWER;
#endif
    return ret; 
}
#endif


static bool flush_buffer(void)
{
    int fd, secs;
    unsigned int i;

    /* don't access the disk when in usb mode, or when no data is available */
    if (in_usb_mode || (buf_idx == 0))
    {
        return false;
    }

    fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT | O_APPEND);
    if (fd < 0)
    {
        return false;
    }

    for (i = 0; i < buf_idx; i++)
    {
        secs = bat[i].ticks/HZ;
        rb->fdprintf(fd,
                "%02d:%02d:%02d,  %05d,     %03d%%,     "
                "%02d:%02d,         %04d,   "
#if CONFIG_CHARGING
                "  %c"
#if CONFIG_CHARGING == CHARGING_MONITOR
                ",  %c"
#endif
#endif
#ifdef HAVE_USB_POWER
                ",  %c"
#endif
                "\n",

                HMS(secs), secs, bat[i].level,
                bat[i].eta / 60, bat[i].eta % 60,
                bat[i].voltage
#if CONFIG_CHARGING
                , (bat[i].flags & BIT_CHARGER) ? 'A' : '-'
#if CONFIG_CHARGING == CHARGING_MONITOR
                , (bat[i].flags & BIT_CHARGING) ? 'C' : '-'
#endif
#endif
#ifdef HAVE_USB_POWER
                , (bat[i].flags & BIT_USB_POWER) ? 'U' : '-'
#endif
        );
    }
    rb->close(fd);

    buf_idx = 0;
    return true;
}


void thread(void)
{
    bool exit = false;
    char *exit_reason = "unknown";
    long sleep_time = 60 * HZ;
    struct queue_event ev;
    int fd;

    in_usb_mode = false;
    buf_idx = 0;

    while (!exit)
    {
        /* add data to buffer */
        if (buf_idx < BUF_ELEMENTS)
        {
            bat[buf_idx].ticks = *rb->current_tick;
            bat[buf_idx].level = rb->battery_level();
            bat[buf_idx].eta = rb->battery_time();
            bat[buf_idx].voltage = rb->battery_voltage();
#if CONFIG_CHARGING || defined(HAVE_USB_POWER)
            bat[buf_idx].flags = charge_state();
#endif
            buf_idx++;
            rb->register_ata_idle_func(flush_buffer);
        }
        
        /* What to do when the measurement buffer is full:
           1) save our measurements to disk but waste some power doing so?
           2) throw away measurements to save some power?
           The choice made here is to save the measurements. It is quite unusual
           for this to occur because it requires > 16 hours of no disk activity.
         */
        if (buf_idx == BUF_ELEMENTS) {
            flush_buffer();
        }
        
        /* sleep some time until next measurement */
        rb->queue_wait_w_tmo(&thread_q, &ev, sleep_time);
        switch (ev.id)
        {
            case SYS_USB_CONNECTED: 
                in_usb_mode = true;
                rb->usb_acknowledge(SYS_USB_CONNECTED_ACK);
                break;
            case SYS_USB_DISCONNECTED:
                in_usb_mode = false;
                rb->usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
                break;
            case SYS_POWEROFF:
                exit_reason = "power off";
                exit = true;
                break;
            case EV_EXIT:
#ifdef HAVE_LCD_BITMAP
                rb->splash(HZ, "Exiting battery_bench...");
#else
                rb->splash(HZ, "bench exit");
#endif
                exit_reason = "plugin exit";
                exit = true;
                break;
        }
    }

    /* unregister flush callback and flush to disk */
    rb->unregister_ata_idle_func(flush_buffer, true);
    
    /* log end of bench and exit reason */
    fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT | O_APPEND);
    if (fd >= 0)
    {
        rb->fdprintf(fd, "--Battery bench ended, reason: %s--\n", exit_reason);
        rb->close(fd);
    }
}


#ifdef HAVE_LCD_BITMAP
typedef void (*plcdfunc)(int x, int y, const unsigned char *str);

static void put_centered_str(const char* str, plcdfunc putsxy, int lcd_width, int line)
{
    int strwdt, strhgt;
    rb->lcd_getstringsize(str, &strwdt, &strhgt);
    putsxy((lcd_width - strwdt)/2, line*(strhgt), str);
}
#endif

int main(void)
{
    int button, fd;
    bool on = false;
#ifdef HAVE_LCD_BITMAP
    int i;
    const char *msgs[] = { "Battery Benchmark","Check file", BATTERY_LOG,
                           "for more info", BATTERY_ON_TXT, BATTERY_OFF_TXT };
#endif    
    rb->lcd_clear_display();

#ifdef HAVE_LCD_BITMAP
    rb->lcd_clear_display();
    rb->lcd_setfont(FONT_SYSFIXED);

    for (i = 0; i<(int)(sizeof(msgs)/sizeof(char *)); i++)
        put_centered_str(msgs[i],rb->lcd_putsxy,LCD_WIDTH,i+1);
#else
    rb->lcd_puts_scroll(0, 0, "Batt.Bench.");
    rb->lcd_puts_scroll(0, 1, "PLAY/STOP");
#endif
    rb->lcd_update();
    
#ifdef HAVE_REMOTE_LCD
    rb->lcd_remote_clear_display();
    put_centered_str(msgs[0],rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,0);
    put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-2],
                    rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,1);
    put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-1],
                    rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,2);
    rb->lcd_remote_update();
#endif
    
    do
    {
        button = rb->button_get(true);
        switch (button)
        {
            case BATTERY_ON:
#ifdef BATTERY_RC_ON
            case BATTERY_RC_ON:
#endif                         
                on = true;
                break;        
            case BATTERY_OFF: 
#ifdef BATTERY_RC_OFF
            case BATTERY_RC_OFF:
#endif
                return PLUGIN_OK;

            default:
                if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
        }
    }while(!on);
    
    fd = rb->open(BATTERY_LOG, O_RDONLY);
    if (fd < 0)
    {
        fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT);
        if (fd >= 0)
        {
            rb->fdprintf(fd,
                "This plugin will log your battery performance in a\n"
                "file (%s) every minute.\n"
                "To properly test your battery:\n"
                "1) Select and playback an album. "
                "(Be sure to be more than the player's buffer)\n"
                "2) Set to repeat.\n"
                "3) Let the player run completely out of battery.\n"
                "4) Recharge and copy (or whatever you want) the txt file to "
                "your computer.\n"
                "Now you can make graphs with the data of the battery log.\n"
                "Do not enter another plugin during the test or else the "
                "logging activity will end.\n\n"
                "P.S: You can decide how you will make your tests.\n"
                "Just don't open another plugin to be sure that your log "
                "will continue.\n\n"
                "Battery type: %d mAh      Buffer Entries: %d\n"
                "  Time:,  Seconds:,  Level:,  Time Left:,  Voltage[mV]:"
#if CONFIG_CHARGING
                ", C:"
#endif
#if CONFIG_CHARGING == CHARGING_MONITOR
                ", S:"
#endif
#ifdef HAVE_USB_POWER
                ", U:"
#endif
                "\n"
                ,BATTERY_LOG,rb->global_settings->battery_capacity,
                (int)BUF_ELEMENTS);
            rb->close(fd);
        }
        else
        {
            rb->splash(HZ / 2, "Cannot create file!");
            return PLUGIN_ERROR;
        }
    }
    else
    {
        rb->close(fd);
        fd = rb->open(BATTERY_LOG, O_RDWR | O_APPEND);
        rb->fdprintf(fd, "\n--File already present. Resuming Benchmark--\n");
        rb->close(fd);
    }
    
    rb->queue_init(&thread_q, true); /* put the thread's queue in the bcast list */
    if ((thread_id = rb->create_thread(thread, thread_stack,
        sizeof(thread_stack), 0, "Battery Benchmark" 
        IF_PRIO(, PRIORITY_BACKGROUND)
        IF_COP(, CPU))) == NULL)
    {
        rb->splash(HZ, "Cannot create thread!");
        return PLUGIN_ERROR;
    }
            
    rb->plugin_tsr(exit_tsr);
    
    return PLUGIN_OK;
}

#endif /* SIMULATOR */