Build doom on clipv2 and clip+
[kugel-rb.git] / apps / plugins / stopwatch.c
blobb09dcdb030120682bf365e225f144041c628b51c
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 PLUGIN_APPS_DIR "/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_HOME|BUTTON_REPEAT)
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
144 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
145 #define STOPWATCH_QUIT BUTTON_BACK
146 #define STOPWATCH_START_STOP BUTTON_PLAY
147 #define STOPWATCH_RESET_TIMER BUTTON_MENU
148 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
149 #define STOPWATCH_SCROLL_UP BUTTON_UP
150 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
152 #elif CONFIG_KEYPAD == MROBE100_PAD
153 #define STOPWATCH_QUIT BUTTON_POWER
154 #define STOPWATCH_START_STOP BUTTON_SELECT
155 #define STOPWATCH_RESET_TIMER BUTTON_DISPLAY
156 #define STOPWATCH_LAP_TIMER BUTTON_MENU
157 #define STOPWATCH_SCROLL_UP BUTTON_UP
158 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
160 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
161 #define STOPWATCH_QUIT BUTTON_RC_REC
162 #define STOPWATCH_START_STOP BUTTON_RC_PLAY
163 #define STOPWATCH_RESET_TIMER BUTTON_RC_REW
164 #define STOPWATCH_LAP_TIMER BUTTON_RC_FF
165 #define STOPWATCH_SCROLL_UP BUTTON_RC_VOL_UP
166 #define STOPWATCH_SCROLL_DOWN BUTTON_RC_VOL_DOWN
167 #define STOPWATCH_RC_QUIT BUTTON_REC
169 #elif CONFIG_KEYPAD == COWON_D2_PAD
170 #define STOPWATCH_QUIT BUTTON_POWER
172 #elif CONFIG_KEYPAD == IAUDIO67_PAD
173 #define STOPWATCH_QUIT BUTTON_MENU
174 #define STOPWATCH_START_STOP BUTTON_PLAY
175 #define STOPWATCH_RESET_TIMER BUTTON_STOP
176 #define STOPWATCH_LAP_TIMER BUTTON_LEFT
177 #define STOPWATCH_SCROLL_UP BUTTON_VOLUP
178 #define STOPWATCH_SCROLL_DOWN BUTTON_VOLDOWN
179 #define STOPWATCH_RC_QUIT BUTTON_POWER
181 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
182 #define STOPWATCH_QUIT BUTTON_BACK
183 #define STOPWATCH_START_STOP BUTTON_PLAY
184 #define STOPWATCH_RESET_TIMER BUTTON_SELECT
185 #define STOPWATCH_LAP_TIMER BUTTON_CUSTOM
186 #define STOPWATCH_SCROLL_UP BUTTON_UP
187 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
189 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
190 #define STOPWATCH_QUIT BUTTON_POWER
191 #define STOPWATCH_START_STOP BUTTON_SELECT
192 #define STOPWATCH_RESET_TIMER BUTTON_MENU
193 #define STOPWATCH_LAP_TIMER BUTTON_VIEW
194 #define STOPWATCH_SCROLL_UP BUTTON_UP
195 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
197 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
198 #define STOPWATCH_QUIT BUTTON_POWER
199 #define STOPWATCH_START_STOP BUTTON_PLAY
200 #define STOPWATCH_RESET_TIMER BUTTON_MENU
201 #define STOPWATCH_LAP_TIMER BUTTON_RIGHT
202 #define STOPWATCH_SCROLL_UP BUTTON_UP
203 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
205 #elif CONFIG_KEYPAD == ONDAVX747_PAD
206 #define STOPWATCH_QUIT BUTTON_POWER
207 #elif CONFIG_KEYPAD == ONDAVX777_PAD
208 #define STOPWATCH_QUIT BUTTON_POWER
210 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
211 #define STOPWATCH_QUIT BUTTON_REC
212 #define STOPWATCH_START_STOP BUTTON_PLAY
213 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
214 #define STOPWATCH_LAP_TIMER BUTTON_RIGHT
215 #define STOPWATCH_SCROLL_UP BUTTON_UP
216 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
218 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
219 #define STOPWATCH_QUIT BUTTON_REC
220 #define STOPWATCH_START_STOP BUTTON_PLAY
221 #define STOPWATCH_RESET_TIMER BUTTON_OK
222 #define STOPWATCH_LAP_TIMER BUTTON_MENU
223 #define STOPWATCH_SCROLL_UP BUTTON_UP
224 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
226 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
227 #define STOPWATCH_QUIT (BUTTON_REC|BUTTON_PLAY)
228 #define STOPWATCH_START_STOP BUTTON_PLAY
229 #define STOPWATCH_RESET_TIMER BUTTON_PREV
230 #define STOPWATCH_LAP_TIMER BUTTON_NEXT
231 #define STOPWATCH_SCROLL_UP BUTTON_VOL_UP
232 #define STOPWATCH_SCROLL_DOWN BUTTON_VOL_DOWN
234 #else
235 #error No keymap defined!
236 #endif
238 #ifdef HAVE_TOUCHSCREEN
239 #ifndef STOPWATCH_QUIT
240 #define STOPWATCH_QUIT BUTTON_TOPLEFT
241 #endif
242 #ifndef STOPWATCH_START_STOP
243 #define STOPWATCH_START_STOP BUTTON_CENTER
244 #endif
245 #ifndef STOPWATCH_RESET_TIMER
246 #define STOPWATCH_RESET_TIMER BUTTON_MIDRIGHT
247 #endif
248 #ifndef STOPWATCH_LAP_TIMER
249 #define STOPWATCH_LAP_TIMER BUTTON_MIDLEFT
250 #endif
251 #ifndef STOPWATCH_SCROLL_UP
252 #define STOPWATCH_SCROLL_UP BUTTON_TOPMIDDLE
253 #endif
254 #ifndef STOPWATCH_SCROLL_DOWN
255 #define STOPWATCH_SCROLL_DOWN BUTTON_BOTTOMMIDDLE
256 #endif
257 #endif
259 static int stopwatch = 0;
260 static long start_at = 0;
261 static int prev_total = 0;
262 static bool counting = false;
263 static int curr_lap = 0;
264 static int lap_scroll = 0;
265 static int lap_start;
266 static int lap_times[MAX_LAPS];
268 static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
270 int hours, minutes, seconds, cs;
272 hours = ticks / (HZ * 3600);
273 ticks -= (HZ * hours * 3600);
274 minutes = ticks / (HZ * 60);
275 ticks -= (HZ * minutes * 60);
276 seconds = ticks / HZ;
277 ticks -= (HZ * seconds);
278 cs = ticks;
279 if (!lap)
281 rb->snprintf(buf, buflen,
282 "%2d:%02d:%02d.%02d",
283 hours, minutes, seconds, cs);
285 else
288 if (lap > 1)
290 int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
291 last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
292 last_hours = last_ticks / (HZ * 3600);
293 last_ticks -= (HZ * last_hours * 3600);
294 last_minutes = last_ticks / (HZ * 60);
295 last_ticks -= (HZ * last_minutes * 60);
296 last_seconds = last_ticks / HZ;
297 last_ticks -= (HZ * last_seconds);
298 last_cs = last_ticks;
300 rb->snprintf(buf, buflen,
301 "%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
302 lap, hours, minutes, seconds, cs, last_hours,
303 last_minutes, last_seconds, last_cs);
305 else
307 rb->snprintf(buf, buflen,
308 "%2d %2d:%02d:%02d.%02d",
309 lap, hours, minutes, seconds, cs);
315 * Load saved stopwatch state, if exists.
317 void load_stopwatch(void)
319 int fd;
321 fd = rb->open(STOPWATCH_FILE, O_RDONLY);
323 if (fd < 0)
325 return;
328 /* variable stopwatch isn't saved/loaded, because it is only used
329 * temporarily in main loop
332 rb->read(fd, &start_at, sizeof(start_at));
333 rb->read(fd, &prev_total, sizeof(prev_total));
334 rb->read(fd, &counting, sizeof(counting));
335 rb->read(fd, &curr_lap, sizeof(curr_lap));
336 rb->read(fd, &lap_scroll, sizeof(lap_scroll));
337 rb->read(fd, &lap_start, sizeof(lap_start));
338 rb->read(fd, lap_times, sizeof(lap_times));
340 if (counting && start_at > *rb->current_tick)
342 /* Stopwatch started in the future? Unlikely; probably started on a
343 * previous session and powered off in-between. We'll keep
344 * everything intact (user can clear manually) but stop the
345 * stopwatch to avoid negative timing.
347 start_at = 0;
348 counting = false;
351 rb->close(fd);
355 * Save stopwatch state.
357 void save_stopwatch(void)
359 int fd;
361 fd = rb->open(STOPWATCH_FILE, O_CREAT|O_WRONLY|O_TRUNC, 0666);
363 if (fd < 0)
365 return;
368 /* variable stopwatch isn't saved/loaded, because it is only used
369 * temporarily in main loop
372 rb->write(fd, &start_at, sizeof(start_at));
373 rb->write(fd, &prev_total, sizeof(prev_total));
374 rb->write(fd, &counting, sizeof(counting));
375 rb->write(fd, &curr_lap, sizeof(curr_lap));
376 rb->write(fd, &lap_scroll, sizeof(lap_scroll));
377 rb->write(fd, &lap_start, sizeof(lap_start));
378 rb->write(fd, lap_times, sizeof(lap_times));
380 rb->close(fd);
383 enum plugin_status plugin_start(const void* parameter)
385 char buf[32];
386 int button;
387 int lap;
388 int done = false;
389 bool update_lap = true;
390 int lines;
392 (void)parameter;
394 #ifdef HAVE_LCD_BITMAP
395 int h;
396 rb->lcd_setfont(FONT_UI);
397 rb->lcd_getstringsize("M", NULL, &h);
398 lines = (LCD_HEIGHT / h) - (LAP_Y);
399 #else
400 lines = 1;
401 #endif
403 load_stopwatch();
405 rb->lcd_clear_display();
407 while (!done)
409 if (counting)
411 stopwatch = prev_total + *rb->current_tick - start_at;
413 else
415 stopwatch = prev_total;
418 ticks_to_string(stopwatch,0,32,buf);
419 rb->lcd_puts(0, TIMER_Y, buf);
421 if(update_lap)
423 lap_start = curr_lap - lap_scroll;
424 for (lap = lap_start; lap > lap_start - lines; lap--)
426 if (lap > 0)
428 ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
429 rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf);
431 else
433 rb->lcd_puts(0, LAP_Y + lap_start - lap,
434 " ");
437 update_lap = false;
440 rb->lcd_update();
442 if (! counting)
444 button = rb->button_get(true);
446 else
448 button = rb->button_get_w_tmo(10);
450 /* Make sure that the jukebox isn't powered off
451 automatically */
452 rb->reset_poweroff_timer();
454 switch (button)
457 /* exit */
458 #ifdef STOPWATCH_RC_QUIT
459 case STOPWATCH_RC_QUIT:
460 #endif
461 case STOPWATCH_QUIT:
462 save_stopwatch();
463 done = true;
464 break;
466 /* Stop/Start toggle */
467 case STOPWATCH_START_STOP:
468 counting = ! counting;
469 if (counting)
471 start_at = *rb->current_tick;
472 stopwatch = prev_total + *rb->current_tick - start_at;
474 else
476 prev_total += *rb->current_tick - start_at;
477 stopwatch = prev_total;
479 break;
481 /* Reset timer */
482 case STOPWATCH_RESET_TIMER:
483 if (!counting)
485 prev_total = 0;
486 curr_lap = 0;
487 update_lap = true;
489 break;
491 /* Lap timer */
492 case STOPWATCH_LAP_TIMER:
493 lap_times[curr_lap%MAX_LAPS] = stopwatch;
494 curr_lap++;
495 update_lap = true;
496 break;
498 /* Scroll Lap timer up */
499 case STOPWATCH_SCROLL_UP:
500 if (lap_scroll > 0)
502 lap_scroll --;
503 update_lap = true;
505 break;
507 /* Scroll Lap timer down */
508 case STOPWATCH_SCROLL_DOWN:
509 if ((lap_scroll < curr_lap - lines) &&
510 (lap_scroll < (MAX_LAPS - lines)) )
512 lap_scroll ++;
513 update_lap = true;
515 break;
517 default:
518 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
519 return PLUGIN_USB_CONNECTED;
520 break;
523 return PLUGIN_OK;