commit bba75fe8e7dd424590091481284843e85c6a1199 Author: Wolfgang Hottgenroth Date: Sun Sep 18 14:09:29 2016 +0200 PontCoopScheduler migrated diff --git a/PontCoopScheduler.c b/PontCoopScheduler.c new file mode 100644 index 0000000..af536e2 --- /dev/null +++ b/PontCoopScheduler.c @@ -0,0 +1,75 @@ +/* + * PontCoopScheduler.c + * + * Created on: 29.08.2016 + * Author: wn + */ + + +#include +#include + + +#include "PontCoopScheduler.h" + +tTask tasks[MAX_NUM_OF_TASKS]; + + +void schInit() { + for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { + tasks[i].delay = 0; + tasks[i].period = 0; + tasks[i].run = 0; + tasks[i].exec = NULL; + tasks[i].handle = NULL; + } +} + +void schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period) { + for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { + if (tasks[i].exec == NULL) { + tasks[i].delay = delay; + tasks[i].period = period; + tasks[i].run = 0; + tasks[i].exec = exec; + tasks[i].handle = handle; + break; + } + } +} + +void schDel(void (*exec)(void *), void *handle) { + for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { + if ((tasks[i].exec == exec) && (tasks[i].handle == handle)) { + tasks[i].exec = NULL; + break; + } + } +} + +void schExec() { + for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { + if (tasks[i].exec != NULL && tasks[i].run > 0) { + tasks[i].run--; + tasks[i].exec(tasks[i].handle); + if (tasks[i].period == 0) { + tasks[i].exec = NULL; + } + } + } +} + + + +void schUpdate() { + for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { + if (tasks[i].exec != NULL) { + if (tasks[i].delay == 0) { + tasks[i].delay = tasks[i].period; + tasks[i].run++; + } else { + tasks[i].delay--; + } + } + } +} diff --git a/PontCoopScheduler.h b/PontCoopScheduler.h new file mode 100644 index 0000000..6dbef16 --- /dev/null +++ b/PontCoopScheduler.h @@ -0,0 +1,35 @@ +/* + * PontCoopScheduler.h + * + * Created on: 29.08.2016 + * Author: wn + */ + +#ifndef PONTCOOPSCHEDULER_H_ +#define PONTCOOPSCHEDULER_H_ + + +#include + + + +#define MAX_NUM_OF_TASKS 8 + + +typedef struct { + uint32_t delay; + uint32_t period; + uint8_t run; + void (*exec)(void *handle); + void *handle; +} tTask; + + +void schInit(); +void schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period); +void schDel(void (*exec)(void *), void *handle); +void schExec(); +void schUpdate(); + + +#endif /* PONTCOOPSCHEDULER_H_ */