Skip to content

Commit

Permalink
moved files into proper package and created setup.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
adammhaile committed Dec 12, 2013
1 parent cf0c366 commit f8a0622
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 357 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The Raspberry Pi cannot even come close to this so a larger power supply is requ

Also, while it *should* work without it to be safe you should add a level converter between the Raspberry Pi and the strip's data lines. This will also help you have longer runs.

In some cases, using py-spidev can have better performance. To install, run the following commands:
In some cases, using py-spidev can have better performance but is compeltely optional. To install, run the following commands:

sudo apt-get install python-dev
git clone https://github.com/doceme/py-spidev.git
Expand All @@ -37,13 +37,14 @@ Assuming your Raspberry Pi has a connection to the internet, run the following.

git clone https://github.com/adammhaile/RPi-LPD8806.git
cd RPi-LPD8806
python setup.py install
python example.py

You should see your LED strip run through a number of animations.

Here is a basic program that will fill the entire strip red

from LPD8806 import *
from raspledstrip.ledstrip import *
led = LEDStrip(32)
led.fillRGB(255,0,0)
led.update()
Expand Down
6 changes: 3 additions & 3 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"""

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

import os.path
import sys
Expand Down Expand Up @@ -44,6 +44,6 @@

num = 36 * 10;
led = LEDStrip(num)
led.setChannelOrder(ChannelOrder.BRG) #Only use this if your strip does not use the GRB order
#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.all_off()
45 changes: 1 addition & 44 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
#!/usr/bin/python

from time import sleep
from LPD8806 import *
from animation import *

import os.path
import sys

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

if not os.path.exists(dev):
sys.stderr.write("""
The SPI device /dev/spidev0.0 does not exist. You may need to load
the appropriate kernel modules. Try:
sudo modprobe spi_bcm2708 ; sudo modprobe spidev
You may also need to unblacklist the spi_bcm2708 module in
/etc/modprobe.d/raspi-blacklist.conf
""")
sys.exit(2)

#permissions check
try:
open(dev)
except IOError as e:
if e.errno == 13:
sys.stderr.write("""
It looks like SPI device /dev/spidev0.0 has the wrong permissions.
Try making it world writable:
sudo chmod a+rw /dev/spidev0.0
""")
sys.exit(2)



num = 32;
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.all_off()
from bootstrap import *

#setup colors to loop through for fade
colors = [
Expand Down
61 changes: 0 additions & 61 deletions halloween.py

This file was deleted.

6 changes: 1 addition & 5 deletions off.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/python

from time import sleep
from LPD8806 import *
from bootstrap import *

num = 36*5*2;
led = LEDStrip(num)
led.setChannelOrder(ChannelOrder.BRG)
led.all_off()


File renamed without changes.
39 changes: 39 additions & 0 deletions raspledstrip/LPD8806.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class LPD8806(object):
"""Main driver for LPD8806 based LED strips"""

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.max_speed_hz = 12000000
print 'py-spidev MHz: %d' % (self.spi.max_speed_hz / 1000000.0 )
else:
self.spi = open(self.dev, "wb")

#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 :)
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
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()
File renamed without changes.
27 changes: 16 additions & 11 deletions animation.py → raspledstrip/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ def step(self, amt = 1):
self._led.set(self._start + i, wheel_color(color))

self._step += amt
if self._step > 384:
self._step = 0
overflow = self._step - 384
if overflow >= 0:
self._step = overflow

class RainbowCycle(BaseAnimation):
"""Generate rainbow wheel equally distributed over strip."""
Expand All @@ -76,8 +77,9 @@ def step(self, amt = 1):
self._led.set(self._start + i, wheel_color(color))

self._step += amt
if self._step > 384:
self._step = 0
overflow = self._step - 384
if overflow >= 0:
self._step = overflow

class ColorPattern(BaseAnimation):
"""Fill the dots progressively along the strip with alternating colors."""
Expand All @@ -96,12 +98,13 @@ def step(self, amt = 1):
self._led.set(self._start + i, self._colors[cIndex])
if self._dir:
self._step += amt
if self._start + self._step > self._end:
self._step = 0
overflow = (self._start + self._step) - self._end
if overflow >= 0:
self._step = overflow
else:
self._step -= amt
if self._step < 0:
self._step = self._end
self._step = self._end + self._step

class ColorWipe(BaseAnimation):
"""Fill the dots progressively along the strip."""
Expand All @@ -117,8 +120,9 @@ def step(self, amt = 1):
self._led.set(self._start + self._step - i, self._color)

self._step += amt
if self._start + self._step > self._end:
self._step = 0
overflow = (self._start + self._step) - self._end
if overflow >= 0:
self._step = overflow

class ColorFade(BaseAnimation):
"""Fill the dots progressively along the strip."""
Expand Down Expand Up @@ -159,8 +163,9 @@ def step(self, amt = 1):
self._led.set(self._start + self._step + i, self._color)

self._step += amt
if self._start + self._step > self._end:
self._step = 0
overflow = (self._start + self._step) - self._end
if overflow >= 0:
self._step = overflow

class PartyMode(BaseAnimation):
"""Stobe Light Effect."""
Expand Down
File renamed without changes.
70 changes: 8 additions & 62 deletions LPD8806.py → raspledstrip/ledstrip.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
#!/usr/bin/env python
from color import Color, ColorHSV

"""
LPD8806.py: Raspberry Pi library for LPD8806 based RGB light strips
Initial code from: https://github.com/Sh4d/LPD8806
Provides the ability to drive a LPD8806 based strand of RGB leds from the
Raspberry Pi
Colors are provided as RGB and converted internally to the strip's 7 bit
values.
Wiring:
Pi MOSI -> Strand DI
Pi SCLK -> Strand CI
Most strips use around 10W per meter (for ~32 LEDs/m) or 2A at 5V.
The Raspberry Pi cannot even come close to this so a larger power supply is required, however, due to voltage loss along long runs you will need to put in a new power supply at least every 5 meters. Technically you can power the Raspberry Pi through the GPIO pins and use the same supply as the strips, but I would recommend just using the USB power as it's a much safer option.
Also, while it *should* work without it to be safe you should add a level converter between the Raspberry Pi and the strip's data lines. This will also help you have longer runs.
Example:
>> import LPD8806
>> led = LPD8806.LEDStrip()
>> led.fill(255, 0, 0)
"""
from LPD8806 import LPD8806

#Not all LPD8806 strands are created equal.
#Some, like Adafruit's use GRB order and the other common order is GRB
Expand All @@ -40,28 +15,19 @@ class ChannelOrder:

class LEDStrip:

def __init__(self, leds, use_py_spi = False, dev="/dev/spidev0.0"):
def __init__(self, leds, use_py_spi = False, dev="/dev/spidev0.0", driver="LPD8806"):
#Variables:
# leds -- strand size
# dev -- spi device

#no alternate drivers for now. Here so they can be added later
self.driver = LPD8806(leds, use_py_spi, dev)

self.c_order = ChannelOrder.GRB
self.dev = dev
self.use_py_spi = use_py_spi
self.leds = leds
self.lastIndex = self.leds - 1
self.gamma = bytearray(256)
self.buffer = [0 for x in range(self.leds + 1)]

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


self.masterBrightness = 1.0

Expand All @@ -74,6 +40,9 @@ def __init__(self, leds, use_py_spi = False, dev="/dev/spidev0.0"):
pow(float(i) / 255.0, 2.5) * 127.0 + 0.5
)

def update(self):
self.driver.update(self.buffer)

#Allows for easily using LED strands with different channel orders
def setChannelOrder(self, order):
self.c_order = order
Expand All @@ -84,29 +53,6 @@ def setMasterBrightness(self, bright):
raise ValueError('Brightness must be between 0.0 and 1.0')
self.masterBrightness = bright

#Push new data to strand
def update(self):
if self.use_py_spi:
for x in range(self.leds):
self.spi.xfer2([i for i in self.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 :)
else:
for x in range(self.leds):
self.spi.write(self.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
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()
self.spi.write(bytearray(b'\x00\x00\x00'))
self.spi.flush()

#Fill the strand (or a subset) with a single color using a Color object
def fill(self, color, start=0, end=0):
if start < 0:
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from distutils.core import setup

setup(
name='
name='RasPiLEDStrip',
version='0.9b',
description='Driver and Animation classes for driving LED Strips from the Raspberry Pi',
author='Adam Haile',
author_email='[email protected]',
url='http://maniacallabs.com',
license='LICENSE',
packages=['raspledstrip']
)
Loading

0 comments on commit f8a0622

Please sign in to comment.