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.cpp@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 Source |
| 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 | #include <usrlib.h> |
| kasturir | 0:906c21fbf97c | 43 | #include <commdefs.h> |
| kasturir | 0:906c21fbf97c | 44 | |
| kasturir | 0:906c21fbf97c | 45 | using namespace usr; |
| kasturir | 0:906c21fbf97c | 46 | |
| kasturir | 0:906c21fbf97c | 47 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 48 | // |
| kasturir | 0:906c21fbf97c | 49 | /// Circular buffer function-member description |
| kasturir | 0:906c21fbf97c | 50 | // |
| kasturir | 0:906c21fbf97c | 51 | // |
| kasturir | 0:906c21fbf97c | 52 | // |
| kasturir | 0:906c21fbf97c | 53 | TCbuf::TCbuf(byte* const Address, const byte Size) : |
| kasturir | 0:906c21fbf97c | 54 | buf(Address), |
| kasturir | 0:906c21fbf97c | 55 | size(Size), |
| kasturir | 0:906c21fbf97c | 56 | count(0), |
| kasturir | 0:906c21fbf97c | 57 | first(0), |
| kasturir | 0:906c21fbf97c | 58 | last(0) |
| kasturir | 0:906c21fbf97c | 59 | { |
| kasturir | 0:906c21fbf97c | 60 | } |
| kasturir | 0:906c21fbf97c | 61 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 62 | bool TCbuf::write(const byte* data, const byte Count) |
| kasturir | 0:906c21fbf97c | 63 | { |
| kasturir | 0:906c21fbf97c | 64 | if( Count > (size - count) ) |
| kasturir | 0:906c21fbf97c | 65 | return false; |
| kasturir | 0:906c21fbf97c | 66 | |
| kasturir | 0:906c21fbf97c | 67 | for(byte i = 0; i < Count; i++) |
| kasturir | 0:906c21fbf97c | 68 | push(*(data++)); |
| kasturir | 0:906c21fbf97c | 69 | |
| kasturir | 0:906c21fbf97c | 70 | return true; |
| kasturir | 0:906c21fbf97c | 71 | } |
| kasturir | 0:906c21fbf97c | 72 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 73 | void TCbuf::read(byte* data, const byte Count) |
| kasturir | 0:906c21fbf97c | 74 | { |
| kasturir | 0:906c21fbf97c | 75 | byte N = Count <= count ? Count : count; |
| kasturir | 0:906c21fbf97c | 76 | |
| kasturir | 0:906c21fbf97c | 77 | for(byte i = 0; i < N; i++) |
| kasturir | 0:906c21fbf97c | 78 | data[i] = pop(); |
| kasturir | 0:906c21fbf97c | 79 | } |
| kasturir | 0:906c21fbf97c | 80 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 81 | byte TCbuf::get_byte(const byte index) const |
| kasturir | 0:906c21fbf97c | 82 | { |
| kasturir | 0:906c21fbf97c | 83 | byte x = first + index; |
| kasturir | 0:906c21fbf97c | 84 | |
| kasturir | 0:906c21fbf97c | 85 | if(x < size) |
| kasturir | 0:906c21fbf97c | 86 | return buf[x]; |
| kasturir | 0:906c21fbf97c | 87 | else |
| kasturir | 0:906c21fbf97c | 88 | return buf[x - size]; |
| kasturir | 0:906c21fbf97c | 89 | } |
| kasturir | 0:906c21fbf97c | 90 | |
| kasturir | 0:906c21fbf97c | 91 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 92 | bool TCbuf::put(const byte item) |
| kasturir | 0:906c21fbf97c | 93 | { |
| kasturir | 0:906c21fbf97c | 94 | if(count == size) |
| kasturir | 0:906c21fbf97c | 95 | return false; |
| kasturir | 0:906c21fbf97c | 96 | |
| kasturir | 0:906c21fbf97c | 97 | push(item); |
| kasturir | 0:906c21fbf97c | 98 | return true; |
| kasturir | 0:906c21fbf97c | 99 | } |
| kasturir | 0:906c21fbf97c | 100 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 101 | byte TCbuf::get() |
| kasturir | 0:906c21fbf97c | 102 | { |
| kasturir | 0:906c21fbf97c | 103 | if(count) |
| kasturir | 0:906c21fbf97c | 104 | return pop(); |
| kasturir | 0:906c21fbf97c | 105 | else |
| kasturir | 0:906c21fbf97c | 106 | return 0; |
| kasturir | 0:906c21fbf97c | 107 | } |
| kasturir | 0:906c21fbf97c | 108 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 109 | // |
| kasturir | 0:906c21fbf97c | 110 | /// \note |
| kasturir | 0:906c21fbf97c | 111 | /// For internal purposes. |
| kasturir | 0:906c21fbf97c | 112 | /// Use this function with care - it doesn't perform free size check. |
| kasturir | 0:906c21fbf97c | 113 | // |
| kasturir | 0:906c21fbf97c | 114 | void TCbuf::push(const byte item) |
| kasturir | 0:906c21fbf97c | 115 | { |
| kasturir | 0:906c21fbf97c | 116 | buf[last] = item; |
| kasturir | 0:906c21fbf97c | 117 | last++; |
| kasturir | 0:906c21fbf97c | 118 | count++; |
| kasturir | 0:906c21fbf97c | 119 | |
| kasturir | 0:906c21fbf97c | 120 | if(last == size) |
| kasturir | 0:906c21fbf97c | 121 | last = 0; |
| kasturir | 0:906c21fbf97c | 122 | } |
| kasturir | 0:906c21fbf97c | 123 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 124 | // |
| kasturir | 0:906c21fbf97c | 125 | /// \note |
| kasturir | 0:906c21fbf97c | 126 | /// For internal purposes. |
| kasturir | 0:906c21fbf97c | 127 | /// Use this function with care - it doesn't perform free size check. |
| kasturir | 0:906c21fbf97c | 128 | // |
| kasturir | 0:906c21fbf97c | 129 | byte TCbuf::pop() |
| kasturir | 0:906c21fbf97c | 130 | { |
| kasturir | 0:906c21fbf97c | 131 | byte item = buf[first]; |
| kasturir | 0:906c21fbf97c | 132 | |
| kasturir | 0:906c21fbf97c | 133 | count--; |
| kasturir | 0:906c21fbf97c | 134 | first++; |
| kasturir | 0:906c21fbf97c | 135 | if(first == size) |
| kasturir | 0:906c21fbf97c | 136 | first = 0; |
| kasturir | 0:906c21fbf97c | 137 | |
| kasturir | 0:906c21fbf97c | 138 | return item; |
| kasturir | 0:906c21fbf97c | 139 | } |
| kasturir | 0:906c21fbf97c | 140 | //------------------------------------------------------------------------------ |