From 1e77f337b16e125fd9fd20eb79911471d74504cc Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sat, 12 Mar 2022 16:00:05 +0000 Subject: [PATCH] Improve shutdown time of Controller Handler. --- controllers/mattchbox_usb.py | 55 +++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/controllers/mattchbox_usb.py b/controllers/mattchbox_usb.py index 6e90d82..ec236e7 100644 --- a/controllers/mattchbox_usb.py +++ b/controllers/mattchbox_usb.py @@ -68,16 +68,18 @@ class MattchBox(Controller): if port: # connect to serial port self.ser = serial.serial_for_url(port, do_not_open=True) - self.ser.baudrate = 2400 - try: - self.ser.open() - self.logger.log.info("Connected to serial port {}".format(port)) - except (FileNotFoundError, serial.SerialException) as e: - self.logger.log.error( - "Could not open serial port {}:\n{}".format(port, e) - ) - self._disconnected() - self.ser = None + if self.ser: + self.ser.baudrate = 2400 + self.ser.timeout = 0.1 # Speed up waiting for a byte. + try: + self.ser.open() + self.logger.log.info("Connected to serial port {}".format(port)) + except (FileNotFoundError, serial.SerialException) as e: + self.logger.log.error( + "Could not open serial port {}:\n{}".format(port, e) + ) + self._disconnected() + self.ser = None else: self.ser = None @@ -88,22 +90,23 @@ class MattchBox(Controller): self.ser and self.ser.is_open and self.port ): # If self.port is changing (via state_handler), we should stop. try: - line = int.from_bytes( - self.ser.read(1), "big" - ) # Endianness doesn't matter for 1 byte. - self.logger.log.info("Received from controller: " + str(line)) - if line == 255: - self.ser.write(b"\xff") # Send 255 back, this is a keepalive. - elif line in [51, 52, 53]: - # We've received a status update about fader live status, fader is down. - self.sendToPlayer(line - 51, "SETLIVE:False") - elif line in [61, 62, 63]: - # We've received a status update about fader live status, fader is up. - self.sendToPlayer(line - 61, "SETLIVE:True") - elif line in [1, 3, 5]: - self.sendToPlayer(int(line / 2), "PLAYPAUSE") - elif line in [2, 4, 6]: - self.sendToPlayer(int(line / 2) - 1, "STOP") + if self.ser.in_waiting > 0: + line = int.from_bytes( + self.ser.read(1), "big" + ) # Endianness doesn't matter for 1 byte. + self.logger.log.info("Received from controller: " + str(line)) + if line == 255: + self.ser.write(b"\xff") # Send 255 back, this is a keepalive. + elif line in [51, 52, 53]: + # We've received a status update about fader live status, fader is down. + self.sendToPlayer(line - 51, "SETLIVE:False") + elif line in [61, 62, 63]: + # We've received a status update about fader live status, fader is up. + self.sendToPlayer(line - 61, "SETLIVE:True") + elif line in [1, 3, 5]: + self.sendToPlayer(int(line / 2), "PLAYPAUSE") + elif line in [2, 4, 6]: + self.sendToPlayer(int(line / 2) - 1, "STOP") except Exception: time.sleep(5) self.connect(self.port)