chokchai Hantanong
/
RtosTest
LED Fork
Fork of RtosTest by
scmRTOS/Common/usrlib.cpp@0:331db0b44b67, 2010-09-21 (annotated)
- Committer:
- mbed714
- Date:
- Tue Sep 21 19:48:05 2010 +0000
- Revision:
- 0:331db0b44b67
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed714 | 0:331db0b44b67 | 1 | //****************************************************************************** |
mbed714 | 0:331db0b44b67 | 2 | //* |
mbed714 | 0:331db0b44b67 | 3 | //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System |
mbed714 | 0:331db0b44b67 | 4 | //* |
mbed714 | 0:331db0b44b67 | 5 | //* NICKNAME: scmRTOS |
mbed714 | 0:331db0b44b67 | 6 | //* |
mbed714 | 0:331db0b44b67 | 7 | //* PURPOSE: User Suport Library Source |
mbed714 | 0:331db0b44b67 | 8 | //* |
mbed714 | 0:331db0b44b67 | 9 | //* Version: 3.10 |
mbed714 | 0:331db0b44b67 | 10 | //* |
mbed714 | 0:331db0b44b67 | 11 | //* $Revision: 256 $ |
mbed714 | 0:331db0b44b67 | 12 | //* $Date:: 2010-01-22 #$ |
mbed714 | 0:331db0b44b67 | 13 | //* |
mbed714 | 0:331db0b44b67 | 14 | //* Copyright (c) 2003-2010, Harry E. Zhurov |
mbed714 | 0:331db0b44b67 | 15 | //* |
mbed714 | 0:331db0b44b67 | 16 | //* Permission is hereby granted, free of charge, to any person |
mbed714 | 0:331db0b44b67 | 17 | //* obtaining a copy of this software and associated documentation |
mbed714 | 0:331db0b44b67 | 18 | //* files (the "Software"), to deal in the Software without restriction, |
mbed714 | 0:331db0b44b67 | 19 | //* including without limitation the rights to use, copy, modify, merge, |
mbed714 | 0:331db0b44b67 | 20 | //* publish, distribute, sublicense, and/or sell copies of the Software, |
mbed714 | 0:331db0b44b67 | 21 | //* and to permit persons to whom the Software is furnished to do so, |
mbed714 | 0:331db0b44b67 | 22 | //* subject to the following conditions: |
mbed714 | 0:331db0b44b67 | 23 | //* |
mbed714 | 0:331db0b44b67 | 24 | //* The above copyright notice and this permission notice shall be included |
mbed714 | 0:331db0b44b67 | 25 | //* in all copies or substantial portions of the Software. |
mbed714 | 0:331db0b44b67 | 26 | //* |
mbed714 | 0:331db0b44b67 | 27 | //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
mbed714 | 0:331db0b44b67 | 28 | //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
mbed714 | 0:331db0b44b67 | 29 | //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
mbed714 | 0:331db0b44b67 | 30 | //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
mbed714 | 0:331db0b44b67 | 31 | //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
mbed714 | 0:331db0b44b67 | 32 | //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
mbed714 | 0:331db0b44b67 | 33 | //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed714 | 0:331db0b44b67 | 34 | //* |
mbed714 | 0:331db0b44b67 | 35 | //* ================================================================= |
mbed714 | 0:331db0b44b67 | 36 | //* See http://scmrtos.sourceforge.net for documentation, latest |
mbed714 | 0:331db0b44b67 | 37 | //* information, license and contact details. |
mbed714 | 0:331db0b44b67 | 38 | //* ================================================================= |
mbed714 | 0:331db0b44b67 | 39 | //* |
mbed714 | 0:331db0b44b67 | 40 | //****************************************************************************** |
mbed714 | 0:331db0b44b67 | 41 | |
mbed714 | 0:331db0b44b67 | 42 | #include <usrlib.h> |
mbed714 | 0:331db0b44b67 | 43 | #include <commdefs.h> |
mbed714 | 0:331db0b44b67 | 44 | |
mbed714 | 0:331db0b44b67 | 45 | using namespace usr; |
mbed714 | 0:331db0b44b67 | 46 | |
mbed714 | 0:331db0b44b67 | 47 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 48 | // |
mbed714 | 0:331db0b44b67 | 49 | /// Circular buffer function-member description |
mbed714 | 0:331db0b44b67 | 50 | // |
mbed714 | 0:331db0b44b67 | 51 | // |
mbed714 | 0:331db0b44b67 | 52 | // |
mbed714 | 0:331db0b44b67 | 53 | TCbuf::TCbuf(byte* const Address, const byte Size) : |
mbed714 | 0:331db0b44b67 | 54 | buf(Address), |
mbed714 | 0:331db0b44b67 | 55 | size(Size), |
mbed714 | 0:331db0b44b67 | 56 | count(0), |
mbed714 | 0:331db0b44b67 | 57 | first(0), |
mbed714 | 0:331db0b44b67 | 58 | last(0) |
mbed714 | 0:331db0b44b67 | 59 | { |
mbed714 | 0:331db0b44b67 | 60 | } |
mbed714 | 0:331db0b44b67 | 61 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 62 | bool TCbuf::write(const byte* data, const byte Count) |
mbed714 | 0:331db0b44b67 | 63 | { |
mbed714 | 0:331db0b44b67 | 64 | if( Count > (size - count) ) |
mbed714 | 0:331db0b44b67 | 65 | return false; |
mbed714 | 0:331db0b44b67 | 66 | |
mbed714 | 0:331db0b44b67 | 67 | for(byte i = 0; i < Count; i++) |
mbed714 | 0:331db0b44b67 | 68 | push(*(data++)); |
mbed714 | 0:331db0b44b67 | 69 | |
mbed714 | 0:331db0b44b67 | 70 | return true; |
mbed714 | 0:331db0b44b67 | 71 | } |
mbed714 | 0:331db0b44b67 | 72 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 73 | void TCbuf::read(byte* data, const byte Count) |
mbed714 | 0:331db0b44b67 | 74 | { |
mbed714 | 0:331db0b44b67 | 75 | byte N = Count <= count ? Count : count; |
mbed714 | 0:331db0b44b67 | 76 | |
mbed714 | 0:331db0b44b67 | 77 | for(byte i = 0; i < N; i++) |
mbed714 | 0:331db0b44b67 | 78 | data[i] = pop(); |
mbed714 | 0:331db0b44b67 | 79 | } |
mbed714 | 0:331db0b44b67 | 80 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 81 | byte TCbuf::get_byte(const byte index) const |
mbed714 | 0:331db0b44b67 | 82 | { |
mbed714 | 0:331db0b44b67 | 83 | byte x = first + index; |
mbed714 | 0:331db0b44b67 | 84 | |
mbed714 | 0:331db0b44b67 | 85 | if(x < size) |
mbed714 | 0:331db0b44b67 | 86 | return buf[x]; |
mbed714 | 0:331db0b44b67 | 87 | else |
mbed714 | 0:331db0b44b67 | 88 | return buf[x - size]; |
mbed714 | 0:331db0b44b67 | 89 | } |
mbed714 | 0:331db0b44b67 | 90 | |
mbed714 | 0:331db0b44b67 | 91 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 92 | bool TCbuf::put(const byte item) |
mbed714 | 0:331db0b44b67 | 93 | { |
mbed714 | 0:331db0b44b67 | 94 | if(count == size) |
mbed714 | 0:331db0b44b67 | 95 | return false; |
mbed714 | 0:331db0b44b67 | 96 | |
mbed714 | 0:331db0b44b67 | 97 | push(item); |
mbed714 | 0:331db0b44b67 | 98 | return true; |
mbed714 | 0:331db0b44b67 | 99 | } |
mbed714 | 0:331db0b44b67 | 100 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 101 | byte TCbuf::get() |
mbed714 | 0:331db0b44b67 | 102 | { |
mbed714 | 0:331db0b44b67 | 103 | if(count) |
mbed714 | 0:331db0b44b67 | 104 | return pop(); |
mbed714 | 0:331db0b44b67 | 105 | else |
mbed714 | 0:331db0b44b67 | 106 | return 0; |
mbed714 | 0:331db0b44b67 | 107 | } |
mbed714 | 0:331db0b44b67 | 108 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 109 | // |
mbed714 | 0:331db0b44b67 | 110 | /// \note |
mbed714 | 0:331db0b44b67 | 111 | /// For internal purposes. |
mbed714 | 0:331db0b44b67 | 112 | /// Use this function with care - it doesn't perform free size check. |
mbed714 | 0:331db0b44b67 | 113 | // |
mbed714 | 0:331db0b44b67 | 114 | void TCbuf::push(const byte item) |
mbed714 | 0:331db0b44b67 | 115 | { |
mbed714 | 0:331db0b44b67 | 116 | buf[last] = item; |
mbed714 | 0:331db0b44b67 | 117 | last++; |
mbed714 | 0:331db0b44b67 | 118 | count++; |
mbed714 | 0:331db0b44b67 | 119 | |
mbed714 | 0:331db0b44b67 | 120 | if(last == size) |
mbed714 | 0:331db0b44b67 | 121 | last = 0; |
mbed714 | 0:331db0b44b67 | 122 | } |
mbed714 | 0:331db0b44b67 | 123 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 124 | // |
mbed714 | 0:331db0b44b67 | 125 | /// \note |
mbed714 | 0:331db0b44b67 | 126 | /// For internal purposes. |
mbed714 | 0:331db0b44b67 | 127 | /// Use this function with care - it doesn't perform free size check. |
mbed714 | 0:331db0b44b67 | 128 | // |
mbed714 | 0:331db0b44b67 | 129 | byte TCbuf::pop() |
mbed714 | 0:331db0b44b67 | 130 | { |
mbed714 | 0:331db0b44b67 | 131 | byte item = buf[first]; |
mbed714 | 0:331db0b44b67 | 132 | |
mbed714 | 0:331db0b44b67 | 133 | count--; |
mbed714 | 0:331db0b44b67 | 134 | first++; |
mbed714 | 0:331db0b44b67 | 135 | if(first == size) |
mbed714 | 0:331db0b44b67 | 136 | first = 0; |
mbed714 | 0:331db0b44b67 | 137 | |
mbed714 | 0:331db0b44b67 | 138 | return item; |
mbed714 | 0:331db0b44b67 | 139 | } |
mbed714 | 0:331db0b44b67 | 140 | //------------------------------------------------------------------------------ |