Hi All,
following the latest feedback I received from some of you, I understood that I must implement a feature enabling the management of binary data.
This could be done using strings but actually the strings are managed using the classic 'C' approach that uses the character ASCII 0 (NUL) as end of string.
The advantage is that they are very easy to manage and have a low memory footprint.
The drawback is that they inhibit the use of the character (NUL) as it will be considered as end of string.
For example, the string
A B C D (NUL) E F G H I (NUL)
Will be cut at the first (NUL) and then considered as
A B C D
If the (NUL) appears at the first position of the string, all the string will be also considered null
For example
(NUL) A B C D (NUL)
will be considered as empty string.
I would like to receive your advice and suggestions on what I try to explain below before starting to code that into Annex and Annex32.
The idea is the creation of a new kind of "object", the IObuffer.
The principle of this object is the capability to hold and manage binary data.
Ideally, it will be interfaced with any function requiring binary data handling.
Basically the IObuffer is a piece of RAM memory that can be written and read byte per byte or as a block.
It has a defined length and can be freely dimensioned and cleared.
There will be several IObuffers available, let's say up-to 5 (numbered from 0 to 4).
This is specified in the following examples with 'buffer_num'
The IObuffer will have the following functions / commands :
IObuff.DIM(buffer_num, length) ' dimension the buffer; it can be used again to resize the buffer
IObuff.CLEAR(buffer_num) ' clear the buffer and returns the ram back
IObuff.WRITE(buffer_num, position, value) ' write 'value' at the position 'position'
' value is a byte and position can be from 0
' to the length of the buffer-1
a = IObuff.READ(buffer_num, position) ' read a byte at the position 'position'
Another possibility could be to use it a kind of "special" array and use like this :
DIM IObuff(buffer_num, length) ' dimension
a = IObuff(buffer_num, position) ' read
IObuff(buffer_num, position) = a 'write
DIM IObuff(buffer_num, 0) ' clear
Then the IOBUFFERS could be interfaced like that :
With UDP :
UDP.READ_IOBUFF buffer_num ' receive the message into the IObuffer
UDP.WRITE_IOBUFF buffer_num, IP, port ' write the buffer from the IObuffer
UDP.REPLY_IOBUFF buffer_num ' reply from the IObuffer
With FILES:
' save the buffer
FILE.SAVE_IOBUFF buffer_num, "/buff.bin"
' append the buffer
FILE.APPEND_IOBUFF buffer_num, "/buff2.bin"
' read the buffer starting from the file position 0 for 500 bytes
FILE.READ_IOBUFF buffer_num, "/buff2.bin", 0, 500
' read the buffer starting from the file position 20 for 1500 bytes
FILE.READ_IOBUFF buffer_num, "/buff2.bin", 20, 1500
A little example
FILE.READ_IOBUFF 0, "/myfile.bin", 500, 1000 ' read 1000 bytes starting from the position 500
IObuff.DIM(0, 2) ' redimension the buffer at 2 bytes
IObuff.WRITE(0, 0, 8) ' write 8 at the position 0
IObuff.WRITE(0, 1, 0 ) ' write 0 at the position 1
FILE.SAVE_IOBUFF 0, "/myfile2.bin" ' save the buffer
With Serial port:
print IObuff(buffer_num) 'print all the content of the buffer
print IObuff(buffer_num, pos) 'print only the byte at the position 'pos' of the buffer
Serial.READ_IOBUFF(buffer_num) ' read all the characters present in the input buffer of the serial port in the IObuffer
The same principle shall be applied with a similar syntax for I2C, SPI, etc .
NOTE:
The IObuffer, when used to RECEIVE data (for example in FILE.READ_IOBUFF) will be automatically dimensioned.
When used again to read data, it will be automatically redimensioned to fit with the received data.
To know how many data have been received, the function IObuff.LEN(buffer_num) shall be used.
The IObuffer dimensioning will be required only for writing purposes and, if used for receiving data,
will be automatically redimensioned to fit with the received data.
The IObuffers will be automatically cleared (removed from the memory) when the program is run.
The size of the IOBuffer will be limited only by the free RAM available (that can be up-to 4MBytes for the ESP32 with PSRAM)
Please do not hesitate to give your feedback / advices / suggestions / points of view.
This should be a useful improvement for the benefit of all the users.
Thanks
cicciocb