led_pillar

Documentation: http://frombelow.net/projects/led_pillar/
Clone: git clone https://git.frombelow.net/led_pillar.git
Log | Files | Refs | Submodules | README | LICENSE

patterns.py (5296B)


      1 #!/usr/bin/env python3
      2 # Based on NeoPixel library strandtest example
      3 # Author: Tony DiCola (tony@tonydicola.com)
      4 #
      5 # Source: https://github.com/rpi-ws281x/rpi-ws281x-python
      6 
      7 import time
      8 from rpi_ws281x import PixelStrip, Color
      9 import random
     10 import sys
     11 sys.path.append('PyIgnition')
     12 try:
     13     import PyIgnition, pygame, os
     14 except:
     15     sys.exit("""\
     16 PyIgnition not found. Please execute
     17 git submodule update --init --recursive""")
     18 import subprocess
     19 import math
     20 
     21 
     22 class Pattern():
     23     def __init__(self, strip, led_matrix_width, led_matrix_height, led_strip_snake, opts=None):
     24         self.strip = strip
     25         self.led_matrix_width = led_matrix_width
     26         self.led_matrix_height = led_matrix_height
     27         self.led_strip_snake = led_strip_snake
     28         self.opts = opts
     29         # self.clear_strip()
     30 
     31     def step(self):
     32         pass
     33 
     34     def stop(self):
     35         pass
     36 
     37     def clear_strip(self):
     38         for i in range(math.ceil(self.led_matrix_width) * math.ceil(self.led_matrix_height)):
     39             self.strip.setPixelColor(i, Color(0x00, 0x00, 0x00))
     40         self.strip.show()
     41 
     42     def xy2i(self, x, y):
     43         i = y * self.led_matrix_width
     44         if self.led_strip_snake and (y % 2):
     45             i += self.led_matrix_width - x -1
     46         else:
     47             i += x
     48         return math.floor(i)
     49 
     50 class Off(Pattern):
     51     def __init__(self, strip, led_matrix_width, led_matrix_height, led_strip_snake, opts=None):
     52         Pattern.__init__(self, strip=strip, led_matrix_width=led_matrix_width, led_matrix_height=led_matrix_height, led_strip_snake = led_strip_snake, opts=opts)
     53         self.clear_strip()
     54 
     55   
     56 class TestPattern(Pattern):
     57 
     58     CELL_ON = Color(0x00, 0xFF, 0x00)
     59     CELL_OFF = Color(0x00, 0x00, 0x00)
     60 
     61     def __init__(self, strip, led_matrix_width, led_matrix_height, led_strip_snake, opts=None):
     62         Pattern.__init__(self, strip=strip, led_matrix_width=led_matrix_width, led_matrix_height=led_matrix_height, led_strip_snake = led_strip_snake, opts=opts)
     63         pygame.display.init()
     64 
     65         self.x = 0
     66         self.y = 0
     67 
     68     def step(self):
     69         print("({x}/{y})".format(x=self.x, y=self.y))
     70         for i in range(math.ceil(self.led_matrix_width * self.led_matrix_height)):
     71             self.strip.setPixelColor(i, self.CELL_OFF)
     72         for col in range(math.ceil(self.led_matrix_width)):
     73             self.strip.setPixelColor(self.xy2i(col, self.y), self.CELL_ON)
     74         for row in range(math.ceil(self.led_matrix_height)):
     75             self.strip.setPixelColor(self.xy2i(self.x, row), self.CELL_ON)
     76         self.strip.show()
     77         time.sleep(1)
     78         self.x = (self.x + 1) % self.led_matrix_width
     79         self.y = (self.y + 1) % self.led_matrix_height
     80 
     81 
     82 
     83 class RGB(Pattern):
     84     def __init__(self, strip, led_matrix_width, led_matrix_height, led_strip_snake, opts=None):
     85         Pattern.__init__(self, strip=strip, led_matrix_width=led_matrix_width, led_matrix_height=led_matrix_height, led_strip_snake = led_strip_snake, opts=opts)
     86         (self.r, self.g, self.b) = opts
     87 
     88     def step(self):
     89         for i in range(math.ceil(self.led_matrix_width*self.led_matrix_height)):
     90             self.strip.setPixelColor(i, Color(self.r, self.g, self.b))
     91         self.strip.show()
     92 
     93 
     94 
     95 class Worm(Pattern):
     96     def __init__(self, strip, led_matrix_width, led_matrix_height, led_strip_snake, opts=None):
     97         Pattern.__init__(self, strip=strip, led_matrix_width=led_matrix_width, led_matrix_height=led_matrix_height, led_strip_snake = led_strip_snake, opts=opts)
     98         # LED_BRIGHTNESS should be at least 15.
     99         self.worm_pattern = list(map(lambda x: Color(x[0], x[1], x[2]),
    100                                      [(0x00, 0x05, 0x00),
    101                                       (0x00, 0x20, 0x00),
    102                                       (0x20, 0x20, 0x00),
    103                                       (0xFF, 0x00, 0x00),
    104                                       (0xFF, 0x00, 0x00),
    105                                       (0xFF, 0x00, 0x00),
    106                                       (0xFF, 0x00, 0x00),
    107                                       (0xFF, 0x00, 0x00),
    108                                       (0x20, 0x20, 0x00),
    109                                       (0x00, 0x20, 0x00),
    110                                       (0x00, 0x05, 0x00),
    111                                      ]))
    112         # Set background color
    113         for i in range(math.ceil(self.led_matrix_width*self.led_matrix_height)):
    114             self.strip.setPixelColor(i, Color(0x00, 0x05, 0x00))
    115         self.strip.show()
    116         self.direction = 1
    117         self.pos = self.led_matrix_width // 2
    118 
    119     def step(self):
    120         # Larson worm
    121         for i in range(len(self.worm_pattern)):
    122             self.strip.setPixelColor(int(self.pos+i), self.worm_pattern[i])
    123         if random.random() < .02:
    124             self.direction *= -1
    125         if self.pos == 0:
    126             self.direction = 1
    127         if (self.pos + len(self.worm_pattern)) == self.led_matrix_width*self.led_matrix_height:
    128             self.direction = -1
    129         self.pos += self.direction
    130         # Random set blue pixel
    131         if (self.pos % 8) == 0:
    132             self.strip.setPixelColor(random.randint(0, math.ceil(self.led_matrix_width*self.led_matrix_height-1)), Color(0x00, 0x00, 0x10))
    133         # Update Strip
    134         self.strip.show()
    135         time.sleep(.04)
    136 
    137