yarf 0.1
Yet Another RepRap Firmware
|
00001 /* 00002 * periodic.c 00003 * 00004 * Copyright 2011 Pieter Agten 00005 * 00006 * This file is part of Yarf. 00007 * 00008 * Yarf is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * Yarf is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with Yarf. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00031 #include "periodic.h" 00032 #include "periodic_notify.h" 00033 00034 #include "util/math.h" 00035 00036 #include <stdio.h> 00037 00038 00042 static periodic_task_t *tasks; 00043 00044 00045 void 00046 periodic_init(void) 00047 { 00048 tasks = NULL; 00049 } 00050 00051 00052 void 00053 periodic_task_set(periodic_task_t *task, void (*f)(void), unsigned long ms) 00054 { 00055 task->f = f; 00056 task->ms_periodic = MAX(ms,1); 00057 task->ms_remaining = task->ms_periodic; 00058 task->next = NULL; 00059 } 00060 00061 void 00062 periodic_add(periodic_task_t *task) 00063 { 00064 periodic_task_t **tp = &tasks; 00065 while (*tp != NULL) { 00066 tp = &((*tp)->next); 00067 } 00068 *tp = task; 00069 } 00070 00071 void 00072 periodic_remove(periodic_task_t *task) 00073 { 00074 periodic_task_t **tp = &tasks; 00075 while (*tp != NULL && *tp != task) { 00076 tp = &((*tp)->next); 00077 } 00078 00079 if (*tp == task) { 00080 *tp = (*tp)->next; 00081 } 00082 } 00083 00084 void 00085 periodic_notify_ms(void) 00086 { 00087 periodic_task_t *task = tasks; 00088 while (task != NULL) { 00089 task->ms_remaining -= 1; 00090 if (task->ms_remaining == 0) { 00091 task->ms_remaining = task->ms_periodic; 00092 task->f(); 00093 } 00094 task = task->next; 00095 } 00096 } 00097 00098