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
Variable length buffer with variable size data.
-
mnfisher
- Valued Contributor
- Posts: 1825
- http://meble-kuchenne.info.pl
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 153 times
- Been thanked: 862 times
Variable length buffer with variable size data.
- Attachments
-
- var_buffer.fcfx
- (17.8 KiB) Downloaded 4 times