Advertisement
Guest User

BatBench

a guest
Aug 13th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.01 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 MIN_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.  
  321. static struct
  322. {
  323.     unsigned int id; /* worker thread id */
  324.     long   *stack;
  325.     ssize_t stacksize;
  326. } gThread;
  327.  
  328. static struct event_queue thread_q SHAREDBSS_ATTR;
  329. static bool in_usb_mode;
  330. static unsigned int buf_idx;
  331.  
  332. static bool exit_tsr(bool reenter)
  333. {
  334.     bool is_exit;
  335.     long button;
  336.     (void)reenter;
  337.     rb->lcd_clear_display();
  338.     rb->lcd_puts_scroll(0, 0, "Batt.Bench is currently running.");
  339.     rb->lcd_puts_scroll(0, 1, "Press " BATTERY_OFF_TXT " to cancel the test");
  340.     rb->lcd_puts_scroll(0, 2, "Anything else will resume");
  341.     if(rb->global_settings->talk_menu)
  342.         rb->talk_id(VOICE_BATTERY_BENCH_IS_ALREADY_RUNNING, true);
  343.     rb->lcd_update();
  344.  
  345.     while (1)
  346.     {
  347.         button = rb->button_get(true);
  348.         if (IS_SYSEVENT(button))
  349.             continue;
  350.         if (button == BATTERY_OFF)
  351.         {
  352.             rb->queue_post(&thread_q, EV_EXIT, 0);
  353.             rb->thread_wait(gThread.id);
  354.             /* remove the thread's queue from the broadcast list */
  355.             rb->queue_delete(&thread_q);
  356.             is_exit = true;
  357.         }
  358.         else is_exit = false;
  359.  
  360.         break;
  361.     }
  362.     FOR_NB_SCREENS(idx)
  363.         rb->screens[idx]->scroll_stop();
  364.  
  365.     return is_exit;
  366. }
  367.  
  368. #define BIT_CHARGER     0x1
  369. #define BIT_CHARGING    0x2
  370. #define BIT_USB_POWER   0x4
  371.  
  372. #define HMS(x) (x)/3600,((x)%3600)/60,((x)%3600)%60
  373.  
  374. #if CONFIG_CHARGING || defined(HAVE_USB_POWER)
  375. static unsigned int charge_state(void)
  376. {
  377.     unsigned int ret = 0;
  378. #if CONFIG_CHARGING
  379.     if (rb->charger_inserted())
  380.         ret = BIT_CHARGER;
  381. #if CONFIG_CHARGING >= CHARGING_MONITOR
  382.     if (rb->charging_state())
  383.         ret |= BIT_CHARGING;
  384. #endif
  385. #endif
  386.     /* USB insertion means nothing if USB cannot power the device */
  387. #ifdef HAVE_USB_POWER
  388.     if (rb->usb_inserted())
  389.         ret |= BIT_USB_POWER;
  390. #endif
  391.     return ret;
  392. }
  393. #endif
  394.  
  395.  
  396. static void flush_buffer(void)
  397. {
  398.     int fd;
  399.     unsigned int i;
  400.  
  401.     /* don't access the disk when in usb mode, or when no data is available */
  402.     if (in_usb_mode || (buf_idx == 0))
  403.         return;
  404.  
  405.     fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT | O_APPEND, 0666);
  406.     if (fd < 0)
  407.         return;
  408.  
  409.     for (i = 0; i < buf_idx; i++)
  410.     {
  411.         rb->fdprintf(fd,
  412.                 "%02d:%02d:%02d,  %05d,     %03d%%,     "
  413.                 "%02d:%02d,         %04d,   "
  414. #if CONFIG_CHARGING
  415.                 "  %c"
  416. #if CONFIG_CHARGING >= CHARGING_MONITOR
  417.                 ",  %c"
  418. #endif
  419. #endif
  420. #ifdef HAVE_USB_POWER
  421.                 ",  %c"
  422. #endif
  423.                 "\n",
  424.  
  425.                 HMS(bat[i].secs), bat[i].secs, bat[i].level,
  426.                 bat[i].eta / 60, bat[i].eta % 60,
  427.                 bat[i].voltage
  428. #if CONFIG_CHARGING
  429.                 , (bat[i].flags & BIT_CHARGER) ? 'A' : '-'
  430. #if CONFIG_CHARGING >= CHARGING_MONITOR
  431.                 , (bat[i].flags & BIT_CHARGING) ? 'C' : '-'
  432. #endif
  433. #endif
  434. #ifdef HAVE_USB_POWER
  435.                 , (bat[i].flags & BIT_USB_POWER) ? 'U' : '-'
  436. #endif
  437.         );
  438.     }
  439.     rb->close(fd);
  440.  
  441.     buf_idx = 0;
  442. }
  443.  
  444.  
  445. static void thread(void)
  446. {
  447.     bool exit = false;
  448.     char *exit_reason = "unknown";
  449.     long sleep_time = 60 * HZ;
  450.     struct queue_event ev;
  451.     int fd;
  452.  
  453.     in_usb_mode = false;
  454.     buf_idx = 0;
  455.  
  456.     while (!exit)
  457.     {
  458.         /* add data to buffer */
  459.         if (buf_idx < BUF_ELEMENTS)
  460.         {
  461. #if 0
  462.             bat[buf_idx].secs = (*rb->current_tick - start_tick) / HZ;
  463.             bat[buf_idx].level = rb->battery_level();
  464.             bat[buf_idx].eta = rb->battery_time();
  465.             bat[buf_idx].voltage = rb->battery_voltage();
  466. #if CONFIG_CHARGING || defined(HAVE_USB_POWER)
  467.             bat[buf_idx].flags = charge_state();
  468. #endif
  469. #endif
  470.             buf_idx++;
  471. #if USING_STORAGE_CALLBACK
  472.             rb->register_storage_idle_func(flush_buffer);
  473. #endif
  474.         }
  475.        
  476.         /* What to do when the measurement buffer is full:
  477.            1) save our measurements to disk but waste some power doing so?
  478.            2) throw away measurements to save some power?
  479.            The choice made here is to save the measurements. It is quite unusual
  480.            for this to occur because it requires > 16 hours of no disk activity.
  481.          */
  482.         if (buf_idx == BUF_ELEMENTS) {
  483.             flush_buffer();
  484.         }
  485.        
  486.         /* sleep some time until next measurement */
  487.         rb->queue_wait_w_tmo(&thread_q, &ev, sleep_time);
  488.         switch (ev.id)
  489.         {
  490.             case SYS_USB_CONNECTED:
  491.                 in_usb_mode = true;
  492.                 rb->usb_acknowledge(SYS_USB_CONNECTED_ACK);
  493.                 break;
  494.             case SYS_USB_DISCONNECTED:
  495.                 in_usb_mode = false;
  496.                 break;
  497.             case SYS_POWEROFF:
  498.                 exit_reason = "power off";
  499.                 exit = true;
  500.                 break;
  501.             case EV_EXIT:
  502.                 rb->splash(HZ, "Exiting battery_bench...");
  503.                 exit_reason = "plugin exit";
  504.                 exit = true;
  505.                 break;
  506.         }
  507.     }
  508.  
  509. #if USING_STORAGE_CALLBACK
  510.     /* unregister flush callback and flush to disk */
  511.     rb->unregister_storage_idle_func(flush_buffer, true);
  512. #else
  513.     flush_buffer();
  514. #endif
  515.    
  516.     /* log end of bench and exit reason */
  517.     fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT | O_APPEND, 0666);
  518.     if (fd >= 0)
  519.     {
  520.         rb->fdprintf(fd, "--Battery bench ended, reason: %s--\n", exit_reason);
  521.         rb->close(fd);
  522.     }
  523. }
  524.  
  525.  
  526. typedef void (*plcdfunc)(int x, int y, const unsigned char *str);
  527.  
  528. static void put_centered_str(const char* str, plcdfunc putsxy, int lcd_width, int line)
  529. {
  530.     int strwdt, strhgt;
  531.     rb->lcd_getstringsize(str, &strwdt, &strhgt);
  532.     putsxy((lcd_width - strwdt)/2, line*(strhgt), str);
  533. }
  534.  
  535. enum plugin_status plugin_start(const void* parameter)
  536. {
  537.     (void)parameter;
  538.     int button, fd;
  539.     bool on = false;
  540.     start_tick = *rb->current_tick;
  541.     int i;
  542.     const char *msgs[] = { "Battery Benchmark","Check file", BATTERY_LOG,
  543.                            "for more info", BATTERY_ON_TXT, BATTERY_OFF_TXT " - quit" };
  544.     rb->lcd_clear_display();
  545.  
  546.     rb->lcd_clear_display();
  547.     rb->lcd_setfont(FONT_SYSFIXED);
  548.  
  549.     for (i = 0; i<(int)(sizeof(msgs)/sizeof(char *)); i++)
  550.         put_centered_str(msgs[i],rb->lcd_putsxy,LCD_WIDTH,i+1);
  551.     if(rb->global_settings->talk_menu)
  552.         rb->talk_id(VOICE_BAT_BENCH_KEYS, true);
  553.     rb->lcd_update();
  554.  
  555. #ifdef HAVE_REMOTE_LCD
  556.     rb->lcd_remote_clear_display();
  557.     put_centered_str(msgs[0],rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,0);
  558.     put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-2],
  559.                     rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,1);
  560.     put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-1],
  561.                     rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,2);
  562.     rb->lcd_remote_update();
  563. #endif
  564.    
  565.     do
  566.     {
  567.         button = rb->button_get(true);
  568.         switch (button)
  569.         {
  570.             case BATTERY_ON:
  571. #ifdef BATTERY_RC_ON
  572.             case BATTERY_RC_ON:
  573. #endif                        
  574.                 on = true;
  575.                 break;        
  576.             case BATTERY_OFF:
  577. #ifdef BATTERY_RC_OFF
  578.             case BATTERY_RC_OFF:
  579. #endif
  580.                 return PLUGIN_OK;
  581.  
  582.             default:
  583.                 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
  584.                     return PLUGIN_USB_CONNECTED;
  585.         }
  586.     }while(!on);
  587.    
  588.     fd = rb->open(BATTERY_LOG, O_RDONLY);
  589.     if (fd < 0)
  590.     {
  591.         fd = rb->open(BATTERY_LOG, O_RDWR | O_CREAT, 0666);
  592.         if (fd >= 0)
  593.         {
  594.             rb->fdprintf(fd,
  595.                 "# This plugin will log your battery performance in a\n"
  596.                 "# file (%s) every minute.\n"
  597.                 "# To properly test your battery:\n"
  598.                 "# 1) Select and playback an album. "
  599.                 "(Be sure to be more than the player's buffer)\n"
  600.                 "# 2) Set to repeat.\n"
  601.                 "# 3) Let the player run completely out of battery.\n"
  602.                 "# 4) Recharge and copy (or whatever you want) the txt file to "
  603.                 "your computer.\n"
  604.                 "# Now you can make graphs with the data of the battery log.\n"
  605.                 "# Do not enter another plugin during the test or else the \n"
  606.                 "# logging activity will end.\n\n"
  607.                 "# P.S: You can decide how you will make your tests.\n"
  608.                 "# Just don't open another plugin to be sure that your log "
  609.                 "will continue.\n\n",BATTERY_LOG);
  610.             rb->fdprintf(fd,
  611.                 "# Battery bench run for %s version %s\n\n"
  612.                 ,MODEL_NAME,rb->rbversion);
  613.  
  614.             rb->fdprintf(fd, "# Battery type: %d mAh      Buffer Entries: %d\n",
  615.                 rb->global_settings->battery_capacity, (int)BUF_ELEMENTS);
  616.  
  617.             rb->fdprintf(fd, "# Rockbox has been running for %02d:%02d:%02d\n",
  618.                 HMS((unsigned)start_tick/HZ));
  619.  
  620.             rb->fdprintf(fd,
  621.                 "# Time:,  Seconds:,  Level:,  Time Left:,  Voltage[mV]:"
  622. #if CONFIG_CHARGING
  623.                 ", C:"
  624. #endif
  625. #if CONFIG_CHARGING >= CHARGING_MONITOR
  626.                 ", S:"
  627. #endif
  628. #ifdef HAVE_USB_POWER
  629.                 ", U:"
  630. #endif
  631.                 "\n");
  632.             rb->close(fd);
  633.         }
  634.         else
  635.         {
  636.             rb->splash(HZ / 2, "Cannot create file!");
  637.             return PLUGIN_ERROR;
  638.         }
  639.     }
  640.     else
  641.     {
  642.         rb->close(fd);
  643.         fd = rb->open(BATTERY_LOG, O_RDWR | O_APPEND);
  644.         rb->fdprintf(fd, "\n# --File already present. Resuming Benchmark--\n");
  645.         rb->fdprintf(fd,
  646.             "# Battery bench run for %s version %s\n\n"
  647.             ,MODEL_NAME,rb->rbversion);
  648.         rb->fdprintf(fd, "# Rockbox has been running for %02d:%02d:%02d\n",
  649.             HMS((unsigned)start_tick/HZ));
  650.         rb->close(fd);
  651.     }
  652.  
  653.     rb->memset(&gThread, 0, sizeof(gThread));
  654.     void   *buf;
  655.     size_t  buf_size;
  656.     buf = rb->plugin_get_buffer(&buf_size);
  657.     ALIGN_BUFFER(buf, buf_size, sizeof(long));
  658.     rb->memset(buf, 0, buf_size);
  659.  
  660.     gThread.stacksize = buf_size;
  661.     gThread.stack = (long *) buf + buf_size; /* stack grows towards *buf */
  662.  
  663.     ALIGN_BUFFER(gThread.stack, gThread.stacksize, sizeof(long));
  664.  
  665.     if (gThread.stacksize < MIN_THREAD_STACK_SIZE)
  666.     {
  667.         rb->splash(HZ*2, "Out of memory");
  668.         gThread.id = UINT_MAX;
  669.         return PLUGIN_ERROR;
  670.     }
  671.  
  672.     rb->queue_init(&thread_q, true); /* put the thread's queue in the bcast list */
  673.     gThread.id = rb->create_thread(thread, gThread.stack,
  674.                                    gThread.stacksize, 0, "Battery Benchmark"
  675.                                    IF_PRIO(, PRIORITY_BACKGROUND)
  676.                                    IF_COP(, CPU));
  677.  
  678.     if (gThread.id == 0 || gThread.id == UINT_MAX)
  679.     {
  680.         rb->splash(HZ, "Cannot create thread!");
  681.         return PLUGIN_ERROR;
  682.     }
  683.  
  684.     rb->plugin_tsr(exit_tsr);
  685.  
  686.     return PLUGIN_OK;
  687. }
  688.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement