Dependencies: PinDetect TextLCD mbed mRotaryEncoder
scmRTOS/Common/usrlib.h@0:afb2650fb49a, 2012-02-13 (annotated)
- Committer:
- cicklaus
- Date:
- Mon Feb 13 02:11:20 2012 +0000
- Revision:
- 0:afb2650fb49a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cicklaus | 0:afb2650fb49a | 1 | //****************************************************************************** |
cicklaus | 0:afb2650fb49a | 2 | //* |
cicklaus | 0:afb2650fb49a | 3 | //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System |
cicklaus | 0:afb2650fb49a | 4 | //* |
cicklaus | 0:afb2650fb49a | 5 | //* NICKNAME: scmRTOS |
cicklaus | 0:afb2650fb49a | 6 | //* |
cicklaus | 0:afb2650fb49a | 7 | //* PURPOSE: User Suport Library Header |
cicklaus | 0:afb2650fb49a | 8 | //* |
cicklaus | 0:afb2650fb49a | 9 | //* Version: 3.10 |
cicklaus | 0:afb2650fb49a | 10 | //* |
cicklaus | 0:afb2650fb49a | 11 | //* $Revision: 256 $ |
cicklaus | 0:afb2650fb49a | 12 | //* $Date:: 2010-01-22 #$ |
cicklaus | 0:afb2650fb49a | 13 | //* |
cicklaus | 0:afb2650fb49a | 14 | //* Copyright (c) 2003-2010, Harry E. Zhurov |
cicklaus | 0:afb2650fb49a | 15 | //* |
cicklaus | 0:afb2650fb49a | 16 | //* Permission is hereby granted, free of charge, to any person |
cicklaus | 0:afb2650fb49a | 17 | //* obtaining a copy of this software and associated documentation |
cicklaus | 0:afb2650fb49a | 18 | //* files (the "Software"), to deal in the Software without restriction, |
cicklaus | 0:afb2650fb49a | 19 | //* including without limitation the rights to use, copy, modify, merge, |
cicklaus | 0:afb2650fb49a | 20 | //* publish, distribute, sublicense, and/or sell copies of the Software, |
cicklaus | 0:afb2650fb49a | 21 | //* and to permit persons to whom the Software is furnished to do so, |
cicklaus | 0:afb2650fb49a | 22 | //* subject to the following conditions: |
cicklaus | 0:afb2650fb49a | 23 | //* |
cicklaus | 0:afb2650fb49a | 24 | //* The above copyright notice and this permission notice shall be included |
cicklaus | 0:afb2650fb49a | 25 | //* in all copies or substantial portions of the Software. |
cicklaus | 0:afb2650fb49a | 26 | //* |
cicklaus | 0:afb2650fb49a | 27 | //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
cicklaus | 0:afb2650fb49a | 28 | //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
cicklaus | 0:afb2650fb49a | 29 | //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
cicklaus | 0:afb2650fb49a | 30 | //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
cicklaus | 0:afb2650fb49a | 31 | //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
cicklaus | 0:afb2650fb49a | 32 | //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
cicklaus | 0:afb2650fb49a | 33 | //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
cicklaus | 0:afb2650fb49a | 34 | //* |
cicklaus | 0:afb2650fb49a | 35 | //* ================================================================= |
cicklaus | 0:afb2650fb49a | 36 | //* See http://scmrtos.sourceforge.net for documentation, latest |
cicklaus | 0:afb2650fb49a | 37 | //* information, license and contact details. |
cicklaus | 0:afb2650fb49a | 38 | //* ================================================================= |
cicklaus | 0:afb2650fb49a | 39 | //* |
cicklaus | 0:afb2650fb49a | 40 | //****************************************************************************** |
cicklaus | 0:afb2650fb49a | 41 | |
cicklaus | 0:afb2650fb49a | 42 | #ifndef USRLIB_H |
cicklaus | 0:afb2650fb49a | 43 | #define USRLIB_H |
cicklaus | 0:afb2650fb49a | 44 | |
cicklaus | 0:afb2650fb49a | 45 | #include <commdefs.h> |
cicklaus | 0:afb2650fb49a | 46 | |
cicklaus | 0:afb2650fb49a | 47 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 48 | // |
cicklaus | 0:afb2650fb49a | 49 | // DESCRIPTON: user namespace for some useful types and functions |
cicklaus | 0:afb2650fb49a | 50 | // |
cicklaus | 0:afb2650fb49a | 51 | // |
cicklaus | 0:afb2650fb49a | 52 | namespace usr |
cicklaus | 0:afb2650fb49a | 53 | { |
cicklaus | 0:afb2650fb49a | 54 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 55 | // |
cicklaus | 0:afb2650fb49a | 56 | /// The Circular Buffer |
cicklaus | 0:afb2650fb49a | 57 | // |
cicklaus | 0:afb2650fb49a | 58 | /// Byte-wide FIFO. |
cicklaus | 0:afb2650fb49a | 59 | // |
cicklaus | 0:afb2650fb49a | 60 | /// Allows to: |
cicklaus | 0:afb2650fb49a | 61 | /// add byte, |
cicklaus | 0:afb2650fb49a | 62 | /// get byte, |
cicklaus | 0:afb2650fb49a | 63 | /// write bytes from array, |
cicklaus | 0:afb2650fb49a | 64 | /// read bytes to array, |
cicklaus | 0:afb2650fb49a | 65 | /// and some other service actions. |
cicklaus | 0:afb2650fb49a | 66 | // |
cicklaus | 0:afb2650fb49a | 67 | class TCbuf |
cicklaus | 0:afb2650fb49a | 68 | { |
cicklaus | 0:afb2650fb49a | 69 | public: |
cicklaus | 0:afb2650fb49a | 70 | TCbuf(byte* const Address, const byte Size); |
cicklaus | 0:afb2650fb49a | 71 | bool write(const byte* data, const byte Count); |
cicklaus | 0:afb2650fb49a | 72 | void read(byte* const data, const byte Count); |
cicklaus | 0:afb2650fb49a | 73 | byte get_count() const { return count; } |
cicklaus | 0:afb2650fb49a | 74 | byte get_free_size() const { return size - count; } |
cicklaus | 0:afb2650fb49a | 75 | byte get_byte(const byte index) const; |
cicklaus | 0:afb2650fb49a | 76 | void clear() { count = 0; last = first; } |
cicklaus | 0:afb2650fb49a | 77 | bool put(const byte item); |
cicklaus | 0:afb2650fb49a | 78 | byte get(); |
cicklaus | 0:afb2650fb49a | 79 | |
cicklaus | 0:afb2650fb49a | 80 | private: |
cicklaus | 0:afb2650fb49a | 81 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 82 | // |
cicklaus | 0:afb2650fb49a | 83 | // DESCRIPTON: For internal purposes |
cicklaus | 0:afb2650fb49a | 84 | // |
cicklaus | 0:afb2650fb49a | 85 | void push(const byte item); ///< Use this function with care - it doesn't perform free size check |
cicklaus | 0:afb2650fb49a | 86 | byte pop(); ///< Use this function with care - it doesn't perform count check |
cicklaus | 0:afb2650fb49a | 87 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 88 | |
cicklaus | 0:afb2650fb49a | 89 | private: |
cicklaus | 0:afb2650fb49a | 90 | byte* buf; |
cicklaus | 0:afb2650fb49a | 91 | byte size; |
cicklaus | 0:afb2650fb49a | 92 | volatile byte count; |
cicklaus | 0:afb2650fb49a | 93 | byte first; |
cicklaus | 0:afb2650fb49a | 94 | byte last; |
cicklaus | 0:afb2650fb49a | 95 | }; |
cicklaus | 0:afb2650fb49a | 96 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 97 | |
cicklaus | 0:afb2650fb49a | 98 | |
cicklaus | 0:afb2650fb49a | 99 | |
cicklaus | 0:afb2650fb49a | 100 | //----------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 101 | // |
cicklaus | 0:afb2650fb49a | 102 | /// The Ring Buffer Template |
cicklaus | 0:afb2650fb49a | 103 | /// |
cicklaus | 0:afb2650fb49a | 104 | /// Carries out FIFO functionality for |
cicklaus | 0:afb2650fb49a | 105 | /// arbitrary data types |
cicklaus | 0:afb2650fb49a | 106 | /// |
cicklaus | 0:afb2650fb49a | 107 | /// Allows to: |
cicklaus | 0:afb2650fb49a | 108 | /// add item to back (default), |
cicklaus | 0:afb2650fb49a | 109 | /// add item to front, |
cicklaus | 0:afb2650fb49a | 110 | /// get item at front (default), |
cicklaus | 0:afb2650fb49a | 111 | /// get item from back, |
cicklaus | 0:afb2650fb49a | 112 | /// write items from array, |
cicklaus | 0:afb2650fb49a | 113 | /// read items to array and some other actions |
cicklaus | 0:afb2650fb49a | 114 | // |
cicklaus | 0:afb2650fb49a | 115 | // |
cicklaus | 0:afb2650fb49a | 116 | // |
cicklaus | 0:afb2650fb49a | 117 | template<typename T, word Size, typename S = byte> |
cicklaus | 0:afb2650fb49a | 118 | class ring_buffer |
cicklaus | 0:afb2650fb49a | 119 | { |
cicklaus | 0:afb2650fb49a | 120 | public: |
cicklaus | 0:afb2650fb49a | 121 | ring_buffer() : Count(0), First(0), Last(0) { } |
cicklaus | 0:afb2650fb49a | 122 | |
cicklaus | 0:afb2650fb49a | 123 | //---------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 124 | // |
cicklaus | 0:afb2650fb49a | 125 | // Data transfer functions |
cicklaus | 0:afb2650fb49a | 126 | // |
cicklaus | 0:afb2650fb49a | 127 | bool write(const T* data, const S cnt); |
cicklaus | 0:afb2650fb49a | 128 | void read(T* const data, const S cnt); |
cicklaus | 0:afb2650fb49a | 129 | |
cicklaus | 0:afb2650fb49a | 130 | bool push_back(const T item); |
cicklaus | 0:afb2650fb49a | 131 | bool push_front(const T item); |
cicklaus | 0:afb2650fb49a | 132 | |
cicklaus | 0:afb2650fb49a | 133 | T pop_front(); |
cicklaus | 0:afb2650fb49a | 134 | T pop_back(); |
cicklaus | 0:afb2650fb49a | 135 | |
cicklaus | 0:afb2650fb49a | 136 | bool push(const T item) { return push_back(item); } |
cicklaus | 0:afb2650fb49a | 137 | T pop() { return pop_front(); } |
cicklaus | 0:afb2650fb49a | 138 | |
cicklaus | 0:afb2650fb49a | 139 | //---------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 140 | // |
cicklaus | 0:afb2650fb49a | 141 | // Service functions |
cicklaus | 0:afb2650fb49a | 142 | // |
cicklaus | 0:afb2650fb49a | 143 | S get_count() const { return Count; } |
cicklaus | 0:afb2650fb49a | 144 | S get_free_size() const { return Size - Count; } |
cicklaus | 0:afb2650fb49a | 145 | T& operator[](const S index); |
cicklaus | 0:afb2650fb49a | 146 | void flush() { Count = 0; Last = First; } |
cicklaus | 0:afb2650fb49a | 147 | |
cicklaus | 0:afb2650fb49a | 148 | private: |
cicklaus | 0:afb2650fb49a | 149 | //-------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 150 | // DESCRIPTON: For internal purposes |
cicklaus | 0:afb2650fb49a | 151 | // Use this functions with care: it don't perform |
cicklaus | 0:afb2650fb49a | 152 | // free size and count check |
cicklaus | 0:afb2650fb49a | 153 | // |
cicklaus | 0:afb2650fb49a | 154 | void push_item(const T item); |
cicklaus | 0:afb2650fb49a | 155 | void push_item_front(const T item); |
cicklaus | 0:afb2650fb49a | 156 | T pop_item(); |
cicklaus | 0:afb2650fb49a | 157 | T pop_item_back(); |
cicklaus | 0:afb2650fb49a | 158 | //-------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 159 | |
cicklaus | 0:afb2650fb49a | 160 | private: |
cicklaus | 0:afb2650fb49a | 161 | S Count; |
cicklaus | 0:afb2650fb49a | 162 | S First; |
cicklaus | 0:afb2650fb49a | 163 | S Last; |
cicklaus | 0:afb2650fb49a | 164 | T Buf[Size]; |
cicklaus | 0:afb2650fb49a | 165 | }; |
cicklaus | 0:afb2650fb49a | 166 | //------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 167 | } |
cicklaus | 0:afb2650fb49a | 168 | //--------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 169 | |
cicklaus | 0:afb2650fb49a | 170 | |
cicklaus | 0:afb2650fb49a | 171 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 172 | // |
cicklaus | 0:afb2650fb49a | 173 | // The ring buffer function-member definitions |
cicklaus | 0:afb2650fb49a | 174 | // |
cicklaus | 0:afb2650fb49a | 175 | // |
cicklaus | 0:afb2650fb49a | 176 | // |
cicklaus | 0:afb2650fb49a | 177 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 178 | bool usr::ring_buffer<T, Size, S>::write(const T* data, const S cnt) |
cicklaus | 0:afb2650fb49a | 179 | { |
cicklaus | 0:afb2650fb49a | 180 | if( cnt > (Size - Count) ) |
cicklaus | 0:afb2650fb49a | 181 | return false; |
cicklaus | 0:afb2650fb49a | 182 | |
cicklaus | 0:afb2650fb49a | 183 | for(S i = 0; i < cnt; i++) |
cicklaus | 0:afb2650fb49a | 184 | push_item(*(data++)); |
cicklaus | 0:afb2650fb49a | 185 | |
cicklaus | 0:afb2650fb49a | 186 | return true; |
cicklaus | 0:afb2650fb49a | 187 | } |
cicklaus | 0:afb2650fb49a | 188 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 189 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 190 | void usr::ring_buffer<T, Size, S>::read(T* data, const S cnt) |
cicklaus | 0:afb2650fb49a | 191 | { |
cicklaus | 0:afb2650fb49a | 192 | S nItems = cnt <= Count ? cnt : Count; |
cicklaus | 0:afb2650fb49a | 193 | |
cicklaus | 0:afb2650fb49a | 194 | for(S i = 0; i < nItems; i++) |
cicklaus | 0:afb2650fb49a | 195 | data[i] = pop_item(); |
cicklaus | 0:afb2650fb49a | 196 | } |
cicklaus | 0:afb2650fb49a | 197 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 198 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 199 | T& usr::ring_buffer<T, Size, S>::operator[](const S index) |
cicklaus | 0:afb2650fb49a | 200 | { |
cicklaus | 0:afb2650fb49a | 201 | S x = First + index; |
cicklaus | 0:afb2650fb49a | 202 | |
cicklaus | 0:afb2650fb49a | 203 | if(x < Size) |
cicklaus | 0:afb2650fb49a | 204 | return Buf[x]; |
cicklaus | 0:afb2650fb49a | 205 | else |
cicklaus | 0:afb2650fb49a | 206 | return Buf[x - Size]; |
cicklaus | 0:afb2650fb49a | 207 | } |
cicklaus | 0:afb2650fb49a | 208 | |
cicklaus | 0:afb2650fb49a | 209 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 210 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 211 | bool usr::ring_buffer<T, Size, S>::push_back(const T item) |
cicklaus | 0:afb2650fb49a | 212 | { |
cicklaus | 0:afb2650fb49a | 213 | if(Count == Size) |
cicklaus | 0:afb2650fb49a | 214 | return false; |
cicklaus | 0:afb2650fb49a | 215 | |
cicklaus | 0:afb2650fb49a | 216 | push_item(item); |
cicklaus | 0:afb2650fb49a | 217 | return true; |
cicklaus | 0:afb2650fb49a | 218 | } |
cicklaus | 0:afb2650fb49a | 219 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 220 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 221 | bool usr::ring_buffer<T, Size, S>::push_front(const T item) |
cicklaus | 0:afb2650fb49a | 222 | { |
cicklaus | 0:afb2650fb49a | 223 | if(Count == Size) |
cicklaus | 0:afb2650fb49a | 224 | return false; |
cicklaus | 0:afb2650fb49a | 225 | |
cicklaus | 0:afb2650fb49a | 226 | push_item_front(item); |
cicklaus | 0:afb2650fb49a | 227 | return true; |
cicklaus | 0:afb2650fb49a | 228 | } |
cicklaus | 0:afb2650fb49a | 229 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 230 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 231 | T usr::ring_buffer<T, Size, S>::pop_front() |
cicklaus | 0:afb2650fb49a | 232 | { |
cicklaus | 0:afb2650fb49a | 233 | if(Count) |
cicklaus | 0:afb2650fb49a | 234 | return pop_item(); |
cicklaus | 0:afb2650fb49a | 235 | else |
cicklaus | 0:afb2650fb49a | 236 | return Buf[First]; |
cicklaus | 0:afb2650fb49a | 237 | } |
cicklaus | 0:afb2650fb49a | 238 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 239 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 240 | T usr::ring_buffer<T, Size, S>::pop_back() |
cicklaus | 0:afb2650fb49a | 241 | { |
cicklaus | 0:afb2650fb49a | 242 | if(Count) |
cicklaus | 0:afb2650fb49a | 243 | return pop_item_back(); |
cicklaus | 0:afb2650fb49a | 244 | else |
cicklaus | 0:afb2650fb49a | 245 | return Buf[First]; |
cicklaus | 0:afb2650fb49a | 246 | } |
cicklaus | 0:afb2650fb49a | 247 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 248 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 249 | void usr::ring_buffer<T, Size, S>::push_item(const T item) |
cicklaus | 0:afb2650fb49a | 250 | { |
cicklaus | 0:afb2650fb49a | 251 | Buf[Last] = item; |
cicklaus | 0:afb2650fb49a | 252 | Last++; |
cicklaus | 0:afb2650fb49a | 253 | Count++; |
cicklaus | 0:afb2650fb49a | 254 | |
cicklaus | 0:afb2650fb49a | 255 | if(Last == Size) |
cicklaus | 0:afb2650fb49a | 256 | Last = 0; |
cicklaus | 0:afb2650fb49a | 257 | } |
cicklaus | 0:afb2650fb49a | 258 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 259 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 260 | void usr::ring_buffer<T, Size, S>::push_item_front(const T item) |
cicklaus | 0:afb2650fb49a | 261 | { |
cicklaus | 0:afb2650fb49a | 262 | if(First == 0) |
cicklaus | 0:afb2650fb49a | 263 | First = Size - 1; |
cicklaus | 0:afb2650fb49a | 264 | else |
cicklaus | 0:afb2650fb49a | 265 | --First; |
cicklaus | 0:afb2650fb49a | 266 | Buf[First] = item; |
cicklaus | 0:afb2650fb49a | 267 | Count++; |
cicklaus | 0:afb2650fb49a | 268 | } |
cicklaus | 0:afb2650fb49a | 269 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 270 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 271 | T usr::ring_buffer<T, Size, S>::pop_item() |
cicklaus | 0:afb2650fb49a | 272 | { |
cicklaus | 0:afb2650fb49a | 273 | T item = Buf[First]; |
cicklaus | 0:afb2650fb49a | 274 | |
cicklaus | 0:afb2650fb49a | 275 | Count--; |
cicklaus | 0:afb2650fb49a | 276 | First++; |
cicklaus | 0:afb2650fb49a | 277 | if(First == Size) |
cicklaus | 0:afb2650fb49a | 278 | First = 0; |
cicklaus | 0:afb2650fb49a | 279 | |
cicklaus | 0:afb2650fb49a | 280 | return item; |
cicklaus | 0:afb2650fb49a | 281 | } |
cicklaus | 0:afb2650fb49a | 282 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 283 | template<typename T, word Size, typename S> |
cicklaus | 0:afb2650fb49a | 284 | T usr::ring_buffer<T, Size, S>::pop_item_back() |
cicklaus | 0:afb2650fb49a | 285 | { |
cicklaus | 0:afb2650fb49a | 286 | |
cicklaus | 0:afb2650fb49a | 287 | if(Last == 0) |
cicklaus | 0:afb2650fb49a | 288 | Last = Size - 1; |
cicklaus | 0:afb2650fb49a | 289 | else |
cicklaus | 0:afb2650fb49a | 290 | --Last; |
cicklaus | 0:afb2650fb49a | 291 | |
cicklaus | 0:afb2650fb49a | 292 | Count--; |
cicklaus | 0:afb2650fb49a | 293 | return Buf[Last];; |
cicklaus | 0:afb2650fb49a | 294 | } |
cicklaus | 0:afb2650fb49a | 295 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 296 | |
cicklaus | 0:afb2650fb49a | 297 | |
cicklaus | 0:afb2650fb49a | 298 | #endif // USRLIB_H |