-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient.py
executable file
·77 lines (58 loc) · 2.11 KB
/
gradient.py
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
#!/usr/bin/python2
# NeoPixel library strandtest example
# Author: Tony DiCola ([email protected])
#
# Direct port of the Arduino NeoPixel library strandtest example. Showcases
# various animations on a strip of NeoPixels.
import time
from neopixel import *
# LED strip configuration:
ROWS = 10
COLS = 20
LED_COUNT = ROWS * COLS # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
def Color(red, green, blue, white = 0):
"""Convert the provided red, green, blue color to a 24-bit color value.
Each color component should be a value 0-255 where 0 is the lowest intensity
and 255 is the highest intensity.
"""
return (white << 24) | (green << 16)| (red << 8) | blue
class LedGrid:
def __init__(self, width, height):
self.strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
self.width = width
self.height = height
def begin(self):
self.strip.begin()
def show(self):
self.strip.show()
def clear(self):
for col in range(0, self.width):
for row in range(0, self.height):
self.set(row, col, Color(0, 0, 0))
def set(self, row, col, color):
self.strip.setPixelColor(self.index(row, col), color)
def index(self, row, col):
if row % 2 == 1:
col = self.width - col - 1
return row * self.width + col
# Main program logic follows:
if __name__ == '__main__':
grid = LedGrid(COLS, ROWS)
grid.begin()
grid.clear()
grid.show()
b = 0
while True:
for col in range(COLS):
for row in range(ROWS):
r = 255 * row / ROWS
g = 255 * col / COLS
grid.set(row, col, Color(r, g, b))
grid.show()
time.sleep(0.1)
b = (b + 16) % 256