Skip to content

Commit

Permalink
Update led.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeanon committed Nov 10, 2023
1 parent dc8a434 commit 7a8a933
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions klippy/extras/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, config, update_func, led_count=1):
blue = config.getfloat('initial_BLUE', 0., minval=0., maxval=1.)
white = config.getfloat('initial_WHITE', 0., minval=0., maxval=1.)
self.led_state = [(red, green, blue, white)] * led_count
self.active_template = None
# Register commands
name = config.get_name().split()[-1]
gcode = self.printer.lookup_object('gcode')
Expand Down Expand Up @@ -93,6 +94,8 @@ def set_color(self, index, color):
new_led_state[index - 1] = color
self.led_state = new_led_state
self.need_transmit = True
def set_active_template(self, template):
self.active_template = template
def check_transmit(self, print_time):
if not self.need_transmit:
return
Expand Down Expand Up @@ -125,7 +128,8 @@ def lookahead_bgfunc(print_time):
#Send update now (so as not to wake toolhead and reset idle_timeout)
lookahead_bgfunc(None)
def get_status(self, eventtime=None):
return {'color_data': self.led_state}
return {'color_data': self.led_state,
'active_template': self.active_template}

# Main LED tracking code
class PrinterLED:
Expand Down Expand Up @@ -157,14 +161,21 @@ def _activate_timer(self):
return
reactor = self.printer.get_reactor()
self.render_timer = reactor.register_timer(self._render, reactor.NOW)
def _activate_template(self, led_helper, index, template, lparams):
def _activate_template(self,
led_helper,
index,
template,
lparams,
tpl_name):
key = (led_helper, index)
if template is not None:
uid = (template,) + tuple(sorted(lparams.items()))
self.active_templates[key] = (uid, template, lparams)
led_helper.set_active_template(tpl_name)
return
if key in self.active_templates:
del self.active_templates[key]
led_helper.set_active_template(None)
def _render(self, eventtime):
if not self.active_templates:
# Nothing to do - unregister timer
Expand Down Expand Up @@ -228,7 +239,11 @@ def cmd_SET_LED_TEMPLATE(self, gcmd):
except ValueError as e:
raise gcmd.error("Unable to parse '%s' as a literal" % (v,))
for index in led_helper.get_indices(gcmd, led_count):
self._activate_template(led_helper, index, template, lparams)
self._activate_template(led_helper,
index,
template,
lparams,
tpl_name)
self._activate_timer()

PIN_MIN_TIME = 0.100
Expand Down

0 comments on commit 7a8a933

Please sign in to comment.