-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhex2ws281x.py
executable file
·62 lines (53 loc) · 2.03 KB
/
hex2ws281x.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
#!/usr/bin/env python3
import argparse
import re
import sys
from rpi_ws281x import PixelStrip, Color
from graceful_killer import GracefulKiller
from palette import Palette
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Pipe pixel colors to a LED strip')
parser.add_argument('--keys', default=61, type=int, help='How many keys on the piano keyboard')
parser.add_argument('--leds-per-key', default=2, type=int, help='How many LEDs per key')
parser.add_argument('--skip', default=0, type=int, help='Skip first N LED addresses')
parser.add_argument('--gpio', default=12, type=int, help='GPIO pin connected to the LED strip')
parser.add_argument('--brightness', default=255, type=int, help='Set to 0 for darkest and 255 for brightest')
args = parser.parse_args()
sys.argv = []
KEYS = args.keys
LEDS_PER_KEY = args.leds_per_key
LED_OFFSET = args.skip
LED_PIN = args.gpio
LED_BRIGHTNESS = args.brightness
LED_COUNT = KEYS * LEDS_PER_KEY + LED_OFFSET
strip = PixelStrip(
LED_COUNT,
LED_PIN,
brightness=LED_BRIGHTNESS,
)
strip.begin()
validator = re.compile(r'^\s*[0-9a-f]{%d}\b' % (KEYS * 2), re.IGNORECASE)
palette = Palette('palette.json')
killer = GracefulKiller()
while not killer.kill_now:
line = sys.stdin.readline()
match = validator.match(line)
if match:
levels = [c / 255 for c in bytes.fromhex(match.group())]
leds = list(
map(
lambda c: Color(c[0], c[1], c[2]),
palette.getKeyColors(levels)
)
)
for key in range(len(leds)):
color = leds[key]
for i in range(LEDS_PER_KEY):
strip.setPixelColor(LED_OFFSET + key * LEDS_PER_KEY + i, color)
strip.show()
else:
print(f'bad input: {line.strip()}')
black = Color(0, 0, 0)
for i in range(LED_COUNT):
strip.setPixelColor(i, black)
strip.show()