Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clock CPU utilization #1656

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/clock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include ../common/config.mk

TARGET = clock
LDFLAGS := $(LDFLAGS) -lSDL -lpthread -lmi_sys -lmi_gfx
CFLAGS := $(CFLAGS) -I./font -Os -ffunction-sections -fdata-sections
CFLAGS := $(CFLAGS) -I./font -Os -flto -fno-fat-lto-objects

include ../common/commands.mk
include ../common/recipes.mk
11 changes: 10 additions & 1 deletion src/clock/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SOFTWARE.
#define MAX_SECOND 59
#define MAX_MINUTE MAX_SECOND
#define MAX_HOUR 23
#define MS_TO_US(ms) ms * 1000

SDL_Surface *sdl_screen, *screen;

Expand Down Expand Up @@ -207,9 +208,10 @@ int main(int argc, char *argv[])
&hour_selected, &minute_selected, &seconds_selected);

SDL_EnableKeyRepeat(500, 50);

int dirty = 1; // first draw
while (!quit) {
while (SDL_PollEvent(&event)) {
dirty = 1;
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
Expand Down Expand Up @@ -296,6 +298,11 @@ int main(int argc, char *argv[])
}
}

if (dirty != 1) {
usleep(MS_TO_US(100));
continue;
}

Check_leap_year();
Dont_go_over_days();
Dont_go_over_hour();
Expand Down Expand Up @@ -330,6 +337,8 @@ int main(int argc, char *argv[])

/* Flip the screen so it gets displayed*/
GFX_Flip(screen);

dirty = 0;
}

/* Make sure to clean up the allocated surfaces before exiting.
Expand Down
Loading