Blinkstick square - Pulse all led


#1

Hi,

I just bought the blinkstick pro and I absolutely love it ! So I started playing aroud and I cannot make this to work :sweat_smile:

I can make all led pulse one after another one but not simultaneously. Any idea how I could achieve that ?

My code so far:

    import time
    import math
    import colorsys
    from random import randint

    from blinkstick import blinkstick

    class Main(blinkstick.BlinkStickPro):
        def run(self):
            self.send_data_all()

            try:
                while True:
                    for x in range(0, self.r_led_count):
                        self.bstick.pulse(index=x, name="red")
                    time.sleep(0.25)

            except KeyboardInterrupt:
                self.off()
                return

            # Change the number of LEDs for r_led_count
            main = Main(r_led_count=8, max_rgb_value=128)
            if main.connect():
                main.run()
            else:
                print "No BlinkSticks found"

Thx in advance !


#2

Hi,

I’m trying to make a python thread for each led but is looks overkill ?
Can someone help please ?


#3

pulse is just a loop with 2 morphs that go from color to off. morph has a sleep in it so multiple pulses will not work at the same time.
I think your best bet to make them all pulse at the same time would be either create a thread for each LED like you said or make your own implementation of pulse and morph with a loop that changes each led by a small value every time while going from from color to off and back.
Bear in min that the changes aren’t actually done at the same time so if you have a lot of LEDs to change you will see a sort of “wave” because the first one is updated earlier than the last.


#4

Working on code now. Just give me a sec :smile:


#5

Try this. Will be extending BlinkStickPro class to include these helper functions.

import time
import math
import colorsys
from random import randint

from blinkstick import blinkstick

class Main(blinkstick.BlinkStickPro):

    def set_color_all(self, channel, red, green, blue, remap_values=True):
        """
        Set the color of all pixels
        @type channel: int
        @param channel: R, G or B channel
        @type r: int
        @param r: red color byte
        @type g: int
        @param g: green color byte
        @type b: int
        @param b: blue color byte
        """

        if remap_values:
            red, green, blue = [blinkstick._remap_color(val, self.max_rgb_value) for val in [red, green, blue]]

        for i in range(0, self.r_led_count):
            self.data[channel][i] = [green, red, blue]

    def morph_all(self, channel, red=0, green=0, blue=0, name=None, hex=None, duration=1000, steps=50):
        """
        Morph to the specified color for all pixels.
        @type channel: int
        @param channel: R, G or B channel
        @type  red: int
        @param red: Red color intensity 0 is off, 255 is full red intensity
        @type  green: int
        @param green: Green color intensity 0 is off, 255 is full green intensity
        @type  blue: int
        @param blue: Blue color intensity 0 is off, 255 is full blue intensity
        @type  name: str
        @param name: Use CSS color name as defined here: U{http://www.w3.org/TR/css3-color/}
        @type  hex: str
        @param hex: Specify color using hexadecimal color value e.g. '#FF3366'
        @type  duration: int
        @param duration: Duration for morph in milliseconds
        @type  steps: int
        @param steps: Number of gradient steps (default 50)
        """

        r_end, g_end, b_end = self.bstick._determine_rgb(red=red, green=green, blue=blue, name=name, hex=hex)

        r_start, g_start, b_start = blinkstick._remap_rgb_value_reverse(self.get_color(channel, 0), self.max_rgb_value)

        if r_start > 255 or g_start > 255 or b_start > 255:
            r_start = 0
            g_start = 0
            b_start = 0

        gradient = []

        steps += 1
        for n in range(1, steps):
            d = 1.0 * n / steps
            r = (r_start * (1 - d)) + (r_end * d)
            g = (g_start * (1 - d)) + (g_end * d)
            b = (b_start * (1 - d)) + (b_end * d)

            gradient.append((r, g, b))

        ms_delay = float(duration) / float(1000 * steps)

        self.set_color_all(channel=channel, red=r_start, green=g_start, blue=b_start)
        self.send_data_all()

        for grad in gradient:
            grad_r, grad_g, grad_b = grad

            self.set_color_all(channel=channel, red=grad_r, green=grad_g, blue=grad_b)
            self.send_data_all()
            time.sleep(ms_delay)

        self.set_color_all(channel=channel, red=r_end, green=g_end, blue=b_end)
        self.send_data_all()


    def run(self):
        self.send_data_all()

        try:
            while True:
                self.morph_all(0, name="red")
                self.morph_all(0, name="black")
                time.sleep(0.25)

        except KeyboardInterrupt:
            self.off()
            return

# Change the number of LEDs for r_led_count
main = Main(r_led_count=8, max_rgb_value=128)
if main.connect():
    main.run()
else:
    print "No BlinkSticks found"

#6

Thanks ! Will try it soon :slight_smile:

Just to make sure I understand :

self.data[channel][i] = [green, red, blue]

What you did is you set the color for each led and then ask the blinkstick to “execute” the changes with self.send_data_all(). Is this what you did to make all led change color simultaneously ?


#7

Yep :wink: BlinkStickPro class holds internal buffer which is sent all in one go. self.data variable holds that buffer.


#8

Thx ! It all makes sense now !

Can’t wait to receive my next blinksticks :grinning: