Advertisement
Guest User

Battery Bench Original

a guest
Aug 14th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.21 KB | None | 0 0
  1. /***************************************************************************
  2.  *             __________               __   ___.
  3.  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
  4.  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
  5.  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
  6.  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
  7.  *                     \/            \/     \/    \/            \/
  8.  * $Id$
  9.  *
  10.  *
  11.  * Copyright (C) 2006 Alexander Spyridakis, Hristo Kovachev
  12.  *
  13.  * This program is free software; you can redistribute it and/or
  14.  * modify it under the terms of the GNU General Public License
  15.  * as published by the Free Software Foundation; either version 2
  16.  * of the License, or (at your option) any later version.
  17.  *
  18.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19.  * KIND, either express or implied.
  20.  *
  21.  ****************************************************************************/
  22.  
  23. #include "version.h"
  24. #include "plugin.h"
  25. #include "lang_enum.h"
  26.  
  27.  
  28. #define BATTERY_LOG  HOME_DIR"/battery_bench.txt"
  29. #define BUF_SIZE 16000
  30.  
  31. #define EV_EXIT 1337
  32.  
  33. /* seems to work with 1300, but who knows... */
  34. #define THREAD_STACK_SIZE DEFAULT_STACK_SIZE + 0x200
  35.  
  36. #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
  37.       (CONFIG_KEYPAD == IRIVER_H300_PAD)
  38.  
  39. #define BATTERY_ON BUTTON_ON
  40. #define BATTERY_RC_ON BUTTON_RC_ON
  41.  
  42. #define BATTERY_OFF BUTTON_OFF
  43. #define BATTERY_RC_OFF BUTTON_RC_STOP
  44.  
  45. #define BATTERY_ON_TXT  "PLAY - start"
  46. #define BATTERY_OFF_TXT "STOP"
  47.  
  48. #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
  49.       (CONFIG_KEYPAD == IPOD_3G_PAD) || \
  50.       (CONFIG_KEYPAD == IPOD_1G2G_PAD)
  51.  
  52. #define BATTERY_ON  BUTTON_PLAY
  53. #define BATTERY_OFF BUTTON_MENU
  54. #define BATTERY_ON_TXT  "PLAY - start"
  55. #define BATTERY_OFF_TXT "MENU"
  56.  
  57. #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
  58.  
  59. #define BATTERY_ON  BUTTON_SELECT
  60. #define BATTERY_OFF BUTTON_POWER
  61. #define BATTERY_ON_TXT  "SELECT - start"
  62. #define BATTERY_OFF_TXT "POWER"
  63.  
  64. #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
  65.  
  66. #define BATTERY_ON  BUTTON_SELECT
  67. #define BATTERY_OFF BUTTON_PLAY
  68. #define BATTERY_ON_TXT  "SELECT - start"
  69. #define BATTERY_OFF_TXT "PLAY"
  70.  
  71. #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
  72.       (CONFIG_KEYPAD == SANSA_C200_PAD) || \
  73.       (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
  74.       (CONFIG_KEYPAD == SANSA_M200_PAD)
  75. #define BATTERY_ON BUTTON_SELECT
  76. #define BATTERY_OFF BUTTON_POWER
  77. #define BATTERY_ON_TXT  "SELECT - start"
  78. #define BATTERY_OFF_TXT "POWER"
  79.  
  80. #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
  81. #define BATTERY_ON BUTTON_SELECT
  82. #define BATTERY_OFF BUTTON_HOME
  83. #define BATTERY_ON_TXT  "SELECT - start"
  84. #define BATTERY_OFF_TXT "HOME"
  85.  
  86. #elif CONFIG_KEYPAD == IRIVER_H10_PAD
  87.  
  88. #define BATTERY_ON  BUTTON_PLAY
  89. #define BATTERY_OFF BUTTON_POWER
  90. #define BATTERY_ON_TXT  "PLAY  - start"
  91. #define BATTERY_OFF_TXT "POWER"
  92.  
  93. #elif CONFIG_KEYPAD == GIGABEAT_PAD
  94.  
  95. #define BATTERY_ON  BUTTON_SELECT
  96. #define BATTERY_OFF BUTTON_POWER
  97. #define BATTERY_ON_TXT  "SELECT - start"
  98. #define BATTERY_OFF_TXT "POWER"
  99.  
  100. #elif CONFIG_KEYPAD == GIGABEAT_S_PAD \
  101.    || CONFIG_KEYPAD == SAMSUNG_YPR0_PAD \
  102.    || CONFIG_KEYPAD == CREATIVE_ZEN_PAD
  103.  
  104. #define BATTERY_ON  BUTTON_SELECT
  105. #define BATTERY_OFF BUTTON_BACK
  106. #define BATTERY_ON_TXT  "SELECT - start"
  107. #define BATTERY_OFF_TXT "BACK"
  108.  
  109. #elif CONFIG_KEYPAD == MROBE500_PAD
  110.  
  111. #define BATTERY_OFF BUTTON_POWER
  112. #define BATTERY_OFF_TXT "POWER"
  113.  
  114. #elif CONFIG_KEYPAD == MROBE100_PAD
  115.  
  116. #define BATTERY_ON  BUTTON_SELECT
  117. #define BATTERY_OFF BUTTON_POWER
  118. #define BATTERY_ON_TXT  "SELECT - start"
  119. #define BATTERY_OFF_TXT "POWER"
  120.  
  121. #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
  122.  
  123. #define BATTERY_ON  BUTTON_PLAY
  124. #define BATTERY_OFF BUTTON_REC
  125. #define BATTERY_RC_ON  BUTTON_RC_PLAY
  126. #define BATTERY_RC_OFF BUTTON_RC_REC
  127. #define BATTERY_ON_TXT  "PLAY - start"
  128. #define BATTERY_OFF_TXT "REC"
  129.  
  130. #elif CONFIG_KEYPAD == COWON_D2_PAD
  131.  
  132. #define BATTERY_OFF BUTTON_POWER
  133. #define BATTERY_OFF_TXT "POWER"
  134.  
  135. #elif CONFIG_KEYPAD == IAUDIO67_PAD
  136.  
  137. #define BATTERY_OFF BUTTON_POWER
  138. #define BATTERY_OFF_TXT "POWER"
  139. #define BATTERY_ON BUTTON_PLAY
  140. #define BATTERY_ON_TXT  "PLAY - start"
  141.  
  142. #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
  143. #define BATTERY_ON BUTTON_PLAY
  144. #define BATTERY_ON_TXT  "PLAY - start"
  145. #define BATTERY_OFF BUTTON_BACK
  146. #define BATTERY_OFF_TXT "BACK"
  147.  
  148. #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
  149.  
  150. #define BATTERY_ON  BUTTON_MENU
  151. #define BATTERY_OFF BUTTON_POWER
  152. #define BATTERY_ON_TXT  "MENU - start"
  153. #define BATTERY_OFF_TXT "POWER"
  154.  
  155. #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
  156.  
  157. #define BATTERY_ON  BUTTON_MENU
  158. #define BATTERY_OFF BUTTON_POWER
  159. #define BATTERY_ON_TXT  "MENU - start"
  160. #define BATTERY_OFF_TXT "POWER"
  161.  
  162. #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
  163.  
  164. #define BATTERY_ON  BUTTON_MENU
  165. #define BATTERY_OFF BUTTON_POWER
  166. #define BATTERY_ON_TXT  "MENU - start"
  167. #define BATTERY_OFF_TXT "POWER"
  168.  
  169. #elif CONFIG_KEYPAD == ONDAVX747_PAD
  170.  
  171. #define BATTERY_OFF BUTTON_POWER
  172. #define BATTERY_OFF_TXT "POWER"
  173. #elif CONFIG_KEYPAD == ONDAVX777_PAD
  174.  
  175. #define BATTERY_OFF BUTTON_POWER
  176. #define BATTERY_OFF_TXT "POWER"
  177.  
  178. #elif (CONFIG_KEYPAD == SAMSUNG_YH820_PAD) || \
  179.       (CONFIG_KEYPAD == SAMSUNG_YH92X_PAD)
  180.      
  181. #define BATTERY_ON      BUTTON_LEFT
  182. #define BATTERY_OFF     BUTTON_RIGHT
  183. #define BATTERY_ON_TXT  "LEFT"
  184. #define BATTERY_OFF_TXT "RIGHT"
  185.  
  186. #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
  187.  
  188. #define BATTERY_ON  BUTTON_PLAY
  189. #define BATTERY_OFF BUTTON_REC
  190. #define BATTERY_ON_TXT  "PLAY - start"
  191. #define BATTERY_OFF_TXT "REC"
  192.  
  193. #elif CONFIG_KEYPAD == MPIO_HD200_PAD
  194. #define BATTERY_ON  BUTTON_PLAY
  195. #define BATTERY_OFF BUTTON_REC
  196. #define BATTERY_ON_TXT  "PLAY - start"
  197. #define BATTERY_OFF_TXT "REC"
  198.  
  199. #elif CONFIG_KEYPAD == MPIO_HD300_PAD
  200. #define BATTERY_ON  BUTTON_PLAY
  201. #define BATTERY_OFF BUTTON_REC
  202. #define BATTERY_ON_TXT  "PLAY - start"
  203. #define BATTERY_OFF_TXT "REC"
  204.  
  205. #elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
  206. #define BATTERY_ON  BUTTON_PLAYPAUSE
  207. #define BATTERY_OFF BUTTON_POWER
  208. #define BATTERY_ON_TXT  "PLAYPAUSE - start"
  209. #define BATTERY_OFF_TXT "POWER"
  210.  
  211. #elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
  212. #define BATTERY_ON BUTTON_SELECT
  213. #define BATTERY_OFF BUTTON_POWER
  214. #define BATTERY_ON_TXT  "SELECT - start"
  215. #define BATTERY_OFF_TXT "POWER"
  216.  
  217. #elif CONFIG_KEYPAD == CREATIVE_ZENXFI3_PAD
  218. #define BATTERY_ON BUTTON_PLAY
  219. #define BATTERY_OFF BUTTON_POWER
  220. #define BATTERY_ON_TXT  "PLAY - start"
  221. #define BATTERY_OFF_TXT "POWER"
  222.  
  223. #elif (CONFIG_KEYPAD == HM60X_PAD) || (CONFIG_KEYPAD == HM801_PAD)
  224. #define BATTERY_ON BUTTON_SELECT
  225. #define BATTERY_OFF BUTTON_POWER
  226. #define BATTERY_ON_TXT  "SELECT - start"
  227.  
  228. #define BATTERY_OFF_TXT "POWER"
  229.  
  230. #elif CONFIG_KEYPAD == SONY_NWZ_PAD
  231. #define BATTERY_ON BUTTON_PLAY
  232. #define BATTERY_OFF BUTTON_POWER
  233. #define BATTERY_ON_TXT "PLAY - start"
  234. #define BATTERY_OFF_TXT "Power"
  235.  
  236. #elif CONFIG_KEYPAD == DX50_PAD
  237. #define BATTERY_ON      BUTTON_PLAY
  238. #define BATTERY_OFF     BUTTON_POWER_LONG
  239. #define BATTERY_OFF_TXT "Power Long"
  240. #define BATTERY_ON_TXT  "Play - start"
  241.  
  242. #elif CONFIG_KEYPAD == CREATIVE_ZENXFI2_PAD
  243. #define BATTERY_ON      BUTTON_MENU
  244. #define BATTERY_OFF     BUTTON_POWER
  245. #define BATTERY_OFF_TXT "Power"
  246. #define BATTERY_ON_TXT  "Menu - start"
  247.  
  248. #elif CONFIG_KEYPAD == AGPTEK_ROCKER_PAD
  249. #define BATTERY_ON      BUTTON_LEFT
  250. #define BATTERY_OFF     BUTTON_RIGHT
  251. #define BATTERY_OFF_TXT "Right"
  252. #define BATTERY_ON_TXT  "Left - start"
  253.  
  254. #elif CONFIG_KEYPAD == XDUOO_X3_PAD
  255. #define BATTERY_ON      BUTTON_PLAY
  256. #define BATTERY_OFF     BUTTON_POWER
  257. #define BATTERY_ON_TXT  "PLAY - start"
  258. #define BATTERY_OFF_TXT "POWER"
  259.  
  260. #elif CONFIG_KEYPAD == IHIFI_770_PAD
  261. #define BATTERY_ON      BUTTON_PLAY
  262. #define BATTERY_OFF     BUTTON_POWER
  263. #define BATTERY_ON_TXT  "PLAY - start"
  264. #define BATTERY_OFF_TXT "POWER"
  265.  
  266. #elif CONFIG_KEYPAD == IHIFI_800_PAD
  267. #define BATTERY_ON      BUTTON_PLAY
  268. #define BATTERY_OFF     BUTTON_POWER
  269. #define BATTERY_ON_TXT  "PLAY - start"
  270. #define BATTERY_OFF_TXT "POWER"
  271.  
  272. #elif CONFIG_KEYPAD == XDUOO_X3II_PAD
  273. #define BATTERY_ON      BUTTON_PLAY
  274. #define BATTERY_OFF     BUTTON_POWER
  275. #define BATTERY_ON_TXT  "Play - start"
  276. #define BATTERY_OFF_TXT "POWER"
  277.  
  278. #elif CONFIG_KEYPAD == XDUOO_X20_PAD
  279. #define BATTERY_ON      BUTTON_PLAY
  280. #define BATTERY_OFF     BUTTON_POWER
  281. #define BATTERY_ON_TXT  "Play - start"
  282. #define BATTERY_OFF_TXT "POWER"
  283.  
  284. #else
  285. #error No keymap defined!
  286. #endif
  287.  
  288. #ifdef HAVE_TOUCHSCREEN
  289. #ifndef BATTERY_ON
  290. #define BATTERY_ON       BUTTON_CENTER
  291. #endif
  292. #ifndef BATTERY_OFF
  293. #define BATTERY_OFF      BUTTON_TOPLEFT
  294. #endif
  295. #ifndef BATTERY_ON_TXT
  296. #define BATTERY_ON_TXT  "CENTRE - start"
  297. #endif
  298. #ifndef BATTERY_OFF_TXT
  299. #define BATTERY_OFF_TXT "TOPLEFT"
  300. #endif
  301. #endif
  302.  
  303. /****************************** Plugin Entry Point ****************************/
  304. static long start_tick;
  305.  
  306. /* Struct for battery information */
  307. static struct batt_info
  308. {
  309.     /* the size of the struct elements is optimised to make the struct size
  310.      * a power of 2 */
  311.     unsigned secs;
  312.     int eta;
  313.     unsigned int voltage;
  314.     short level;
  315.     unsigned short flags;
  316. } bat[BUF_SIZE/sizeof(struct batt_info)];
  317.  
  318. #define BUF_ELEMENTS    (sizeof(bat)/sizeof(struct batt_info))
  319.  
  320. static unsigned int thread_id;
  321. static struct event_queue thread_q SHAREDBSS_ATTR;
  322. static bool in_usb_mode;
  323. static unsigned int buf_idx;
  324.  
  325. static bool exit_tsr(bool reenter)
  326. {
  327.     long button;
  328.     (void)reenter;
  329.     rb->lcd_clear_display();
  330.     rb->lcd_puts_scroll(0, 0, "Batt.Bench is currently running.");
  331.     rb->lcd_puts_scroll(0, 1, "Press " BATTERY_OFF_TXT " to cancel the test");
  332.     rb->lcd_puts_scroll(0, 2, "Anything else will resume");
  333.     if(rb->global_settings->talk_menu)
  334.         rb->talk_id(VOICE_BATTERY_BENCH_IS_ALREADY_RUNNING, true);
  335.     rb->lcd_update();
  336.  
  337.     while (1)
  338.     {
  339.         button = rb->button_get(true);
  340.         if (IS_SYSEVENT(button))
  341.             continue;
  342.         if (button == BATTERY_OFF)
  343.         {
  344.             rb->queue_post(&thread_q, EV_EXIT, 0);
  345.             rb->thread_wait(thread_id);
  346.             /* remove the thread's queue from the broadcast list */
  347.             rb->queue_delete(&thread_q);
  348.             return true;
  349.         }
  350.         else return false;
  351.     }
  352. }
  353.  
  354. #define BIT_CHARGER     0x1
  355. #define BIT_CHARGING    0x2
  356. #define BIT_USB_POWER   0x4
  357.  
  358. #define HMS(x) (x)/3600,((x)%3600)/60,((x)%3600)%60
  359.  
  360. /* use long for aligning */
  361. static unsigned long thread_stack[THREAD_STACK_SIZE/sizeof(long)];
  362.  
  363. #if CONFIG_CHARGING || defined(HAVE_USB_POWER)
  364. static unsigned int charge_state(void)
  365. {
  366.     unsigned int ret = 0;
  367. #if CONFIG_CHARGING
  368.     if (rb->charger_inserted())
  369.         ret = BIT_CHARGER;
  370. #if CONFIG_CHARGING >= CHARGING_MONITOR
  371.     if (rb->charging_state())
  372.         ret |= BIT_CHARGING;
  373. #endif
  374. #endif
  375.     /* USB insertion means nothing if USB cannot power the device */
  376. #ifdef HAVE_USB_POWER
  377.     if (rb->usb_inserted())
  378.         ret |= BIT_USB_POWER;
  379. #endif
  380.     return ret;
  381. }
  382. #endif
  383.  
  384.  
  385. static void flush_buffer(void)
  386. {
  387.     int fd;
  388.     unsigned int i;
  389.  
  390.     /* don't access the disk when in usb mode, or when no data is available */
  391.     if (in_usb_mode || (buf_idx == 0))
  392.         return;
  393.  
  394.     fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT | O_APPEND, 0666);
  395.     if (fd < 0)
  396.         return;
  397.  
  398.     for (i = 0; i < buf_idx; i++)
  399.     {
  400.         rb->fdprintf(fd,
  401.                 "%02d:%02d:%02d,  %05d,     %03d%%,     "
  402.                 "%02d:%02d,         %04d,   "
  403. #if CONFIG_CHARGING
  404.                 "  %c"
  405. #if CONFIG_CHARGING >= CHARGING_MONITOR
  406.                 ",  %c"
  407. #endif
  408. #endif
  409. #ifdef HAVE_USB_POWER
  410.                 ",  %c"
  411. #endif
  412.                 "\n",
  413.  
  414.                 HMS(bat[i].secs), bat[i].secs, bat[i].level,
  415.                 bat[i].eta / 60, bat[i].eta % 60,
  416.                 bat[i].voltage
  417. #if CONFIG_CHARGING
  418.                 , (bat[i].flags & BIT_CHARGER) ? 'A' : '-'
  419. #if CONFIG_CHARGING >= CHARGING_MONITOR
  420.                 , (bat[i].flags & BIT_CHARGING) ? 'C' : '-'
  421. #endif
  422. #endif
  423. #ifdef HAVE_USB_POWER
  424.                 , (bat[i].flags & BIT_USB_POWER) ? 'U' : '-'
  425. #endif
  426.         );
  427.     }
  428.     rb->close(fd);
  429.  
  430.     buf_idx = 0;
  431. }
  432.  
  433.  
  434. static void thread(void)
  435. {
  436.     bool exit = false;
  437.     char *exit_reason = "unknown";
  438.     long sleep_time = 60 * HZ;
  439.     struct queue_event ev;
  440.     int fd;
  441.  
  442.     in_usb_mode = false;
  443.     buf_idx = 0;
  444.  
  445.     while (!exit)
  446.     {
  447.         /* add data to buffer */
  448.         if (buf_idx < BUF_ELEMENTS)
  449.         {
  450.             bat[buf_idx].secs = (*rb->current_tick - start_tick) / HZ;
  451.             bat[buf_idx].level = rb->battery_level();
  452.             bat[buf_idx].eta = rb->battery_time();
  453.             bat[buf_idx].voltage = rb->battery_voltage();
  454. #if CONFIG_CHARGING || defined(HAVE_USB_POWER)
  455.             bat[buf_idx].flags = charge_state();
  456. #endif
  457.             buf_idx++;
  458. #if USING_STORAGE_CALLBACK
  459.             rb->register_storage_idle_func(flush_buffer);
  460. #endif
  461.         }
  462.        
  463.         /* What to do when the measurement buffer is full:
  464.            1) save our measurements to disk but waste some power doing so?
  465.            2) throw away measurements to save some power?
  466.            The choice made here is to save the measurements. It is quite unusual
  467.            for this to occur because it requires > 16 hours of no disk activity.
  468.          */
  469.         if (buf_idx == BUF_ELEMENTS) {
  470.             flush_buffer();
  471.         }
  472.        
  473.         /* sleep some time until next measurement */
  474.         rb->queue_wait_w_tmo(&thread_q, &ev, sleep_time);
  475.         switch (ev.id)
  476.         {
  477.             case SYS_USB_CONNECTED:
  478.                 in_usb_mode = true;
  479.                 rb->usb_acknowledge(SYS_USB_CONNECTED_ACK);
  480.                 break;
  481.             case SYS_USB_DISCONNECTED:
  482.                 in_usb_mode = false;
  483.                 break;
  484.             case SYS_POWEROFF:
  485.                 exit_reason = "power off";
  486.                 exit = true;
  487.                 break;
  488.             case EV_EXIT:
  489.                 rb->splash(HZ, "Exiting battery_bench...");
  490.                 exit_reason = "plugin exit";
  491.                 exit = true;
  492.                 break;
  493.         }
  494.     }
  495.  
  496. #if USING_STORAGE_CALLBACK
  497.     /* unregister flush callback and flush to disk */
  498.     rb->unregister_storage_idle_func(flush_buffer, true);
  499. #else
  500.     flush_buffer();
  501. #endif
  502.    
  503.     /* log end of bench and exit reason */
  504.     fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT | O_APPEND, 0666);
  505.     if (fd >= 0)
  506.     {
  507.         rb->fdprintf(fd, "--Battery bench ended, reason: %s--\n", exit_reason);
  508.         rb->close(fd);
  509.     }
  510. }
  511.  
  512.  
  513. typedef void (*plcdfunc)(int x, int y, const unsigned char *str);
  514.  
  515. static void put_centered_str(const char* str, plcdfunc putsxy, int lcd_width, int line)
  516. {
  517.     int strwdt, strhgt;
  518.     rb->lcd_getstringsize(str, &strwdt, &strhgt);
  519.     putsxy((lcd_width - strwdt)/2, line*(strhgt), str);
  520. }
  521.  
  522. enum plugin_status plugin_start(const void* parameter)
  523. {
  524.     (void)parameter;
  525.     int button, fd;
  526.     bool on = false;
  527.     start_tick = *rb->current_tick;
  528.     int i;
  529.     const char *msgs[] = { "Battery Benchmark","Check file", BATTERY_LOG,
  530.                            "for more info", BATTERY_ON_TXT, BATTERY_OFF_TXT " - quit" };
  531.     rb->lcd_clear_display();
  532.  
  533.     rb->lcd_clear_display();
  534.     rb->lcd_setfont(FONT_SYSFIXED);
  535.  
  536.     for (i = 0; i<(int)(sizeof(msgs)/sizeof(char *)); i++)
  537.         put_centered_str(msgs[i],rb->lcd_putsxy,LCD_WIDTH,i+1);
  538.     if(rb->global_settings->talk_menu)
  539.         rb->talk_id(VOICE_BAT_BENCH_KEYS, true);
  540.     rb->lcd_update();
  541.  
  542. #ifdef HAVE_REMOTE_LCD
  543.     rb->lcd_remote_clear_display();
  544.     put_centered_str(msgs[0],rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,0);
  545.     put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-2],
  546.                     rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,1);
  547.     put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-1],
  548.                     rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,2);
  549.     rb->lcd_remote_update();
  550. #endif
  551.    
  552.     do
  553.     {
  554.         button = rb->button_get(true);
  555.         switch (button)
  556.         {
  557.             case BATTERY_ON:
  558. #ifdef BATTERY_RC_ON
  559.             case BATTERY_RC_ON:
  560. #endif                        
  561.                 on = true;
  562.                 break;        
  563.             case BATTERY_OFF:
  564. #ifdef BATTERY_RC_OFF
  565.             case BATTERY_RC_OFF:
  566. #endif
  567.                 return PLUGIN_OK;
  568.  
  569.             default:
  570.                 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
  571.                     return PLUGIN_USB_CONNECTED;
  572.         }
  573.     }while(!on);
  574.    
  575.     fd = rb->open(BATTERY_LOG, O_RDONLY);
  576.     if (fd < 0)
  577.     {
  578.         fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT, 0666);
  579.         if (fd >= 0)
  580.         {
  581.             rb->fdprintf(fd,
  582.                 "# This plugin will log your battery performance in a\n"
  583.                 "# file (%s) every minute.\n"
  584.                 "# To properly test your battery:\n"
  585.                 "# 1) Select and playback an album. "
  586.                 "(Be sure to be more than the player's buffer)\n"
  587.                 "# 2) Set to repeat.\n"
  588.                 "# 3) Let the player run completely out of battery.\n"
  589.                 "# 4) Recharge and copy (or whatever you want) the txt file to "
  590.                 "your computer.\n"
  591.                 "# Now you can make graphs with the data of the battery log.\n"
  592.                 "# Do not enter another plugin during the test or else the \n"
  593.                 "# logging activity will end.\n\n"
  594.                 "# P.S: You can decide how you will make your tests.\n"
  595.                 "# Just don't open another plugin to be sure that your log "
  596.                 "will continue.\n\n",BATTERY_LOG);
  597.             rb->fdprintf(fd,
  598.                 "# Battery bench run for %s version %s\n\n"
  599.                 ,MODEL_NAME,rb->rbversion);
  600.  
  601.             rb->fdprintf(fd, "# Battery type: %d mAh      Buffer Entries: %d\n",
  602.                 rb->global_settings->battery_capacity, (int)BUF_ELEMENTS);
  603.  
  604.             rb->fdprintf(fd, "# Rockbox has been running for %02d:%02d:%02d\n",
  605.                 HMS((unsigned)start_tick/HZ));
  606.  
  607.             rb->fdprintf(fd,
  608.                 "# Time:,  Seconds:,  Level:,  Time Left:,  Voltage[mV]:"
  609. #if CONFIG_CHARGING
  610.                 ", C:"
  611. #endif
  612. #if CONFIG_CHARGING >= CHARGING_MONITOR
  613.                 ", S:"
  614. #endif
  615. #ifdef HAVE_USB_POWER
  616.                 ", U:"
  617. #endif
  618.                 "\n");
  619.             rb->close(fd);
  620.         }
  621.         else
  622.         {
  623.             rb->splash(HZ / 2, "Cannot create file!");
  624.             return PLUGIN_ERROR;
  625.         }
  626.     }
  627.     else
  628.     {
  629.         rb->close(fd);
  630.         fd = rb->open(BATTERY_LOG, O_RDWR | O_APPEND);
  631.         rb->fdprintf(fd, "\n# --File already present. Resuming Benchmark--\n");
  632.         rb->fdprintf(fd,
  633.             "# Battery bench run for %s version %s\n\n"
  634.             ,MODEL_NAME,rb->rbversion);
  635.         rb->fdprintf(fd, "# Rockbox has been running for %02d:%02d:%02d\n",
  636.             HMS((unsigned)start_tick/HZ));
  637.         rb->close(fd);
  638.     }
  639.    
  640.     rb->queue_init(&thread_q, true); /* put the thread's queue in the bcast list */
  641.     if ((thread_id = rb->create_thread(thread, thread_stack,
  642.         sizeof(thread_stack), 0, "Battery Benchmark"
  643.         IF_PRIO(, PRIORITY_BACKGROUND)
  644.         IF_COP(, CPU))) == 0)
  645.     {
  646.         rb->splash(HZ, "Cannot create thread!");
  647.         return PLUGIN_ERROR;
  648.     }
  649.            
  650.     rb->plugin_tsr(exit_tsr);
  651.    
  652.     return PLUGIN_OK;
  653. }
  654.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement