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