From a3980cebcc041d4f07063d17b34837bcba5138a2 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 9 Aug 2019 20:58:49 -0400 Subject: [PATCH] neopixel: Add support for WS2811 RGB pixel color order Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 3 +++ klippy/extras/neopixel.py | 31 +++++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index f3f1d1f8..898e2d33 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1684,6 +1684,9 @@ # The number of Neopixel chips that are "daisy chained" to the # provided pin. The default is 1 (which indices only a single # Neopixel is connected to the pin). +#color_order_GRB: True +# Set the pixel order to green, red, blue. If using the WS2811 chip +# (in 800Khz mode) then set this to False. The default is True. #initial_RED: 0.0 #initial_GREEN: 0.0 #initial_BLUE: 0.0 diff --git a/klippy/extras/neopixel.py b/klippy/extras/neopixel.py index 936e6ad0..abd1c637 100644 --- a/klippy/extras/neopixel.py +++ b/klippy/extras/neopixel.py @@ -18,16 +18,14 @@ class PrinterNeoPixel: self.mcu.add_config_cmd("config_neopixel oid=%d pin=%s" % (self.oid, pin_params['pin'])) self.mcu.register_config_callback(self.build_config) + self.color_order_GRB = config.getboolean("color_order_GRB", True) self.chain_count = config.getint('chain_count', 1, minval=1, maxval=18) 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.chain_count + self.update_color_data(red, green, blue) self.printer.register_event_handler("klippy:connect", self.send_data) # Register commands self.gcode = self.printer.lookup_object('gcode') @@ -38,6 +36,18 @@ 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 update_color_data(self, red, green, blue, index=None): + red = int(red * 255. + .5) + blue = int(blue * 255. + .5) + green = int(green * 255. + .5) + if self.color_order_GRB: + color_data = [green, red, blue] + else: + color_data = [red, green, blue] + if index is None: + self.color_data = color_data * self.chain_count + else: + self.color_data[(index-1)*3:index*3] = color_data def send_data(self, minclock=0): self.neopixel_send_cmd.send([self.oid, self.color_data], minclock=minclock, @@ -48,16 +58,9 @@ class PrinterNeoPixel: red = self.gcode.get_float('RED', params, 0., minval=0., maxval=1.) green = self.gcode.get_float('GREEN', params, 0., minval=0., maxval=1.) blue = self.gcode.get_float('BLUE', params, 0., minval=0., maxval=1.) - red = int(red * 255. + .5) - blue = int(blue * 255. + .5) - green = int(green * 255. + .5) - color_data = [green, red, blue] - if 'INDEX' in params: - index = self.gcode.get_int('INDEX', params, - minval=1, maxval=self.chain_count) - self.color_data[(index-1)*3:index*3] = color_data - else: - self.color_data = color_data * self.chain_count + index = self.gcode.get_int('INDEX', params, None, + minval=1, maxval=self.chain_count) + self.update_color_data(red, green, blue, index) # Send command print_time = self.printer.lookup_object('toolhead').get_last_move_time() self.send_data(self.mcu.print_time_to_clock(print_time))