示例#1
0
    def _step(self, state, leds):
        delta_time = time() - self.prev_time

        # step for each Wave
        for each in self.waves:
            each.step(delta_time)
        for each in self.waves_g:
            each.step(delta_time)

        # concat waves
        for pos in range(0, self.len):
            b = self.waves[0].get(pos) * self.waves[1].get(
                pos) + self.waves[2].get(pos) * self.waves[3].get(pos)
            g = self.waves_g[0].get(pos) * self.waves_g[1].get(pos)
            if state == State.START:
                leds[pos] = color_blend(to_color(0.0, b * g, b), leds[pos],
                                        (self.fade_in / 5.0)**2)
            else:
                leds[pos] = to_color(0.0, b * g, b)

        # simple state machine
        if state == State.START:
            self.fade_in += delta_time
            if self.fade_in > 5:
                return State.RUNNING
        elif state == State.STOP:
            return State.OFF

        self.prev_time = time()
示例#2
0
 def render(self, leds):
     # draw center color
     for x in range(int(-self.cur_radius), int(self.cur_radius + 1)):
         leds[self.pos + x] = self.color
     # clear edges
     if self.state < 0:
         leds[int(self.pos + self.cur_radius)] = to_color()
         leds[int(self.pos - self.cur_radius)] = to_color()
示例#3
0
    def _step(self, state, leds):
        '''
            This is called each frame of the main loop.

            state: current state of the pattern. See base.py -> State(Enum)
            leds: hw access to leds
        '''

        # === sample method 1
        # Iter through all leds in a random order.
        # Each time we enter this _step we're only updating 1 led.
        # This is nice for high cost patterns.
        if self.i >= len(self.strip_order):
            self.i = 0
            shuffle(self.strip_order)
            # state machine only updates when we've visited every led at least once.
            if state == State.START:
                return State.RUNNING
            elif state == State.STOP:
                return State.OFF
        # set led to random color
        leds[self.strip_order[self.i]] = color_wheel(random())
        self.i += 1

        # === sample method 2
        # Update all leds every frame.
        # This is do-able for low cost patterns.
        for i in range(self.len):
            leds[i] = wheel(random())
        # Instantly update state machine.
        if state == State.START:
            return State.RUNNING
        elif state == State.STOP:
            return State.OFF


        # Instantly update state machine.
        if state == State.START:
            return State.RUNNING
        elif state == State.STOP:
            return State.OFF

        # === other examples  (see utils.py for more useful functions)

        # select a random LED
        index = randint(0, self.len - 1)

        # set that LED to white
        leds[index] = to_color(1.0, 1.0, 1.0)

        # set that LED to a random color dimmed by half
        leds[index] = color_wheel(random) * 0.5

        # set that LED to blend between white and red
        leds[index] = color_blend(to_color(1.0, 0.0, 0.0), to_color(1.0, 1.0, 1.0), 0.5)
示例#4
0
    def _step(self, state, leds):
        for each in self.stripes:
            if each.pos - each.length > self.len:
                self.stripes.remove(each)
                if state != State.STOP:
                    self.stripes.append(Stripe())
                else:
                    if len(self.stripes) == 0:
                        return State.OFF
            else:
                speed = 4 if state == State.STOP else 2
                for rep in range(speed):
                    # clear tail
                    leds[min(self.len - 1,
                             max(0, each.pos - each.length))] = to_color()

                    # set leader
                    if each.pos < self.len:
                        leds[each.pos] = each.color

                    # move stripe
                    each.pos += 1

        if state == State.START:
            if len(self.stripes) < self.num_stripes:
                if randint(0, 5) == 0:
                    self.stripes.append(Stripe())
            else:
                return State.RUNNING
示例#5
0
 def __init__(self, length, pin, dma, channel):
     super(LedInterface, self).__init__()
     self._hw = Adafruit_NeoPixel(length,
                                  pin=pin,
                                  dma=dma,
                                  channel=channel,
                                  strip_type=ws.WS2811_STRIP_GRB)
     self._hw.begin()
     self.length = length
     self.buffer = [to_color()] * length
