Go to the documentation of this file.
14 #ifndef INC_MESHBUFFERS_H_
15 #define INC_MESHBUFFERS_H_
35 template <
typename T, u
int8_t BufferSize>
64 if (
endPos == 0) index = BufferSize - 1;
76 if (
endPos == 0) index = BufferSize - 1;
87 bool get(T& item, uint8_t index)
const {
163 return std::move(
buffer[index]);
233 if (index >=
count())
return false;
234 if (index <
count() / 2) {
236 for (uint8_t i = index; i > 0; i--) {
243 for (uint8_t i = index+1; i <
count(); i++) {
270 if (startPos_ >=
count() || itmCount == 0)
return 0;
272 bool lowerHalf = (startPos_ + itmCount / 2) <
count() / 2;
273 uint8_t i = startPos_ + itmCount;
276 itmCount = i - startPos_;
282 for (; i > itmCount; i--) {
289 for (; i <
count(); i++) {
313 if (
count() == 0)
return 0;
314 uint8_t removeCnt = 0;
315 for (uint8_t readPos = 0; readPos <
count(); readPos++) {
321 else if (removeCnt > 0) {
340 for(uint8_t i = 0; i < BufferSize; i++){
354 if (std::is_base_of<String, T>::value) {
356 for (uint8_t i = 0; i <
usedSize; i++) {
357 const String* item = (String*)
getRowPtr(i);
358 ret += item->length() + 4;
362 else if (std::is_base_of<RawPrintable, T>::value) {
364 for (uint8_t i = 0; i <
usedSize; i++) {
394 crc.setInputReverse(
true);
395 crc.setOutputReverse(
true);
396 crc.setPolynomial32();
397 crc.reset(0xFFFFFFFFUL);
400 ret = stream.print(
"MFF:");
406 char ctag[4] = { 0 };
407 for (uint8_t i = 0; i < 4 && customTag[i] != 0; i++) ctag[i] = customTag[i];
408 ret += stream.write(ctag, 4);
409 crc.append((uint8_t*)ctag, 4);
411 ret += stream.write(BufferSize);
412 crc.append(BufferSize);
414 if (std::is_base_of<String, T>::value || std::is_base_of<RawPrintable, T>::value) {
415 ret += stream.write((uint8_t)255);
416 crc.append((uint8_t)255);
419 ret += stream.write((uint8_t)
sizeof(T));
420 crc.append((uint8_t)
sizeof(T));
426 for (uint8_t i = 0; i <
usedSize; i++) {
429 if (std::is_base_of<String, T>::value) {
430 String txt = (String&)*item;
431 uint32_t len = (uint32_t)txt.length();
432 stream.write((uint8_t*)&len, 4);
433 crc.append((uint8_t*)&len, 4);
434 uint32_t bytesCount = (uint32_t)stream.print(txt);
435 crc.append((uint8_t*)txt.c_str(), bytesCount);
438 else if (std::is_base_of<RawPrintable, T>::value) {
441 crc.append(
buffer, (uint32_t)length);
445 uint32_t bytesCount = (uint32_t)stream.write((uint8_t*)item, bytesCount);
446 crc.append((uint8_t*)item, bytesCount);
451 uint32_t CRC_val = crc.getLastCRC(0xFFFFFFFFUL);
452 ret += stream.write((uint8_t*)&CRC_val, 4);
468 ret += stream.print(
"Index|");
472 if (std::is_base_of<InlinePrintable, T>::value) {
475 const T prHeader = T();
485 size_t dashCount = ret;
486 for (
size_t i = 0; i < dashCount; i++) {
487 ret += stream.print(
'-');
489 ret += stream.println();
493 ret += stream.println(
"Empty");
497 for (
int i = 0; i <
usedSize; i++) {
499 char txtBuffer[10] = { 0 };
500 ITOA(i, txtBuffer,
sizeof(txtBuffer) /
sizeof(
char), 10);
502 ret += stream.print(txtBuffer);
503 ret += stream.print(
'|');
513 ret += stream.println(
"Value");
516 size_t dashCount = ret;
517 for (
size_t i = 0; i < dashCount; i++) {
518 ret += stream.print(
'-');
520 ret += stream.println();
524 ret += stream.println(
"Empty");
528 for (
int i = 0; i <
usedSize; i++) {
530 char txtBuffer[10] = { 0 };
531 ITOA(i, txtBuffer,
sizeof(txtBuffer) /
sizeof(
char), 10);
533 ret += stream.print(txtBuffer);
534 ret += stream.print(
'|');
538 ret += stream.println(item);
541 ret += stream.println();
551 if (std::is_base_of<String, T>::value) {
553 for (uint8_t i = 0; i <
usedSize; i++) {
554 const String* item = (String*)
getRowPtr(i);
555 ret += item->length();
558 return sizeof(*this) + ret;
560 else if (std::is_base_of<RawPrintable, T>::value) {
562 for (uint8_t i = 0; i <
usedSize; i++) {
564 ret += rp->
sizeOf() -
sizeof(T);
566 return sizeof(*this) + ret;
568 return sizeof(*this);
588 if (index >= BufferSize)
return -1;
589 uint16_t rawIndex = (
startPos + index) % BufferSize;
663 template <u
int8_t BufferSize>
673 for (uint8_t i = 0; i < MicroFIFO<MeshPacket, BufferSize>::count(); i++) {
689 for (uint8_t i = 0; i < MicroFIFO<MeshPacket, BufferSize>::count(); i++) {
710 uint8_t removeCnt = 0;
711 for (uint8_t readPos = 0; readPos < MicroFIFO<MeshPacket, BufferSize>::count(); readPos++) {
717 else if(removeCnt > 0) {
751 template <u
int8_t ROUTE_BufferSize, u
int8_t FLOOD_BufferSize>
875 if (item.PacketHeader.FrameControl.RoutingEnabled) {
948 if (remCnt == 0)
return 0;
953 startPos -= oldRBCount;
972 if (
count() == 0)
return 0;
1028 return count() == 0;
1158 crc.setInputReverse(
true);
1159 crc.setOutputReverse(
true);
1160 crc.setPolynomial32();
1161 crc.reset(0xFFFFFFFFUL);
1164 ret = stream.print(
"MFF:");
1170 char ctag[4] = { 0 };
1171 for (uint8_t i = 0; i < 4 && customTag[i] != 0; i++) ctag[i] = customTag[i];
1172 ret += stream.write(ctag, 4);
1173 crc.append((uint8_t*)ctag, 4);
1175 ret += stream.write(
size());
1178 ret += stream.write((uint8_t)255);
1179 crc.append((uint8_t)255);
1181 ret += stream.write(
count());
1182 crc.append(
count());
1186 for (uint8_t i = 0; i <
count(); i++) {
1189 if (index < len_p0) {
1198 ret += rp->
printRaw(&stream, [](uint8_t* buffer,
size_t length) {
1199 crc.append(buffer, (uint32_t)length);
1203 uint32_t CRC_val = crc.getLastCRC(0xFFFFFFFFUL);
1204 ret += stream.write((uint8_t*)&CRC_val, 4);
1220 ret += stream.print(
"Index|");
1223 ret += stream.print(
"Prior |");
1225 uint8_t len =
count();
1238 size_t dashCount = ret;
1239 for (
size_t i = 0; i < dashCount; i++) {
1240 ret += stream.print(
'-');
1242 ret += stream.println();
1246 ret += stream.println(
"Empty");
1252 for (
int i = 0; i < len; i++) {
1254 char txtBuffer[10] = { 0 };
1256 ITOA(i, txtBuffer,
sizeof(txtBuffer) /
sizeof(
char), 10);
1258 ret += stream.print(txtBuffer);
1259 ret += stream.print(
'|');
1262 if (index < len_p0) {
1263 ret += stream.print(
" High |");
1269 ret += stream.print(
" Low |");
1303 #endif // !INC_MESHBUFFERS_H_
#define ITOA
Definition: meshConfig.h:17
T buffer[BufferSize]
Definition: meshBuffers.h:639
void restartLostItemsCounter()
Restarts lostItemsCount counter.
Definition: meshBuffers.h:217
uint8_t size() const
Gets maximum count of items, that can be stored in buffer.
Definition: meshBuffers.h:1059
MeshPacketFIFO< ROUTE_BufferSize > routeBuffer
Definition: meshBuffers.h:1299
volatile bool RoutingEnabled
Enables or disables routing. When routing is enabled, packet will contain address of next node,...
Definition: meshPacketFlags.h:51
bool removeAt(uint8_t index)
Removes item at specified index. After removing, count() will return new value.
Definition: meshBuffers.h:232
bool get(MeshPacket &item, uint8_t index) const
Gets item from the buffer by index.
Definition: meshBuffers.h:817
Interface for printing derived class collection as table with header and rows.
Definition: meshHelper.h:134
void push(T const &item)
Pushes item after last item in buffer. When buffer is full, first (oldest) item will be overwritten a...
Definition: meshBuffers.h:122
uint8_t highSize() const
Gets maximum count of items, that can be stored in high priority buffer.
Definition: meshBuffers.h:1067
int16_t getIndexOfPacket(uint8_t ID, uint8_t source) const
Gets index of packet from unique packet identifier, which consists from ID and packet source address.
Definition: meshBuffers.h:1017
void clear()
Clears whole buffer.
Definition: meshBuffers.h:339
virtual size_t printRaw(OSTREAM &stream, const char *customTag)
Prints raw FIFO data through stream. Those data can be decoded in PC or in another MCU.
Definition: meshBuffers.h:392
MeshPacket & operator[](int index)
Gets item from the buffer by index.
Definition: meshBuffers.h:847
virtual size_t sizeOf() const
Gets size of whole current instance uncompressed (including dynamically allocated memory) in bytes.
Definition: meshHelper.h:168
const T * getRowPtr(uint8_t index) const
Gets pointer to the item in buffer by index.
Definition: meshBuffers.h:598
const MeshPacket & front() const
Gets the first (oldest with the highest priority) item from the buffer.
Definition: meshBuffers.h:760
uint8_t removeByDestination(uint8_t destination)
Removes all packets with specified destination.
Definition: meshBuffers.h:986
size_t printTo(Print &p) const override
Makes this class printable.
Definition: meshBuffers.h:576
const T & front() const
Gets the first (oldest) item from the buffer.
Definition: meshBuffers.h:44
T & back()
Gets the last (newest) item from the buffer.
Definition: meshBuffers.h:74
uint8_t startPos
Definition: meshBuffers.h:640
MeshPacketHeader PacketHeader
Mesh packet header.
Definition: meshPacket.h:192
const T & back() const
Gets the last (newest) item from the buffer.
Definition: meshBuffers.h:62
uint8_t count() const
Gets how many items are stored in buffer.
Definition: meshBuffers.h:1035
const MeshPacket & operator[](int index) const
Gets item from the buffer by index.
Definition: meshBuffers.h:833
uint8_t usedSize
Definition: meshBuffers.h:642
void alignText(char *text, int size, char fill, TextAlignment alignment)
Aligns text in char array.
Definition: meshHelper.cpp:97
Optimized FIFO buffer for microcontrolers, that can store only MeshPacket structure.
Definition: meshBuffers.h:664
virtual size_t printRaw(OSTREAM &stream, const char *customTag)
Prints raw FIFO data through stream. Those data can be decoded in PC or in another MCU.
Definition: meshBuffers.h:1156
T & operator[](int index)
Gets item from the buffer by index.
Definition: meshBuffers.h:112
virtual size_t printRaw(OSTREAM *stream, void(*CRC_calculation)(uint8_t *, size_t)) const
Prints current instance into stream in raw bytes.
Definition: meshHelper.h:178
uint8_t removeMulti(uint8_t startPos, uint8_t itmCount)
Removes number of item from specified index. After removing, count() will return new value.
Definition: meshBuffers.h:943
This file contains class which can be used to handling mesh packets.
virtual size_t print(OSTREAM &stream, bool showIndexes=true) const
Prints items as table from FIFO buffer to stream.
Definition: meshBuffers.h:465
@ T_LEFT
Definition: meshHelper.h:116
uint8_t removeIf(bool(*dec)(const MeshPacket &))
Removes specified items.
Definition: meshBuffers.h:971
uint8_t count() const
Gets how many items are stored in buffer.
Definition: meshBuffers.h:194
bool get(T &item, uint8_t index) const
Gets item from the buffer by index.
Definition: meshBuffers.h:87
uint8_t lowCount() const
Gets how many items are stored in low priority buffer.
Definition: meshBuffers.h:1051
void push(MeshPacket const &item)
Pushes item after last item in buffer. When buffer is full, first (oldest with the highest priority) ...
Definition: meshBuffers.h:860
Class, that stores mesh packet with it's data. The size of this class can be slightly bigger then rea...
Definition: meshPacket.h:158
uint8_t removeMulti(uint8_t startPos_, uint8_t itmCount)
Removes number of item from specified index. After removing, count() will return new value.
Definition: meshBuffers.h:269
Interface for converting class or structure instance to raw bytes, which can be sent.
Definition: meshHelper.h:161
uint8_t lostItemsCount() const
Definition: meshBuffers.h:210
size_t getRawDataSize() const
Calculates, how many bytes will be printed, when using command printRaw.
Definition: meshBuffers.h:1135
Optimized FIFO buffer for microcontrolers, that can store only MeshPacket structure....
Definition: meshBuffers.h:752
bool empty() const
Checks if buffer is empty.
Definition: meshBuffers.h:186
MeshPacket && popMove()
Reads and removes first (oldest with the highest priority) item from the buffer. When buffer is empty...
Definition: meshBuffers.h:888
virtual size_t print(OSTREAM &stream, bool showIndexes=true) const
Prints items as table from FIFO buffer to stream.
Definition: meshBuffers.h:1217
bool pop()
Removes first (oldest with the highest priority) item from the buffer.
Definition: meshBuffers.h:902
uint8_t removeIf(bool(*dec)(const T &))
Removes specified items.
Definition: meshBuffers.h:312
#define OSTREAM
Definition: meshConfig.h:41
uint8_t lowSize() const
Gets maximum count of items, that can be stored in low priority buffer.
Definition: meshBuffers.h:1075
uint8_t lostItemsCount() const
Definition: meshBuffers.h:1083
const uint8_t size() const
Gets maximum count of items, that can be stored in buffer.
Definition: meshBuffers.h:202
T * getRowPtr(uint8_t index)
Gets pointer to the item in buffer by index.
Definition: meshBuffers.h:611
MeshPacketFIFO< FLOOD_BufferSize > floodBuffer
Definition: meshBuffers.h:1300
virtual size_t getRawSize() const
Gets size of data, that will be printed to stream using printRaw() in bytes.
Definition: meshHelper.h:186
bool empty() const
Checks if buffer is empty.
Definition: meshBuffers.h:1027
const MeshPacketFIFO< ROUTE_BufferSize > & lowFIFO() const
Gets reference to low priority FIFO.
Definition: meshBuffers.h:1127
virtual size_t printHeader(OSTREAM *stream) const
Prints header of table.
Definition: meshHelper.h:142
uint8_t highCount() const
Gets how many items are stored in high priority buffer.
Definition: meshBuffers.h:1043
bool existsID(uint8_t ID, uint8_t source) const
Checks if there is packet in buffer with specified ID and source address, which creates unique packet...
Definition: meshBuffers.h:672
void restartLostItemsCounter()
Restarts lostItemsCount counter.
Definition: meshBuffers.h:1090
int16_t getIndexOfPacket(uint8_t ID, uint8_t source) const
Gets index of packet from unique packet identifier, which consists from ID and packet source address.
Definition: meshBuffers.h:688
void clear()
Clears whole buffer.
Definition: meshBuffers.h:1098
virtual size_t sizeOf() const
Gets size of the whole FIFO in bytes.
Definition: meshBuffers.h:550
size_t getRawDataSize() const
Calculates, how many bytes will be printed, when using command printRaw.
Definition: meshBuffers.h:353
MeshPacket & back()
Gets the last (newest with the lowest priority) item from the buffer.
Definition: meshBuffers.h:802
virtual size_t sizeOf() const
Gets size of the whole FIFO in bytes.
Definition: meshBuffers.h:1284
bool pop()
Removes first (oldest) item from the buffer.
Definition: meshBuffers.h:171
Optimized FIFO buffer for microcontrolers.
Definition: meshBuffers.h:36
bool removeAt(uint8_t index)
Removes item at specified index. After removing, count() will return new value.
Definition: meshBuffers.h:922
uint8_t removeByDestination(uint8_t destination)
Removes all packets with specified destination.
Definition: meshBuffers.h:708
const T & operator[](int index) const
Gets item from the buffer by index.
Definition: meshBuffers.h:101
size_t printTo(Print &p) const override
Makes this class printable.
Definition: meshBuffers.h:1293
bool isRawIndexOccupied(uint8_t rawIndex) const
Checks if there is item stored at raw index.
Definition: meshBuffers.h:624
int16_t getRawIndex(uint8_t index) const
Gets raw index in buffer from index. Index starts from startPos. Raw index starts from the beginning ...
Definition: meshBuffers.h:587
const MeshPacket & back() const
Gets the last (newest with the lowest priority) item from the buffer.
Definition: meshBuffers.h:788
uint8_t lostItems
Definition: meshBuffers.h:643
T & front()
Gets the first (oldest) item from the buffer.
Definition: meshBuffers.h:53
MeshPacketFIFO< ROUTE_BufferSize > & lowFIFO()
Gets reference to low priority FIFO.
Definition: meshBuffers.h:1120
MeshPacket & front()
Gets the first (oldest with the highest priority) item from the buffer.
Definition: meshBuffers.h:774
T && popMove()
Reads and removes first (oldest) item from the buffer. When buffer is empty, exception may be thrown....
Definition: meshBuffers.h:159
const MeshPacketFIFO< ROUTE_BufferSize > & highFIFO() const
Gets reference to high priority FIFO.
Definition: meshBuffers.h:1113
void push(T &&item)
Pushes item after last item in buffer. When buffer is full, first (oldest) item will be overwritten a...
Definition: meshBuffers.h:140
MeshPacketFIFO< ROUTE_BufferSize > & highFIFO()
Gets reference to high priority FIFO.
Definition: meshBuffers.h:1106
bool existsID(uint8_t ID, uint8_t source) const
Checks if there is packet in buffer with specified ID and source address, which creates unique packet...
Definition: meshBuffers.h:997
uint8_t endPos
Definition: meshBuffers.h:641
virtual size_t printLine(OSTREAM *stream) const
Prints this data as row to table.
Definition: meshHelper.h:152
void push(MeshPacket &&item)
Pushes item after last item in buffer. When buffer is full, first (oldest with the highest priority) ...
Definition: meshBuffers.h:874