mavriksc
Posts: 1
Joined: Thu May 29, 2014 3:42 pm

losing data when using Spidev xfer()

Thu May 29, 2014 4:01 pm

the following code will set the colors on my ws2801 lights to red blue green and then off. while still going thru the loop. it will continue to display the counter but the lights do nothing. the person i'm working on this with moved the declaration and assignment to inside the loop and it began working. i don't think it's a scope issue as the first time thru the loop it works. i think as part of the xfer2() it is being cleared. Can anyone confirm. It was late and i don't have access now to add some debugging statements now.

Thank you.

Code: Select all

import spidev
import time

red=[0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255]
blue=[255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0]
green=[0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,0,255,0,]

spi = spidev.SpiDev() 
spi.open(0, 1)

x=0
while x<100:
    spi.xfer2(red)
    time.sleep(.1)
    spi.xfer2(blue)
    time.sleep(.1)
    spi.xfer2(green)
    time.sleep(.1)
    print x
    x+=1

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: losing data when using Spidev xfer()

Fri May 30, 2014 7:43 pm

Hello,

try using "list(red)" inside the calls to xfer2. I had same problem and found that xfer2 is writing inside the parameter array. list() is duplicating the array to a copy, so overwriting it does not kill the original.

Regards,
Gerhard

Return to “Python”