Explorar el Código

changed Log_handler and Log Window

w.mueller hace 3 años
padre
commit
51fb2d5c4d

+ 11 - 2
raspberry-pi/Log_handler.py

@@ -1,6 +1,6 @@
 import datetime
 
-class log_list():
+class LogList():
     def __init__(self):
         self.log_list = list()
 
@@ -13,4 +13,13 @@ class log_list():
             self.log_list.pop(0)
 
     def get_log_list(self):
-        return self.log_list
+        return self.log_list
+
+
+_log_handler = None
+
+def get_log_handler():
+    global _log_handler
+    if not _log_handler:
+        _log_handler = LogList()
+    return _log_handler

+ 25 - 7
raspberry-pi/gui/Logscreen.py

@@ -1,25 +1,38 @@
 import tkinter as tk
 from tkinter.ttk import Progressbar
 
+import Log_handler
+
 class LogScreen(tk.Frame):
-    def __init__(self, root, log_handler):
+    def __init__(self, root):
         self.root = root
         tk.Frame.__init__(self, root)
 
-        self.y_scroll = tk.Scrollbar(self.root)
+        self.log_handler = Log_handler.get_log_handler()
+
+        self.text = tk.Frame(self,relief="sunken",borderwidth=1)
+        self.text.pack(expand=True,fill=tk.BOTH)
+
+        self.y_scroll = tk.Scrollbar(self.text)
         self.y_scroll.pack(side="right", fill="y")
 
-        self.x_scroll = tk.Scrollbar(self.root, orient='horizontal')
+        self.x_scroll = tk.Scrollbar(self.text, orient='horizontal')
         self.x_scroll.pack(side="bottom", fill="x")
 
-        self.textfield = tk.Listbox(self.root, yscrollcommand=self.y_scroll.set, xscrollcommand=self.x_scroll.set)
+        self.textfield = tk.Listbox(self.text, yscrollcommand=self.y_scroll.set, xscrollcommand=self.x_scroll.set)
         self.textfield.pack(side="left",expand=True, fill=tk.BOTH)
-        for element in log_handler.get_log_list():
-            self.textfield.insert(tk.END, element)
 
         self.y_scroll.config(command=self.textfield.yview)
         self.x_scroll.config(command=self.textfield.xview)
 
+        self.quit_button = tk.Button(self, text="Quit", command=self.close, height=2, width = 10)
+        self.quit_button.pack(side="right", fill="both")
+
+        self.refresh_button = tk.Button(self, text="Refresh", command=self.fill_textfield, height=2, width = 10)
+        self.refresh_button.pack(side="right", fill="both")
+
+        self.fill_textfield()
+
         root.bind('<Escape>', self.close)
     
     def update(self):
@@ -28,4 +41,9 @@ class LogScreen(tk.Frame):
 
     def close(self):
         if self.root.winfo_exists():
-            self.root.destroy()
+            self.root.destroy()
+    
+    def fill_textfield(self):
+        self.textfield.delete(0,"end")
+        for element in self.log_handler.get_log_list():
+            self.textfield.insert(tk.END, element)

+ 5 - 4
raspberry-pi/gui/mainWindow.py

@@ -7,17 +7,18 @@ import numpy as np
 from gui.popup import CalibrationPopUp
 from gui.graph import Graph
 from gui.Logscreen import LogScreen
+import Log_handler
 
 
 class MainWindow(tk.Frame):
-  def __init__(self, root, ac_sensor, ac_queue, ac_cal_state, log_handler):
+  def __init__(self, root, ac_sensor, ac_queue, ac_cal_state):
     self.root = root
     self.popup = None
     self.log_window = None
     self.ac_cal_state = ac_cal_state
     self.ac_sensor = ac_sensor
     self.ac_queue = ac_queue
-    self.log_handler = log_handler
+    self.log_handler = Log_handler.get_log_handler()
     self.log_handler.add_item("start Main Window")
 
     tk.Frame.__init__(self, root)
@@ -51,7 +52,7 @@ class MainWindow(tk.Frame):
     clear_button = tk.Button(self.controls, text="Clear graph", command=self.graph.clear, height=4)
     clear_button.pack(side="bottom", fill="both")
 
-    logscreen_button = tk.Button(self.controls, text="Log", command=self.open_log, height=2, foreground="red")
+    logscreen_button = tk.Button(self.controls, text="Log", command=self.open_log, height=4)
     logscreen_button.pack(side="bottom", fill="both")
 
   def update(self):
@@ -131,6 +132,6 @@ class MainWindow(tk.Frame):
     self.log_root.geometry(f'1000x400+{int(x)}+{int(y)}')
     # deactivate mainWindow
     self.log_root.grab_set()
