From a67306c76b0c0c2083f9e39e1186a7c3e0550744 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 29 Jan 2018 10:10:27 -0500 Subject: [PATCH] msgproto: Support default values in get_constant() calls Signed-off-by: Kevin O'Connor --- klippy/msgproto.py | 14 +++++++++----- klippy/serialhdl.py | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/klippy/msgproto.py b/klippy/msgproto.py index 736fe1b8..3d655faa 100644 --- a/klippy/msgproto.py +++ b/klippy/msgproto.py @@ -324,12 +324,16 @@ class MessageParser: except Exception as e: logging.exception("process_identify error") raise error("Error during identify: %s" % (str(e),)) - def get_constant(self, name): - try: - return self.config[name] - except KeyError: + class sentinel: pass + def get_constant(self, name, default=sentinel): + if name not in self.config: + if default is not self.sentinel: + return default raise error("Firmware constant '%s' not found" % (name,)) - def get_constant_float(self, name): + return self.config[name] + def get_constant_float(self, name, default=sentinel): + if name not in self.config and default is not self.sentinel: + return default try: return float(self.config[name]) except ValueError: diff --git a/klippy/serialhdl.py b/klippy/serialhdl.py index 08f7ef23..6040f3ab 100644 --- a/klippy/serialhdl.py +++ b/klippy/serialhdl.py @@ -90,8 +90,8 @@ class SerialReader: logging.info("MCU config: %s", " ".join( ["%s=%s" % (k, v) for k, v in msgparser.config.items()])) # Setup baud adjust - mcu_baud = float(msgparser.config.get('SERIAL_BAUD', 0.)) - if mcu_baud: + mcu_baud = msgparser.get_constant_float('SERIAL_BAUD', None) + if mcu_baud is not None: baud_adjust = self.BITS_PER_BYTE / mcu_baud self.ffi_lib.serialqueue_set_baud_adjust( self.serialqueue, baud_adjust)