2019-07-13 02:37:50 +03:00
|
|
|
# Support for "neopixel" leds
|
|
|
|
#
|
2022-03-23 05:49:47 +03:00
|
|
|
# Copyright (C) 2019-2022 Kevin O'Connor <kevin@koconnor.net>
|
2019-07-13 02:37:50 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2020-09-28 20:44:36 +03:00
|
|
|
import logging
|
2019-07-13 02:37:50 +03:00
|
|
|
|
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
|
|
|
|
|
2020-09-28 20:44:36 +03:00
|
|
|
MAX_MCU_SIZE = 500 # Sanity check on LED chain length
|
|
|
|
|
2019-07-13 02:37:50 +03:00
|
|
|
class PrinterNeoPixel:
|
|
|
|
def __init__(self, config):
|
2022-03-23 05:49:47 +03:00
|
|
|
self.printer = printer = config.get_printer()
|
|
|
|
self.mutex = printer.get_reactor().mutex()
|
2019-07-13 02:37:50 +03:00
|
|
|
# Configure neopixel
|
2022-03-23 05:49:47 +03:00
|
|
|
ppins = printer.lookup_object('pins')
|
2019-07-13 02:37:50 +03:00
|
|
|
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)
|
2020-09-28 20:44:36 +03:00
|
|
|
self.neopixel_update_cmd = self.neopixel_send_cmd = None
|
2022-04-18 16:34:31 +03:00
|
|
|
# Build color map
|
|
|
|
chain_count = config.getint('chain_count', 1, minval=1)
|
|
|
|
color_order = config.getlist("color_order", ["GRB"])
|
|
|
|
if len(color_order) == 1:
|
|
|
|
color_order = [color_order[0]] * chain_count
|
|
|
|
if len(color_order) != chain_count:
|
|
|
|
raise config.error("color_order does not match chain_count")
|
|
|
|
color_indexes = []
|
|
|
|
for lidx, co in enumerate(color_order):
|
2022-04-18 19:55:32 +03:00
|
|
|
if sorted(co) not in (sorted("RGB"), sorted("RGBW")):
|
2022-04-18 16:34:31 +03:00
|
|
|
raise config.error("Invalid color_order '%s'" % (co,))
|
2022-04-18 19:55:32 +03:00
|
|
|
color_indexes.extend([(lidx, "RGBW".index(c)) for c in co])
|
2022-04-18 16:34:31 +03:00
|
|
|
self.color_map = list(enumerate(color_indexes))
|
|
|
|
if len(self.color_map) > MAX_MCU_SIZE:
|
|
|
|
raise config.error("neopixel chain too long")
|
2022-03-23 05:49:47 +03:00
|
|
|
# Initialize color data
|
|
|
|
pled = printer.load_object(config, "led")
|
|
|
|
self.led_helper = pled.setup_helper(config, self.update_leds,
|
2022-04-18 16:34:31 +03:00
|
|
|
chain_count)
|
|
|
|
self.color_data = bytearray(len(self.color_map))
|
2022-03-23 05:49:47 +03:00
|
|
|
self.update_color_data(self.led_helper.get_status()['color_data'])
|
2020-09-28 20:44:36 +03:00
|
|
|
self.old_color_data = bytearray([d ^ 1 for d in self.color_data])
|
2022-03-23 05:49:47 +03:00
|
|
|
# Register callbacks
|
|
|
|
printer.register_event_handler("klippy:connect", self.send_data)
|
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)
|
2020-09-28 20:44:36 +03:00
|
|
|
self.mcu.add_config_cmd("config_neopixel oid=%d pin=%s data_size=%d"
|
2020-04-15 20:28:54 +03:00
|
|
|
" bit_max_ticks=%d reset_min_ticks=%d"
|
2020-10-09 03:06:59 +03:00
|
|
|
% (self.oid, self.pin, len(self.color_data),
|
2020-09-28 20:44:36 +03:00
|
|
|
bmt, rmt))
|
2019-07-13 02:37:50 +03:00
|
|
|
cmd_queue = self.mcu.alloc_command_queue()
|
2020-09-28 20:44:36 +03: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(
|
2020-11-05 19:40:34 +03:00
|
|
|
"neopixel_send oid=%c", "neopixel_result oid=%c success=%c",
|
2020-11-05 02:47:36 +03:00
|
|
|
oid=self.oid, cq=cmd_queue)
|
2022-03-23 05:49:47 +03:00
|
|
|
def update_color_data(self, led_state):
|
|
|
|
color_data = self.color_data
|
2022-04-18 16:34:31 +03:00
|
|
|
for cdidx, (lidx, cidx) in self.color_map:
|
|
|
|
color_data[cdidx] = int(led_state[lidx][cidx] * 255. + .5)
|
2020-10-21 19:23:18 +03:00
|
|
|
def send_data(self, print_time=None):
|
2020-11-05 20:54:34 +03: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
|
|
|
|
minclock = 0
|
|
|
|
if print_time is not None:
|
|
|
|
minclock = self.mcu.print_time_to_clock(print_time)
|
|
|
|
scmd = self.neopixel_send_cmd.send
|
2020-12-01 19:09:00 +03:00
|
|
|
if self.printer.get_start_args().get('debugoutput') is not None:
|
|
|
|
return
|
2020-11-05 20:54:34 +03:00
|
|
|
for i in range(8):
|
|
|
|
params = scmd([self.oid], minclock=minclock,
|
|
|
|
reqclock=BACKGROUND_PRIORITY_CLOCK)
|
|
|
|
if params['success']:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
logging.info("Neopixel update did not succeed")
|
2022-03-23 05:49:47 +03:00
|
|
|
def update_leds(self, led_state, print_time):
|
|
|
|
def reactor_bgfunc(eventtime):
|
2020-11-05 20:54:34 +03:00
|
|
|
with self.mutex:
|
2022-03-23 05:49:47 +03:00
|
|
|
self.update_color_data(led_state)
|
|
|
|
self.send_data(print_time)
|
|
|
|
self.printer.get_reactor().register_callback(reactor_bgfunc)
|
|
|
|
def get_status(self, eventtime=None):
|
|
|
|
return self.led_helper.get_status(eventtime)
|
2019-07-13 02:37:50 +03:00
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return PrinterNeoPixel(config)
|