Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rainbow animation for LED strip
#1
First of all: I am completely new to python - this is my 2nd day! I've done half of the 13 hour codeacademy course and have a bit of experience in programming before that (I must say, python is a lot different from the other languages I've played around with)

What I did is basically did was rip off a function from adafruits wonderful LPD8806 library and rewrite it in python. This function assumes that the LPD8806 cannot be lit more than an RGB value of 127, 127, 127. The code works, but it's a bit buggy/laggy. Could definitely use some rework so that it works nicely with BiblioPixel, but here goes.

Code:
from bibliopixel.led import *
from bibliopixel.animation import StripChannelTest
from bibliopixel.drivers.LPD8806 import *
from bibliopixel import LEDStrip
import bibliopixel.colors as colors

numLeds = 240
driver = DriverLPD8806(numLeds, c_order = ChannelOrder.BRG)
led = LEDStrip(driver)


def rainbowCycle():

        for j in range(0,384,2):
                for i in range(0,numLeds):
                        led.set(i, Wheel( ((i * 384 / numLeds + j) % 384) ))
                led.update()


def Wheel(WheelPos):
        r = 0
        g = 0
        b = 0

        tmpVar = WheelPos/128
        if tmpVar == 0:
                r = 127 - WheelPos % 128        # red down
                g = WheelPos % 128              # green up
                b = 0                           # blue off
        elif tmpVar == 1:
                g = 127 - WheelPos % 128        # green down
                b = WheelPos % 128                      # blue up
                r = 0                                           # red off
        elif tmpVar == 2:
                b = 127 - WheelPos % 128;       #blue down
                r = WheelPos % 128;             #red up
                g = 0;                          #green off
        return(r,g,b)

rainbowCycle()

Also, I'm propably importing way too much stuff at the beginning of the file, so that could easily be fixed. Thanks a lot to adafruit for creating this very realistic looking rainbow - great job!
#2
@hackstage - Nice work with the port! For only a handful of hours with Python you did a great job Smile

I have to admit, a great deal of BiblioPixel's code is borrowed from Adafruit's examples... including that animation Smile
RainbowCycle comes with strip_animation.py
Though, I'm happy to see you write it yourself... no better way to learn! Just wanted to make you aware that it's there.

And you are right, their LPD8806 code works with 0-127 because that chipset is only 7 bits per channel. But with BiblioPixel you can write to it as if it were 8 bits per channel and the driver will figure out what you mean Smile

The wheel function is also already in bibliopixel.colors for those used to using Adafruit's version. Pretty much anything color-wise that Adafruit has done can be done with those functions. And still uses their 0-384 value range. Though I recommend using the hue2rgb_rainbow() method instead as it uses a 0-255 value range which is faster.

Check the colors wiki page for more details.
#3
Goddamnit, I should have checked up on that before I posted here. Well.. I'll have to look deeper into BiblioPixel and explore all the features. Thanks for the lengthy reply.
#4
Hey, no worries Smile Like I said, glad to see it's helping you learn. Porting other code to python is how I learned it. Just thought I would let you know what was there... there is a ton of stuff in BP Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)