PIDUINO
Loading...
Searching...
No Matches
fifo.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 * This file is part of the Piduino Library.
3 *
4 * I2cDev is a modified and simplified version of QIODevice,
5 * from Qt according to the LGPL and Copyright (C) 2015 The Qt Company Ltd.
6 *
7 * The Piduino Library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * The Piduino Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
19 */
20#pragma once
21
22#include <cstring>
23#include <algorithm>
24
25namespace Piduino {
26
27 class Fifo {
28 public:
29
30 Fifo (uint16_t max) : _data (new uint8_t[max]), _size (max), _in (_data), _out (_data) {}
31
33
34 delete[] _data;
35 }
36
37 void clear() {
38
39 _in = _out = _data;
40 }
41
42 uint16_t length() const {
43
44 return (_in - _out) % _size;
45 }
46
47 uint16_t free() const {
48
49 return _size - length();
50 }
51
52 uint16_t size() const {
53
54 return _size;
55 }
56
57 uint8_t * data() const {
58
59 return _data;
60 }
61
62 uint8_t * in() const {
63
64 return _in;
65 }
66
67 uint8_t * out() const {
68
69 return _out;
70 }
71
72 uint16_t pull (uint8_t * buf, uint16_t max) {
73
74 max = std::min (max, length());
75 ::memcpy (buf, _out, max);
76 _out += max;
77 return max;
78 }
79
80 int pull() {
81
82 if ( length()) {
83 return *_out++;
84 }
85 return -1;
86 }
87
88 uint16_t push (const uint8_t * buf, uint16_t len) {
89
90 len = std::min (len, free());
91 ::memcpy (_in, buf, len);
92 _in += len;
93 return len;
94 }
95
96 int push(uint8_t b) {
97 if (free()) {
98 *_in++ = b;
99 return 0;
100 }
101 return -1;
102 }
103
104 void seek (uint16_t len) {
105
106 len = std::min (len, static_cast<uint16_t>(_size - static_cast<uint16_t>(_out - _data)));
107 _in += len;
108 }
109
110 protected:
111 uint8_t * _data;
112 uint16_t _size;
113 uint8_t * _in;
114 uint8_t * _out;
115 };
116
117}
118/* ========================================================================== */
void seek(uint16_t len)
Definition fifo.h:104
uint16_t pull(uint8_t *buf, uint16_t max)
Definition fifo.h:72
uint16_t size() const
Definition fifo.h:52
void clear()
Definition fifo.h:37
uint16_t free() const
Definition fifo.h:47
uint8_t * _in
Definition fifo.h:113
uint8_t * in() const
Definition fifo.h:62
uint8_t * _out
Definition fifo.h:114
int push(uint8_t b)
Definition fifo.h:96
uint8_t * data() const
Definition fifo.h:57
uint16_t _size
Definition fifo.h:112
uint8_t * out() const
Definition fifo.h:67
uint16_t push(const uint8_t *buf, uint16_t len)
Definition fifo.h:88
uint8_t * _data
Definition fifo.h:111
int pull()
Definition fifo.h:80
Fifo(uint16_t max)
Definition fifo.h:30
uint16_t length() const
Definition fifo.h:42
Global namespace for Piduino.
Definition board.h:28