1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2006 Alexander Spyridakis, Hristo Kovachev
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 #ifndef SIMULATOR /* not for the simulator */
25 #define BATTERY_LOG "/battery_bench.txt"
26 #define BUF_SIZE 16000
27 #define DISK_SPINDOWN_TIMEOUT 3600
31 /* seems to work with 1300, but who knows... */
32 #define THREAD_STACK_SIZE DEFAULT_STACK_SIZE + 0x200
34 #if CONFIG_KEYPAD == RECORDER_PAD
35 #define BATTERY_ON BUTTON_PLAY
36 #define BATTERY_OFF BUTTON_OFF
38 #elif CONFIG_KEYPAD == ONDIO_PAD
39 #define BATTERY_ON BUTTON_RIGHT
40 #define BATTERY_OFF BUTTON_OFF
42 #elif CONFIG_KEYPAD == PLAYER_PAD
43 #define BATTERY_ON BUTTON_ON
44 #define BATTERY_OFF BUTTON_STOP
46 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
47 (CONFIG_KEYPAD == IRIVER_H300_PAD)
49 #define BATTERY_ON BUTTON_ON
50 #define BATTERY_RC_ON BUTTON_RC_ON
52 #define BATTERY_OFF BUTTON_OFF
53 #define BATTERY_RC_OFF BUTTON_RC_STOP
55 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
56 (CONFIG_KEYPAD == IPOD_3G_PAD)
58 #define BATTERY_ON BUTTON_PLAY
59 #define BATTERY_OFF BUTTON_MENU
61 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
63 #define BATTERY_ON BUTTON_SELECT
64 #define BATTERY_OFF BUTTON_PLAY
69 /****************************** Plugin Entry Point ****************************/
70 static struct plugin_api
* rb
;
76 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
90 /* Struct for battery information */
93 int ticks
, level
, eta
;
95 } bat
[BUF_SIZE
/sizeof(struct batt_info
)];
97 struct event_queue thread_q
;
101 rb
->queue_post(&thread_q
, EV_EXIT
, NULL
);
102 while (!s_thread
.ended
)
104 /* remove the thread's queue from the broadcast list */
105 rb
->queue_delete(&thread_q
);
108 #define BIT_CHARGER 0x1000
109 #define BIT_CHARGING 0x2000
110 #define BIT_USB_POWER 0x4000
112 #define HMS(x) (x)/3600,((x)%3600)/60,((x)%3600)%60
114 /* use long for aligning */
115 unsigned long thread_stack
[THREAD_STACK_SIZE
/sizeof(long)];
117 #if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
118 unsigned int charge_state(void)
120 unsigned int ret
= 0;
122 if(rb
->charger_inserted())
124 #ifdef HAVE_CHARGE_STATE
125 if(rb
->charging_state())
129 #ifdef HAVE_USB_POWER
130 if(rb
->usb_powered())
131 ret
|= BIT_USB_POWER
;
139 bool got_info
= false, timeflag
= false, in_usb_mode
= false;
140 int fd
, buffelements
, tick
= 1, i
= 0, skipped
= 0, exit
= 0;
141 int fst
= 0, lst
= 0; /* first and last skipped tick */
142 unsigned int last_voltage
= 0;
143 #if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
144 unsigned int last_state
= 0;
150 buffelements
= sizeof(bat
)/sizeof(struct batt_info
);
152 sleep_time
= (rb
->global_settings
->disk_spindown
> 1) ?
153 (rb
->global_settings
->disk_spindown
- 1) * HZ
: 5 * HZ
;
157 if(!in_usb_mode
&& got_info
&&
158 (exit
|| timeflag
|| rb
->ata_disk_is_active()) )
160 int last
, secs
, j
, temp
= skipped
;
162 fd
= rb
->open(BATTERY_LOG
, O_RDWR
| O_CREAT
| O_APPEND
);
174 rb
->fdprintf(fd
,"-Skipped %d measurements from "
175 "%02d:%02d:%02d to %02d:%02d:%02d-\n",skipped
,
185 for(j
= i
; j
< last
; j
++)
187 secs
= bat
[j
].ticks
/HZ
;
189 "%02d:%02d:%02d, %05d, %03d%%, "
190 "%02d:%02d, %04d, %04d"
193 #ifdef HAVE_CHARGE_STATE
197 #ifdef HAVE_USB_POWER
202 HMS(secs
), secs
, bat
[j
].level
,
203 bat
[j
].eta
/ 60, bat
[j
].eta
% 60,
204 #if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
206 (~(BIT_CHARGER
|BIT_CHARGING
|BIT_USB_POWER
)))
213 ,(bat
[j
].voltage
& BIT_CHARGER
)?'A':'-'
214 #ifdef HAVE_CHARGE_STATE
215 ,(bat
[j
].voltage
& BIT_CHARGING
)?'C':'-'
218 #ifdef HAVE_USB_POWER
219 ,(bat
[j
].voltage
& BIT_USB_POWER
)?'U':'-'
223 if(!j
% 100 && !j
) /* yield() at every 100 writes */
231 tick
= *rb
->current_tick
;
238 unsigned int current_voltage
;
240 #if CONFIG_CODEC == SWCODEC
241 !rb
->pcm_is_playing()
243 !rb
->mp3_is_playing()
245 && (*rb
->current_tick
- tick
) > DISK_SPINDOWN_TIMEOUT
* HZ
)
248 if(last_voltage
!= (current_voltage
=rb
->battery_voltage())
249 #if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
250 || last_state
!= charge_state()
254 if(i
== buffelements
)
265 bat
[i
].ticks
= *rb
->current_tick
;
266 bat
[i
].level
= rb
->battery_level();
267 bat
[i
].eta
= rb
->battery_time();
268 last_voltage
= bat
[i
].voltage
= current_voltage
;
269 #if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
270 bat
[i
].voltage
|= last_state
= charge_state();
282 #ifdef HAVE_LCD_BITMAP
283 "Exiting battery_bench...");
287 s_thread
.ended
= true;
288 rb
->remove_thread(s_thread
.id
);
289 rb
->yield(); /* exit the thread, this yield() won't return */
292 rb
->queue_wait_w_tmo(&thread_q
, &ev
, sleep_time
);
295 case SYS_USB_CONNECTED
:
297 rb
->usb_acknowledge(SYS_USB_CONNECTED_ACK
);
299 case SYS_USB_DISCONNECTED
:
301 rb
->usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
315 #ifdef HAVE_LCD_BITMAP
316 typedef void (*plcdfunc
)(int x
, int y
, const unsigned char *str
);
318 void put_centered_str(const char* str
, plcdfunc putsxy
, int lcd_width
, int line
)
321 rb
->lcd_getstringsize(str
, &strwdt
, &strhgt
);
322 putsxy((lcd_width
- strwdt
)/2, line
*(strhgt
), str
);
330 #ifdef HAVE_LCD_BITMAP
331 const char *msgs
[] = { "Battery Benchmark","Check file", BATTERY_LOG
,
332 "for more info", "PLAY - start", "OFF - quit" };
334 rb
->lcd_clear_display();
336 #ifdef HAVE_LCD_BITMAP
339 rb
->lcd_clear_display();
340 rb
->lcd_setfont(FONT_SYSFIXED
);
342 for(i
= 0; i
<(int)(sizeof(msgs
)/sizeof(char *)); i
++)
343 put_centered_str(msgs
[i
],rb
->lcd_putsxy
,LCD_WIDTH
,i
+1);
346 #ifdef HAVE_REMOTE_LCD
347 rb
->lcd_remote_clear_display();
348 put_centered_str(msgs
[0],rb
->lcd_remote_putsxy
,LCD_REMOTE_WIDTH
,0);
349 put_centered_str(msgs
[sizeof(msgs
)/sizeof(char*)-2],
350 rb
->lcd_remote_putsxy
,LCD_REMOTE_WIDTH
,1);
351 put_centered_str(msgs
[sizeof(msgs
)/sizeof(char*)-1],
352 rb
->lcd_remote_putsxy
,LCD_REMOTE_WIDTH
,2);
353 rb
->lcd_remote_update();
357 rb
->lcd_puts_scroll(0, 1, "Batt.Bench.");
358 rb
->lcd_puts_scroll(0, 2, "PLAY/STOP");
363 button
= rb
->button_get(true);
373 #ifdef BATTERY_RC_OFF
378 default: if(rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
379 return PLUGIN_USB_CONNECTED
;
383 fd
= rb
->open(BATTERY_LOG
, O_RDONLY
);
386 fd
= rb
->open(BATTERY_LOG
, O_RDWR
| O_CREAT
);
390 "This plugin will log your battery performance in a\n"
391 "file (%s) every time the disk is accessed (or every hour).\n"
392 "To properly test your battery:\n"
393 "1) Select and playback an album. "
394 "(Be sure to be more than the player's buffer)\n"
395 "2) Set to repeat.\n"
396 "3) Let the player run completely out of battery.\n"
397 "4) Recharge and copy (or whatever you want) the txt file to "
399 "Now you can make graphs with the data of the battery log.\n"
400 "Do not enter another plugin during the test or else the "
401 "logging activity will end.\n\n"
402 "P.S: You can decide how you will make your tests.\n"
403 "Just don't open another plugin to be sure that your log "
405 "M/DA (Measurements per Disk Activity) shows how many times\n"
406 "data was logged in the buffer between Disk Activity.\n\n"
407 "Battery type: %d mAh Buffer Entries: %d\n"
408 " Time:, Seconds:, Level:, Time Left:, Voltage[mV]:,"
413 #ifdef HAVE_CHARGE_STATE
416 #ifdef HAVE_USB_POWER
420 ,BATTERY_LOG
,rb
->global_settings
->battery_capacity
,
421 BUF_SIZE
/ sizeof(struct batt_info
));
426 rb
->splash(HZ
/ 2, true, "Cannot create file!");
433 fd
= rb
->open(BATTERY_LOG
, O_RDWR
| O_APPEND
);
434 rb
->fdprintf(fd
, "\n--File already present. Resuming Benchmark--\n");
438 rb
->queue_init(&thread_q
); /* put the thread's queue in the bcast list */
439 rb
->memset(&s_thread
, 0, sizeof(s_thread
)); /* zero the struct */
440 s_thread
.id
= rb
->create_thread(thread
, thread_stack
,
441 sizeof(thread_stack
), "Battery Benchmark");
442 rb
->plugin_tsr(exit_tsr
);