Added MP3::Info.pm inside script to make it standalone.
[kugel-rb.git] / apps / plugins / battery_test.c
blobbc6e580dccebb02c0ecc90da41706bfc44ea2eb7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
21 /* This plugin is designed to measure your battery performance in real-time.
22 It will create a big file, read it every ~90 seconds and log the
23 battery level in /battery.log
25 When battery level goes below 5% the plugin exits, to avoid writing to
26 disk in very low battery situations.
28 Note that this test will run for 10-15 hours or more and is very boring
29 to watch.
32 /* variable button definitions */
33 #if CONFIG_KEYPAD == RECORDER_PAD
34 #define BATTERY_TEST_QUIT BUTTON_ON
35 #define BATTERY_TEST_QUIT2 BUTTON_OFF
36 #elif CONFIG_KEYPAD == ONDIO_PAD
37 #define BATTERY_TEST_QUIT BUTTON_OFF
38 #elif CONFIG_KEYPAD == PLAYER_PAD
39 #define BATTERY_TEST_QUIT BUTTON_STOP
40 #endif
42 static struct plugin_api* rb;
44 void* buffer;
45 int buffersize;
47 int init(void)
49 int f;
50 buffer = rb->plugin_get_mp3_buffer(&buffersize);
52 /* create a big dummy file */
53 f = rb->creat("/battery.dummy", 0);
54 if (f<0) {
55 rb->splash(HZ, true, "Can't create /battery.dummy");
56 return -1;
58 rb->write(f, buffer, buffersize);
59 rb->close(f);
61 return 0;
64 enum plugin_status loop(void)
66 while (true) {
67 struct tm* t;
68 char buf[80];
69 int f;
70 int batt = rb->battery_level();
71 int button;
73 /* stop measuring when <5% battery left */
74 if ((batt > 0) && (batt < 5))
75 break;
77 /* log current time */
78 f = rb->open("/battery.log", O_WRONLY | O_APPEND | O_CREAT);
79 if (f<0) {
80 rb->splash(HZ, true, "Failed creating /battery.log");
81 break;
83 #ifdef HAVE_RTC
84 t = rb->get_time();
85 #else
87 static struct tm temp;
88 long t2 = *rb->current_tick/HZ;
89 temp.tm_hour=t2/3600;
90 temp.tm_min=(t2/60)%60;
91 temp.tm_sec=t2%60;
92 t=&temp;
94 #endif
95 rb->snprintf(buf, sizeof buf, "%02d:%02d:%02d Battery %d%%\n",
96 t->tm_hour, t->tm_min, t->tm_sec, batt);
97 rb->write(f, buf, rb->strlen(buf));
98 rb->close(f);
100 rb->snprintf(buf, sizeof buf, "%02d:%02d:%02d Battery %d%%%%",
101 t->tm_hour, t->tm_min, t->tm_sec, batt);
102 rb->splash(0, true, buf);
104 /* simulate 128kbit/s (16kbyte/s) playback duration */
105 do {
106 button = rb->button_get_w_tmo(HZ * (buffersize / 16384) - HZ*10);
108 switch (button) {
109 /* Check if we shall exit the plugin */
110 case BATTERY_TEST_QUIT:
111 #ifdef BATTERY_TEST_QUIT2
112 case BATTERY_TEST_QUIT2:
113 #endif
114 return PLUGIN_OK;
116 default:
117 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
118 return PLUGIN_USB_CONNECTED;
119 break;
121 } while (!(button&(BUTTON_REL|BUTTON_REPEAT)));
123 /* simulate filling the mp3 buffer */
124 f = rb->open("/battery.dummy", O_RDONLY);
125 if (f<0) {
126 rb->splash(HZ, true, "Failed opening /battery.dummy");
127 break;
129 rb->read(f, buffer, buffersize);
130 rb->close(f);
132 return PLUGIN_OK;
135 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
137 TEST_PLUGIN_API(api);
138 (void)parameter;
139 rb = api;
141 if (init() < 0)
142 return PLUGIN_OK;
144 return loop();