Files
@ 835bac5d3b3c
Branch filter:
Location: led-matrix-software/demos/rainbow.py
835bac5d3b3c
4.4 KiB
text/x-python
Added new demos and handle splash screens and controller input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | ### 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.
### Inspired by https://krazydad.com/tutorials/makecolors.php
import sys
import time
import signal
import logging
import configparser
from enum import Enum
import math
import matrix
class Rainbow:
class MODES(Enum):
TWO_D = 0
LINEAR = 1
SOLID = 2
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):
r = matrix.Colors.RED.value
o = matrix.Colors.ORANGE.value
y = matrix.Colors.YELLOW.value
g = matrix.Colors.GREEN.value
b = matrix.Colors.BLUE.value
p = matrix.Colors.PURPLE.value
q = matrix.Colors.OFF.value
splash = [
[q, q, q, q, q, q, q, q],
[q, q, q, q, q, q, q, q],
[q, q, q, q, q, q, q, q],
[q, r, o, y, g, b, p, q],
[q, r, o, y, g, b, p, q],
[q, q, q, q, q, q, q, q],
[q, q, q, q, q, q, q, q],
[q, q, q, q, q, q, q, q],
]
for x in range(0, self.matrix.WIDTH):
for y in range(0, self.matrix.HEIGHT):
self.matrix.set_pixel(x, y, splash[y][x])
self.matrix.update()
def run(self):
center = 128;
width = 127;
frequency = .1;
mode = self.MODES.TWO_D
#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
elif event.code == 305 and event.value == 1:
mode = self.MODES((mode.value + 1) % len(self.MODES))
elif event.code == 16:
if event.value == 1:
#dpad right
pass
if event.value == -1:
#dpad left
pass
elif event.code == 17:
if event.value == 1:
#dpad down
frequency = frequency - 0.05
if frequency <= 0.05:
frequency = 0.05
print(frequency)
if event.value == -1:
#dpad up
frequency = frequency + 0.05
if frequency >= 1:
frequency = 1
print(frequency)
if time.time() > last_time + 0.1:
last_time = time.time()
for y in range(self.matrix.HEIGHT):
if mode == self.MODES.TWO_D:
phaseShift = 2*math.pi - y*(math.pi/(self.matrix.HEIGHT-1))
else:
phaseShift = 4
for x in range(self.matrix.WIDTH):
if mode == self.MODES.SOLID:
i = frame_count % 128
else:
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)
|