yarf 0.1
Yet Another RepRap Firmware
|
00001 /* 00002 * planner_queue.h 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 00035 #ifndef PLANNER_QUEUE_H 00036 #define PLANNER_QUEUE_H 00037 00038 #include "yarf.h" 00039 #include "block.h" 00040 00041 #include <stdbool.h> 00042 #include <stddef.h> 00043 00047 extern block_t q[PLANNER_QUEUE_SIZE]; 00048 00053 extern volatile uint8_t q_head; 00054 00059 extern volatile uint8_t q_tail; 00060 00061 00062 00063 00071 inline uint8_t 00072 plan_q_next_index(uint8_t block_index) 00073 { 00074 block_index += 1; 00075 if (block_index == PLANNER_QUEUE_SIZE) { 00076 block_index = 0; 00077 } 00078 return block_index; 00079 } 00080 00081 00089 inline uint8_t 00090 plan_q_prev_index(uint8_t block_index) { 00091 if (block_index == 0) { 00092 block_index = PLANNER_QUEUE_SIZE; 00093 } 00094 block_index -= 1; 00095 return block_index; 00096 } 00097 00103 inline bool 00104 plan_q_full(void) 00105 { 00106 return (plan_q_next_index(q_head) == q_tail); 00107 } 00108 00114 inline bool 00115 plan_q_empty(void) 00116 { 00117 return (q_head == q_tail); 00118 } 00119 00125 inline uint8_t 00126 plan_q_size(void) 00127 { 00128 uint8_t size = q_head - q_tail; 00129 return size >= 0 ? size : (PLANNER_QUEUE_SIZE - size); 00130 } 00131 00138 inline block_t * 00139 plan_q_get(uint8_t index) 00140 { 00141 return &q[index]; 00142 } 00143 00144 00153 inline block_t * 00154 plan_q_head(void) 00155 { 00156 if (plan_q_full()) { 00157 return NULL; 00158 } 00159 return &q[q_head]; 00160 } 00161 00170 inline uint8_t 00171 plan_q_head_index(void) 00172 { 00173 return q_head; 00174 } 00175 00180 inline void 00181 plan_q_shift_head(void) 00182 { 00183 if (!plan_q_full()) { 00184 q_head = plan_q_next_index(q_head); 00185 } 00186 } 00187 00196 inline block_t * 00197 plan_q_tail(void) 00198 { 00199 if (plan_q_empty()) { 00200 return NULL; 00201 } 00202 return &q[q_tail]; 00203 } 00204 00205 00214 inline uint8_t 00215 plan_q_tail_index(void) 00216 { 00217 return q_tail; 00218 } 00219 00224 inline void 00225 plan_q_discard_tail(void) 00226 { 00227 if (!plan_q_empty()) { 00228 q_tail = plan_q_next_index(q_tail); 00229 } 00230 } 00231 00235 inline void 00236 plan_q_clear(void) 00237 { 00238 q_head = 0; 00239 q_tail = 0; 00240 } 00241 00245 inline void 00246 plan_q_init(void) 00247 { 00248 plan_q_clear(); 00249 } 00250 00251 #endif //PLANNER_QUEUE_H