3 * Implementation uses arrays to conserve memory
5 * The MIT License (MIT)
7 * Copyright (c) 2015 Daniel Eisterhold
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * Modified/Ammended by Alessandro Carcione 2020
44 void ICACHE_RAM_ATTR
FIFO::push(const uint8_t data
)
46 if (numElements
== FIFO_SIZE
)
48 ERRLN("Buffer full, will flush");
56 tail
= (tail
+ 1) % FIFO_SIZE
;
60 void ICACHE_RAM_ATTR
FIFO::pushBytes(const uint8_t *data
, uint8_t len
)
62 if (numElements
+ len
> FIFO_SIZE
)
64 ERRLN("Buffer full, will flush");
68 for (int i
= 0; i
< len
; i
++)
70 buffer
[tail
] = data
[i
];
71 tail
= (tail
+ 1) % FIFO_SIZE
;
76 uint8_t ICACHE_RAM_ATTR
FIFO::pop()
80 // DBGLN(F("Buffer empty"));
86 uint8_t data
= buffer
[head
];
87 head
= (head
+ 1) % FIFO_SIZE
;
92 void ICACHE_RAM_ATTR
FIFO::popBytes(uint8_t *data
, uint8_t len
)
94 if (numElements
< len
)
96 // DBGLN(F("Buffer underrun"));
102 for (int i
= 0; i
< len
; i
++)
104 data
[i
] = buffer
[head
];
105 head
= (head
+ 1) % FIFO_SIZE
;
110 uint8_t ICACHE_RAM_ATTR
FIFO::peek()
112 if (numElements
== 0)
114 // DBGLN(F("Buffer empty"));
119 uint8_t data
= buffer
[head
];
124 uint16_t ICACHE_RAM_ATTR
FIFO::size()
129 void ICACHE_RAM_ATTR
FIFO::flush()
136 bool ICACHE_RAM_ATTR
FIFO::ensure(uint8_t requiredSize
)
138 if(requiredSize
> FIFO_SIZE
)
140 while(!available(requiredSize
)) {
142 head
= (head
+ len
) % FIFO_SIZE
;