2019-07-13 02:37:50 +03:00
|
|
|
# Support for "neopixel" leds
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2019-08-07 05:25:26 +03:00
|
|
|
BACKGROUND_PRIORITY_CLOCK = 0x7fffffff00000000
|
|
|
|
|
2020-04-15 20:28:54 +03:00
|
|
|
BIT_MAX_TIME=.000004
|
|
|
|
RESET_MIN_TIME=.000050
|
|
|
|
|
2019-07-13 02:37:50 +03: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 20:28:54 +03:00
|
|
|
self.pin = pin_params['pin']
|
2019-07-13 02:37:50 +03:00
|
|
|
self.mcu.register_config_callback(self.build_config)
|
2019-08-10 03:58:49 +03:00
|
|
|
self.color_order_GRB = config.getboolean("color_order_GRB", True)
|
2019-07-18 02:58:37 +03:00
|
|
|
self.chain_count = config.getint('chain_count', 1, minval=1, maxval=18)
|
2019-07-13 02:37:50 +03:00
|
|
|
self.neopixel_send_cmd = None
|
2019-07-18 02:19:51 +03:00
|
|
|
# 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.)
|
2019-08-10 03:58:49 +03:00
|
|
|
self.update_color_data(red, green, blue)
|
2019-07-18 02:19:51 +03:00
|
|
|
self.printer.register_event_handler("klippy:connect", self.send_data)
|
2019-07-13 02:37:50 +03:00
|
|
|
# Register commands
|
2020-04-25 06:52:49 +03: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-13 02:37:50 +03:00
|
|
|
def build_config(self):
|
2020-04-15 20:28:54 +03:00
|
|
|
bmt = self.mcu.seconds_to_clock(BIT_MAX_TIME)
|
|
|
|
rmt = self.mcu.seconds_to_clock(RESET_MIN_TIME)
|
|
|
|
self.mcu.add_config_cmd("config_neopixel oid=%d pin=%s"
|
|
|
|
" bit_max_ticks=%d reset_min_ticks=%d"
|
|
|
|
% (self.oid, self.pin, bmt, rmt))
|
2019-07-13 02:37:50 +03:00
|
|
|
cmd_queue = self.mcu.alloc_command_queue()
|
|
|
|
self.neopixel_send_cmd = self.mcu.lookup_command(
|
2019-07-17 23:36:28 +03:00
|
|
|
"neopixel_send oid=%c data=%*s", cq=cmd_queue)
|
2019-08-10 03:58:49 +03:00
|
|
|
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
|
2019-07-18 02:19:51 +03:00
|
|
|
def send_data(self, minclock=0):
|
|
|
|
self.neopixel_send_cmd.send([self.oid, self.color_data],
|
2019-08-07 05:25:26 +03:00
|
|
|
minclock=minclock,
|
|
|
|
reqclock=BACKGROUND_PRIORITY_CLOCK)
|
2019-08-07 02:30:48 +03:00
|
|
|
cmd_SET_LED_help = "Set the color of an LED"
|
2020-04-25 06:52:49 +03:00
|
|
|
def cmd_SET_LED(self, gcmd):
|
2019-07-13 02:37:50 +03:00
|
|
|
# Parse parameters
|
2020-04-25 06:52:49 +03: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.)
|
|
|
|
index = gcmd.get_int('INDEX', None, minval=1, maxval=self.chain_count)
|
|
|
|
transmit = gcmd.get_int('TRANSMIT', 1)
|
2019-08-10 03:58:49 +03:00
|
|
|
self.update_color_data(red, green, blue, index)
|
2019-07-13 02:37:50 +03:00
|
|
|
# Send command
|
2019-12-16 07:24:17 +03:00
|
|
|
if not transmit:
|
|
|
|
return
|
2019-07-13 02:37:50 +03:00
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
2019-07-18 02:19:51 +03:00
|
|
|
self.send_data(self.mcu.print_time_to_clock(print_time))
|
2019-07-13 02:37:50 +03:00
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return PrinterNeoPixel(config)
|