示例#6
0
    def _step(self, state, leds):
        # scan from top to bottom
        for pos in range(self.len - 1, 0, -1):
            if numpy.sum(leds[pos - 1]) < 0.2:
                leds[pos] = to_color()
            else:
                leds[pos] = color_blend(leds[pos], leds[pos - 1],
                                        random()**4) * 0.97

        # add fire
        for x in range(1):
            bri = random()**3
            leds[int(random()**3 * self.len)] = to_color(
                1.0, bri, bri / (6.0 + random()))

        if state == State.START:
            return State.RUNNING
        elif state == State.STOP:
            return State.OFF
示例#7
0
    def _step(self, state, leds):
        if self.i >= self.len:
            self.i = 0
            shuffle(self.strip_order)
        leds[self.strip_order[self.i]] = to_color()
        self.i += 1

        if state == State.START:
            return State.RUNNING
        elif state == State.STOP:
            return State.OFF
示例#8
0
    def _step(self, state, leds):
        for each in self.wisps:
            if each.pos > each.end:
                leds[each.pos - 1] = to_color()
                leds[each.pos] = to_color()
                self.wisps.remove(each)
                if state != State.STOP:
                    self.wisps.append(Wisp(self.len))
                else:
                    if len(self.wisps) == 0:
                        return State.OFF
            else:
                c = max(0, ((0.5 - abs(
                    (float(each.pos - each.start) / each.length) - 0.5)) *
                            2.0)**2.0)
                leds[each.pos - 1] = to_color()
                leds[each.pos +
                     0] = to_color(1.0, 1.0, 1.0) * c * each.intensity / 3
                if each.pos + 1 < self.len:
                    leds[each.pos +
                         1] = to_color(1.0, 1.0, 1.0) * c * each.intensity
                if each.pos + 2 < self.len:
                    leds[each.pos +
                         2] = to_color(1.0, 1.0, 1.0) * c * each.intensity / 3
                each.pos += 1

        if state == State.START:
            if len(self.wisps) < self.num_wisps:
                if self.loopCount % 6 == 0:
                    self.wisps.append(Wisp(self.len))
            else:
                return State.RUNNING
示例#9
0
    def _step(self, state, leds):
        for wisp in self.wisps:
            shuffle(wisp.px)
            # check if wisp needs to turn back toward nearest end of the led stirp
            if state == State.STOP:
                if wisp.dir > 0 and wisp.pos < self.len / 2 or wisp.dir < 0 and wisp.pos > self.len / 2:
                    wisp.dir = -wisp.dir
            # check wisp finished
            if wisp.pos > self.len + wisp.length or wisp.pos < -wisp.length:
                self.wisps.remove(wisp)
                if state == State.STOP and len(self.wisps) == 0:
                    return State.OFF
            else:
                # clear tail
                if wisp.pos - wisp.length * wisp.dir >= 0 and wisp.pos - wisp.length * wisp.dir < self.len:
                    leds[wisp.pos - wisp.length * wisp.dir] = to_color()
                # white leading spark
                if wisp.pos >= 0 and wisp.pos < self.len:
                    leds[wisp.pos] = to_color(1.0, 1.0, 1.0)
                # sparkly tail
                for x in wisp.px[0:int(wisp.length / 3)]:
                    x = x * wisp.dir
                    if wisp.pos - x >= 0 and wisp.pos - x < self.len:
                        b = (((wisp.length + 1) - abs(x)) /
                             float(wisp.length - 1))**2 * self.strip_b[
                                 (wisp.pos + x) % self.len]
                        leds[wisp.pos - x] = color_wheel(
                            wisp.color +
                            self.strip_c[(wisp.pos + x) % self.len], b)
                # incr move wisp
                wisp.pos += wisp.dir

        # spawning in new wisps
        if state == State.START:
            return State.RUNNING
        if state == State.RUNNING:
            if len(self.wisps) < self.num_wisps and randint(0, 20) == 0:
                self.wisps.append(Wisp(self.len))
