Fix red in bootloaders
[maemo-rb.git] / apps / plugins / stopwatch.c
blob4bacf413fad9ae2b57c3123d4b88317e22f732be
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 == COWOND2_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 == ONDAVX747_PAD
198 #define STOPWATCH_QUIT BUTTON_POWER
200 #else
201 #error No keymap defined!
202 #endif
204 #ifdef HAVE_TOUCHSCREEN
205 #ifndef STOPWATCH_QUIT
206 #define STOPWATCH_QUIT BUTTON_TOPLEFT
207 #endif
208 #ifndef STOPWATCH_START_STOP
209 #define STOPWATCH_START_STOP BUTTON_CENTER
210 #endif
211 #ifndef STOPWATCH_RESET_TIMER
212 #define STOPWATCH_RESET_TIMER BUTTON_MIDRIGHT
213 #endif
214 #ifndef STOPWATCH_LAP_TIMER
215 #define STOPWATCH_LAP_TIMER BUTTON_MIDLEFT
216 #endif
217 #ifndef STOPWATCH_SCROLL_UP
218 #define STOPWATCH_SCROLL_UP BUTTON_TOPMIDDLE
219 #endif
220 #ifndef STOPWATCH_SCROLL_DOWN
221 #define STOPWATCH_SCROLL_DOWN BUTTON_BOTTOMMIDDLE
222 #endif
223 #endif
225 static int stopwatch = 0;
226 static long start_at = 0;
227 static int prev_total = 0;
228 static bool counting = false;
229 static int curr_lap = 0;
230 static int lap_scroll = 0;
231 static int lap_start;
232 static int lap_times[MAX_LAPS];
234 static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
236 int hours, minutes, seconds, cs;
238 hours = ticks / (HZ * 3600);
239 ticks -= (HZ * hours * 3600);
240 minutes = ticks / (HZ * 60);
241 ticks -= (HZ * minutes * 60);
242 seconds = ticks / HZ;
243 ticks -= (HZ * seconds);
244 cs = ticks;
245 if (!lap)
247 rb->snprintf(buf, buflen,
248 "%2d:%02d:%02d.%02d",
249 hours, minutes, seconds, cs);
251 else
254 if (lap > 1)
256 int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
257 last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
258 last_hours = last_ticks / (HZ * 3600);
259 last_ticks -= (HZ * last_hours * 3600);
260 last_minutes = last_ticks / (HZ * 60);
261 last_ticks -= (HZ * last_minutes * 60);
262 last_seconds = last_ticks / HZ;
263 last_ticks -= (HZ * last_seconds);
264 last_cs = last_ticks;
266 rb->snprintf(buf, buflen,
267 "%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
268 lap, hours, minutes, seconds, cs, last_hours,
269 last_minutes, last_seconds, last_cs);
271 else
273 rb->snprintf(buf, buflen,
274 "%2d %2d:%02d:%02d.%02d",
275 lap, hours, minutes, seconds, cs);
281 * Load saved stopwatch state, if exists.
283 void load_stopwatch(void)
285 int fd;
287 fd = rb->open(STOPWATCH_FILE, O_RDONLY);
289 if (fd < 0)
291 return;
294 /* variable stopwatch isn't saved/loaded, because it is only used
295 * temporarily in main loop
298 rb->read(fd, &start_at, sizeof(start_at));
299 rb->read(fd, &prev_total, sizeof(prev_total));
300 rb->read(fd, &counting, sizeof(counting));
301 rb->read(fd, &curr_lap, sizeof(curr_lap));
302 rb->read(fd, &lap_scroll, sizeof(lap_scroll));
303 rb->read(fd, &lap_start, sizeof(lap_start));
304 rb->read(fd, lap_times, sizeof(lap_times));
306 if (counting && start_at > *rb->current_tick)
308 /* Stopwatch started in the future? Unlikely; probably started on a
309 * previous session and powered off in-between. We'll keep
310 * everything intact (user can clear manually) but stop the
311 * stopwatch to avoid negative timing.
313 start_at = 0;
314 counting = false;
317 rb->close(fd);
321 * Save stopwatch state.
323 void save_stopwatch(void)
325 int fd;
327 fd = rb->open(STOPWATCH_FILE, O_CREAT|O_WRONLY|O_TRUNC);
329 if (fd < 0)
331 return;
334 /* variable stopwatch isn't saved/loaded, because it is only used
335 * temporarily in main loop
338 rb->write(fd, &start_at, sizeof(start_at));
339 rb->write(fd, &prev_total, sizeof(prev_total));
340 rb->write(fd, &counting, sizeof(counting));
341 rb->write(fd, &curr_lap, sizeof(curr_lap));
342 rb->write(fd, &lap_scroll, sizeof(lap_scroll));
343 rb->write(fd, &lap_start, sizeof(lap_start));
344 rb->write(fd, lap_times, sizeof(lap_times));
346 rb->close(fd);
349 enum plugin_status plugin_start(const void* parameter)
351 char buf[32];
352 int button;
353 int lap;
354 int done = false;
355 bool update_lap = true;
356 int lines;
358 (void)parameter;
360 #ifdef HAVE_LCD_BITMAP
361 int h;
362 rb->lcd_setfont(FONT_UI);
363 rb->lcd_getstringsize("M", NULL, &h);
364 lines = (LCD_HEIGHT / h) - (LAP_Y);
365 #else
366 lines = 1;
367 #endif
369 load_stopwatch();
371 rb->lcd_clear_display();
373 while (!done)
375 if (counting)
377 stopwatch = prev_total + *rb->current_tick - start_at;
379 else
381 stopwatch = prev_total;
384 ticks_to_string(stopwatch,0,32,buf);
385 rb->lcd_puts(0, TIMER_Y, buf);
387 if(update_lap)
389 lap_start = curr_lap - lap_scroll;
390 for (lap = lap_start; lap > lap_start - lines; lap--)
392 if (lap > 0)
394 ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
395 rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf);
397 else
399 rb->lcd_puts(0, LAP_Y + lap_start - lap,
400 " ");
403 update_lap = false;
406 rb->lcd_update();
408 if (! counting)
410 button = rb->button_get(true);
412 else
414 button = rb->button_get_w_tmo(10);
416 /* Make sure that the jukebox isn't powered off
417 automatically */
418 rb->reset_poweroff_timer();
420 switch (button)
423 /* exit */
424 #ifdef STOPWATCH_RC_QUIT
425 case STOPWATCH_RC_QUIT:
426 #endif
427 case STOPWATCH_QUIT:
428 save_stopwatch();
429 done = true;
430 break;
432 /* Stop/Start toggle */
433 case STOPWATCH_START_STOP:
434 counting = ! counting;
435 if (counting)
437 start_at = *rb->current_tick;
438 stopwatch = prev_total + *rb->current_tick - start_at;
440 else
442 prev_total += *rb->current_tick - start_at;
443 stopwatch = prev_total;
445 break;
447 /* Reset timer */
448 case STOPWATCH_RESET_TIMER:
449 if (!counting)
451 prev_total = 0;
452 curr_lap = 0;
453 update_lap = true;
455 break;
457 /* Lap timer */
458 case STOPWATCH_LAP_TIMER:
459 lap_times[curr_lap%MAX_LAPS] = stopwatch;
460 curr_lap++;
461 update_lap = true;
462 break;
464 /* Scroll Lap timer up */
465 case STOPWATCH_SCROLL_UP:
466 if (lap_scroll > 0)
468 lap_scroll --;
469 update_lap = true;
471 break;
473 /* Scroll Lap timer down */
474 case STOPWATCH_SCROLL_DOWN:
475 if ((lap_scroll < curr_lap - lines) &&
476 (lap_scroll < (MAX_LAPS - lines)) )
478 lap_scroll ++;
479 update_lap = true;
481 break;
483 default:
484 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
485 return PLUGIN_USB_CONNECTED;
486 break;
489 return PLUGIN_OK;