Page 1 of 1

spi.write with bytearray

Posted: Sat Jul 06, 2019 7:39 pm
by tim&struppi
Hello
I am new in this forum and new in micropython. First, no code tags or BBCode is working in my post? :shock: Or i am to stuppid
I want to send a byte over spi.write, stored in an bytearray. Here are my testcode:

Code: Select all

from machine import Pin, SPI
import time

spi = SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(5), mosi=Pin(16), miso=Pin(4))

latch = Pin(12, Pin.OUT)
latch.on()

a = bytearray(2)
a[0] = 0b00000000
a[1] = 0b00001111

latch.off()
spi.write(a)    
latch.on()
spi.write(a) works. I think two bytes are send over spi. But i want something like this:
spi.write(a[0])
This doest work. This are the message from REPL: TypeError: object with buffer protocol required

Any idea?
Thanks

Re: spi.write with bytearray

Posted: Sat Jul 06, 2019 7:50 pm
by Roberthh
spi.write() expects a bytearray or bytes object as argument, a[0] is an integer.
a[:1] would again by a bytearray with a[0] as content. bytes([a[0]]) would do the same, .....

Re: spi.write with bytearray

Posted: Sat Jul 06, 2019 10:34 pm
by tim&struppi
Hello Roberthh
Thanks, it works. :D
I think, i must learn a lot of Basics....