allow splash image to be loaded from SD on Horus (#5463)
[opentx.git] / radio / src / fifo.h
blobe24b9ba80007b1a51e05ae99f9532eb1155cf84d
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _FIFO_H_
22 #define _FIFO_H_
24 template <class T, int N>
25 class Fifo
27 static_assert((N > 1) & !(N & (N - 1)), "Fifo size must be a power of two!");
29 public:
30 Fifo():
31 widx(0),
32 ridx(0)
36 void clear()
38 widx = ridx = 0;
41 void push(T element)
43 uint32_t next = (widx+1) & (N-1);
44 if (next != ridx) {
45 fifo[widx] = element;
46 widx = next;
50 bool pop(T & element)
52 if (isEmpty()) {
53 return false;
55 else {
56 element = fifo[ridx];
57 ridx = (ridx+1) & (N-1);
58 return true;
62 bool isEmpty() const
64 return (ridx == widx);
67 bool isFull()
69 uint32_t next = (widx+1) & (N-1);
70 return (next == ridx);
73 void flush()
75 while (!isEmpty()) {};
78 uint32_t size() const
80 return (N + widx - ridx) & (N-1);
83 uint32_t hasSpace(uint32_t n) const
85 return (N > (size() + n));
88 bool probe(T & element) const
90 if (isEmpty()) {
91 return false;
93 else {
94 element = fifo[ridx];
95 return true;
99 protected:
100 T fifo[N];
101 volatile uint32_t widx;
102 volatile uint32_t ridx;
105 #endif // _FIFO_H_