Page 1 of 1

Variable length buffer with variable size data.

Posted: Fri Jan 30, 2026 10:33 pm
by mnfisher
An idea...

A data buffer to hold (up to) a fixed number of samples - but the number of samples and the size of the sample (byte, int, or long) - can be decided at run time - not at compile time.

This allows a user selectable range - with more elements stored if using a smaller data size (byte (size 1) cf long (size 4))

This is currently implemented as a Initialise(number_elements, size of elements) - and uses malloc to allocate RAM (this means the MCU and compiler must support malloc - esp32, AVR (for example Arduino) and larger PICs) - the user should check that memory has been allocated (Initialise return true)

AddElement - adds an item to the data buffer - and if the buffer is full - removes the oldest element first.
GetNumberElements() - returns the number of elements.
GetElementAtIdx(returns the element at Idx (this is unspecified if Idx >= number of elements) - if 5 elements then Idx should be 0..4
Free - deletes the buffer (user changes range?)

It could be a component - there is no simulation element (malloc / C doesn't simulate - it would be fairly easy to add a 'fixed' size simulation element using an array)

It also only works (correctly) with unsigned numbers (CHAR, UINT, ULONG) - it could be extended to signed values - I needed to store a 'count' for each sample.

Here as an esp32 example (5 word buffer) - Should also work AOK on an Arduino or other MCU.

Martin