Skip to content

Commit

Permalink
Python2 to Python3
Browse files Browse the repository at this point in the history
Some changes to make the code PEP8 compilant
  • Loading branch information
gu1ll0me committed Oct 28, 2014
1 parent fc05488 commit 76784e5
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 276 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ Here is a basic program that will fill the entire strip red

from raspledstrip.ledstrip import *
led = LEDStrip(32)
led.fillRGB(255,0,0)
led.fill_rgb(255,0,0)
led.update()

LEDStrip() initializes the class to control the strip and takes the number of LEDs as the argument. The arguments for fillRGB() are Red (0-255), Blue (0-255) and Green (0-255). Finally update() writes the colors out to the strip. The LED strip won't change until update is called (common mistake).
LEDStrip() initializes the class to control the strip and takes the number of LEDs as the argument. The arguments for fill_rgb() are Red (0-255), Blue (0-255) and Green (0-255). Finally update() writes the colors out to the strip. The LED strip won't change until update is called (common mistake).

Animations
----
Expand All @@ -71,13 +71,13 @@ Download, extract, then run the help:



* The LPD8806 chip does not seem to really specify in what order the color channels are, so there is a helper function in case yours happen to be different. The most common seems to be GRB order but I have found some strips that use BRG order as well. If yours (like the one's from Adafruit) use GRB order nothing needs to be done as this is the default. But if the channels are swapped call the method setChannelOrder() with the proper ChannelOrder value. Those are the only two I've ever encountered, but if anyone ever encounters another, please let me know so I can add it.
* The LPD8806 chip does not seem to really specify in what order the color channels are, so there is a helper function in case yours happen to be different. The most common seems to be GRB order but I have found some strips that use BRG order as well. If yours (like the one's from Adafruit) use GRB order nothing needs to be done as this is the default. But if the channels are swapped call the method set_channel_order() with the proper ChannelOrder value. Those are the only two I've ever encountered, but if anyone ever encounters another, please let me know so I can add it.

* All of the animations are designed to allow you to do other things on the same thread in between frames. So, everytime you want to actually progress the animation, call it's method and then call update() to push the data to the the strip. You could do any other processing on the buffer before pushing the update if needed. Each animation has a step variable that can be manually reset or modified externally. See variables in the __init__ of LEDStrip

* If any of the built in animations are not enough you can use any of the set or fill methods to manually manipulate the strip data.

* These strips can get extremely bright (the above video was filmed using 50% brightness) so you can use setMasterBrightness() to set a global level which all output values are multiplied by. This way you don't have to manually modify all of the RGB values to adjust the levels. However, Color takes an optional brightness value so that it can be set on an individual level. Last, if using HSV, you can just set it's "Value" component to adjust the brightness level.
* These strips can get extremely bright (the above video was filmed using 50% brightness) so you can use set_master_brightness() to set a global level which all output values are multiplied by. This way you don't have to manually modify all of the RGB values to adjust the levels. However, Color takes an optional brightness value so that it can be set on an individual level. Last, if using HSV, you can just set it's "Value" component to adjust the brightness level.

* ColorHSV is there for easily fading through a natural color progression. However, all methods take a Color object, so call ColorHSV.getColorRGB() before passing to any of the set, fill, or animation methods.

Expand Down
17 changes: 8 additions & 9 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
and checks needed by any script
"""

from time import sleep
from raspledstrip.ledstrip import *
from raspledstrip.animation import *

import os.path
import sys

from raspledstrip.ledstrip import *
from raspledstrip.animation import *

# Check that the system is set up like we want it
dev = '/dev/spidev0.0'

Expand All @@ -28,10 +27,10 @@
""")
sys.exit(2)

#permissions check
# permissions check
try:
open(dev)
except IOError as e:
except IOError as e:
if e.errno == 13:
sys.stderr.write("""
It looks like SPI device /dev/spidev0.0 has the wrong permissions.
Expand All @@ -42,8 +41,8 @@
""")
sys.exit(2)

num = 36 * 10;
num = 36 * 10
led = LEDStrip(num)
#led.setChannelOrder(ChannelOrder.BRG) #Only use this if your strip does not use the GRB order
#led.setMasterBrightness(0.5) #use this to set the overall max brightness of the strip
# led.set_channel_order(ChannelOrder.BRG) #Only use this if your strip does not use the GRB order
# led.set_master_brightness(0.5) #use this to set the overall max brightness of the strip
led.all_off()
61 changes: 29 additions & 32 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from bootstrap import *

#setup colors to loop through for fade
# setup colors to loop through for fade
colors = [
(255.0,0.0,0.0),
(0.0,255.0,0.0),
(0.0,0.0,255.0),
(255.0,255.0,255.0),
(255.0, 0.0, 0.0),
(0.0, 255.0, 0.0),
(0.0, 0.0, 255.0),
(255.0, 255.0, 255.0),
]

step = 0.01
Expand All @@ -18,46 +18,46 @@
while level >= 0.0:
led.fill(Color(r, g, b, level))
led.update()
if(level >= 0.99):
if level >= 0.99:
dir = -step
level += dir
#sleep(0.005)
# sleep(0.005)

led.all_off()

#animations - each animation method moves the animation forward one step on each call
#after each step, call update() to push it to the LED strip
#sin wave animations
# animations - each animation method moves the animation forward one step on each call
# after each step, call update() to push it to the LED strip
# sin wave animations
anim = Wave(led, Color(255, 0, 0), 4)
for i in range(led.lastIndex):
anim.step()
led.update()
#sleep(0.15)
# sleep(0.15)

anim = Wave(led, Color(0, 0, 100), 2)
for i in range(led.lastIndex):
anim.step()
led.update()
#sleep(0.15)
# sleep(0.15)


#rolling rainbow
# rolling rainbow
anim = Rainbow(led)
for i in range(384):
anim.step()
led.update()

led.fillOff()
led.fill_off()

#evenly distributed rainbow
# evenly distributed rainbow
anim = RainbowCycle(led)
for i in range(384*2):
for i in range(384 * 2):
anim.step()
led.update()

led.fillOff()
led.fill_off()

#setup colors for wipe and chase
# setup colors for wipe and chase
colors = [
Color(255, 0, 0),
Color(0, 255, 0),
Expand All @@ -71,36 +71,33 @@
for i in range(num):
anim.step()
led.update()
#sleep(0.03)
# sleep(0.03)

led.fillOff()
led.fill_off()

for c in range(4):
anim = ColorChase(led, colors[c])

for i in range(num):
anim.step()
led.update()
#sleep(0.03)
# sleep(0.03)

led.fillOff()
led.fill_off()

#scanner: single color and changing color
# scanner: single color and changing color
anim = LarsonScanner(led, Color(255, 0, 0))
for i in range(led.lastIndex*4):
for i in range(led.lastIndex * 4):
anim.step()
led.update()
#sleep(0.03)
# sleep(0.03)

led.fillOff()
led.fill_off()

anim = LarsonRainbow(led, 2, 0.5)
for i in range(led.lastIndex*4):
for i in range(led.lastIndex * 4):
anim.step()
led.update()
#sleep(0.03)

led.all_off()


# sleep(0.03)

led.all_off()
4 changes: 1 addition & 3 deletions off.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

from bootstrap import *

led.all_off()


led.all_off()
18 changes: 9 additions & 9 deletions raspledstrip/LPD8806.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
class LPD8806(object):
"""Main driver for LPD8806 based LED strips"""

def __init__(self, leds, use_py_spi = False, dev="/dev/spidev0.0"):
def __init__(self, leds, use_py_spi=False, dev="/dev/spidev0.0"):
self.leds = leds
self.dev = dev
self.use_py_spi = use_py_spi

if self.use_py_spi:
import spidev
self.spi = spidev.SpiDev()
self.spi.open(0,0)
self.spi.open(0, 0)
self.spi.max_speed_hz = 12000000
print 'py-spidev MHz: %d' % (self.spi.max_speed_hz / 1000000.0 )
print('py-spidev MHz: %d' % (self.spi.max_speed_hz / 1000000.0))
else:
self.spi = open(self.dev, "wb")

#Push new data to strand
# Push new data to strand
def update(self, buffer):
if self.use_py_spi:
for x in range(self.leds):
self.spi.xfer2([i for i in buffer[x]])

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 :)
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 :)
else:
for x in range(self.leds):
self.spi.write(buffer[x])
self.spi.flush()
#seems that the more lights we have the more you have to push zeros
#not 100% sure why this is yet, but it seems to work
self.spi.write(bytearray(b'\x00\x00\x00')) #zero fill the last to prevent stray colors at the end
# seems that the more lights we have the more you have to push zeros
# not 100% sure why this is yet, but it seems to work
self.spi.write(bytearray(b'\x00\x00\x00')) # zero fill the last to prevent stray colors at the end
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()
Expand Down
Loading

0 comments on commit 76784e5

Please sign in to comment.