Example #1
0
class LightUp:
    def __init__(self,
                 led_strip,
                 rgb_colors=None,
                 brightness=1,
                 loop_limit=None,
                 duration_ms=200,
                 pause_ms=200,
                 num_random_colors=5,
                 sections='all'):
        self.led_strip = led_strip
        self.loop_limit = loop_limit
        self.loops = 0
        self.duration_ms = duration_ms
        self.pause_ms = pause_ms
        self.sections = sections
        self.colors = Color(rgb_colors=rgb_colors,
                            brightness=brightness,
                            num_random_colors=num_random_colors)

        self.write_wait_time = (self.duration_ms / (
            (round(self.colors.brightness_max, 1) / 0.1) * 2) / 1000)

    def glow(self):
        print('Light up:')
        try:
            # make sure leds are off
            self.led_strip.off()

            while True:
                # go over levels of brightness from 0 to 1 and
                self.colors.brightness = 0
                self.colors.correct()

                self.selected_leds = self.led_strip.get_led_selectors(
                    self.sections)

                # light up in 10 steps
                steps = 0
                step_height = round(self.colors.brightness_max / 10, 2)
                while steps < 10:
                    for i in self.selected_leds:
                        self.led_strip.leds[i] = self.colors.selected
                    self.led_strip.write(s_after_wait=self.write_wait_time)

                    # update color
                    self.colors.brightness += step_height
                    self.colors.correct()
                    steps += 1

                steps = 0
                while steps < 10:
                    for i in self.selected_leds:
                        self.led_strip.leds[i] = self.colors.selected
                    self.led_strip.write(s_after_wait=self.write_wait_time)

                    # update color
                    self.colors.brightness -= step_height
                    self.colors.correct()
                    steps += 1

                # change to next color
                self.colors.next()

                if self.pause_ms:
                    time.sleep(self.pause_ms / 1000)

                self.loops += 1
                if self.loop_limit and self.loop_limit == self.loops:
                    print()
                    break

        except KeyboardInterrupt:
            self.led_strip.fadeout()

            import sys
            print()
            sys.exit(0)
