Committer:
segundo
Date:
Thu Nov 11 10:30:39 2010 +0000
Revision:
7:6fab7e5aa489
Parent:
0:d7810ff946c1

        

Who changed what in which revision?

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