From 61a9c2ca927b8ec820c06d479e254d4178d40233 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Tue, 23 Jul 2024 09:34:31 -0700 Subject: [PATCH] Add intercom --- navpoint/intercom.py | 34 ++++++++++++++++++++++++++ navpoint/usb.py | 57 ++++++++++++++++---------------------------- 2 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 navpoint/intercom.py diff --git a/navpoint/intercom.py b/navpoint/intercom.py new file mode 100644 index 0000000..615af60 --- /dev/null +++ b/navpoint/intercom.py @@ -0,0 +1,34 @@ +import queue + + +class Intercom: + def __init__(self, window, handler): + self.window = window + self.queue = queue.Queue() + self.handler = handler + + self.window.bind("<>", self._event) + + def clear(self): + try: + while True: + self.queue.get(block=False) + except queue.Empty: + pass + + def put(self, purpose, content): + self.queue.put( + ( + purpose, + content, + ) + ) + self.window.event_generate("<>", when="tail", state=1) + + def _event(self, event): + try: + purpose, content = self.queue.get(block=False) + except queue.Empty: + return + + self.handler(purpose, content) diff --git a/navpoint/usb.py b/navpoint/usb.py index 36239ca..ea4cb16 100644 --- a/navpoint/usb.py +++ b/navpoint/usb.py @@ -9,19 +9,17 @@ import serial.tools.list_ports import serial.serialutil import navpoint.content import navpoint.fix_path +import navpoint.intercom -def main(port, window, updates): +def main(port, intercom): try: with Serial(port, timeout=3) as stream: reader = NMEAReader(stream) - updates.put( - ( - "Connected", - port, - ) + intercom.put( + "Connected", + port, ) - window.event_generate("<>", when="tail", state=1) while True: _, parsed_data = reader.read() @@ -31,26 +29,21 @@ def main(port, window, updates): time = parsed_data.time date = parsed_data.date navpoint.content.content = f"""Navpoint1Position#icon{lon},{lat},0{date} {time} UTC""" - updates.put( - ( - "LocationUpdate", - { - "lat": lat, - "lon": lon, - "time": f"{date} {time} UTC", - }, - ) + intercom.put( + "LocationUpdate", + { + "lat": lat, + "lon": lon, + "time": f"{date} {time} UTC", + }, ) - window.event_generate("<>", when="tail", state=1) except AttributeError: pass except serial.serialutil.SerialException as e: - updates.put( - ( - "Disconnected", - port, - ) + intercom.put( + "Disconnected", + port, ) window.event_generate("<>", when="tail", state=1) messagebox.showerror( @@ -58,10 +51,8 @@ def main(port, window, updates): ) -def run(port, window, updates): - threading.Thread( - target=main, args=(port, window, updates), daemon=True - ).start() +def run(port, intercom): + threading.Thread(target=main, args=(port, intercom), daemon=True).start() def clear(queue_to_clear): @@ -84,15 +75,10 @@ def run_ui(): def selected(event): selection = dropdown.get().split(": ", 1)[0] if not selection.startswith("["): - clear(updates) - run(selection, window, updates) - - def new_data(event): - try: - purpose, content = updates.get(block=False) - except queue.Empty: - return + intercom.clear() + run(selection, intercom) + def new_data(purpose, content): if purpose == "LocationUpdate": updated.config(text="Last updated: " + content["time"]) elif purpose == "Connected": @@ -129,9 +115,8 @@ def run_ui(): refresh.pack() dropdown.bind("<>", selected) - window.bind("<>", new_data) - updates = queue.Queue() + intercom = navpoint.intercom.Intercom(window, new_data) try: window.iconbitmap(navpoint.fix_path.fix_path("icon.ico"))