示例#10
0
 def _step(self, state, leds):
     for i, x in enumerate(self.stars):
         if x.state == 0:
             # dimming
             if sum(x.color) < 0.1:
                 if state == State.STOP:
                     self.stars.remove(x)
                     if len(self.stars) == 0:
                         return State.OFF
                     break
                 else:
                     while True:
                         idx = randint(0, self.len - 1)
                         flag = False
                         for each in self.stars:
                             if idx == each.pos:
                                 break
                         if not flag:
                             break
                     x.pos = idx
                     x.state = 1
             else:
                 x.color *= 0.5 if state == State.STOP else 0.9
         else:
             # brightening
             if sum(x.color) > 2.9:
                 x.state = 0
             else:
                 x.color = to_color(*[min(1.0, c + (random()**3)/(5.0 if state == State.STOP else 10.0)) for c in x.color])
         leds[x.pos] = x.color
     if state == State.START:
         if len(self.stars) < self.num_stars:
             if self.loopCount % 5 == 0:
                 self.stars.append(Star(randint(0, self.len - 1), 1, to_color()))
         else:
             return State.RUNNING
示例#11
0
    def _step(self, state, leds):
        for each in self.dots:
            each.life -= 5 if state == State.STOP else 1
            if each.life <= 0:
                if state != State.STOP:
                    # clear old led and set to new one
                    leds[each.pos + each.offset] = to_color()
                    each.reset()
                else:
                    # check if empty
                    self.dots.remove(each)
                    if len(self.dots) == 0:
                        return State.OFF
            # draw current led
            leds[each.pos + each.offset] = each.color

        if state == State.START:
            return State.RUNNING
示例#12
0
    def _step(self, state, leds):
        for pos in range(self.len):
            # sweep when starting pattern
            if pos > self.sweep_in:
                continue

            # randomly target new colors
            if randint(0, 15) == 0:
                self.noise_color_t[pos] = random() / 10.0
            if randint(0, 20) == 0:
                self.noise_bri_t[pos] = random()**2
            # adjust brightness toward target
            self.noise_color[pos] = (self.noise_color_t[pos] -
                                     self.noise_color[pos]) * 0.05
            self.noise_bri[pos] += (self.noise_bri_t[pos] -
                                    self.noise_bri[pos]) * 0.05

            # set the colors
            base_color = to_color(1.0, 0.8, 0.5) * self.noise_bri[pos]
            leaf_color = color_wheel(
                abs((time() / 150.0) % 0.6 - 0.3) + self.noise_color[pos],
                self.noise_bri[pos])

            if pos < self.trunk_taper:
                # blend from base to leaves
                px_color = color_blend(leaf_color, base_color,
                                       float(pos) / float(self.trunk_taper))
            else:
                px_color = leaf_color

            leds[pos] = px_color

        if state == State.START:
            self.sweep_in += 1
            if self.sweep_in > self.len:
                return State.RUNNING
        elif state == State.STOP:
            return State.OFF
示例#13
0
 def __init__(self, pos):
     self.pos = pos
     self.offset = randint(0, 3)
     self.color = to_color()
     self.life = randint(0, 20)
示例#14
0
 def reset(self):
     self.offset = randint(0, 3)
     self.color = to_color(1.0, 0.9, 0.6)
     self.life = randint(100, 1000)
示例#15
0
 def __init__(self):
     self.length = randint(4, 12)
     self.pos = 0
     self.color = to_color(1.0, 0, 0) if randint(0, 1) else to_color(
         1.0, 1.0, 1.0)
示例#16
0
 def __init__(self, strip_length):
     super(candycane, self).__init__(strip_length)
     self.color_a = to_color(1.0, 1.0, 1.0)
     self.color_b = to_color(1.0, 0.0, 0.0)
     self.stripe_width = 18
     self.fade_width = 5.0