From abf3fa4b717ba655f0715e8ee9540afa48784d25 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 17 Jul 2019 19:19:51 -0400 Subject: [PATCH] neopixel: Add support for setting a default color at startup Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 5 +++++ klippy/extras/neopixel.py | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 27ad4ec5..0c0a5b0d 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1656,6 +1656,11 @@ #pin: # The pin connected to the neopixel. This parameter must be # provided. +#initial_RED: 0.0 +#initial_GREEN: 0.0 +#initial_BLUE: 0.0 +# Sets the initial LED color of the Neopixel. Each value should be +# between 0.0 and 1.0. The default for each color is 0. # Firmware filament retraction. This enables G10 (retract) and G11 diff --git a/klippy/extras/neopixel.py b/klippy/extras/neopixel.py index 9c9d4bee..5a2fdec5 100644 --- a/klippy/extras/neopixel.py +++ b/klippy/extras/neopixel.py @@ -17,6 +17,15 @@ class PrinterNeoPixel: % (self.oid, pin_params['pin'])) self.mcu.register_config_callback(self.build_config) self.neopixel_send_cmd = None + # Initial color + 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) + self.color_data = [green, red, blue] + self.printer.register_event_handler("klippy:connect", self.send_data) # Register commands self.gcode = self.printer.lookup_object('gcode') self.gcode.register_mux_command("SET_NEOPIXEL", "NEOPIXEL", name, @@ -29,6 +38,9 @@ class PrinterNeoPixel: cmd_queue = self.mcu.alloc_command_queue() self.neopixel_send_cmd = self.mcu.lookup_command( "neopixel_send oid=%c data=%*s", cq=cmd_queue) + def send_data(self, minclock=0): + self.neopixel_send_cmd.send([self.oid, self.color_data], + minclock=minclock) cmd_SET_NEOPIXEL_help = "Set the color of a neopixel led" def cmd_SET_NEOPIXEL(self, params): # Parse parameters @@ -38,11 +50,10 @@ class PrinterNeoPixel: red = int(red * 255. + .5) blue = int(blue * 255. + .5) green = int(green * 255. + .5) + self.color_data = [green, red, blue] # Send command print_time = self.printer.lookup_object('toolhead').get_last_move_time() - minclock = self.mcu.print_time_to_clock(print_time) - self.neopixel_send_cmd.send([self.oid, [green, red, blue]], - minclock=minclock) + self.send_data(self.mcu.print_time_to_clock(print_time)) def load_config_prefix(config): return PrinterNeoPixel(config)