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.
How much data does SPI.readinto reads?
Re: How much data does SPI.readinto reads?
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:then it will do a read of 15 bytes.
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])
-
- Posts: 5
- Joined: Mon Jan 02, 2017 4:06 pm
Re: How much data does SPI.readinto reads?
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.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:then it will do a read of 15 bytes.Code: Select all
buf = bytearray(256) mv = memoryview(buf) SPI.readinto(mv[0:15])