Skip to content

Commit

Permalink
Fixed serial choosing
Browse files Browse the repository at this point in the history
  • Loading branch information
vovagorodok committed Jul 5, 2024
1 parent dc4c227 commit b94805e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ArduinoStreamLogger",
"version": "1.1.3",
"version": "1.1.4",
"description": "Simple ostream logger",
"keywords": "logger, logging, log, ostream, stream",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=vovagorodok_ArduinoStreamLogger
version=1.1.3
version=1.1.4
author=vovagorodok
maintainer=vovagorodok <[email protected]>
sentence=Simple ostream logger
Expand Down
54 changes: 41 additions & 13 deletions tools/serial_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def pull(self, ch: int):
self.searching = False
self._redraw()
elif ch == curses.KEY_F10:
exit()
exit_stdscr(self.stdscr)

if self.filtering:
if ch == curses.KEY_BACKSPACE:
Expand All @@ -547,7 +547,7 @@ def pull(self, ch: int):
self._redraw()
else:
if ch == ord('q'):
exit()
exit_stdscr(self.stdscr)

def _redraw(self):
col = self._draw_panel()
Expand Down Expand Up @@ -755,12 +755,39 @@ def pull(self):
self.nav.pull(ch)


def exit_with_error(error):
def start_stdscr():
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
curses.start_color()
curses.use_default_colors()
stdscr.nodelay(True)
return stdscr


def stop_stdscr(stdscr):
stdscr.keypad(1)
curses.echo()
curses.nocbreak()
curses.endwin()
print(f"\n{error}")


def exit_with_error(error):
print(error)
exit()


def exit_stdscr(stdscr):
stop_stdscr(stdscr)
exit()


def exit_stdscr_with_error(stdscr, error):
stop_stdscr(stdscr)
exit_with_error(error)


def find_serial_port():
ports = serial.tools.list_ports.comports()
ports = list(filter(lambda port: port.hwid != 'n/a', ports))
Expand All @@ -773,7 +800,7 @@ def find_serial_port():

print("Ports:")
for index, port in enumerate(ports):
print(f"{index}. {port.name}: {port.description} [{port.hwid}]")
print(f"{index}. {port.name}: {port.description}")

user_input = input("Chose port [0]: ")

Expand All @@ -789,7 +816,7 @@ def find_serial_port():
return ports[device_num].device


def main(stdscr):
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
default_config_path = os.path.join(script_dir, "config.yaml")
default_logs_dir = os.path.join(script_dir, "logs")
Expand Down Expand Up @@ -820,10 +847,10 @@ def main(stdscr):
timeout=.01)
except serial.serialutil.SerialException as e:
exit_with_error(e)
except KeyboardInterrupt:
exit()

curses.start_color()
curses.use_default_colors()
stdscr.nodelay(True)
stdscr = start_stdscr()
logs_monitor = LogsMonitor(stdscr, config, args.logs_dir)

try:
Expand All @@ -835,15 +862,16 @@ def main(stdscr):
except UnicodeDecodeError:
continue
except serial.serialutil.SerialException as e:
exit_with_error(e)
exit_stdscr_with_error(stdscr, e)

if len(log):
logs_monitor.on_log(log)

except KeyboardInterrupt:
exit()
exit_stdscr(stdscr)
except ValueError as e:
exit_with_error(e)
exit_stdscr_with_error(stdscr, e)


curses.wrapper(main)
if __name__ == "__main__":
main()

0 comments on commit b94805e

Please sign in to comment.