summaryrefslogtreecommitdiff
path: root/apps/plugins/battery_test.c
blob: bc6e580dccebb02c0ecc90da41706bfc44ea2eb7 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2004 Björn Stenberg
 *
 * 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 "plugin.h"

/* This plugin is designed to measure your battery performance in real-time.
   It will create a big file, read it every ~90 seconds and log the
   battery level in /battery.log

   When battery level goes below 5% the plugin exits, to avoid writing to
   disk in very low battery situations.
   
   Note that this test will run for 10-15 hours or more and is very boring
   to watch.
*/

/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define BATTERY_TEST_QUIT BUTTON_ON
#define BATTERY_TEST_QUIT2 BUTTON_OFF
#elif CONFIG_KEYPAD == ONDIO_PAD
#define BATTERY_TEST_QUIT BUTTON_OFF
#elif CONFIG_KEYPAD == PLAYER_PAD
#define BATTERY_TEST_QUIT BUTTON_STOP
#endif

static struct plugin_api* rb;

void* buffer;
int buffersize;

int init(void)
{
    int f;
    buffer = rb->plugin_get_mp3_buffer(&buffersize);

    /* create a big dummy file */
    f = rb->creat("/battery.dummy", 0);
    if (f<0) {
        rb->splash(HZ, true, "Can't create /battery.dummy");
        return -1;
    }
    rb->write(f, buffer, buffersize);
    rb->close(f);

    return 0;
}

enum plugin_status loop(void)
{
    while (true) {
        struct tm* t;
        char buf[80];
        int f;
        int batt = rb->battery_level();
        int button;

        /* stop measuring when <5% battery left */
        if ((batt > 0) && (batt < 5))
            break;
        
        /* log current time */
        f = rb->open("/battery.log", O_WRONLY | O_APPEND | O_CREAT);
        if (f<0) {
            rb->splash(HZ, true, "Failed creating /battery.log");
            break;
        }
#ifdef HAVE_RTC
        t = rb->get_time();
#else
        {
            static struct tm temp;
            long t2 = *rb->current_tick/HZ;
            temp.tm_hour=t2/3600;
            temp.tm_min=(t2/60)%60;
            temp.tm_sec=t2%60;
            t=&temp;
        }
#endif
        rb->snprintf(buf, sizeof buf, "%02d:%02d:%02d Battery %d%%\n",
                     t->tm_hour, t->tm_min, t->tm_sec, batt);
        rb->write(f, buf, rb->strlen(buf));
        rb->close(f);

        rb->snprintf(buf, sizeof buf, "%02d:%02d:%02d Battery %d%%%%",
                     t->tm_hour, t->tm_min, t->tm_sec, batt);
        rb->splash(0, true, buf);
        
        /* simulate 128kbit/s (16kbyte/s) playback duration */
        do {
            button = rb->button_get_w_tmo(HZ * (buffersize / 16384) - HZ*10);
            
            switch (button) {
                /* Check if we shall exit the plugin */
                case BATTERY_TEST_QUIT:
#ifdef BATTERY_TEST_QUIT2
                case BATTERY_TEST_QUIT2:
#endif
                    return PLUGIN_OK;
                    
                default:
                    if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                        return PLUGIN_USB_CONNECTED;
                    break;
            }
        } while (!(button&(BUTTON_REL|BUTTON_REPEAT)));

        /* simulate filling the mp3 buffer */
        f = rb->open("/battery.dummy", O_RDONLY);
        if (f<0) {
            rb->splash(HZ, true, "Failed opening /battery.dummy");
            break;
        }
        rb->read(f, buffer, buffersize);
        rb->close(f);
    }
    return PLUGIN_OK;
}

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    TEST_PLUGIN_API(api);
    (void)parameter;
    rb = api;

    if (init() < 0)
        return PLUGIN_OK;

    return loop();
}