Some improvements to rocklife (FS#10087, but slightly less paranoid). Main improvemen...
[kugel-rb.git] / apps / plugins / stopwatch.c
blob4cba167738fca3789c20b7784eda973f4d431882
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Mike Holden
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
24 PLUGIN_HEADER
26 #ifdef HAVE_LCD_BITMAP
27 #define TIMER_Y 1
28 #else
29 #define TIMER_Y 0
30 #endif
32 #define LAP_Y TIMER_Y+1
33 #define MAX_LAPS 64
35 #define STOPWATCH_FILE ROCKBOX_DIR "/apps/stopwatch.dat"
37 /* variable button definitions */
38 #if CONFIG_KEYPAD == RECORDER_PAD
39 #define STOPWATCH_QUIT BUTTON_OFF
40 #define STOPWATCH_START_STOP BUTTON_PLAY
41 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
42 #define STOPWATCH_LAP_TIMER BUTTON_ON
43 #define STOPWATCH_SCROLL_UP BUTTON_UP
44 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
46 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
47 #define STOPWATCH_QUIT BUTTON_OFF
48 #define STOPWATCH_START_STOP BUTTON_SELECT
49 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
50 #define STOPWATCH_LAP_TIMER BUTTON_ON
51 #define STOPWATCH_SCROLL_UP BUTTON_UP
52 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
54 #elif CONFIG_KEYPAD == ONDIO_PAD
55 #define STOPWATCH_QUIT BUTTON_OFF
56 #define STOPWATCH_START_STOP BUTTON_RIGHT
57 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
58 #define STOPWATCH_LAP_TIMER BUTTON_MENU
59 #define STOPWATCH_SCROLL_UP BUTTON_UP
60 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
62 #elif CONFIG_KEYPAD == PLAYER_PAD
63 #define STOPWATCH_QUIT BUTTON_MENU
64 #define STOPWATCH_START_STOP BUTTON_PLAY
65 #define STOPWATCH_RESET_TIMER BUTTON_STOP
66 #define STOPWATCH_LAP_TIMER BUTTON_ON
67 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
68 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
70 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
71 (CONFIG_KEYPAD == IRIVER_H300_PAD)
72 #define STOPWATCH_QUIT BUTTON_OFF
73 #define STOPWATCH_START_STOP BUTTON_SELECT
74 #define STOPWATCH_RESET_TIMER BUTTON_DOWN
75 #define STOPWATCH_LAP_TIMER BUTTON_ON
76 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
77 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
78 #define STOPWATCH_RC_QUIT BUTTON_RC_STOP
80 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
81 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
82 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
83 #define STOPWATCH_QUIT BUTTON_MENU
84 #define STOPWATCH_START_STOP BUTTON_SELECT
85 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
86 #define STOPWATCH_LAP_TIMER BUTTON_RIGHT
87 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_FWD
88 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_BACK
90 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
91 #define STOPWATCH_QUIT BUTTON_PLAY
92 #define STOPWATCH_START_STOP BUTTON_MODE
93 #define STOPWATCH_RESET_TIMER BUTTON_EQ
94 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
95 #define STOPWATCH_SCROLL_UP BUTTON_UP
96 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
98 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
99 #define STOPWATCH_QUIT BUTTON_POWER
100 #define STOPWATCH_START_STOP BUTTON_PLAY
101 #define STOPWATCH_RESET_TIMER BUTTON_REC
102 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
103 #define STOPWATCH_SCROLL_UP BUTTON_UP
104 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
106 #elif CONFIG_KEYPAD == GIGABEAT_PAD
107 #define STOPWATCH_QUIT BUTTON_POWER
108 #define STOPWATCH_START_STOP BUTTON_SELECT
109 #define STOPWATCH_RESET_TIMER BUTTON_A
110 #define STOPWATCH_LAP_TIMER BUTTON_MENU
111 #define STOPWATCH_SCROLL_UP BUTTON_UP
112 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
114 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
115 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
116 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
117 (CONFIG_KEYPAD == SANSA_M200_PAD)
118 #define STOPWATCH_QUIT BUTTON_POWER
119 #define STOPWATCH_START_STOP BUTTON_RIGHT
120 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
121 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
122 #define STOPWATCH_SCROLL_UP BUTTON_UP
123 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
125 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
126 #define STOPWATCH_QUIT BUTTON_POWER
127 #define STOPWATCH_START_STOP BUTTON_RIGHT
128 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
129 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
130 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_BACK
131 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_FWD
133 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
134 #define STOPWATCH_QUIT BUTTON_POWER
135 #define STOPWATCH_START_STOP BUTTON_PLAY
136 #define STOPWATCH_RESET_TIMER BUTTON_REW
137 #define STOPWATCH_LAP_TIMER BUTTON_FF
138 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_UP
139 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_DOWN
141 #elif CONFIG_KEYPAD == MROBE500_PAD
142 #define STOPWATCH_QUIT BUTTON_POWER
143 #define STOPWATCH_START_STOP BUTTON_RC_HEART
144 #define STOPWATCH_RESET_TIMER BUTTON_RC_MODE
145 #define STOPWATCH_LAP_TIMER BUTTON_RC_PLAY
146 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
147 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
149 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
150 #define STOPWATCH_QUIT BUTTON_BACK
151 #define STOPWATCH_START_STOP BUTTON_PLAY
152 #define STOPWATCH_RESET_TIMER BUTTON_MENU
153 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
154 #define STOPWATCH_SCROLL_UP BUTTON_UP
155 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
157 #elif CONFIG_KEYPAD == MROBE100_PAD
158 #define STOPWATCH_QUIT BUTTON_POWER
159 #define STOPWATCH_START_STOP BUTTON_SELECT
160 #define STOPWATCH_RESET_TIMER BUTTON_DISPLAY
161 #define STOPWATCH_LAP_TIMER BUTTON_MENU
162 #define STOPWATCH_SCROLL_UP BUTTON_UP
163 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
165 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
166 #define STOPWATCH_QUIT BUTTON_RC_REC
167 #define STOPWATCH_START_STOP BUTTON_RC_PLAY
168 #define STOPWATCH_RESET_TIMER BUTTON_RC_REW
169 #define STOPWATCH_LAP_TIMER BUTTON_RC_FF
170 #define STOPWATCH_SCROLL_UP BUTTON_RC_VOL_UP
171 #define STOPWATCH_SCROLL_DOWN BUTTON_RC_VOL_DOWN
172 #define STOPWATCH_RC_QUIT BUTTON_REC
174 #elif CONFIG_KEYPAD == COWOND2_PAD
175 #define STOPWATCH_QUIT BUTTON_POWER
177 #elif CONFIG_KEYPAD == IAUDIO67_PAD
178 #define STOPWATCH_QUIT BUTTON_MENU
179 #define STOPWATCH_START_STOP BUTTON_PLAY
180 #define STOPWATCH_RESET_TIMER BUTTON_STOP
181 #define STOPWATCH_LAP_TIMER BUTTON_LEFT
182 #define STOPWATCH_SCROLL_UP BUTTON_VOLUP
183 #define STOPWATCH_SCROLL_DOWN BUTTON_VOLDOWN
184 #define STOPWATCH_RC_QUIT BUTTON_POWER
186 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
187 #define STOPWATCH_QUIT BUTTON_BACK
188 #define STOPWATCH_START_STOP BUTTON_PLAY
189 #define STOPWATCH_RESET_TIMER BUTTON_SELECT
190 #define STOPWATCH_LAP_TIMER BUTTON_CUSTOM
191 #define STOPWATCH_SCROLL_UP BUTTON_UP
192 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
194 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
195 #define STOPWATCH_QUIT BUTTON_POWER
196 #define STOPWATCH_START_STOP BUTTON_SELECT
197 #define STOPWATCH_RESET_TIMER BUTTON_MENU
198 #define STOPWATCH_LAP_TIMER BUTTON_VIEW
199 #define STOPWATCH_SCROLL_UP BUTTON_UP
200 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
202 #else
203 #error No keymap defined!
204 #endif
206 #ifdef HAVE_TOUCHSCREEN
207 #ifndef STOPWATCH_QUIT
208 #define STOPWATCH_QUIT BUTTON_TOPLEFT
209 #endif
210 #ifndef STOPWATCH_START_STOP
211 #define STOPWATCH_START_STOP BUTTON_CENTER
212 #endif
213 #ifndef STOPWATCH_RESET_TIMER
214 #define STOPWATCH_RESET_TIMER BUTTON_MIDRIGHT
215 #endif
216 #ifndef STOPWATCH_LAP_TIMER
217 #define STOPWATCH_LAP_TIMER BUTTON_MIDLEFT
218 #endif
219 #ifndef STOPWATCH_SCROLL_UP
220 #define STOPWATCH_SCROLL_UP BUTTON_TOPMIDDLE
221 #endif
222 #ifndef STOPWATCH_SCROLL_DOWN
223 #define STOPWATCH_SCROLL_DOWN BUTTON_BOTTOMMIDDLE
224 #endif
225 #endif
227 static int stopwatch = 0;
228 static long start_at = 0;
229 static int prev_total = 0;
230 static bool counting = false;
231 static int curr_lap = 0;
232 static int lap_scroll = 0;
233 static int lap_start;
234 static int lap_times[MAX_LAPS];
236 static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
238 int hours, minutes, seconds, cs;
240 hours = ticks / (HZ * 3600);
241 ticks -= (HZ * hours * 3600);
242 minutes = ticks / (HZ * 60);
243 ticks -= (HZ * minutes * 60);
244 seconds = ticks / HZ;
245 ticks -= (HZ * seconds);
246 cs = ticks;
247 if (!lap)
249 rb->snprintf(buf, buflen,
250 "%2d:%02d:%02d.%02d",
251 hours, minutes, seconds, cs);
253 else
256 if (lap > 1)
258 int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
259 last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
260 last_hours = last_ticks / (HZ * 3600);
261 last_ticks -= (HZ * last_hours * 3600);
262 last_minutes = last_ticks / (HZ * 60);
263 last_ticks -= (HZ * last_minutes * 60);
264 last_seconds = last_ticks / HZ;
265 last_ticks -= (HZ * last_seconds);
266 last_cs = last_ticks;
268 rb->snprintf(buf, buflen,
269 "%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
270 lap, hours, minutes, seconds, cs, last_hours,
271 last_minutes, last_seconds, last_cs);
273 else
275 rb->snprintf(buf, buflen,
276 "%2d %2d:%02d:%02d.%02d",
277 lap, hours, minutes, seconds, cs);
283 * Load saved stopwatch state, if exists.
285 void load_stopwatch(void)
287 int fd;
289 fd = rb->open(STOPWATCH_FILE, O_RDONLY);
291 if (fd < 0)
293 return;
296 /* variable stopwatch isn't saved/loaded, because it is only used
297 * temporarily in main loop
300 rb->read(fd, &start_at, sizeof(start_at));
301 rb->read(fd, &prev_total, sizeof(prev_total));
302 rb->read(fd, &counting, sizeof(counting));
303 rb->read(fd, &curr_lap, sizeof(curr_lap));
304 rb->read(fd, &lap_scroll, sizeof(lap_scroll));
305 rb->read(fd, &lap_start, sizeof(lap_start));
306 rb->read(fd, lap_times, sizeof(lap_times));
308 if (counting && start_at > *rb->current_tick)
310 /* Stopwatch started in the future? Unlikely; probably started on a
311 * previous session and powered off in-between. We'll keep
312 * everything intact (user can clear manually) but stop the
313 * stopwatch to avoid negative timing.
315 start_at = 0;
316 counting = false;
319 rb->close(fd);
323 * Save stopwatch state.
325 void save_stopwatch(void)
327 int fd;
329 fd = rb->open(STOPWATCH_FILE, O_CREAT|O_WRONLY|O_TRUNC);
331 if (fd < 0)
333 return;
336 /* variable stopwatch isn't saved/loaded, because it is only used
337 * temporarily in main loop
340 rb->write(fd, &start_at, sizeof(start_at));
341 rb->write(fd, &prev_total, sizeof(prev_total));
342 rb->write(fd, &counting, sizeof(counting));
343 rb->write(fd, &curr_lap, sizeof(curr_lap));
344 rb->write(fd, &lap_scroll, sizeof(lap_scroll));
345 rb->write(fd, &lap_start, sizeof(lap_start));
346 rb->write(fd, lap_times, sizeof(lap_times));
348 rb->close(fd);
351 enum plugin_status plugin_start(const void* parameter)
353 char buf[32];
354 int button;
355 int lap;
356 int done = false;
357 bool update_lap = true;
358 int lines;
360 (void)parameter;
362 #ifdef HAVE_LCD_BITMAP
363 int h;
364 rb->lcd_setfont(FONT_UI);
365 rb->lcd_getstringsize("M", NULL, &h);
366 lines = (LCD_HEIGHT / h) - (LAP_Y);
367 #else
368 lines = 1;
369 #endif
371 load_stopwatch();
373 rb->lcd_clear_display();
375 while (!done)
377 if (counting)
379 stopwatch = prev_total + *rb->current_tick - start_at;
381 else
383 stopwatch = prev_total;
386 ticks_to_string(stopwatch,0,32,buf);
387 rb->lcd_puts(0, TIMER_Y, buf);
389 if(update_lap)
391 lap_start = curr_lap - lap_scroll;
392 for (lap = lap_start; lap > lap_start - lines; lap--)
394 if (lap > 0)
396 ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
397 rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf);
399 else
401 rb->lcd_puts(0, LAP_Y + lap_start - lap,
402 " ");
405 update_lap = false;
408 rb->lcd_update();
410 if (! counting)
412 button = rb->button_get(true);
414 else
416 button = rb->button_get_w_tmo(10);
418 /* Make sure that the jukebox isn't powered off
419 automatically */
420 rb->reset_poweroff_timer();
422 switch (button)
425 /* exit */
426 #ifdef STOPWATCH_RC_QUIT
427 case STOPWATCH_RC_QUIT:
428 #endif
429 case STOPWATCH_QUIT:
430 save_stopwatch();
431 done = true;
432 break;
434 /* Stop/Start toggle */
435 case STOPWATCH_START_STOP:
436 counting = ! counting;
437 if (counting)
439 start_at = *rb->current_tick;
440 stopwatch = prev_total + *rb->current_tick - start_at;
442 else
444 prev_total += *rb->current_tick - start_at;
445 stopwatch = prev_total;
447 break;
449 /* Reset timer */
450 case STOPWATCH_RESET_TIMER:
451 if (!counting)
453 prev_total = 0;
454 curr_lap = 0;
455 update_lap = true;
457 break;
459 /* Lap timer */
460 case STOPWATCH_LAP_TIMER:
461 lap_times[curr_lap%MAX_LAPS] = stopwatch;
462 curr_lap++;
463 update_lap = true;
464 break;
466 /* Scroll Lap timer up */
467 case STOPWATCH_SCROLL_UP:
468 if (lap_scroll > 0)
470 lap_scroll --;
471 update_lap = true;
473 break;
475 /* Scroll Lap timer down */
476 case STOPWATCH_SCROLL_DOWN:
477 if ((lap_scroll < curr_lap - lines) &&
478 (lap_scroll < (MAX_LAPS - lines)) )
480 lap_scroll ++;
481 update_lap = true;
483 break;
485 default:
486 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
487 return PLUGIN_USB_CONNECTED;
488 break;
491 return PLUGIN_OK;