Added MP3::Info.pm inside script to make it standalone.
[kugel-rb.git] / apps / plugins / stopwatch.c
blob3843476f78b03231f543851e5be77e61d8092adf
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 #ifdef HAVE_LCD_BITMAP
23 #define LAP_LINES 6
24 #define TIMER_Y 1
25 #else
26 #define LAP_LINES 1
27 #define TIMER_Y 0
28 #endif
30 #define LAP_Y TIMER_Y+1
31 #define MAX_LAPS 10
32 #define MAX_SCROLL (MAX_LAPS - LAP_LINES)
34 /* variable button definitions */
35 #if CONFIG_KEYPAD == RECORDER_PAD
36 #define STOPWATCH_QUIT BUTTON_OFF
37 #define STOPWATCH_START_STOP BUTTON_PLAY
38 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
39 #define STOPWATCH_LAP_TIMER BUTTON_ON
40 #define STOPWATCH_SCROLL_UP BUTTON_UP
41 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
42 #elif CONFIG_KEYPAD == ONDIO_PAD
43 #define STOPWATCH_QUIT BUTTON_OFF
44 #define STOPWATCH_START_STOP BUTTON_RIGHT
45 #define STOPWATCH_RESET_TIMER BUTTON_LEFT
46 #define STOPWATCH_LAP_TIMER BUTTON_MENU
47 #define STOPWATCH_SCROLL_UP BUTTON_UP
48 #define STOPWATCH_SCROLL_DOWN BUTTON_DOWN
49 #elif CONFIG_KEYPAD == PLAYER_PAD
50 #define STOPWATCH_QUIT BUTTON_MENU
51 #define STOPWATCH_START_STOP BUTTON_PLAY
52 #define STOPWATCH_RESET_TIMER BUTTON_STOP
53 #define STOPWATCH_LAP_TIMER BUTTON_ON
54 #define STOPWATCH_SCROLL_UP BUTTON_RIGHT
55 #define STOPWATCH_SCROLL_DOWN BUTTON_LEFT
56 #endif
58 static struct plugin_api* rb;
60 static int stopwatch = 0;
61 static long start_at = 0;
62 static int prev_total = 0;
63 static bool counting = false;
64 static int curr_lap = 0;
65 static int lap_scroll = 0;
66 static int lap_start;
67 static int lap_times[MAX_LAPS];
69 static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
71 int hours, minutes, seconds, cs;
73 hours = ticks / (HZ * 3600);
74 ticks -= (HZ * hours * 3600);
75 minutes = ticks / (HZ * 60);
76 ticks -= (HZ * minutes * 60);
77 seconds = ticks / HZ;
78 ticks -= (HZ * seconds);
79 cs = ticks;
80 if (!lap)
82 rb->snprintf(buf, buflen,
83 "%2d:%02d:%02d.%02d",
84 hours, minutes, seconds, cs);
86 else
88 rb->snprintf(buf, buflen,
89 "%2d %2d:%02d:%02d.%02d",
90 lap, hours, minutes, seconds, cs);
94 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
96 char buf[32];
97 int button;
98 int lap;
99 int done = false;
100 bool update_lap = true;
102 TEST_PLUGIN_API(api);
103 (void)parameter;
104 rb = api;
106 #ifdef HAVE_LCD_BITMAP
107 rb->lcd_setfont(FONT_UI);
108 #endif
110 rb->lcd_clear_display();
112 while (!done)
114 if (counting)
116 stopwatch = prev_total + *rb->current_tick - start_at;
118 else
120 stopwatch = prev_total;
123 ticks_to_string(stopwatch,0,32,buf);
124 rb->lcd_puts(0, TIMER_Y, buf);
126 if(update_lap)
128 lap_start = curr_lap - lap_scroll;
129 for (lap = lap_start; lap > lap_start - LAP_LINES; lap--)
131 if (lap > 0)
133 ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
134 rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf);
136 else
138 rb->lcd_puts(0, LAP_Y + lap_start - lap,
139 " ");
142 update_lap = false;
145 #ifdef HAVE_LCD_BITMAP
146 rb->lcd_update();
147 #endif
149 if (! counting)
151 button = rb->button_get(true);
153 else
155 button = rb->button_get_w_tmo(10);
157 /* Make sure that the jukebox isn't powered off
158 automatically */
159 rb->reset_poweroff_timer();
161 switch (button)
164 /* exit */
165 case STOPWATCH_QUIT:
166 done = true;
167 break;
169 /* Stop/Start toggle */
170 case STOPWATCH_START_STOP:
171 counting = ! counting;
172 if (counting)
174 start_at = *rb->current_tick;
175 stopwatch = prev_total + *rb->current_tick - start_at;
177 else
179 prev_total += *rb->current_tick - start_at;
180 stopwatch = prev_total;
182 break;
184 /* Reset timer */
185 case STOPWATCH_RESET_TIMER:
186 if (!counting)
188 prev_total = 0;
189 curr_lap = 0;
190 update_lap = true;
192 break;
194 /* Lap timer */
195 case STOPWATCH_LAP_TIMER:
196 lap_times[curr_lap%MAX_LAPS] = stopwatch;
197 curr_lap++;
198 update_lap = true;
199 break;
201 /* Scroll Lap timer up */
202 case STOPWATCH_SCROLL_UP:
203 if (lap_scroll > 0)
205 lap_scroll --;
206 update_lap = true;
208 break;
210 /* Scroll Lap timer down */
211 case STOPWATCH_SCROLL_DOWN:
212 if ((lap_scroll < curr_lap - LAP_LINES) &&
213 (lap_scroll < MAX_SCROLL) )
215 lap_scroll ++;
216 update_lap = true;
218 break;
220 default:
221 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
222 return PLUGIN_USB_CONNECTED;
223 break;
226 return PLUGIN_OK;