-    self.log_window = LogScreen(self.log_root,self.log_handler)
+    self.log_window = LogScreen(self.log_root)
     self.log_window.pack(side="top", fill="both", expand=True)
 

+ 3 - 3
raspberry-pi/main.py

@@ -12,17 +12,17 @@ conf = configparser.ConfigParser()
 conf.read('config.ini')
 
 def main():
-  log_handler = Log_handler.log_list()
+  log_handler = Log_handler.get_log_handler()
   ac_queue = queue.Queue()
   ac_calibration_state = CalibrationStateMashine(log_handler)
-  ac_sensor = AcusticSensor(conf, ac_queue, ac_calibration_state,log_handler)
+  ac_sensor = AcusticSensor(conf, ac_queue, ac_calibration_state)
 
   try:
     ac_sensor.start()
     root = tk.Tk()
     root.title("Tracking System")
     root.attributes('-fullscreen', True)
-    view = MainWindow(root, ac_sensor, ac_queue, ac_calibration_state,log_handler)
+    view = MainWindow(root, ac_sensor, ac_queue, ac_calibration_state)
     view.pack(side="top", fill="both", expand=True)
     view.update()
     root.mainloop()

+ 3 - 2
raspberry-pi/sensors/acusticSensor.py

@@ -6,12 +6,13 @@ import random
 import traceback
 
 from sensors.connection import globalArduinoSlave
+import Log_handler
 
 conn = globalArduinoSlave()
 
 
 class AcusticSensor:
-  def __init__(self, conf, ac_queue, calibration_state,log_handler):
+  def __init__(self, conf, ac_queue, calibration_state):
     self.conf = conf
     self.ac_queue               = ac_queue
     self.calibration_state      = calibration_state
@@ -25,7 +26,7 @@ class AcusticSensor:
     self.overhead_left          = float(conf["ac_sensor"]["overhead_left"])
     self.overhead_right         = float(conf["ac_sensor"]["overhead_right"])
 
-    self.log_handler = log_handler
+    self.log_handler = Log_handler.get_log_handler()
     self.log_handler.add_item("start ac_sensor")
 
     # temporary calibration variables

+ 13 - 0
raspberry-pi/sensors/connection.py

@@ -3,12 +3,14 @@ import time
 
 import serial
 import serial.tools.list_ports
+import Log_handler
 
 
 class SerialConnection:
   def __init__(self):
     self._ser = None
     self.port = None
+    self.log_handler = Log_handler.get_log_handler()
   
   # try connecting to an available port
   def open(self, port = None, baudrate = 1000000):
@@ -19,21 +21,27 @@ class SerialConnection:
       try:
         self._ser = serial.Serial(self.port, baudrate)
         print("SERIAL: connected to " + self.port)
+        self.log_handler.add_item("SERIAL: connected to " + self.port)
+
       except serial.SerialException as e:
         print("SERIAL:", e)
+        self.log_handler.add_item("SERIAL:", e)
 
     else:
       for port, name, _ in serial.tools.list_ports.comports():
         try:
           self._ser = serial.Serial(port, baudrate)
           print("SERIAL: connected to " + port +  ", name: " + name)
+          self.log_handler.add_item("SERIAL: connected to " + port +  ", name: " + name)
           self.port = port
           break
         except serial.SerialException as e:
           print("SERIAL:", e)
+          self.log_handler.add_item("SERIAL:", e)
 
     if not self._ser:
         print("SERIAL: no device available")
+        self.log_handler.add_item("SERIAL: no device available")
     
   def send(self, bytes):
     if self._ser:
@@ -52,9 +60,11 @@ class SerialConnection:
         return self._ser.read(n)
       except AttributeError as e:
         print("SERIAL:", e)
+        self.log_handler.add_item("SERIAL:", e)
         return None
       except serial.SerialException as e:
         print("SERIAL:", e)
+        self.log_handler.add_item("SERIAL:", e)
         if self._ser.isOpen():
           self._ser.close()
         self._ser = None
@@ -70,9 +80,11 @@ class SerialConnection:
         return self._ser.readline()
       except AttributeError as e:
         print("SERIAL:", e)
+        self.log_handler.add_item("SERIAL:", e)
         return None
       except serial.SerialException as e:
         print("SERIAL:", e)
+        self.log_handler.add_item("SERIAL:", e)
         if self._ser.isOpen():
           self._ser.close()
         self._ser = None
@@ -151,6 +163,7 @@ class ArduinoSlave(SerialConnection):
             cb(self.sensorData)
         else:
           print("SERIAL: ", data[:-2])
+          self.log_handler.add_item("SERIAL: ", data[:-2])
 
 # this allows the usage of a single instance of ArduinoSlave
 _conn = None