-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluxafor.py
executable file
·106 lines (79 loc) · 3.46 KB
/
luxafor.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import time
import usb.core
class Commands:
set_color = 0x01
fade_to = 0x02
class Device:
def __init__(self):
self.dev = usb.core.find(idVendor=0x04d8, idProduct=0xf372)
if self.dev is None:
print('Not connected')
return
try:
self.dev.detach_kernel_driver(0)
except usb.core.USBError:
pass
try:
self.dev.set_configuration()
except usb.core.USBError as e:
print("Unable to connect to USB device, have you run install.sh?")
raise e
def fade_to(self, led, r, g, b, time):
self._write([0x02, led, r, g, b, time, 0])
def set_color(self, led, r, g, b):
self._write([0x01, led, r, g, b, 0, 0])
def strobe(self, led, r, g, b, speed, repeat):
self._write([0x03, led, r, g, b, speed, 0, repeat])
def wave(self, wave_type, r, g, b, repeat, speed):
self._write([0x4, wave_type, r, g, b, 0, repeat, speed])
def built_in(self, pattern, repeat):
self._write([0x6, pattern, repeat, 0, 0, 0, 0, 0])
def _write(self, command):
self.dev.write(1, command)
def rgb_from_hex(hex_color):
hex_color = hex_color.strip('#').strip()
return(
int(hex_color[0:2], 16),
int(hex_color[2:4], 16),
int(hex_color[4:6], 16),
)
def parse_input_color(arg):
named_colors = {
'off': (0x00, 0x00, 0x00),
'white': (0xff, 0xff, 0xff),
'cyan': (0x00, 0xff, 0xff),
'magenta': (0xff, 0x00, 0xff),
'yellow': (0xff, 0xff, 0x00),
'red': (0xff, 0x00, 0x00),
'green': (0x00, 0xff, 0x00),
'blue': (0x00, 0x00, 0xff),
}
color = named_colors.get(arg)
return color if color else rgb_from_hex(arg)
def call_func(func, device, led, r, g, b, speed, repeat, type_):
func(device, led, r, g, b, speed, repeat, type_)
def main():
commands = {
'set-color': lambda d, led, r, g, b, speed, repeat, type_: d.set_color(led, r, g, b),
'fade': lambda d, led, r, g, b, speed, repeat, type_: d.fade_to(led, r, g, b, speed),
'strobe': lambda d, led, r, g, b, speed, repeat, type_: d.strobe(led, r, g, b, speed, repeat),
'wave': lambda d, led, r, g, b, speed, repeat, type_: d.wave(type_, r, g, b, repeat, speed),
'built-in': lambda d, led, r, g, b, speed, repeat, type_: d.built_in(type_, repeat),
'off': lambda d, led, r, g, b, speed, repeat, type_: d.fade_to(0xff, 0, 0, 0, 10),
}
parser = argparse.ArgumentParser(description='Set Luxafor state')
parser.add_argument('--command', choices=commands.keys(), help='Command to run')
parser.add_argument('--color', '-c', help='Color in hex value, #ff00ff for instance')
parser.add_argument('--leds', '-l', type=lambda x: int(x, 0), choices=[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xFF, 0x41, 0x42], help='')
parser.add_argument('--speed', '-s', type=int, help='Speed (fade-in/wave/strobe)')
parser.add_argument('--repeat', '-r', type=int, help='Repeats (0-255 -> 0 = infinite)')
parser.add_argument('--type', '-t', type=int, help='Type (1-5 for wave or 0-8 for built-ins)')
args = parser.parse_args()
d = Device()
r, g, b = parse_input_color(args.color) if args.color else (0,0,0)
call_func(commands[args.command], d, args.leds, r, g, b, args.speed, args.repeat, args.type)
if __name__ == '__main__':
main()