diff --git a/include/block.h b/include/block.h index f1c9aff..555549c 100644 --- a/include/block.h +++ b/include/block.h @@ -24,6 +24,5 @@ int block_init(block *const block); int block_deinit(block *const block); int block_execute(block *const block, const uint8_t button); int block_update(block *const block); -bool block_must_run(const block *const block, const unsigned int time); #endif // BLOCK_H diff --git a/include/cli.h b/include/cli.h index 3d2d5a5..2f93f62 100644 --- a/include/cli.h +++ b/include/cli.h @@ -7,7 +7,6 @@ typedef struct { bool is_debug_mode; } cli_arguments; -int cli_init(cli_arguments* const args, const char* const argv[], - const int argc); +cli_arguments cli_parse_arguments(const char* const argv[], const int argc); #endif // CLI_H diff --git a/include/timer.h b/include/timer.h index eccf4a1..1ec7f75 100644 --- a/include/timer.h +++ b/include/timer.h @@ -2,6 +2,7 @@ #define TIMER_H #include +#include #include "block.h" @@ -15,5 +16,6 @@ typedef struct { timer timer_new(const block *const blocks, const unsigned short block_count); int timer_arm(timer *const timer); +bool timer_must_run_block(const timer *const timer, const block *const block); #endif // TIMER_H diff --git a/src/block.c b/src/block.c index 5020b02..0a73af6 100644 --- a/src/block.c +++ b/src/block.c @@ -144,15 +144,3 @@ int block_update(block *const block) { return 0; } - -bool block_must_run(const block *const block, const unsigned int time) { - if (time == 0) { - return true; - } - - if (block->interval == 0) { - return false; - } - - return time % block->interval == 0; -} diff --git a/src/cli.c b/src/cli.c index 1a48863..b1849ec 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1,19 +1,22 @@ #include "cli.h" +#include #include #include #include -int cli_init(cli_arguments *const args, const char *const argv[], - const int argc) { - args->is_debug_mode = false; +cli_arguments cli_parse_arguments(const char *const argv[], const int argc) { + errno = 0; + cli_arguments args = { + .is_debug_mode = false, + }; int opt = -1; opterr = 0; // Suppress getopt's built-in invalid opt message while ((opt = getopt(argc, (char *const *)argv, "dh")) != -1) { switch (opt) { case 'd': - args->is_debug_mode = true; + args.is_debug_mode = true; break; case '?': (void)fprintf(stderr, "error: unknown option `-%c'\n", optopt); @@ -22,9 +25,9 @@ int cli_init(cli_arguments *const args, const char *const argv[], // fall through default: (void)fprintf(stderr, "usage: %s [-d]\n", BINARY); - return 1; + errno = 1; } } - return 0; + return args; } diff --git a/src/main.c b/src/main.c index a858d88..4484f0a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include "main.h" +#include #include #include @@ -38,10 +39,10 @@ static int deinit_blocks(block *const blocks, static int execute_blocks(block *const blocks, const unsigned short block_count, - const unsigned int time) { + const timer *const timer) { for (unsigned short i = 0; i < block_count; ++i) { block *const block = &blocks[i]; - if (!block_must_run(block, time)) { + if (!timer_must_run_block(timer, block)) { continue; } @@ -55,7 +56,7 @@ static int execute_blocks(block *const blocks, static int trigger_event(block *const blocks, const unsigned short block_count, timer *const timer) { - if (execute_blocks(blocks, block_count, timer->time) != 0) { + if (execute_blocks(blocks, block_count, timer) != 0) { return 1; } @@ -117,8 +118,8 @@ static int event_loop(block *const blocks, const unsigned short block_count, } int main(const int argc, const char *const argv[]) { - cli_arguments cli_args; - if (cli_init(&cli_args, argv, argc) != 0) { + const cli_arguments cli_args = cli_parse_arguments(argv, argc); + if (errno != 0) { return 1; } diff --git a/src/timer.c b/src/timer.c index a9a383c..2454fb9 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,6 +1,7 @@ #include "timer.h" #include +#include #include #include @@ -32,10 +33,12 @@ static unsigned int compute_reset_value(const block *const blocks, } timer timer_new(const block *const blocks, const unsigned short block_count) { + const unsigned int reset_value = compute_reset_value(blocks, block_count); + timer timer = { - .time = 0, + .time = reset_value, // Initial value to execute all blocks. .tick = compute_tick(blocks, block_count), - .reset_value = compute_reset_value(blocks, block_count), + .reset_value = reset_value, }; return timer; @@ -51,7 +54,19 @@ int timer_arm(timer *const timer) { } // Wrap `time` to the interval [1, reset_value]. - timer->time = (timer->time + timer->tick) % timer->reset_value + 1; + timer->time = (timer->time + timer->tick) % timer->reset_value; return 0; } + +bool timer_must_run_block(const timer *const timer, const block *const block) { + if (timer->time == timer->reset_value) { + return true; + } + + if (block->interval == 0) { + return false; + } + + return timer->time % block->interval == 0; +}