spi.write with bytearray

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
tim&struppi
Posts: 2
Joined: Sat Jul 06, 2019 6:59 pm

spi.write with bytearray

Post by tim&struppi » Sat Jul 06, 2019 7:39 pm

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: spi.write with bytearray

Post by Roberthh » Sat Jul 06, 2019 7:50 pm

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, .....

tim&struppi
Posts: 2
Joined: Sat Jul 06, 2019 6:59 pm

Re: spi.write with bytearray

Post by tim&struppi » Sat Jul 06, 2019 10:34 pm

Hello Roberthh
Thanks, it works. :D
I think, i must learn a lot of Basics....

Post Reply