summaryrefslogtreecommitdiff
path: root/apps/plugins/battery_bench.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/battery_bench.c')
-rw-r--r--apps/plugins/battery_bench.c176
1 files changed, 133 insertions, 43 deletions
diff --git a/apps/plugins/battery_bench.c b/apps/plugins/battery_bench.c
index 18052324a3..3d9556d924 100644
--- a/apps/plugins/battery_bench.c
+++ b/apps/plugins/battery_bench.c
@@ -28,8 +28,11 @@ PLUGIN_HEADER
28 28
29#define EV_EXIT 1337 29#define EV_EXIT 1337
30 30
31/* seems to work with 1300, but who knows... */
32#define THREAD_STACK_SIZE DEFAULT_STACK_SIZE + 0x200
33
31#if CONFIG_KEYPAD == RECORDER_PAD 34#if CONFIG_KEYPAD == RECORDER_PAD
32#define BATTERY_ON BUTTON_ON 35#define BATTERY_ON BUTTON_PLAY
33#define BATTERY_OFF BUTTON_OFF 36#define BATTERY_OFF BUTTON_OFF
34 37
35#elif CONFIG_KEYPAD == ONDIO_PAD 38#elif CONFIG_KEYPAD == ONDIO_PAD
@@ -62,12 +65,13 @@ PLUGIN_HEADER
62#endif 65#endif
63 66
64 67
65/***************** Plugin Entry Point *****************/ 68/****************************** Plugin Entry Point ****************************/
66static struct plugin_api* rb; 69static struct plugin_api* rb;
67int main(void); 70int main(void);
68void exit_tsr(void); 71void exit_tsr(void);
69void thread(void); 72void thread(void);
70 73
74
71enum plugin_status plugin_start(struct plugin_api* api, void* parameter) 75enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
72{ 76{
73 (void)parameter; 77 (void)parameter;
@@ -87,7 +91,7 @@ struct batt_info
87{ 91{
88 int ticks, level, eta; 92 int ticks, level, eta;
89 unsigned int voltage; 93 unsigned int voltage;
90}; 94} bat[BUF_SIZE/sizeof(struct batt_info)];
91 95
92struct event_queue thread_q; 96struct event_queue thread_q;
93 97
@@ -100,18 +104,49 @@ void exit_tsr(void)
100 rb->queue_delete(&thread_q); 104 rb->queue_delete(&thread_q);
101} 105}
102 106
107#define BIT_CHARGER 0x1000
108#define BIT_CHARGING 0x2000
109#define BIT_USB_POWER 0x4000
110
103#define HMS(x) (x)/3600,((x)%3600)/60,((x)%3600)%60 111#define HMS(x) (x)/3600,((x)%3600)/60,((x)%3600)%60
112
113/* use long for aligning */
114unsigned long thread_stack[THREAD_STACK_SIZE/sizeof(long)];
115
116#if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
117unsigned int charge_state(void)
118{
119 unsigned int ret = 0;
120#ifdef HAVE_CHARGING
121 if(rb->charger_inserted())
122 ret = BIT_CHARGER;
123#ifdef HAVE_CHARGE_STATE
124 if(rb->charging_state())
125 ret |= BIT_CHARGING;
126#endif
127#endif
128#ifdef HAVE_USB_POWER
129 if(rb->usb_powered())
130 ret |= BIT_USB_POWER;
131#endif
132 return ret;
133}
134#endif
135
104void thread(void) 136void thread(void)
105{ 137{
106 bool got_info = false, timeflag = false, in_usb_mode = false; 138 bool got_info = false, timeflag = false, in_usb_mode = false;
107 int fd, buffelements, tick = 1, i = 0, skipped = 0, exit = 0; 139 int fd, buffelements, tick = 1, i = 0, skipped = 0, exit = 0;
108 int fst = 0, lst = 0; /* first and last skipped tick */ 140 int fst = 0, lst = 0; /* first and last skipped tick */
109 unsigned int last_voltage = 0; 141 unsigned int last_voltage = 0;
142#if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
143 unsigned int last_state = 0;
144#endif
110 long sleep_time; 145 long sleep_time;
111 146
112 struct event ev; 147 struct event ev;
113 148
114 struct batt_info bat[buffelements = (BUF_SIZE / sizeof(struct batt_info))]; 149 buffelements = sizeof(bat)/sizeof(struct batt_info);
115 150
116 sleep_time = (rb->global_settings->disk_spindown > 1) ? 151 sleep_time = (rb->global_settings->disk_spindown > 1) ?
117 (rb->global_settings->disk_spindown - 1) * HZ : 5 * HZ; 152 (rb->global_settings->disk_spindown - 1) * HZ : 5 * HZ;
@@ -151,10 +186,39 @@ void thread(void)
151 secs = bat[j].ticks/HZ; 186 secs = bat[j].ticks/HZ;
152 rb->fdprintf(fd, 187 rb->fdprintf(fd,
153 "%02d:%02d:%02d, %05d, %03d%%, " 188 "%02d:%02d:%02d, %05d, %03d%%, "
154 "%02d:%02d, %04d, %04d\n", 189 "%02d:%02d, %04d, %04d"
190#ifdef HAVE_CHARGING
191 ", %c"
192#ifdef HAVE_CHARGE_STATE
193 ", %c"
194#endif
195#endif
196#ifdef HAVE_USB_POWER
197 ", %c"
198#endif
199 "\n",
200
155 HMS(secs), secs, bat[j].level, 201 HMS(secs), secs, bat[j].level,
156 bat[j].eta / 60, bat[j].eta % 60, 202 bat[j].eta / 60, bat[j].eta % 60,
157 bat[j].voltage * 10, temp + 1 + (j-i)); 203#if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
204 (bat[j].voltage &
205 (~(BIT_CHARGER|BIT_CHARGING|BIT_USB_POWER)))
206 *10,
207#else
208 bat[j].voltage * 10,
209#endif
210 temp + 1 + (j-i)
211#ifdef HAVE_CHARGING
212 ,(bat[j].voltage & BIT_CHARGER)?'A':'-'
213#ifdef HAVE_CHARGE_STATE
214 ,(bat[j].voltage & BIT_CHARGING)?'C':'-'
215#endif
216#endif
217#ifdef HAVE_USB_POWER
218 ,(bat[j].voltage & BIT_USB_POWER)?'U':'-'
219#endif
220
221 );
158 if(!j % 100 && !j) /* yield() at every 100 writes */ 222 if(!j % 100 && !j) /* yield() at every 100 writes */
159 rb->yield(); 223 rb->yield();
160 } 224 }
@@ -170,6 +234,7 @@ void thread(void)
170 } 234 }
171 else 235 else
172 { 236 {
237 unsigned int current_voltage;
173 if( 238 if(
174#if CONFIG_CODEC == SWCODEC 239#if CONFIG_CODEC == SWCODEC
175 !rb->pcm_is_playing() 240 !rb->pcm_is_playing()
@@ -179,7 +244,11 @@ void thread(void)
179 && (*rb->current_tick - tick) > DISK_SPINDOWN_TIMEOUT * HZ) 244 && (*rb->current_tick - tick) > DISK_SPINDOWN_TIMEOUT * HZ)
180 timeflag = true; 245 timeflag = true;
181 246
182 if(last_voltage != rb->battery_voltage()) 247 if(last_voltage != (current_voltage=rb->battery_voltage())
248#if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
249 || last_state != charge_state()
250#endif
251 )
183 { 252 {
184 if(i == buffelements) 253 if(i == buffelements)
185 { 254 {
@@ -195,7 +264,11 @@ void thread(void)
195 bat[i].ticks = *rb->current_tick; 264 bat[i].ticks = *rb->current_tick;
196 bat[i].level = rb->battery_level(); 265 bat[i].level = rb->battery_level();
197 bat[i].eta = rb->battery_time(); 266 bat[i].eta = rb->battery_time();
198 last_voltage = bat[i++].voltage = rb->battery_voltage(); 267 last_voltage = bat[i].voltage = current_voltage;
268#if defined(HAVE_CHARGING) || defined(HAVE_USB_POWER)
269 bat[i].voltage |= last_state = charge_state();
270#endif
271 i++;
199 got_info = true; 272 got_info = true;
200 } 273 }
201 274
@@ -204,7 +277,12 @@ void thread(void)
204 if(exit) 277 if(exit)
205 { 278 {
206 if(exit == 2) 279 if(exit == 2)
207 rb->splash(HZ,true,"Exiting battery_bench..."); 280 rb->splash(HZ,true,
281#ifdef HAVE_LCD_BITMAP
282 "Exiting battery_bench...");
283#else
284 "bench exit");
285#endif
208 s_thread.ended = true; 286 s_thread.ended = true;
209 rb->remove_thread(s_thread.id); 287 rb->remove_thread(s_thread.id);
210 rb->yield(); /* exit the thread, this yield() won't return */ 288 rb->yield(); /* exit the thread, this yield() won't return */
@@ -232,37 +310,50 @@ void thread(void)
232 310
233} 311}
234 312
313
314#ifdef HAVE_LCD_BITMAP
315typedef void (*plcdfunc)(int x, int y, const unsigned char *str);
316
317void put_centered_str(const char* str, plcdfunc putsxy, int lcd_width, int line)
318{
319 int strwdt, strhgt;
320 rb->lcd_getstringsize(str, &strwdt, &strhgt);
321 putsxy((lcd_width - strwdt)/2, line*(strhgt), str);
322}
323#endif
324
235int main(void) 325int main(void)
236{ 326{
237 int stacksize, button, fd; 327 int button, fd;
238 bool on = false; 328 bool on = false;
239 void* stack; 329 const char *msgs[] = { "Battery Benchmark","Check file", BATTERY_LOG,
240 330 "for more info", "PLAY - start", "OFF - quit" };
241 331
242 rb->lcd_clear_display(); 332 rb->lcd_clear_display();
243 333
244#ifdef HAVE_LCD_BITMAP 334#ifdef HAVE_LCD_BITMAP
245 int strwdt, strhgt; 335 int i;
246 336
337 rb->lcd_clear_display();
247 rb->lcd_setfont(FONT_SYSFIXED); 338 rb->lcd_setfont(FONT_SYSFIXED);
248 339
249 rb->lcd_getstringsize("Battery Benchmark", &strwdt, &strhgt); 340 for(i = 0; i<(int)(sizeof(msgs)/sizeof(char *)); i++)
250 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt, "Battery Benchmark"); 341 put_centered_str(msgs[i],rb->lcd_putsxy,LCD_WIDTH,i+1);
251
252 rb->lcd_getstringsize("Check /battery_bench.txt", &strwdt, &strhgt);
253 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2,strhgt * 3,
254 "Check /battery_bench.txt");
255 rb->lcd_getstringsize("file for more info.", &strwdt, &strhgt);
256 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 4, "file for more info.");
257 rb->lcd_getstringsize("Play to start, OFF to quit", &strwdt, &strhgt);
258 rb->lcd_putsxy((LCD_WIDTH - strwdt)/2, strhgt * 5,
259 "PLAY to start, OFF to quit");
260 342
261 rb->lcd_update(); 343 rb->lcd_update();
262 344#ifdef HAVE_REMOTE_LCD
345 rb->lcd_remote_clear_display();
346 put_centered_str(msgs[0],rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,0);
347 put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-2],
348 rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,1);
349 put_centered_str(msgs[sizeof(msgs)/sizeof(char*)-1],
350 rb->lcd_remote_putsxy,LCD_REMOTE_WIDTH,2);
351 rb->lcd_remote_update();
352#endif
353
263#else 354#else
264 rb->lcd_puts_scroll(0, 1, "Battery Benchmark"); 355 rb->lcd_puts_scroll(0, 1, "Batt.Bench.");
265 rb->lcd_puts_scroll(0, 2, "PLAY to start, OFF to quit"); 356 rb->lcd_puts_scroll(0, 2, "PLAY/STOP");
266#endif 357#endif
267 358
268 do 359 do
@@ -287,17 +378,6 @@ int main(void)
287 } 378 }
288 }while(!on); 379 }while(!on);
289 380
290 stack = rb->plugin_get_buffer(&stacksize);
291 /* long align it and leave some space (200bytes) for vars */
292 stack = (void*)(((unsigned int)stack + 200) & ~3);
293
294 stacksize = (stacksize - 200) & ~3;
295 if (stacksize < BUF_SIZE)
296 {
297 rb->splash(HZ*2, true, "Out of memory");
298 return PLUGIN_ERROR;
299 }
300
301 fd = rb->open(BATTERY_LOG, O_RDONLY); 381 fd = rb->open(BATTERY_LOG, O_RDONLY);
302 if(fd < 0) 382 if(fd < 0)
303 { 383 {
@@ -324,7 +404,17 @@ int main(void)
324 "data was logged in the buffer between Disk Activity.\n\n" 404 "data was logged in the buffer between Disk Activity.\n\n"
325 "Battery type: %d mAh Buffer Entries: %d\n" 405 "Battery type: %d mAh Buffer Entries: %d\n"
326 " Time:, Seconds:, Level:, Time Left:, Voltage[mV]:," 406 " Time:, Seconds:, Level:, Time Left:, Voltage[mV]:,"
327 " M/DA:\n" 407 " M/DA:"
408#ifdef HAVE_CHARGING
409 ", C:"
410#endif
411#ifdef HAVE_CHARGE_STATE
412 ", S:"
413#endif
414#ifdef HAVE_USB_POWER
415 ", U:"
416#endif
417 "\n"
328 ,BATTERY_LOG,rb->global_settings->battery_capacity, 418 ,BATTERY_LOG,rb->global_settings->battery_capacity,
329 BUF_SIZE / sizeof(struct batt_info)); 419 BUF_SIZE / sizeof(struct batt_info));
330 rb->close(fd); 420 rb->close(fd);
@@ -339,14 +429,14 @@ int main(void)
339 { 429 {
340 rb->close(fd); 430 rb->close(fd);
341 fd = rb->open(BATTERY_LOG, O_RDWR | O_APPEND); 431 fd = rb->open(BATTERY_LOG, O_RDWR | O_APPEND);
342 rb->fdprintf(fd, "\nFile already present. Resuming Benchmark\n"); 432 rb->fdprintf(fd, "\n--File already present. Resuming Benchmark--\n");
343 rb->close(fd); 433 rb->close(fd);
344 } 434 }
345 435
346 rb->queue_init(&thread_q); /* put the thread's queue in the bcast list */ 436 rb->queue_init(&thread_q); /* put the thread's queue in the bcast list */
347 rb->memset(&s_thread, 0, sizeof(s_thread)); /* zero the struct */ 437 rb->memset(&s_thread, 0, sizeof(s_thread)); /* zero the struct */
348 s_thread.id = rb->create_thread(thread, stack, 438 s_thread.id = rb->create_thread(thread, thread_stack,
349 stacksize, "Battery Benchmark"); 439 sizeof(thread_stack), "Battery Benchmark");
350 rb->plugin_tsr(exit_tsr); 440 rb->plugin_tsr(exit_tsr);
351 441
352 return PLUGIN_OK; 442 return PLUGIN_OK;