I've hit a snag with spi.write() and buffer protocols.
In some cases the data to spi.write() is an int, a const() or bytes.
So I can't just wrap all values with bytearray([...]), otherwise it throws a TypeError (see below) on bytes.
Is there a way to check if the variable is compatible with spi.write() directly, or needs to be wrapped in a bytearray()?
Code: Select all
>>> buf = 10
>>> spi.write(bytearray([buf]))
works
>>> SOME_CONST = const(0x04)
>>> spi.write(bytearray([SOME_CONST]))
works
>>> buf = b'\x05'
>>> spi.write(bytearray([buf]))
TypeError: can't convert bytes to int
>>> buf = b'\x05'
>>> spi.write(bytearray(buf))
works, without the []
>>> buf = 10
>>> spi.write(bytearray(buf))
works, but sends a 10 byte empty bytearray, not what I want
Code: Select all
>>> buf = bytearray([10])
>>> isinstance(buf, bytearray)
True
>>> buf = b'\x05'
>>> isinstance(buf, bytearray)
False
>>> isinstance(buf, bytes)
True
Does that sound like the right thing to do?