diff --git a/*.pyc b/*.pyc deleted file mode 100644 index 0d20b64..0000000 --- a/*.pyc +++ /dev/null @@ -1 +0,0 @@ -*.pyc diff --git a/bin/run.sh b/bin/run.sh index 65a5b4e..db8afb5 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -1 +1 @@ -python3 -m meshtastic --debug +python3 -m meshtastic --debug "$@" diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 5d59436..82598fb 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -3,7 +3,7 @@ import argparse from .interface import StreamInterface import logging -from time import sleep +import sys def main(): @@ -15,13 +15,27 @@ def main(): help="The port the Meshtastic device is connected to, i.e. /dev/ttyUSB0. If unspecified, we'll try to find it.", default=None) - parser.add_argument("--debug", help="Show debug log message", + parser.add_argument( + "--seriallog", + help="Log device serial output to either 'stdout', 'none' or a filename to append to. Defaults to stdout.", + default="stdout") + + parser.add_argument("--debug", help="Show API library debug log messages", action="store_true") args = parser.parse_args() - logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) - client = StreamInterface(args.device) + + if args.seriallog == "stdout": + logfile = sys.stdout + elif args.seriallog == "none": + logging.debug("Not logging serial output") + logfile = None + else: + logging.info(f"Logging serial output to {args.seriallog}") + logfile = open(args.seriallog, 'w+', buffering=1) # line buffering + + client = StreamInterface(args.device, debugOut=logfile) if __name__ == "__main__": diff --git a/meshtastic/interface.py b/meshtastic/interface.py index dea26c7..0eae726 100644 --- a/meshtastic/interface.py +++ b/meshtastic/interface.py @@ -137,7 +137,7 @@ class StreamInterface(MeshInterface): devPath, 921600, exclusive=True, timeout=0.5) self._rxThread = threading.Thread(target=self.__reader, args=()) self._rxThread.start() - MeshInterface.__init__(self) + MeshInterface.__init__(self, debugOut=debugOut) def _sendToRadio(self, toRadio): """Send a ToRadio protobuf to the device""" @@ -172,10 +172,11 @@ class StreamInterface(MeshInterface): if ptr == 0: # looking for START1 if c != START1: self._rxBuf = empty # failed to find start - try: - self.debugOut.write(b.decode("utf-8")) - except: - self.debugOut.write('?') + if self.debugOut != None: + try: + self.debugOut.write(b.decode("utf-8")) + except: + self.debugOut.write('?') elif ptr == 1: # looking for START2 if c != START2: