From 46041f541158067206f29139df188dfa876e3763 Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Wed, 17 Jul 2019 19:58:37 -0400
Subject: [PATCH] neopixel: Add support for daisy-chained chips

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
---
 config/example-extras.cfg |  4 ++++
 docs/G-Codes.md           | 10 +++++++---
 klippy/extras/neopixel.py | 11 +++++++++--
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/config/example-extras.cfg b/config/example-extras.cfg
index 0c0a5b0d9..53321aef7 100644
--- a/config/example-extras.cfg
+++ b/config/example-extras.cfg
@@ -1656,6 +1656,10 @@
 #pin:
 #   The pin connected to the neopixel. This parameter must be
 #   provided.
+#chain_count:
+#   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).
 #initial_RED: 0.0
 #initial_GREEN: 0.0
 #initial_BLUE: 0.0
diff --git a/docs/G-Codes.md b/docs/G-Codes.md
index 78be3e723..fef5680bb 100644
--- a/docs/G-Codes.md
+++ b/docs/G-Codes.md
@@ -195,9 +195,13 @@ is enabled:
 
 The following command is available when a "neopixel" config section
 is enabled:
-- `SET_NEOPIXEL NEOPIXEL=<config_name> RED=<value> GREEN=<value>
-  BLUE=<value>`: This sets the neopixel LED output. Each <value> must
-  be between 0.0 and 1.0.
+- `SET_NEOPIXEL NEOPIXEL=<config_name> INDEX=<index> RED=<value>
+  GREEN=<value> BLUE=<value>`: This sets the Neopixel LED output. Each
+  color <value> must be between 0.0 and 1.0. If multiple Neopixel
+  chips are daisy-chained then one may specify INDEX to alter the
+  color of just the given Neopixel chip (1 for the first Neopixel, 2
+  for the second, etc.). If INDEX is not provided then all Neopixels
+  in the daisy-chain will be set to the provided color.
 
 ## Servo Commands
 
diff --git a/klippy/extras/neopixel.py b/klippy/extras/neopixel.py
index 5a2fdec57..4920adf64 100644
--- a/klippy/extras/neopixel.py
+++ b/klippy/extras/neopixel.py
@@ -16,6 +16,7 @@ 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.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.)
@@ -24,7 +25,7 @@ class PrinterNeoPixel:
         red = int(red * 255. + .5)
         blue = int(blue * 255. + .5)
         green = int(green * 255. + .5)
-        self.color_data = [green, red, blue]
+        self.color_data = [green, red, blue] * self.chain_count
         self.printer.register_event_handler("klippy:connect", self.send_data)
         # Register commands
         self.gcode = self.printer.lookup_object('gcode')
@@ -50,7 +51,13 @@ class PrinterNeoPixel:
         red = int(red * 255. + .5)
         blue = int(blue * 255. + .5)
         green = int(green * 255. + .5)
-        self.color_data = [green, red, blue]
+        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
         # Send command
         print_time = self.printer.lookup_object('toolhead').get_last_move_time()
         self.send_data(self.mcu.print_time_to_clock(print_time))