How much data does SPI.readinto reads?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
MyUsername
Posts: 5
Joined: Mon Jan 02, 2017 4:06 pm

How much data does SPI.readinto reads?

Post by MyUsername » Mon Jan 23, 2017 2:52 pm

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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How much data does SPI.readinto reads?

Post by dhylands » Mon Jan 23, 2017 4:55 pm

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.

MyUsername
Posts: 5
Joined: Mon Jan 02, 2017 4:06 pm

Re: How much data does SPI.readinto reads?

Post by MyUsername » Mon Feb 06, 2017 8:05 pm

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.

Post Reply