aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras
diff options
context:
space:
mode:
Diffstat (limited to 'klippy/extras')
-rw-r--r--klippy/extras/dotstar.py50
-rw-r--r--klippy/extras/neopixel.py10
2 files changed, 42 insertions, 18 deletions
diff --git a/klippy/extras/dotstar.py b/klippy/extras/dotstar.py
index 08c370fa..6c7182a2 100644
--- a/klippy/extras/dotstar.py
+++ b/klippy/extras/dotstar.py
@@ -11,6 +11,7 @@ class PrinterDotstar:
def __init__(self, config):
self.printer = config.get_printer()
name = config.get_name().split()[1]
+ self.mutex = self.printer.get_reactor().mutex()
# Configure a software spi bus
ppins = self.printer.lookup_object('pins')
data_pin_params = ppins.lookup_pin(config.get('data_pin'))
@@ -26,17 +27,29 @@ class PrinterDotstar:
red = config.getfloat('initial_RED', 0., minval=0., maxval=1.)
green = config.getfloat('initial_GREEN', 0., minval=0., maxval=1.)
blue = config.getfloat('initial_BLUE', 0., minval=0., maxval=1.)
- red = int(red * 255. + .5)
- blue = int(blue * 255. + .5)
- green = int(green * 255. + .5)
color_data = [0xff, blue, green, red] * self.chain_count
self.color_data = [0, 0, 0, 0] + color_data + [0xff, 0xff, 0xff, 0xff]
- self.printer.register_event_handler("klippy:connect", self.send_data)
+ self.update_color_data(red, green, blue)
+ self.old_color_data = bytearray([d ^ 1 for d in self.color_data])
# Register commands
+ self.printer.register_event_handler("klippy:connect", self.send_data)
gcode = self.printer.lookup_object('gcode')
gcode.register_mux_command("SET_LED", "LED", name, self.cmd_SET_LED,
desc=self.cmd_SET_LED_help)
+ def update_color_data(self, red, green, blue, white=None, index=None):
+ red = int(red * 255. + .5)
+ blue = int(blue * 255. + .5)
+ green = int(green * 255. + .5)
+ color_data = [0xff, blue, green, red]
+ if index is not None:
+ self.color_data[index*4:(index+1)*4] = color_data
+ else:
+ self.color_data[4:-4] = color_data * self.chain_count
def send_data(self, print_time=None):
+ old_data, new_data = self.old_color_data, self.color_data
+ if new_data == old_data:
+ return
+
minclock = 0
if print_time is not None:
minclock = self.spi.get_mcu().print_time_to_clock(print_time)
@@ -50,20 +63,25 @@ class PrinterDotstar:
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.)
- transmit = gcmd.get_int('TRANSMIT', 1)
- red = int(red * 255. + .5)
- blue = int(blue * 255. + .5)
- green = int(green * 255. + .5)
- color_data = [0xff, blue, green, red]
+ white = 0.0 #dotstar's dont have white yet
index = gcmd.get_int('INDEX', None, minval=1, maxval=self.chain_count)
- if index is not None:
- self.color_data[index*4:(index+1)*4] = color_data
- else:
- self.color_data[4:-4] = color_data * self.chain_count
- # Send command
- if transmit:
+ transmit = gcmd.get_int('TRANSMIT', 1)
+ sync = gcmd.get_int('SYNC', 1)
+ def reactor_bgfunc(print_time):
+ with self.mutex:
+ self.update_color_data(red, green, blue, index=index)
+ if transmit:
+ self.send_data(print_time)
+ def lookahead_bgfunc(print_time):
+ reactor = self.printer.get_reactor()
+ reactor.register_callback(lambda et: reactor_bgfunc(print_time))
+ if sync:
+ #Sync LED Update with print time and send
toolhead = self.printer.lookup_object('toolhead')
- toolhead.register_lookahead_callback(self.send_data)
+ toolhead.register_lookahead_callback(lookahead_bgfunc)
+ else:
+ #Send update now (so as not to wake toolhead and reset idle_timeout)
+ lookahead_bgfunc(None)
def load_config_prefix(config):
return PrinterDotstar(config)
diff --git a/klippy/extras/neopixel.py b/klippy/extras/neopixel.py
index e7785d97..1af0def4 100644
--- a/klippy/extras/neopixel.py
+++ b/klippy/extras/neopixel.py
@@ -119,6 +119,7 @@ class PrinterNeoPixel:
white = gcmd.get_float('WHITE', 0., minval=0., maxval=1.)
index = gcmd.get_int('INDEX', None, minval=1, maxval=self.chain_count)
transmit = gcmd.get_int('TRANSMIT', 1)
+ sync = gcmd.get_int('SYNC', 1)
# Update and transmit data
def reactor_bgfunc(print_time):
with self.mutex:
@@ -128,8 +129,13 @@ class PrinterNeoPixel:
def lookahead_bgfunc(print_time):
reactor = self.printer.get_reactor()
reactor.register_callback(lambda et: reactor_bgfunc(print_time))
- toolhead = self.printer.lookup_object('toolhead')
- toolhead.register_lookahead_callback(lookahead_bgfunc)
+ if sync:
+ #Sync LED Update with print time and send
+ toolhead = self.printer.lookup_object('toolhead')
+ toolhead.register_lookahead_callback(lookahead_bgfunc)
+ else:
+ #Send update now (so as not to wake toolhead and reset idle_timeout)
+ lookahead_bgfunc(None)
def load_config_prefix(config):
return PrinterNeoPixel(config)