class BeatsUpAndDown:
    def __init__(
        self,
        led_strip,
        loop_limit=None,
        duration_ms=200,
        pause_ms=300,
        start='start',
        rgb_colors=None,
        brightness=1,
        brightness_fixed=False,
        max_height=1,
        num_random_colors=5,
    ):
        self.led_strip = led_strip
        self.loop_limit = loop_limit
        self.loops = 0
        self.duration_ms = duration_ms
        self.pause_ms = pause_ms
        self.start = start
        self.max_height = max_height

        self.write_wait_time = (self.duration_ms / 2 /
                                self.led_strip.addressable_strip_length) / 1000

        self.colors = Color(rgb_colors=rgb_colors,
                            brightness=brightness,
                            brightness_fixed=brightness_fixed,
                            num_random_colors=num_random_colors)

        if self.start == 'start' or self.start == 'end':
            self.led_strip.addressable_strip_length = round(
                self.led_strip.strip_length * self.max_height)
            self.selected_leds = range(self.led_strip.addressable_strip_length)
            self.selected_leds_counter_up = self.led_strip.addressable_strip_length
            self.selected_leds_counter_down = round(
                self.led_strip.addressable_strip_length -
                self.selected_leds_counter_up)
        else:
            self.led_strip.addressable_strip_length = self.led_strip.strip_length
            self.selected_leds = range(
                round(self.led_strip.addressable_strip_length / 2))
            self.selected_leds_counter_up = round(
                self.max_height *
                round(self.led_strip.addressable_strip_length / 2))
            self.selected_leds_counter_down = round(
                round(self.led_strip.addressable_strip_length / 2) -
                self.selected_leds_counter_up)

    def color_leds(self):
        if self.led_strip.debug:
            print('BeatsUpAndDown().color_leds()')

        # color LEDs
        for i in self.selected_leds[:self.selected_leds_counter_up]:
            # if brightness_fixed==False: set brightness depending on what led is glowing up
            if self.colors.brightness_fixed == False:
                # led 1: 30% of self.brightness_max
                # led 2: 30% of max + (i * 70%/self.led_strip.strip_length)
                # last LED: 100% * self.brightness_max
                if self.start == 'start + end' or self.start == 'center':
                    self.colors.brightness = round(
                        (0.3 * self.colors.brightness_max) +
                        ((i + 1) * (0.7 /
                                    ((self.led_strip.addressable_strip_length *
                                      self.max_height) / 2))), 2)
                else:
                    self.colors.brightness = round(
                        (0.3 * self.colors.brightness_max) +
                        ((i + 1) *
                         (0.7 / self.led_strip.addressable_strip_length)), 2)
                self.colors.correct()

            # "start" option "start & end" and "center"
            if self.start == 'end':
                i = [-(i + 1)]
            elif self.start == 'start':
                i = [i]
            elif self.start == 'start + end':
                i = [i, -(i + 1)]
            elif self.start == 'center':
                i = [
                    i + round(self.led_strip.addressable_strip_length / 2),
                    -(i + 1) +
                    round(self.led_strip.addressable_strip_length / 2)
                ]

            for led in i:
                led = self.led_strip.get_led(led, self.start)
                self.led_strip.leds[led] = self.colors.selected

            self.led_strip.write(s_after_wait=self.write_wait_time)

    def make_leds_black(self):
        if self.led_strip.debug:
            print('BeatsUpAndDown().make_leds_black()')

        # then make them black
        for i in self.selected_leds[self.selected_leds_counter_down:]:
            if self.start == 'start':
                i = [-(i + 1)]
            elif self.start == 'end':
                i = [i]
            elif self.start == 'start + end':
                i = [
                    -(i + 1) +
                    round(self.led_strip.addressable_strip_length / 2),
                    i + round(self.led_strip.addressable_strip_length / 2)
                ]
            elif self.start == 'center':
                i = [i, -(i + 1)]

            for led in i:
                led = self.led_strip.get_led(led, self.start)
                self.led_strip.leds[led] = self.colors.black

            self.led_strip.write(s_after_wait=self.write_wait_time)

    def glow(self):
        if self.led_strip.debug:
            print('BeatsUpAndDown().glow()')

        print('Beats up and down:')
        try:
            # make sure leds are off
            self.led_strip.off()

            while True:
                # update color if brightness different
                if self.colors.brightness != 1 and self.colors.brightness_fixed:
                    self.colors.correct()

                self.color_leds()
                self.make_leds_black()

                # change to next color
                self.colors.next()

                if self.pause_ms:
                    time.sleep(self.pause_ms / 1000)

                self.loops += 1
                if self.loop_limit and self.loop_limit == self.loops:
                    print()
                    break
        except KeyboardInterrupt:
            self.led_strip.fadeout()

            import sys
            print()
            sys.exit(0)
Example #3
0
class Transition:
    def __init__(self,
                 led_strip,
                 rgb_colors=None,
                 brightness=1,
                 loop_limit=None,
                 duration_ms=200,
                 pause_ms=200,
                 num_random_colors=5,
                 sections='all'):
        self.led_strip = led_strip
        self.loop_limit = loop_limit
        self.loops = 0
        self.duration_ms = duration_ms
        self.pause_ms = pause_ms
        self.sections = sections
        self.colors = Color(rgb_colors=rgb_colors,
                            brightness=brightness,
                            num_random_colors=num_random_colors)

        self.transition_steps = 20
        self.write_wait_time = (self.duration_ms /
                                self.transition_steps) / 1000

        self.selected_leds = self.led_strip.get_led_selectors(self.sections)

    def glow(self):
        if self.led_strip.debug:
            print('Transition().glow()')

        print('Transition:')
        try:
            # make sure leds are off
            self.led_strip.off()

            while True:
                # add or substract difference between rgb values and update color - to make transition

                steps_counter = 0
                target_color = self.colors.selected
                difference = [
                    target_color[0] -
                    self.led_strip.leds[self.selected_leds[0]][0],
                    target_color[1] -
                    self.led_strip.leds[self.selected_leds[0]][1],
                    target_color[2] -
                    self.led_strip.leds[self.selected_leds[0]][2],
                ]

                difference_per_step = [
                    round(difference[0] / self.transition_steps),
                    round(difference[1] / self.transition_steps),
                    round(difference[2] / self.transition_steps),
                ]

                while steps_counter != self.transition_steps:

                    self.colors.selected = [
                        self.led_strip.leds[self.selected_leds[0]][0] +
                        difference_per_step[0],
                        self.led_strip.leds[self.selected_leds[0]][1] +
                        difference_per_step[1],
                        self.led_strip.leds[self.selected_leds[0]][2] +
                        difference_per_step[2],
                    ]

                    # make to not select invalid RGB values
                    new_list = []
                    for number in self.colors.selected:
                        if number < 0:
                            new_list.append(0)
                        elif number > 255:
                            new_list.append(255)
                        else:
                            new_list.append(number)
                    self.colors.selected = new_list

                    for i in self.selected_leds:
                        self.led_strip.leds[i] = self.colors.selected
                    self.led_strip.write(s_after_wait=self.write_wait_time)

                    steps_counter += 1

                # change to next color
                self.colors.next()

                if self.pause_ms:
                    time.sleep(self.pause_ms / 1000)

                self.loops += 1
                if self.loop_limit and self.loop_limit == self.loops:
                    print()
                    break

        except KeyboardInterrupt:
            self.led_strip.fadeout()

            import sys
            print()
            sys.exit(0)
