commit 11c092f9280a090b2a993c2d4bbd8125757470fa
parent 4abc910b5e2c964e64770a6645894d8fd6e03863
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Sun, 28 Sep 2025 13:02:55 -0300
`scheduler_init` take tasks array as argument
this is to know the amount of tasks at compile time
tasks now have an id
Diffstat:
3 files changed, 42 insertions(+), 31 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -17,24 +17,38 @@ void toggle_led_2(void) {
}
void read_button(void) {
- uint8_t btn_pin_state = (PIND & (1<<BTN_PIN));
-
- if (!btn_pin_state) {
- PORTD |= (1<<LED_3_PIN);
- } else {
+ if (PIND & (1<<BTN_PIN)) {
PORTD &= ~(1<<LED_3_PIN);
+ } else {
+ PORTD |= (1<<LED_3_PIN);
}
}
+/*
+typedef enum {
+ TOGGLE_LED_1 = 0,
+ TOGGLE_LED_2,
+ READ_BUTTON,
+ TASKS_COUNT
+} task_id_e;
+*/
+
+task_t tasks[] = {
+ { toggle_led_1, 1000 },
+ { toggle_led_2, 500 },
+ { read_button, 0 }
+};
+
+const uint8_t tasks_count = sizeof(tasks)/sizeof(tasks[0]);
+
void setup(void) {
- DDRD = (DDRD | (1<<LED_1_PIN) | (1<<LED_2_PIN) | (1<<LED_3_PIN)) & ~(1<<BTN_PIN);
+ DDRD = (DDRD | (1<<LED_1_PIN) | (1<<LED_2_PIN) | (1<<LED_3_PIN))
+ & ~(1<<BTN_PIN);
+
PORTD |= (1<<BTN_PIN);
millis_init();
-
- scheduler_append_task(&toggle_led_1, 1000);
- scheduler_append_task(&toggle_led_2, 500);
- scheduler_append_task(&read_button, 0);
+ scheduler_init(tasks, tasks_count);
}
int main(void) {
diff --git a/src/scheduler.c b/src/scheduler.c
@@ -4,18 +4,6 @@
extern "C" {
#endif
-typedef struct {
- void (*task_update)(void);
- uint16_t update_interval;
- uint16_t ticks;
-} task_t;
-
-typedef struct {
- uint32_t t_ms_dt;
- uint8_t task_cnt;
- task_t tasks[SCHEDULER_MAX_TASKS_CNT];
-} scheduler_t;
-
static scheduler_t scheduler = {0};
// update every 1 ms
@@ -27,7 +15,7 @@ void scheduler_update(uint32_t t_ms) {
scheduler.t_ms_dt = t_ms;
for (uint8_t i = 0; i < scheduler.task_cnt; ++i) {
- if (scheduler.tasks[i].ticks > 0) {
+ if (scheduler.tasks[i].ticks > 1) {
scheduler.tasks[i].ticks--;
continue;
}
@@ -37,11 +25,9 @@ void scheduler_update(uint32_t t_ms) {
}
}
-void scheduler_append_task(void (*update_func)(void), uint16_t update_interval) {
- scheduler.tasks[scheduler.task_cnt].task_update = update_func;
- scheduler.tasks[scheduler.task_cnt].update_interval =
- (update_interval == 0) ? update_interval : update_interval - 1;
- scheduler.task_cnt++;
+void scheduler_init(task_t *tasks, uint8_t task_count) {
+ scheduler.tasks = tasks;
+ scheduler.task_cnt = task_count;
}
#ifdef __cplusplus
diff --git a/src/scheduler.h b/src/scheduler.h
@@ -1,16 +1,27 @@
#ifndef _SCHEDULER_H_
#define _SCHEDULER_H_
-#define SCHEDULER_MAX_TASKS_CNT 10
-
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
+typedef struct {
+ void (*task_update)(void);
+ uint16_t update_interval;
+ uint8_t task_id;
+ uint16_t ticks;
+} task_t;
+
+typedef struct {
+ uint32_t t_ms_dt;
+ uint8_t task_cnt;
+ task_t *tasks;
+} scheduler_t;
+
+void scheduler_init(task_t *tasks, uint8_t task_count);
void scheduler_update(uint32_t t_ms);
-void scheduler_append_task(void (*update_func)(void), uint16_t update_interval);
#ifdef __cplusplus
}