Skyward boardcore
Loading...
Searching...
No Matches
IRQCircularBuffer.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2018 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Davide Mor
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <miosix.h>
26
27#include <type_traits>
28
29#include "CircularBuffer.h"
30
31using miosix::FastInterruptDisableLock;
32using miosix::FastInterruptEnableLock;
33using miosix::Thread;
34
35namespace Boardcore
36{
37
42template <typename T, unsigned int Size>
44{
45 static_assert(std::is_trivially_copy_constructible<T>::value,
46 "T must be trivially copy constructible!");
47
48public:
52 T& put(const T& elem)
53 {
54 FastInterruptDisableLock d;
55 IRQwakeWaitingThread();
56 return buffer.put(elem);
57 }
58
70 T get(unsigned int i = 0)
71 {
72 FastInterruptDisableLock d;
73 return buffer.get(i);
74 }
75
83 T last()
84 {
85 FastInterruptDisableLock d;
86 return buffer.last();
87 }
88
96 T pop()
97 {
98 FastInterruptDisableLock d;
99 return buffer.pop();
100 }
101
107 size_t count() const
108 {
109 FastInterruptDisableLock d;
110 return buffer.count();
111 }
112
113 bool isEmpty() const
114 {
115 FastInterruptDisableLock d;
116 return buffer.isEmpty();
117 }
118
119 bool isFull() const
120 {
121 FastInterruptDisableLock d;
122 return buffer.isFull();
123 }
124
130 void IRQput(const T& elem)
131 {
132 IRQwakeWaitingThread();
133 buffer.put(elem);
134 }
135
145 void IRQput(const T& elem, bool& hppw)
146 {
147 if (waiting && (waiting->IRQgetPriority() >
148 Thread::IRQgetCurrentThread()->IRQgetPriority()))
149 {
150 hppw = true;
151 }
152
153 IRQwakeWaitingThread();
154 buffer.put(elem);
155 }
156
170 T IRQget(unsigned int i = 0) { return buffer.get(i); }
171
181 T IRQpop() { return buffer.pop(); }
182
190 size_t IRQcount() const { return buffer.count(); }
191
195 bool IRQisEmpty() const { return buffer.isEmpty(); }
196
200 bool IRQisFull() const { return buffer.isFull(); }
201
206 {
207 FastInterruptDisableLock d;
208 while (buffer.isEmpty())
209 {
210 IRQwakeWaitingThread();
211 waiting = Thread::IRQgetCurrentThread();
212
213 Thread::IRQwait();
214 {
215 FastInterruptEnableLock e(d);
216 Thread::yield();
217 }
218 }
219 }
220
227 size_t getSize() const { return Size; }
228
229private:
230 void IRQwakeWaitingThread()
231 {
232 if (waiting)
233 {
234 waiting->IRQwakeup();
235 waiting = 0;
236 }
237 }
238
239 Thread* waiting = nullptr;
240 CircularBuffer<T, Size> buffer;
241};
242
243} // namespace Boardcore
T IRQget(unsigned int i=0)
Gets an element from the buffer, without removing it.
T IRQpop()
Pops the first element in the buffer.
void waitUntilNotEmpty()
Waits until the buffer contains at least one element.
void IRQput(const T &elem)
Puts a copy of the element in the buffer.
size_t count() const
Counts the elements in the buffer.
T pop()
Pops the first element in the buffer.
void IRQput(const T &elem, bool &hppw)
Puts a copy of the element in the buffer.
size_t getSize() const
Returns the maximum number of elements that can be stored in the buffer.
T & put(const T &elem)
Puts a copy of the element in the buffer.
T last()
Returns the last element added in the buffer.
size_t IRQcount() const
Counts the elements in the buffer.
T get(unsigned int i=0)
Gets an element from the buffer, without removing it.
This file includes all the types the logdecoder script will decode.