2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Support for "neopixel" leds
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Copyright (C) 2019-2020  Kevin O'Connor <kevin@koconnor.net>
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# This file may be distributed under the terms of the GNU GPLv3 license.
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import logging
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-06 22:25:26 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								BACKGROUND_PRIORITY_CLOCK = 0x7fffffff00000000
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-15 13:28:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								BIT_MAX_TIME=.000004
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								RESET_MIN_TIME=.000050
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								MAX_MCU_SIZE = 500  # Sanity check on LED chain length
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class PrinterNeoPixel:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, config):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.printer = config.get_printer()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        name = config.get_name().split()[1]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Configure neopixel
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ppins = self.printer.lookup_object('pins')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pin_params = ppins.lookup_pin(config.get('pin'))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.mcu = pin_params['chip']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.oid = self.mcu.create_oid()
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-15 13:28:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.pin = pin_params['pin']
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.mcu.register_config_callback(self.build_config)
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        formats = {v: v for v in ["RGB", "GRB", "RGBW", "GRBW"]}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.color_order = config.getchoice("color_order", formats, "GRB")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        elem_size = len(self.color_order)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.chain_count = config.getint('chain_count', 1, minval=1,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                         maxval=MAX_MCU_SIZE//elem_size)
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.neopixel_update_cmd = self.neopixel_send_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-17 19:19:51 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        # Initial color
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.color_data = bytearray(self.chain_count * elem_size)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-17 19:19:51 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        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.)
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        white = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if elem_size == 4:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            white = config.getfloat('initial_WHITE', 0., minval=0., maxval=1.)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.update_color_data(red, green, blue, white)
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.old_color_data = bytearray([d ^ 1 for d in self.color_data])
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Register commands
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.printer.register_event_handler("klippy:connect", self.send_data)
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-24 23:52:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        gcode = self.printer.lookup_object('gcode')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        gcode.register_mux_command("SET_LED", "LED", name, self.cmd_SET_LED,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                   desc=self.cmd_SET_LED_help)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def build_config(self):
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-15 13:28:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        bmt = self.mcu.seconds_to_clock(BIT_MAX_TIME)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        rmt = self.mcu.seconds_to_clock(RESET_MIN_TIME)
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.mcu.add_config_cmd("config_neopixel oid=%d pin=%s data_size=%d"
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-15 13:28:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                " bit_max_ticks=%d reset_min_ticks=%d"
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                % (self.oid, self.pin, len(self.color_data),
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                   bmt, rmt))
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cmd_queue = self.mcu.alloc_command_queue()
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.neopixel_update_cmd = self.mcu.lookup_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "neopixel_update oid=%c pos=%hu data=%*s", cq=cmd_queue)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.neopixel_send_cmd = self.mcu.lookup_query_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "neopixel_send oid=%c", "neopixel_result success=%c", cq=cmd_queue)
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def update_color_data(self, red, green, blue, white, index=None):
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-09 20:58:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        red = int(red * 255. + .5)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        blue = int(blue * 255. + .5)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        green = int(green * 255. + .5)
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        white = int(white * 255. + .5)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self.color_order == "GRB":
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-09 20:58:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            color_data = [green, red, blue]
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif self.color_order == "RGB":
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-09 20:58:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            color_data = [red, green, blue]
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif self.color_order == "GRBW":
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            color_data = [green, red, blue, white]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            color_data = [red, green, blue, white]
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-09 20:58:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if index is None:
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.color_data[:] = color_data * self.chain_count
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-09 20:58:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            elem_size = len(color_data)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.color_data[(index-1)*elem_size:index*elem_size] = color_data
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-17 19:19:51 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def send_data(self, minclock=0):
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-28 13:44:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        old_data, new_data = self.old_color_data, self.color_data
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if new_data == old_data:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Find the position of all changed bytes in this framebuffer
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        diffs = [[i, 1] for i, (n, o) in enumerate(zip(new_data, old_data))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                 if n != o]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Batch together changes that are close to each other
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for i in range(len(diffs)-2, -1, -1):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            pos, count = diffs[i]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            nextpos, nextcount = diffs[i+1]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if pos + 5 >= nextpos and nextcount < 16:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                diffs[i][1] = nextcount + (nextpos - pos)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                del diffs[i+1]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Transmit changes
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ucmd = self.neopixel_update_cmd.send
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for pos, count in diffs:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            ucmd([self.oid, pos, new_data[pos:pos+count]],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                 reqclock=BACKGROUND_PRIORITY_CLOCK)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        old_data[:] = new_data
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Instruct mcu to update the LEDs
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        scmd = self.neopixel_send_cmd.send
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for i in range(8):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            params = scmd([self.oid], minclock=minclock)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if params['success']:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                break
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            logging.info("Neopixel update did not succeed")
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-06 19:30:48 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    cmd_SET_LED_help = "Set the color of an LED"
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-24 23:52:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def cmd_SET_LED(self, gcmd):
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Parse parameters
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-24 23:52:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        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.)
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        white = gcmd.get_float('WHITE', 0., minval=0., maxval=1.)
							 | 
						
					
						
							
								
									
										
										
										
											2020-04-24 23:52:49 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        index = gcmd.get_int('INDEX', None, minval=1, maxval=self.chain_count)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        transmit = gcmd.get_int('TRANSMIT', 1)
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-08 20:06:59 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.update_color_data(red, green, blue, white, index)
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Send command
							 | 
						
					
						
							
								
									
										
										
										
											2019-12-15 23:24:17 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if not transmit:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        print_time = self.printer.lookup_object('toolhead').get_last_move_time()
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-17 19:19:51 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.send_data(self.mcu.print_time_to_clock(print_time))
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-12 19:37:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def load_config_prefix(config):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return PrinterNeoPixel(config)
							 |