Update the Swedish translation.
[kugel-rb.git] / apps / plugins / stopwatch.c
blob9a66103429b1e9c7d96a8130b71e237743cfc4fc
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 #define STOPWATCH_QUIT BUTTON_MENU
76 #define STOPWATCH_START_STOP BUTTON_SELECT
77 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
78 #define STOPWATCH_LAP_TIMER BUTTON_RIGHT
79 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_FWD
80 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_BACK
81 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
82 #define STOPWATCH_QUIT BUTTON_PLAY
83 #define STOPWATCH_START_STOP BUTTON_MODE
84 #define STOPWATCH_RESET_TIMER BUTTON_EQ
85 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
86 #define STOPWATCH_SCROLL_UP BUTTON_UP
87 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
88 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
89 #define STOPWATCH_QUIT BUTTON_POWER
90 #define STOPWATCH_START_STOP BUTTON_PLAY
91 #define STOPWATCH_RESET_TIMER BUTTON_REC
92 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
93 #define STOPWATCH_SCROLL_UP BUTTON_UP
94 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
95 #elif CONFIG_KEYPAD == GIGABEAT_PAD
96 #define STOPWATCH_QUIT BUTTON_POWER
97 #define STOPWATCH_START_STOP BUTTON_SELECT
98 #define STOPWATCH_RESET_TIMER BUTTON_A
99 #define STOPWATCH_LAP_TIMER BUTTON_MENU
100 #define STOPWATCH_SCROLL_UP BUTTON_UP
101 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
102 #elif CONFIG_KEYPAD == SANSA_E200_PAD
103 #define STOPWATCH_QUIT BUTTON_POWER
104 #define STOPWATCH_START_STOP BUTTON_LEFT
105 #define STOPWATCH_RESET_TIMER BUTTON_REC
106 #define STOPWATCH_LAP_TIMER BUTTON_SELECT
107 #define STOPWATCH_SCROLL_UP BUTTON_UP
108 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
109 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
110 #define STOPWATCH_QUIT BUTTON_POWER
111 #define STOPWATCH_START_STOP BUTTON_PLAY
112 #define STOPWATCH_RESET_TIMER BUTTON_REW
113 #define STOPWATCH_LAP_TIMER BUTTON_FF
114 #define STOPWATCH_SCROLL_UP BUTTON_SCROLL_UP
115 #define STOPWATCH_SCROLL_DOWN BUTTON_SCROLL_DOWN
116 #endif
118 static struct plugin_api* rb;
120 static int stopwatch = 0;
121 static long start_at = 0;
122 static int prev_total = 0;
123 static bool counting = false;
124 static int curr_lap = 0;
125 static int lap_scroll = 0;
126 static int lap_start;
127 static int lap_times[MAX_LAPS];
129 static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
131 int hours, minutes, seconds, cs;
133 hours = ticks / (HZ * 3600);
134 ticks -= (HZ * hours * 3600);
135 minutes = ticks / (HZ * 60);
136 ticks -= (HZ * minutes * 60);
137 seconds = ticks / HZ;
138 ticks -= (HZ * seconds);
139 cs = ticks;
140 if (!lap)
142 rb->snprintf(buf, buflen,
143 "%2d:%02d:%02d.%02d",
144 hours, minutes, seconds, cs);
146 else
149 if (lap > 1)
151 int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
152 last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
153 last_hours = last_ticks / (HZ * 3600);
154 last_ticks -= (HZ * last_hours * 3600);
155 last_minutes = last_ticks / (HZ * 60);
156 last_ticks -= (HZ * last_minutes * 60);
157 last_seconds = last_ticks / HZ;
158 last_ticks -= (HZ * last_seconds);
159 last_cs = last_ticks;
161 rb->snprintf(buf, buflen,
162 "%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
163 lap, hours, minutes, seconds, cs, last_hours,
164 last_minutes, last_seconds, last_cs);
166 else
168 rb->snprintf(buf, buflen,
169 "%2d %2d:%02d:%02d.%02d",
170 lap, hours, minutes, seconds, cs);
175 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
177 char buf[32];
178 int button;
179 int lap;
180 int done = false;
181 bool update_lap = true;
182 int lines;
184 (void)parameter;
185 rb = api;
187 #ifdef HAVE_LCD_BITMAP
188 int h;
189 rb->lcd_setfont(FONT_UI);
190 rb->lcd_getstringsize("M", NULL, &h);
191 lines = (LCD_HEIGHT / h) - (LAP_Y);
192 #else
193 lines = 1;
194 #endif
196 rb->lcd_clear_display();
198 while (!done)
200 if (counting)
202 stopwatch = prev_total + *rb->current_tick - start_at;
204 else
206 stopwatch = prev_total;
209 ticks_to_string(stopwatch,0,32,buf);
210 rb->lcd_puts(0, TIMER_Y, buf);
212 if(update_lap)
214 lap_start = curr_lap - lap_scroll;
215 for (lap = lap_start; lap > lap_start - lines; lap--)
217 if (lap > 0)
219 ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
220 rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf);
222 else
224 rb->lcd_puts(0, LAP_Y + lap_start - lap,
225 " ");
228 update_lap = false;
231 rb->lcd_update();
233 if (! counting)
235 button = rb->button_get(true);
237 else
239 button = rb->button_get_w_tmo(10);
241 /* Make sure that the jukebox isn't powered off
242 automatically */
243 rb->reset_poweroff_timer();
245 switch (button)
248 /* exit */
249 #ifdef STOPWATCH_RC_QUIT
250 case STOPWATCH_RC_QUIT:
251 #endif
252 case STOPWATCH_QUIT:
253 done = true;
254 break;
256 /* Stop/Start toggle */
257 case STOPWATCH_START_STOP:
258 counting = ! counting;
259 if (counting)
261 start_at = *rb->current_tick;
262 stopwatch = prev_total + *rb->current_tick - start_at;
264 else
266 prev_total += *rb->current_tick - start_at;
267 stopwatch = prev_total;
269 break;
271 /* Reset timer */
272 case STOPWATCH_RESET_TIMER:
273 if (!counting)
275 prev_total = 0;
276 curr_lap = 0;
277 update_lap = true;
279 break;
281 /* Lap timer */
282 case STOPWATCH_LAP_TIMER:
283 lap_times[curr_lap%MAX_LAPS] = stopwatch;
284 curr_lap++;
285 update_lap = true;
286 break;
288 /* Scroll Lap timer up */
289 case STOPWATCH_SCROLL_UP:
290 if (lap_scroll > 0)
292 lap_scroll --;
293 update_lap = true;
295 break;
297 /* Scroll Lap timer down */
298 case STOPWATCH_SCROLL_DOWN:
299 if ((lap_scroll < curr_lap - lines) &&
300 (lap_scroll < (MAX_LAPS - lines)) )
302 lap_scroll ++;
303 update_lap = true;
305 break;
307 default:
308 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
309 return PLUGIN_USB_CONNECTED;
310 break;
313 return PLUGIN_OK;