1
0
Fork 0
forked from len0rd/rockbox

plugins: stopwatch: use UI viewport, fix vp not cleared

- lap times were not cleared after reset
- fix stuck lap scroll position after reset
- the running counter displayed stuck pixels
  depending on the font's character width

Change-Id: I0427bb2257ae8646ae675ae92ae3a5601083c4d1
This commit is contained in:
Christian Soffke 2025-06-07 17:13:34 +02:00
parent 42e5fa3ea5
commit 9593dca0ef

View file

@ -352,38 +352,31 @@ static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
ticks -= (HZ * seconds); ticks -= (HZ * seconds);
cs = ticks; cs = ticks;
if (!lap) if (!lap)
{
rb->snprintf(buf, buflen, rb->snprintf(buf, buflen,
"%2d:%02d:%02d.%02d", "%2d:%02d:%02d.%02d ", /* add some blank space, in case */
hours, minutes, seconds, cs); hours, minutes, seconds, cs); /* char width differs, since we */
/* don't clear the viewport */
else if (lap > 1)
{
int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
last_hours = last_ticks / (HZ * 3600);
last_ticks -= (HZ * last_hours * 3600);
last_minutes = last_ticks / (HZ * 60);
last_ticks -= (HZ * last_minutes * 60);
last_seconds = last_ticks / HZ;
last_ticks -= (HZ * last_seconds);
last_cs = last_ticks;
rb->snprintf(buf, buflen,
"%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
lap, hours, minutes, seconds, cs, last_hours,
last_minutes, last_seconds, last_cs);
} }
else else
{ rb->snprintf(buf, buflen,
"%2d %2d:%02d:%02d.%02d",
if (lap > 1) lap, hours, minutes, seconds, cs);
{
int last_ticks, last_hours, last_minutes, last_seconds, last_cs;
last_ticks = lap_times[(lap-1)%MAX_LAPS] - lap_times[(lap-2)%MAX_LAPS];
last_hours = last_ticks / (HZ * 3600);
last_ticks -= (HZ * last_hours * 3600);
last_minutes = last_ticks / (HZ * 60);
last_ticks -= (HZ * last_minutes * 60);
last_seconds = last_ticks / HZ;
last_ticks -= (HZ * last_seconds);
last_cs = last_ticks;
rb->snprintf(buf, buflen,
"%2d %2d:%02d:%02d.%02d [%2d:%02d:%02d.%02d]",
lap, hours, minutes, seconds, cs, last_hours,
last_minutes, last_seconds, last_cs);
}
else
{
rb->snprintf(buf, buflen,
"%2d %2d:%02d:%02d.%02d",
lap, hours, minutes, seconds, cs);
}
}
} }
/* /*
@ -391,14 +384,9 @@ static void ticks_to_string(int ticks,int lap,int buflen, char * buf)
*/ */
static void load_stopwatch(void) static void load_stopwatch(void)
{ {
int fd; int fd = rb->open(STOPWATCH_FILE, O_RDONLY);
fd = rb->open(STOPWATCH_FILE, O_RDONLY);
if (fd < 0) if (fd < 0)
{
return; return;
}
/* variable stopwatch isn't saved/loaded, because it is only used /* variable stopwatch isn't saved/loaded, because it is only used
* temporarily in main loop * temporarily in main loop
@ -431,14 +419,9 @@ static void load_stopwatch(void)
*/ */
static void save_stopwatch(void) static void save_stopwatch(void)
{ {
int fd; int fd = rb->open(STOPWATCH_FILE, O_CREAT|O_WRONLY|O_TRUNC, 0666);
fd = rb->open(STOPWATCH_FILE, O_CREAT|O_WRONLY|O_TRUNC, 0666);
if (fd < 0) if (fd < 0)
{
return; return;
}
/* variable stopwatch isn't saved/loaded, because it is only used /* variable stopwatch isn't saved/loaded, because it is only used
* temporarily in main loop * temporarily in main loop
@ -457,75 +440,72 @@ static void save_stopwatch(void)
enum plugin_status plugin_start(const void* parameter) enum plugin_status plugin_start(const void* parameter)
{ {
struct viewport vp, *last_vp;
struct screen *display;
char buf[32]; char buf[32];
int button; int button;
int lap; int lap;
int done = false; int done = false;
bool update_lap = true; bool update_lap = true;
int lines; int lines;
int ret = PLUGIN_OK;
(void)parameter; (void)parameter;
int h; int h;
rb->lcd_setfont(FONT_UI);
rb->lcd_getstringsize("M", NULL, &h); FOR_NB_SCREENS(i)
lines = (LCD_HEIGHT / h) - (LAP_Y); rb->viewportmanager_theme_enable(i, true, NULL);
rb->viewport_set_defaults(&vp, SCREEN_MAIN);
display = rb->screens[SCREEN_MAIN];
display->setfont(FONT_UI);
display->getstringsize("M", NULL, &h);
lines = (vp.height / h) - (LAP_Y);
load_stopwatch(); load_stopwatch();
rb->lcd_clear_display();
while (!done) while (!done)
{ {
rb->send_event(GUI_EVENT_ACTIONUPDATE, NULL); /* update status bar */
if (counting) if (counting)
{
stopwatch = prev_total + *rb->current_tick - start_at; stopwatch = prev_total + *rb->current_tick - start_at;
}
else else
{
stopwatch = prev_total; stopwatch = prev_total;
}
ticks_to_string(stopwatch,0,32,buf); ticks_to_string(stopwatch,0,32,buf);
rb->lcd_puts(0, TIMER_Y, buf); last_vp = display->set_viewport(&vp);
if(update_lap) if(update_lap)
{ {
display->clear_viewport();
display->puts(0, TIMER_Y, buf);
lap_start = curr_lap - lap_scroll; lap_start = curr_lap - lap_scroll;
for (lap = lap_start; lap > lap_start - lines; lap--) for (lap = lap_start; lap > lap_start - lines; lap--)
{ {
if (lap > 0) if (lap > 0)
{ {
ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf); ticks_to_string(lap_times[(lap-1)%MAX_LAPS],lap,32,buf);
rb->lcd_puts_scroll(0, LAP_Y + lap_start - lap, buf); display->puts_scroll(0, LAP_Y + lap_start - lap, buf);
} }
else else
{ display->puts(0, LAP_Y + lap_start - lap,
rb->lcd_puts(0, LAP_Y + lap_start - lap, " ");
" ");
}
} }
update_lap = false; update_lap = false;
} }
else
rb->lcd_update(); display->puts(0, TIMER_Y, buf);
display->update_viewport();
if (! counting) display->set_viewport(last_vp);
{ if (!counting)
button = rb->button_get(true); button = rb->button_get_w_tmo(HZ/2);
}
else else
{ {
button = rb->button_get_w_tmo(10); button = rb->button_get_w_tmo(HZ/10);
/* Make sure that the jukebox isn't powered off
automatically */
rb->reset_poweroff_timer(); rb->reset_poweroff_timer();
} }
switch (button) switch (button)
{ {
/* exit */
#ifdef STOPWATCH_RC_QUIT #ifdef STOPWATCH_RC_QUIT
case STOPWATCH_RC_QUIT: case STOPWATCH_RC_QUIT:
#endif #endif
@ -555,6 +535,7 @@ enum plugin_status plugin_start(const void* parameter)
{ {
prev_total = 0; prev_total = 0;
curr_lap = 0; curr_lap = 0;
lap_scroll = 0;
update_lap = true; update_lap = true;
} }
break; break;
@ -580,7 +561,7 @@ enum plugin_status plugin_start(const void* parameter)
case STOPWATCH_SCROLL_UP: case STOPWATCH_SCROLL_UP:
if (lap_scroll > 0) if (lap_scroll > 0)
{ {
lap_scroll --; lap_scroll--;
update_lap = true; update_lap = true;
} }
break; break;
@ -590,16 +571,18 @@ enum plugin_status plugin_start(const void* parameter)
if ((lap_scroll < curr_lap - lines) && if ((lap_scroll < curr_lap - lines) &&
(lap_scroll < (MAX_LAPS - lines)) ) (lap_scroll < (MAX_LAPS - lines)) )
{ {
lap_scroll ++; lap_scroll++;
update_lap = true; update_lap = true;
} }
break; break;
default: default:
if (rb->default_event_handler(button) == SYS_USB_CONNECTED) if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED; ret = PLUGIN_USB_CONNECTED;
break; break;
} }
} }
return PLUGIN_OK; FOR_NB_SCREENS(i)
rb->viewportmanager_theme_undo(i, false);
return ret;
} }