I just added the loop around check for peekBytes and readBytes, so they fall through to /for loop/ if it's not aligned for memmove
Test code:
Output:
as you can see, the returned "count" was actually 4 when we requested 6, because the size of the queue wasnt A) large enough, and/or B) queue was too small
the last 2 dwords are garbage from printing the local array where only the first 4 values were updated!
Heres the updated v1.1 with the protection added to readBytes and peekBytes with fallback /for loop/ support when memmove wont wrap
View attachment circular_buffer v1.1.zip
the peek() overload really helped the peekBytes method for loop fallback:
Test code:
Code:
Circular_Buffer<uint16_t, 4> cb2;
Serial.println("----------------------");
cb2.write(0x8807);
cb2.write(6);
cb2.write(0x2113);
cb2.write(0x2114);
cb2.write(0x2115);
cb2.write(0x2116);
cb2.write(0x2117);
cb2.write(0x2118);
cb2.write(0x2119);
Serial.print("Available: "); Serial.println(cb2.available());
Serial.println("readbytes:");
uint16_t buf[6];
Serial.print("Count: "); Serial.println(cb2.readBytes(buf, 6));
for ( uint16_t i = 0; i < 6; i++ ) {
Serial.print(buf[i], HEX); Serial.print(" ");
} Serial.println();
Serial.print("Print FIFO: "); Serial.println(cb2.available());
while ( cb2.available() > 0 ) {
Serial.println(cb2.peek(), HEX);
Serial.println(cb2.read(), HEX);
}
Output:
Code:
----------------------
Available: 4
readbytes:
Count: 4
2116 2117 2118 2119 82C0 1FFF
Print FIFO: 0
as you can see, the returned "count" was actually 4 when we requested 6, because the size of the queue wasnt A) large enough, and/or B) queue was too small
the last 2 dwords are garbage from printing the local array where only the first 4 values were updated!
Heres the updated v1.1 with the protection added to readBytes and peekBytes with fallback /for loop/ support when memmove wont wrap
View attachment circular_buffer v1.1.zip
the peek() overload really helped the peekBytes method for loop fallback:
Code:
template<typename T, uint16_t _size>
T Circular_Buffer<T,_size>::peekBytes(T *buffer, uint16_t length) {
uint16_t _count;
( available() < length ) ? _count = available() : _count = length;
if ( _count < ( _size - head ) ) {
memmove(buffer,_cbuf,_count*sizeof(T));
}
[COLOR="#FF0000"][B]else for ( uint16_t i = 0; i < _count; i++ ) buffer[i] = peek(i);[/B][/COLOR]
return _count;
}