source: rtos_arduino/trunk/arduino_lib/hardware/arduino/samd/cores/arduino/RingBuffer.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 1.9 KB
Line 
1/*
2 Copyright (c) 2011 Arduino. All right reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 See the GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "RingBuffer.h"
20#include <string.h>
21
22RingBuffer::RingBuffer( void )
23{
24 memset( _aucBuffer, 0, SERIAL_BUFFER_SIZE ) ;
25 clear();
26}
27
28void RingBuffer::store_char( uint8_t c )
29{
30 int i = nextIndex(_iHead);
31
32 // if we should be storing the received character into the location
33 // just before the tail (meaning that the head would advance to the
34 // current location of the tail), we're about to overflow the buffer
35 // and so we don't write the character or advance the head.
36 if ( i != _iTail )
37 {
38 _aucBuffer[_iHead] = c ;
39 _iHead = i ;
40 }
41}
42
43void RingBuffer::clear()
44{
45 _iHead = 0;
46 _iTail = 0;
47}
48
49int RingBuffer::read_char()
50{
51 if(_iTail == _iHead)
52 return -1;
53
54 uint8_t value = _aucBuffer[_iTail];
55 _iTail = nextIndex(_iTail);
56
57 return value;
58}
59
60int RingBuffer::available()
61{
62 int delta = _iHead - _iTail;
63
64 if(delta < 0)
65 return SERIAL_BUFFER_SIZE + delta;
66 else
67 return delta;
68}
69
70int RingBuffer::peek()
71{
72 if(_iTail == _iHead)
73 return -1;
74
75 return _aucBuffer[_iTail];
76}
77
78int RingBuffer::nextIndex(int index)
79{
80 return (uint32_t)(index + 1) % SERIAL_BUFFER_SIZE;
81}
82
83bool RingBuffer::isFull()
84{
85 return (nextIndex(_iHead) == _iTail);
86}
Note: See TracBrowser for help on using the repository browser.