Example #1
0
 def __init__(self, leds, count, speed):
     self.screen = ZigzagLedScreen(leds, 5, 5, 1)
     self.speed = speed
     self.counter = 0
Example #2
0
class LedPattern():
    screen=None
    points=[]
    def __init__(self, leds, count, speed):
        self.screen = ZigzagLedScreen(leds, 5, 5, 1)
        self.speed = speed
        self.counter = 0

    def clear(self):
        self.screen.clear()

    def tick(self):
        self.counter = (self.counter + 1) % self.speed
        if self.counter == 0:
            self.advance(0)

    def has_point(self, coord):
        if len(self.points) >= self.screen.width * self.screen.height:
            return True
        for point in self.points:
            if coord == point[0]:
                return True
        return False

    def add(self, value):
        if len(self.points) >= self.screen.width * self.screen.height:
            # filled. Just set it to be the position of the one that happened longest ago
            coord = self.points[0][0]
            del(self.points[0])
            self.points.append([coord, value, 100])
        else:
            while True:
                x = random.randint(0, self.screen.width)
                y = random.randint(0, self.screen.height)
                coord = (x, y)
                if not self.has_point(coord):
                    break
            self.points.append([coord, value, 100])

    def advance(self, value):
        for point in self.points:
            point[2] -= 1
        self.points[:] = [l for l in self.points if l[2] > 0]
        self.show()

    def show(self):
        self.screen.clear()
        for i, point in enumerate(self.points[:]):
            color = fade_color(point[1], (point[2])/100.0)
            self.screen.setPixel(point[0], color)
        self.screen.show()

    def test(self):
        self.screen.clear()
        #self.screen.setPixel((0, 0), 0xff0000)
        #self.screen.setPixel((1, 0), 0x00ff00)
        #self.screen.setPixel((2, 0), 0x0000ff)

        self.screen.setPixel((4, 3), ORANGE)
        self.screen.setPixel((4, 4), BLUE)
        self.screen.setPixel((3, 4), GREEN)
        self.screen.show()