-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (57 loc) · 2.29 KB
/
app.py
File metadata and controls
71 lines (57 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# imports
import tkinter as tk
import threading
import queue
from ui.components import NumberBox
import data_io # this is the module that fetch our data
# main app class
class App:
def __init__(self):
# initialize app
self.root = tk.Tk()
self.root.title("Boxes and numbers")
self.root.geometry("800x600")
# Create a Queue for thread safe data communication with the serial-thingy
self.data_queue : queue.Queue = queue.Queue()
# Make the boxes - User interface
self.speedBox = NumberBox(self.root,"Speed",23,2,"mm/s")
self.rpmBox = NumberBox(self.root,"RPM",123,1,"rpm")
# Place the boxes
self.speedBox.grid(row=0, column=0, padx=20, pady=20)
self.rpmBox.grid(row=0, column=1 , padx=20, pady=20)
def update_loop(self):
# Drain the queue and display the latest data
latest_data = None
while not self.data_queue.empty():
try:
# get_nowait means: Get data immediately. If nothing is there, it raises queue.Empty exception
latest_data = self.data_queue.get_nowait()
except queue.Empty:
# Safe way to handle if the queue was emptied somewhere else for some reason
break # break from the 'while not self.data_queue.empty()'
if latest_data is not None:
# keys match what data_io.py sends into the queue
if "speed" in latest_data:
self.speedBox.update_value(latest_data["speed"])
if "rpm" in latest_data:
self.rpmBox.update_value(latest_data["rpm"])
# schedule the next run of the check for updates in 100ms
self.root.after(100, self.update_loop)
def run(self):
# Start fetching data in another thread to keep the UI thread responsive
data_thread = threading.Thread(
target=data_io.fetch_data,
args=(self.data_queue,),
daemon=True
)
data_thread.start()
# Start the "heartbeat" the updates the ui at regular intervals
self.update_loop()
# Start the main event loop of tkinter
self.root.mainloop()
# main entry point
if __name__ == "__main__":
# create app instance
app = App()
# run app
app.run()