-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummypixel.py
75 lines (52 loc) · 2.24 KB
/
dummypixel.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
from __future__ import annotations
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
from configfile import ConfigWrapper
from gcode import GCodeDispatch
from klippy import Printer
from extras.led import PrinterLED
Color = tuple[float, float, float, float]
class Extension:
name: str
chain_count: int
current_state: list[Color]
def __init__(self, config: ConfigWrapper):
gcode: GCodeDispatch = config.get_printer().lookup_object('gcode')
# Config
self.name = config.get_name().split()[-1]
self.chain_count = config.getint('chain_count', minval=1)
# Init fields
self.current_state = [(0.0, 0.0, 0.0, 0.0)] * self.chain_count
# Register handlers
gcode.register_mux_command("SET_LED", "LED", self.name, self.cmd_SET_LED, desc=self.cmd_SET_LED_help)
def get_led_count(self):
return self.chain_count
def get_status(self, *_):
return {'color_data': self.current_state}
def check_transmit(self, *_):
pass
def set_color(self, index: Optional[int], color: Color):
if index is None:
for i in range(self.chain_count):
self.current_state[i] = color
else:
self.current_state[index - 1] = color
cmd_SET_LED_help = "Set the color of an LED"
def cmd_SET_LED(self, gcmd):
red = gcmd.get_float('RED', 0., minval=0., maxval=1.)
green = gcmd.get_float('GREEN', 0., minval=0., maxval=1.)
blue = gcmd.get_float('BLUE', 0., minval=0., maxval=1.)
white = gcmd.get_float('WHITE', 0., minval=0., maxval=1.)
index = gcmd.get_int('INDEX', None, minval=1, maxval=self.chain_count)
self.set_color(index, (red, green, blue, white))
def load_config_prefix(config: ConfigWrapper):
extension = Extension(config)
neo_key = f'neopixel {extension.name}'
printer: Printer = config.get_printer()
printer.objects[neo_key] = extension
pled: PrinterLED = printer.load_object(config, 'led')
pled.led_helpers[extension.name] = extension
config.fileconfig.add_section(neo_key)
config.fileconfig.set(neo_key, 'chain_count', extension.get_led_count())
config.getsection(neo_key).getint('chain_count')
return extension