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
25
namespace
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
32
~Fifo
() {
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
/* ========================================================================== */
Piduino::Fifo
Definition
fifo.h:27
Piduino::Fifo::seek
void seek(uint16_t len)
Definition
fifo.h:104
Piduino::Fifo::pull
uint16_t pull(uint8_t *buf, uint16_t max)
Definition
fifo.h:72
Piduino::Fifo::size
uint16_t size() const
Definition
fifo.h:52
Piduino::Fifo::clear
void clear()
Definition
fifo.h:37
Piduino::Fifo::~Fifo
~Fifo()
Definition
fifo.h:32
Piduino::Fifo::free
uint16_t free() const
Definition
fifo.h:47
Piduino::Fifo::_in
uint8_t * _in
Definition
fifo.h:113
Piduino::Fifo::in
uint8_t * in() const
Definition
fifo.h:62
Piduino::Fifo::_out
uint8_t * _out
Definition
fifo.h:114
Piduino::Fifo::push
int push(uint8_t b)
Definition
fifo.h:96
Piduino::Fifo::data
uint8_t * data() const
Definition
fifo.h:57
Piduino::Fifo::_size
uint16_t _size
Definition
fifo.h:112
Piduino::Fifo::out
uint8_t * out() const
Definition
fifo.h:67
Piduino::Fifo::push
uint16_t push(const uint8_t *buf, uint16_t len)
Definition
fifo.h:88
Piduino::Fifo::_data
uint8_t * _data
Definition
fifo.h:111
Piduino::Fifo::pull
int pull()
Definition
fifo.h:80
Piduino::Fifo::Fifo
Fifo(uint16_t max)
Definition
fifo.h:30
Piduino::Fifo::length
uint16_t length() const
Definition
fifo.h:42
Piduino
Global namespace for Piduino.
Definition
board.h:28
include
piduino
fifo.h
Generated by
1.9.8