-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
45 lines (33 loc) · 1.22 KB
/
controller.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
from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import PWMLED
from config import Config
from console import Console
class Controller:
factory = False
pwms = {}
def __init__(self):
self.factory = PiGPIOFactory()
def update(self, pins, color):
self.set_pin(int(pins['red']), self.balance(color.get_red(), 'red'))
self.set_pin(int(pins['green']), self.balance(color.get_green(), 'green'))
self.set_pin(int(pins['blue']), self.balance(color.get_blue(), 'blue'))
def set_pin(self, pin, value):
console = Console()
console.log('SET PIN #' + str(pin) + ': ' + str(value))
if pin in self.pwms:
self.pwms[pin].value = value
else:
self.pwms[pin] = PWMLED(pin, pin_factory=self.factory)
self.pwms[pin].value = value
def kill(self):
for pwm in self.pwms:
self.pwms[pwm].close()
self.pwms = {}
def balance(self, value, color):
config_ctrl = Config()
conf = config_ctrl.get()
value = int(float(value) * float(conf['balance'][color]))
return self.percentage(value)
# changed to 0-1
def percentage(self, value):
return int(value) / 255