mlot / nanopb

Automatically exported from code.google.com/p/nanopb
zlib License
0 stars 0 forks source link

Circular Buffer support #55

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Is it possible to support encoding/decoding to a circular/ring buffer? Maybe 
have a callback for when the encoder/decoder reaches the end of the buffer and 
needs more data or for the buffer to be emptied? I'm dealing with reading from 
flash memory and would prefer if I could keep the read buffer to a minimum set 
size.

Original issue reported on code.google.com by ScottALyons on 2 Feb 2013 at 8:30

GoogleCodeExporter commented 9 years ago
This is probably possible using the stream mechanism currently in place.
http://koti.kapsi.fi/~jpa/nanopb/docs/concepts.html#streams

If your flash memory supports random-access, you can just implement your own 
pb_istream_t callback and fill the buffer there. The buffer passed to stream 
callbacks is either a 1-10 byte buffer allocated on stack or a buffer contained 
in the destination structure itself.

If your flash memory is a SD card or other medium where it's more efficient to 
read larger blocks, I suggest the following:
1) Have your own stream callback and have it keep a block-sized buffer.
2) When stream callback is called:
2a) If block buffer is empty, read a new block.
2b) Copy data from block buffer to destination buffer.
2c) If enough data has been read, return. Otherwise repeat from 2a.

Original comment by Petteri.Aimonen on 2 Feb 2013 at 9:05

GoogleCodeExporter commented 9 years ago
I'm closing this as from my perspective the current support for stream 
callbacks is sufficient.

If you have more information, don't hesitate to comment or re-open.

Original comment by Petteri.Aimonen on 7 Feb 2013 at 3:28