Example #4
0
class MovingDot:
    def __init__(self,
                 led_strip,
                 loop_limit=None,
                 duration_ms=200,
                 pause_a_ms=0,
                 pause_b_ms=300,
                 start='start',
                 rgb_colors=None,
                 brightness=1,
                 num_random_colors=5):
        self.led_strip = led_strip
        self.loop_limit = loop_limit
        self.loops = 0
        self.duration_ms = duration_ms
        self.pause_a_ms = pause_a_ms
        self.pause_b_ms = pause_b_ms
        self.start = start
        self.selector = {
            "start": [-1, -2, -3, -4, -5],
            "end": [0, 1, 2, 3, 4]
        }

        self.colors = Color(
            rgb_colors=rgb_colors,
            brightness=brightness,
            num_random_colors=num_random_colors
        )

        self.write_wait_time = (
            self.duration_ms/2/self.led_strip.strip_length)/1000

    def create_dot(self):
        if self.led_strip.debug:
            print('MovingDot().create_dot()')

        self.dot = [self.colors.black]*5

        counter = 0
        for selected in self.selector[self.start]:
            self.colors.brightness = (
                1-(counter*0.225)) * self.colors.brightness_max
            self.colors.correct()
            self.dot[selected] = self.colors.selected
            counter += 1

    def change_direction(self):
        if self.led_strip.debug:
            print('MovingDot().change_direction()')

        if self.start == 'start':
            self.start = 'end'
        else:
            self.start = 'start'

    def move_dot(self):
        if self.led_strip.debug:
            print('MovingDot().move_dot()')

        # move dot into view
        for selected in self.selector[self.start]:
            if self.start == 'start':
                self.led_strip.insert_led(0, self.dot[selected])
            else:
                self.led_strip.append_led(self.dot[selected])
            self.led_strip.write(s_after_wait=self.write_wait_time)

        # add black led to front and remove last led, to move dot
        while True:
            if self.start == 'start':
                self.led_strip.insert_led()
            else:
                self.led_strip.append_led()
            self.led_strip.write(s_after_wait=self.write_wait_time)

            # if all leds black, exit loop
            for led in self.led_strip.leds:
                if led[0] != 0 or led[1] != 0 or led[2] != 0:
                    break
            else:
                break

    def glow(self):
        if self.led_strip.debug:
            print('MovingDot().glow()')

        print('Moving dot:')
        try:
            # make sure leds are off
            self.led_strip.off()

            while True:
                # make sure duration is correct
                # create dot with tail
                self.create_dot()

                # move dot with tail and write
                self.move_dot()

                # once dot disappeared at the end: pause_a
                time.sleep(self.pause_a_ms/1000)

                # create new dot with tail and move in opposit start
                self.change_direction()
                self.create_dot()
                self.move_dot()
                self.change_direction()

                # once dot disappeared at the end: pause_b
                time.sleep(self.pause_b_ms/1000)

                # change to next color
                self.colors.next()

                self.loops += 1
                if self.loop_limit and self.loop_limit == self.loops:
                    print()
                    break

        except KeyboardInterrupt:
            self.led_strip.fadeout()

            import sys
            print()
            sys.exit(0)