Skip to content
Snippets Groups Projects
Commit 2a982032 authored by Leonardo Martinez Hornak's avatar Leonardo Martinez Hornak
Browse files

Chart is being updated every 500 mseconds

parent 98e8fd53
Branches
Tags
No related merge requests found
#Reference for Threading and GUI architecture: https://maldus512.medium.com/how-to-setup-correctly-an-application-with-python-and-tkinter-107c6bc5a45
#Reference for function animate https://towardsdatascience.com/plotting-live-data-with-matplotlib-d871fac7500b
import queue
import tkinter as tk
import matplotlib.pyplot as plt
......@@ -6,6 +7,7 @@ import numpy as np
from tkinter import TOP, BOTH, X, LEFT, RIGHT
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.animation import FuncAnimation
from types import SimpleNamespace
from enum import Enum
from threading import Thread
......@@ -35,7 +37,7 @@ def refreshUI(guiRef, model, guiQueue):
pass
#Other tasks...
print("Han pasado 500 milisegundos")
#print("Han pasado 500 milisegundos")
#Class for the User Interface
......@@ -46,7 +48,11 @@ class Application(tk.Frame):
#self.master.geometry('1000x1000')
self.master.title('Smart Watch')
self.pack(fill=BOTH, expand=True)
#Animate needs to be created here and declared self. If not, will be gargabe collected
self.figure, self.ax = plt.subplots(1, 1, figsize=(6,5))
self.create_widgets()
self.animate = FuncAnimation(self.figure, self.refreshChart, interval=500)
def create_widgets(self):
......@@ -60,20 +66,9 @@ class Application(tk.Frame):
#FRAME 2 - GRAPH
self.frame_two = tk.Frame(self.master)
self.frame_two.pack(fill=X)
figure = plt.Figure(figsize=(6,5), dpi=100)
ax = figure.add_subplot(111)
chart_type = FigureCanvasTkAgg(figure, self.frame_two)
chart_type = FigureCanvasTkAgg(self.figure, self.frame_two)
chart_type.get_tk_widget().pack()
x = np.arange(10)
y = 2.5 * np.sin(x / 20 * np.pi)
ax.plot(x, y, color='tab:blue')
ax.set_xlim([0, 10])
ax.set_ylim([-1, 1])
ax.set_title('PPG - RED')
#FRAME 3
self.frame_three = tk.Frame(self.master)
self.frame_three.pack(fill=X)
......@@ -136,7 +131,6 @@ class Application(tk.Frame):
print("Alarma HR!")
#Method that provides reference to the user interface. It uses a queue to communicate the UI with the exterior. It returns references to
#inner objetcs so the exterior can communicate with it. More references can be given in "SimpleNameSpace"
def gui_reference(self, root, queue):
......@@ -152,6 +146,20 @@ class Application(tk.Frame):
entry_two = self.entry_heartrate_minimum
return SimpleNamespace(entry=entry, entry_two = entry_two)
#Function used to update the chart with external data. It is called every 500 mseconds. Data needs to be passed by reference using "gui_reference"
def refreshChart(self, x):
#get data
x = np.arange(10)
y = 2.5 * np.sin(x / 20 * np.pi)
self.ax.plot(x, y, color='tab:blue')
print("Chart refreshed")
#set limits and title
self.ax.set_xlim([0, 10])
self.ax.set_ylim([-1, 1])
self.ax.set_title('PPG - RED')
if __name__ == '__main__':
root = tk.Tk()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment