Rename variables sectorbuf and verbose to avoid clashes in rbutil. Cleanup exports...
[Rockbox.git] / apps / plugins / stopwatch.c
blobece19b1e82faeb33aa3fa6ab0265eb2fae4eb7b9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Mike Holden
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
22 PLUGIN_HEADER
24 #ifdef HAVE_LCD_BITMAP
25 #define TIMER_Y 1
26 #else
27 #define TIMER_Y 0
28 #endif
30 #define LAP_Y TIMER_Y+1
31 #define MAX_LAPS 64
33 /* variable button definitions */
34 #if CONFIG_KEYPAD == RECORDER_PAD
35 #define STOPWATCH_QUIT BUTTON_OFF
36 #define STOPWATCH_START_STOP BUTTON_PLAY
37 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
38 #define STOPWATCH_LAP_TIMER BUTTON_ON
39 #define STOPWATCH_SCROLL_UP BUTTON_UP
40 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
41 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
42 #define STOPWATCH_QUIT BUTTON_OFF
43 #define STOPWATCH_START_STOP BUTTON_SELECT
44 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
45 #define STOPWATCH_LAP_TIMER BUTTON_ON
46 #define STOPWATCH_SCROLL_UP BUTTON_UP
47 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
48 #elif CONFIG_KEYPAD == ONDIO_PAD
49 #define STOPWATCH_QUIT BUTTON_OFF
50 #define STOPWATCH_START_STOP BUTTON_RIGHT
51 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
52 #define STOPWATCH_LAP_TIMER BUTTON_MENU
53 #define STOPWATCH_SCROLL_UP BUTTON_UP
54 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
55 #elif CONFIG_KEYPAD == PLAYER_PAD
56 #define STOPWATCH_QUIT BUTTON_MENU
57 #define STOPWATCH_START_STOP BUTTON_PLAY
58 #define STOPWATCH_RESET_TIMER BUTTON_STOP
59 #define STOPWATCH_LAP_TIMER BUTTON_ON
60 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
61 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
62 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
63 (CONFIG_KEYPAD == IRIVER_H300_PAD)
64 #define STOPWATCH_QUIT BUTTON_OFF
65 #define STOPWATCH_START_STOP BUTTON_SELECT
66 #define STOPWATCH_RESET_TIMER BUTTON_DOWN
67 #define STOPWATCH_LAP_TIMER BUTTON_ON
68 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
69 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
71 #define STOPWATCH_RC_QUIT BUTTON_RC_STOP
73 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
74 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
75 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
76 #define STOPWATCH_QUIT BUTTON_MENU
77 #define STOPWATCH_START_STOP BUTTON_SELECT
78 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
79 #define STOPWATCH_LAP_TIMER BUTTON_RIGHT
80 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_FWD
81 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_BACK
82 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
83 #define STOPWATCH_QUIT BUTTON_PLAY
84 #define STOPWATCH_START_STOP BUTTON_MODE
85 #define STOPWATCH_RESET_TIMER BUTTON_EQ
86 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
87 #define STOPWATCH_SCROLL_UP BUTTON_UP
88 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
89 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
90 #define STOPWATCH_QUIT BUTTON_POWER
91 #define STOPWATCH_START_STOP BUTTON_PLAY
92 #define STOPWATCH_RESET_TIMER BUTTON_REC
93 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
94 #define STOPWATCH_SCROLL_UP BUTTON_UP
95 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
96 #elif CONFIG_KEYPAD == GIGABEAT_PAD
97 #define STOPWATCH_QUIT BUTTON_POWER
98 #define STOPWATCH_START_STOP BUTTON_SELECT
99 #define STOPWATCH_RESET_TIMER BUTTON_A
100 #define STOPWATCH_LAP_TIMER BUTTON_MENU
101 #define STOPWATCH_SCROLL_UP BUTTON_UP
102 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
103 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
104 (CONFIG_KEYPAD == SANSA_C200_PAD)
105 #define STOPWATCH_QUIT BUTTON_POWER
106 #define STOPWATCH_START_STOP BUTTON_RIGHT
107 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
108 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
109 #define STOPWATCH_SCROLL_UP BUTTON_UP
110 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
111 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
112 #define STOPWATCH_QUIT BUTTON_POWER
113 #define STOPWATCH_START_STOP BUTTON_PLAY
114 #define STOPWATCH_RESET_TIMER BUTTON_REW
115 #define STOPWATCH_LAP_TIMER BUTTON_FF
116 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_UP
117 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_DOWN
118 #elif CONFIG_KEYPAD == MROBE500_PAD
119 #define STOPWATCH_QUIT BUTTON_POWER
120 #define STOPWATCH_START_STOP BUTTON_RC_HEART
121 #define STOPWATCH_RESET_TIMER BUTTON_RC_MODE
122 #define STOPWATCH_LAP_TIMER BUTTON_RC_PLAY
123 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
124 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
125 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
126 #define STOPWATCH_QUIT BUTTON_BACK
127 #define STOPWATCH_START_STOP BUTTON_PLAY
128 #define STOPWATCH_RESET_TIMER BUTTON_MENU
129 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
130 #define STOPWATCH_SCROLL_UP BUTTON_UP
131 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
132 #elif CONFIG_KEYPAD == MROBE100_PAD
133 #define STOPWATCH_QUIT BUTTON_POWER
134 #define STOPWATCH_START_STOP BUTTON_SELECT
135 #define STOPWATCH_RESET_TIMER BUTTON_DISPLAY
136 #define STOPWATCH_LAP_TIMER BUTTON_MENU
137 #define STOPWATCH_SCROLL_UP BUTTON_UP
138 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
139 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
140 #define STOPWATCH_QUIT BUTTON_RC_REC
141 #define STOPWATCH_START_STOP BUTTON_RC_PLAY
142 #define STOPWATCH_RESET_TIMER BUTTON_RC_REW
143 #define STOPWATCH_LAP_TIMER BUTTON_RC_FF
144 #define STOPWATCH_SCROLL_UP BUTTON_RC_VOL_UP
145 #define STOPWATCH_SCROLL_DOWN BUTTON_RC_VOL_DOWN
146 #define STOPWATCH_RC_QUIT BUTTON_REC
147 #elif CONFIG_KEYPAD == COWOND2_PAD
148 #define STOPWATCH_QUIT BUTTON_POWER
149 #else
150 #error No keymap defined!
151 #endif
153 #ifdef HAVE_TOUCHPAD
154 #ifndef STOPWATCH_QUIT
155 #define STOPWATCH_QUIT BUTTON_TOPLEFT
156 #endif
157 #ifndef STOPWATCH_START_STOP
158 #define STOPWATCH_START_STOP BUTTON_CENTER
159 #endif
160 #ifndef STOPWATCH_RESET_TIMER
161 #define STOPWATCH_RESET_TIMER BUTTON_MIDRIGHT
162 #endif
163 #ifndef STOPWATCH_LAP_TIMER
164 #define STOPWATCH_LAP_TIMER BUTTON_MIDLEFT
165 #endif
166 #ifndef STOPWATCH_SCROLL_UP
167 #define STOPWATCH_SCROLL_UP BUTTON_TOPMIDDLE
168 #endif
169 #ifndef STOPWATCH_SCROLL_DOWN
170 #define STOPWATCH_SCROLL_DOWN BUTTON_BOTTOMMIDDLE
171 #endif
172 #endif
174 static const struct plugin_api* rb;
176 static int stopwatch = 0;
177 static long start_at = 0;
178 static int prev_total = 0;
179 static bool counting = false;
180 static int curr_lap = 0;
181 static int lap_scroll = 0;
182 static int lap_start;
183 static int lap_times[MAX_LAPS];
185 static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
187 int hours, minutes, seconds, cs;
189 hours = ticks / (HZ * 3600);
190 ticks -= (HZ * hours * 3600);
191 minutes = ticks / (HZ * 60);
192 ticks -= (HZ * minutes * 60);
193 seconds = ticks / HZ;
194 ticks -= (HZ * seconds);
195 cs = ticks;
196 if (!lap)
198 rb->snprintf(buf, buflen,
199 "%2d:%02d:%02d.%02d",
200 hours, minutes, seconds, cs);
202 else
205 if (lap > 1)
207 int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
208 last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
209 last_hours = last_ticks / (HZ * 3600);
210 last_ticks -= (HZ * last_hours * 3600);
211 last_minutes = last_ticks / (HZ * 60);
212 last_ticks -= (HZ * last_minutes * 60);
213 last_seconds = last_ticks / HZ;
214 last_ticks -= (HZ * last_seconds);
215 last_cs = last_ticks;
217 rb->snprintf(buf, buflen,
218 "%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
219 lap, hours, minutes, seconds, cs, last_hours,
220 last_minutes, last_seconds, last_cs);
222 else
224 rb->snprintf(buf, buflen,
225 "%2d %2d:%02d:%02d.%02d",
226 lap, hours, minutes, seconds, cs);
231 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
233 char buf[32];
234 int button;
235 int lap;
236 int done = false;
237 bool update_lap = true;
238 int lines;
240 (void)parameter;
241 rb = api;
243 #ifdef HAVE_LCD_BITMAP
244 int h;
245 rb->lcd_setfont(FONT_UI);
246 rb->lcd_getstringsize("M", NULL, &h);
247 lines = (LCD_HEIGHT / h) - (LAP_Y);
248 #else
249 lines = 1;
250 #endif
252 rb->lcd_clear_display();
254 while (!done)
256 if (counting)
258 stopwatch = prev_total + *rb->current_tick - start_at;
260 else
262 stopwatch = prev_total;
265 ticks_to_string(stopwatch,0,32,buf);
266 rb->lcd_puts(0, TIMER_Y, buf);
268 if(update_lap)
270 lap_start = curr_lap - lap_scroll;
271 for (lap = lap_start; lap > lap_start - lines; lap--)
273 if (lap > 0)
275 ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
276 rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf);
278 else
280 rb->lcd_puts(0, LAP_Y + lap_start - lap,
281 " ");
284 update_lap = false;
287 rb->lcd_update();
289 if (! counting)
291 button = rb->button_get(true);
293 else
295 button = rb->button_get_w_tmo(10);
297 /* Make sure that the jukebox isn't powered off
298 automatically */
299 rb->reset_poweroff_timer();
301 switch (button)
304 /* exit */
305 #ifdef STOPWATCH_RC_QUIT
306 case STOPWATCH_RC_QUIT:
307 #endif
308 case STOPWATCH_QUIT:
309 done = true;
310 break;
312 /* Stop/Start toggle */
313 case STOPWATCH_START_STOP:
314 counting = ! counting;
315 if (counting)
317 start_at = *rb->current_tick;
318 stopwatch = prev_total + *rb->current_tick - start_at;
320 else
322 prev_total += *rb->current_tick - start_at;
323 stopwatch = prev_total;
325 break;
327 /* Reset timer */
328 case STOPWATCH_RESET_TIMER:
329 if (!counting)
331 prev_total = 0;
332 curr_lap = 0;
333 update_lap = true;
335 break;
337 /* Lap timer */
338 case STOPWATCH_LAP_TIMER:
339 lap_times[curr_lap%MAX_LAPS] = stopwatch;
340 curr_lap++;
341 update_lap = true;
342 break;
344 /* Scroll Lap timer up */
345 case STOPWATCH_SCROLL_UP:
346 if (lap_scroll > 0)
348 lap_scroll --;
349 update_lap = true;
351 break;
353 /* Scroll Lap timer down */
354 case STOPWATCH_SCROLL_DOWN:
355 if ((lap_scroll < curr_lap - lines) &&
356 (lap_scroll < (MAX_LAPS - lines)) )
358 lap_scroll ++;
359 update_lap = true;
361 break;
363 default:
364 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
365 return PLUGIN_USB_CONNECTED;
366 break;
369 return PLUGIN_OK;