Files @ 6e77747319ab
Branch filter:

Location: led-matrix-software/demos/rainbow.py

matthewreed
Added base software to select and run demos/games, along with a couple demos
### rainbow.py
### Author: Matthew Reed
### Rainbow array scrolls across the display. Rainbow generated using overlapping sin waves. The frequency, phase shift, center, and width all affect the output.

import sys
import time
import signal
import logging
import configparser
from enum import Enum

import math

class Rainbow:

    def __init__(self, config, parent, matrix, controller):
        self.logger = logging.getLogger('paint')
        self.config = config
        self.parent = parent
        self.matrix = matrix
        self.controller = controller
        
    def reset(self):
        pass
        
    def splash(self):
        self.matrix.set_matrix((0,255,0))
        self.matrix.update()
        
    def run(self):
    
        center = 128;
        width = 100;
        frequency = .1;
    
        #start timers and counters
        self.start_time = time.time()
        last_time = time.time()
        
        led_iteration_count = 0
        frame_count = 0
        
        keep_going = True
        
        while keep_going:
            
            for event in self.controller.read_input():
                if event.code == 313 and event.value == 1:
                    keep_going = False
            
            if time.time() > last_time + 0.1:
                last_time = time.time()
                    
                for y in range(self.matrix.HEIGHT):
                    phaseShift = 2*math.pi - y*(1.5*math.pi/(self.matrix.HEIGHT-1))
                    for x in range(self.matrix.WIDTH):
                        i = (x + frame_count) % 128
                        red = math.sin(frequency*i + 0) * width + center;
                        green = math.sin(frequency*i + phaseShift/2) * width + center;
                        blue = math.sin(frequency*i + phaseShift) * width + center;
                        
                        self.matrix.set_pixel(x, y, (red, green, blue))
                
                self.matrix.update()
                
                led_iteration_count = (led_iteration_count + 1) % self.matrix.NUM_LEDS
                frame_count = frame_count + 1
                
            time.sleep(0.01)