arduino-emulator
Loading...
Searching...
No Matches
RingBuffer.h
1/*
2 RingBuffer.h - Ring buffer implementation
3 Copyright (c) 2014 Arduino. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#ifdef __cplusplus
21
22#ifndef _RING_BUFFER_
23#define _RING_BUFFER_
24
25#include <stdint.h>
26#include <string.h>
27
28namespace arduino {
29
30// Define constants and variables for buffering incoming serial data. We're
31// using a ring buffer (I think), in which head is the index of the location
32// to which to write the next incoming character and tail is the index of the
33// location from which to read.
34#define SERIAL_BUFFER_SIZE 64
35
36template <int N>
38{
39 public:
40 uint8_t _aucBuffer[N] ;
41 volatile int _iHead ;
42 volatile int _iTail ;
43 volatile int _numElems;
44
45 public:
46 RingBufferN( void ) ;
47 void store_char( uint8_t c ) ;
48 void clear();
49 int read_char();
50 int available();
51 int availableForStore();
52 int peek();
53 bool isFull();
54
55 private:
56 int nextIndex(int index);
57 inline bool isEmpty() const { return (_numElems == 0); }
58};
59
61
62
63template <int N>
65{
66 memset( _aucBuffer, 0, N ) ;
67 clear();
68}
69
70template <int N>
71void RingBufferN<N>::store_char( uint8_t c )
72{
73 // if we should be storing the received character into the location
74 // just before the tail (meaning that the head would advance to the
75 // current location of the tail), we're about to overflow the buffer
76 // and so we don't write the character or advance the head.
77 if (!isFull())
78 {
79 _aucBuffer[_iHead] = c ;
80 _iHead = nextIndex(_iHead);
81 _numElems = _numElems + 1;
82 }
83}
84
85template <int N>
86void RingBufferN<N>::clear()
87{
88 _iHead = 0;
89 _iTail = 0;
90 _numElems = 0;
91}
92
93template <int N>
94int RingBufferN<N>::read_char()
95{
96 if (isEmpty())
97 return -1;
98
99 uint8_t value = _aucBuffer[_iTail];
100 _iTail = nextIndex(_iTail);
101 _numElems = _numElems - 1;
102
103 return value;
104}
105
106template <int N>
107int RingBufferN<N>::available()
108{
109 return _numElems;
110}
111
112template <int N>
113int RingBufferN<N>::availableForStore()
114{
115 return (N - _numElems);
116}
117
118template <int N>
119int RingBufferN<N>::peek()
120{
121 if (isEmpty())
122 return -1;
123
124 return _aucBuffer[_iTail];
125}
126
127template <int N>
128int RingBufferN<N>::nextIndex(int index)
129{
130 return (uint32_t)(index + 1) % N;
131}
132
133template <int N>
134bool RingBufferN<N>::isFull()
135{
136 return (_numElems == N);
137}
138
139}
140
141#endif /* _RING_BUFFER_ */
142#endif /* __cplusplus */
Definition DMAPool.h:103
Definition RingBuffer.h:38
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31