Skip to content

Commit

Permalink
Added configuration checks. Moved config parsing to klippy:connect
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Schill <j.schill@web.de>
  • Loading branch information
julianschill committed Sep 25, 2022
1 parent 23c5226 commit 85a2310
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions klippy/extras/led_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,32 @@ def __init__(self, config):
self.configChains = self.configLeds.split('\n')
self.leds=[]
self.ledChains=[]
self.printer.register_event_handler('klippy:ready', self._handle_ready)
self.printer.register_event_handler('klippy:connect',
self._handle_connect)

def _handle_ready(self):
def _handle_connect(self):
for chain in self.configChains:
chain = chain.strip()
parms = [parameter.strip() for parameter in chain.split()
if parameter.strip()]
if parms:
ledChain = self.printer.lookup_object(parms[0]\
if parms[0].startswith("led_group"):
raise self.config.error(
"Error in '%s': LED group of LED groups is not allowed"
% (self.config.get_name(),))
ledChain = self.printer.lookup_object(parms[0]\
.replace(':',' '))
ledIndices = ''.join(parms[1:]).strip('()').split(',')
chainLen = ledChain.led_helper.get_led_count()
ledIndices = ''.join(parms[1:]).strip('()').split(',')
for led in ledIndices:
if led:
if '-' in led:
start, stop = map(int,led.split('-'))
if start > chainLen or stop > chainLen or \
start < 1 or stop < 1:
raise self.config.error(
"LED index out of range for '%s' in '%s'"
% (parms[0],self.config.get_name(),))
if stop == start:
ledList = [start-1]
elif stop > start:
Expand All @@ -35,11 +46,14 @@ def _handle_ready(self):
for i in ledList:
self.leds.append((ledChain, int(i)))
else:
for i in led.split(','):
self.leds.append((ledChain, \
(int(i)-1)))
i = int(led)
if i > chainLen or i < 1:
raise self.config.error(
"LED index out of range for '%s' in '%s'"
% (parms[0],self.config.get_name(),))
self.leds.append((ledChain, (int(i)-1)))
else:
for i in range(ledChain.led_helper.get_led_count()):
for i in range(chainLen):
self.leds.append((ledChain, int(i)))
if ledChain not in self.ledChains:
self.ledChains.append(ledChain)
Expand Down

0 comments on commit 85a2310

Please sign in to comment.