00001 /**********************************************************************************************************************\ 00002 00003 C++ library for Atmel AVR microcontrollers 00004 Copyright (C) 2007 Lauri Kirikal, Mikk Leini, MT� TT� Robotiklubi 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 See http://creativecommons.org/licenses/GPL/2.0/ 00021 00022 MT� TT� Robotiklubi http://www.robotiklubi.ee robotiklubi@gmail.com 00023 Lauri Kirikal laurikirikal@gmail.com 00024 Mikk Leini mikk.leini@gmail.com 00025 00026 \**********************************************************************************************************************/ 00027 00028 #ifndef __AVR_CPP_STATIC_QUEUE_H__ 00029 #define __AVR_CPP_STATIC_QUEUE_H__ 00030 00031 #include "BaseQueue.h" 00032 00033 00034 /**********************************************************************************************************************\ 00035 00036 AVRCppLib collection objects 00037 00038 \**********************************************************************************************************************/ 00039 00040 namespace AVRCpp 00041 { 00042 namespace Collection 00043 { 00044 00045 00046 /**********************************************************************************************************************\ 00047 00048 Static queue (FIFO) class 00049 00050 Supports item pushing to the end of queue and popping out first one. Fixed size. 00051 00052 \**********************************************************************************************************************/ 00053 00054 template <typename DataType, typename SizeType, SizeType queue_capacity> class StaticQueue : public BaseQueue<DataType, SizeType, DataType[queue_capacity]> 00055 { 00056 public: 00057 00061 StaticQueue() 00062 { 00063 BaseQueue<DataType, SizeType, DataType[queue_capacity]>(); 00064 } 00065 00069 bool Push(const DataType &value) 00070 { 00071 // If queue full then exit 00072 if (this->is_full) return false; 00073 00074 // Write item and increase write pointer 00075 this->data[this->write_pointer++] = value; 00076 this->write_pointer %= queue_capacity; 00077 00078 // If new write pointer matches read pointer then mark queue as full 00079 if (this->write_pointer == this->read_pointer) this->is_full = 1; 00080 00081 return true; 00082 } 00083 00087 bool Pop(DataType &value = 0) 00088 { 00089 // If queue empty then exit 00090 if (this->IsEmpty()) return false; 00091 00092 // Read item and increase read pointer 00093 value = this->data[this->read_pointer++]; 00094 this->read_pointer %= queue_capacity; 00095 00096 // Queue can't be full now 00097 this->is_full = 0; 00098 00099 return true; 00100 } 00101 00105 inline SizeType GetCapacity(void) 00106 { 00107 return queue_capacity; 00108 } 00109 00113 volatile SizeType GetSize(void) 00114 { 00115 // If queue marked full then return total size 00116 if (this->is_full) return queue_capacity; 00117 00118 // This functions relies on assumption that read pointer never passes write pointer! 00119 // Described situation cannot be evoked by user, only by software bug 00120 if (this->read_pointer <= this->write_pointer) 00121 return (this->write_pointer - this->read_pointer); 00122 else 00123 return ((queue_capacity - this->read_pointer) + this->write_pointer); 00124 } 00125 00126 }; // class StaticQueue 00127 00128 00129 /**********************************************************************************************************************/ 00130 00131 } // namespace Collection 00132 00133 } // namespace AVRCpp 00134 00135 00136 /**********************************************************************************************************************/ 00137 00138 #endif // ifndef __AVR_CPP_STATIC_QUEUE_H__
MTÜ TTÜ Robotiklubi |