#this is a first step you can try it in

1 messages · Page 1 of 1 (latest)

civic jungle
#

hi

wide canopy
civic jungle
#

Neradoc — Today at 5:22 PM
the big question is how to change:
def _read(self, register, length=1):
result = []
for x in range(length):
GPIO.output(self.spi_cs_gpio, 0)
value = self.spi_dev.xfer2([register + x, 0])
GPIO.output(self.spi_cs_gpio, 1)
result.append(value[1])

into:
def _read(self, register, length=1):
result = []
buffer = bytearray(2) # ?????
for x in range(length):
with self.device as bus:
bus.write_readinto( ????? )
result.append(buffer[1])

#

Do you know how to call it from your code.py?

mark01 — Today at 5:22 PM
scripts?

Neradoc — Today at 5:22 PM
specifically:
value = self.spi_dev.xfer2([register + x, 0])
[5:22 PM]
the code I posted has a test script at the end, when put it code.py
[5:23 PM]
you have to change the spi definition to match your setup
[5:23 PM]
that part
# spi = board.SPI()
spi = busio.SPI(board.GP2, board.GP3, board.GP4)
cs = DigitalInOut(board.GP5)

spovlot — Today at 5:25 PM
Oh. I see you can use this as code.py. Nice.

mark01 — Today at 5:26 PM
how do i save this to thread?

spovlot — Today at 5:26 PM
I didn't see that if name == "main": part.

Neradoc — Today at 5:26 PM
if I understand correctly, this xfer2 call sends one byte as the read command, and then one empty byte, during which the device sends the response byte, which is why it reads value[1], due to how SPI full duplex works. (edited)
[5:27 PM]
I have never used it in CP though, so I'll check if my syntax is correct

@civic jungle
how do i save this to thread?