fix: Fix timer logic for blocks

This commit is contained in:
Utkarsh Verma 2023-12-19 09:51:41 +05:30
parent c42a4215c8
commit 12d4decdd4
No known key found for this signature in database
GPG Key ID: 7A4885A7162BDF20
7 changed files with 36 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -2,6 +2,7 @@
#define TIMER_H
#include <signal.h>
#include <stdbool.h>
#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

View File

@ -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;
}

View File

@ -1,19 +1,22 @@
#include "cli.h"
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
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;
}

View File

@ -1,5 +1,6 @@
#include "main.h"
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
@ -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;
}

View File

@ -1,6 +1,7 @@
#include "timer.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
@ -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;
}