Page 1 of 1

How much data does SPI.readinto reads?

Posted: Mon Jan 23, 2017 2:52 pm
by MyUsername
Official documentation says:
SPI.readinto(buf, write=0x00)
Read into the buffer specified by buf while continuously writing the single byte given by write. Returns None.
Note: on WiPy this function returns the number of bytes read.

I wonder how can I specify length of data to be read by SPI.readinto() method? If it is the same as buffer length then what is the purpose for it? I have a board connected to esp which takes commands of different lengths and replies with answers of different lengths. To optimize reading from it I want to use pre-allocated buffer. However this way it will have constant length and I will need to shrink or expand it every time length of replies changes. That is not the optimization.

Re: How much data does SPI.readinto reads?

Posted: Mon Jan 23, 2017 4:55 pm
by dhylands
If you want to use a pre-allocated buffer and create variable length views into that buffer then you should use memoryview.
See: http://forum.micropython.org/viewtopic. ... iew#p10117

And yes SPI uses the length 'buf' to determine how much to read. so if you do something like this:

Code: Select all

buf = bytearray(256)
mv = memoryview(buf)
SPI.readinto(mv[0:15])
then it will do a read of 15 bytes.

Re: How much data does SPI.readinto reads?

Posted: Mon Feb 06, 2017 8:05 pm
by MyUsername
dhylands wrote:If you want to use a pre-allocated buffer and create variable length views into that buffer then you should use memoryview.
See: http://forum.micropython.org/viewtopic. ... iew#p10117

And yes SPI uses the length 'buf' to determine how much to read. so if you do something like this:

Code: Select all

buf = bytearray(256)
mv = memoryview(buf)
SPI.readinto(mv[0:15])
then it will do a read of 15 bytes.
Oh.. Sorry, I have found out about memory view by myself and forgot about this topic. However thanks, it is the very useful feature that I missed before.