Speed up the RGB Strip Software

The RGB python code is a bit slow at the time of this writing, but a few tweaks can dramatically speed it up.

First edit ledstrip.py to use the hardware SPI, note the argument use_py_spi = True.

My ledstrip.py was located at /home/pi/RPi-LPD8806/raspledstrip/ledstrip.py
Download: file
def __init__(self, leds, use_py_spi = True, dev="/dev/spidev0.0", driver="LPD8806"):
Now inside LPD8806.py, we're going to change the SPI speed to 16MHz.
Download: file
if self.use_py_spi:
	import spidev
	self.spi = spidev.SpiDev()
	self.spi.open(0,0)
	self.spi.max_speed_hz = 16000000
	print 'py-spidev MHz: %d' % (self.spi.max_speed_hz / 1000000.0 )
That print statement is there just to make sure everything gets set correctly.

One final change to the LPD8806.py file is in the update() function. Every call to self.spi.xfer2() seems to have a 160ms delay at the end, so we're simply going to change the update function so that it calls the spi.xfer2() fewer times.
Download: file
def update(self, buffer):
	temp_buffer = []
	if self.use_py_spi:
		for x in range(self.leds):
			temp_buffer = temp_buffer + [i for i in buffer[x]]
			#self.spi.xfer2([i for i in buffer[x]])
		self.spi.xfer2(temp_buffer)
		self.spi.xfer2([0x00,0x00,0x00]) #zero fill the last to prevent stray colors at the end
		self.spi.xfer2([0x00]) #once more with feeling - this helps :) time2 = time.time()
If you re-run the example.py python file, you should see a substantial increase in speed.
This guide was first published on Jan 13, 2014. It was last updated on Jan 13, 2014. This page (Speed up the RGB Strip Software) was last updated on Jul 17, 2019.