Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Proyecto Smart Watch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Leonardo Martinez Hornak
Proyecto Smart Watch
Commits
98e8fd53
Commit
98e8fd53
authored
4 years ago
by
Leonardo Martinez Hornak
Browse files
Options
Downloads
Patches
Plain Diff
Thread added for communication between tasks
parent
de646d72
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
software/smartwatch.py
+68
-5
68 additions, 5 deletions
software/smartwatch.py
with
68 additions
and
5 deletions
software/smartwatch.py
+
68
−
5
View file @
98e8fd53
#Reference for Threading and GUI architecture: https://maldus512.medium.com/how-to-setup-correctly-an-application-with-python-and-tkinter-107c6bc5a45
import
queue
import
tkinter
as
tk
import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
tkinter
import
TOP
,
BOTH
,
X
,
LEFT
,
RIGHT
from
matplotlib.backends.backend_tkagg
import
FigureCanvasTkAgg
from
types
import
SimpleNamespace
from
enum
import
Enum
from
threading
import
Thread
TIME_OUT
=
0.5
#Time out used for refreshing the User Interface
class
Messages
(
Enum
):
HRSETALARM
=
0
#Method in charge of updating the UI. It is called from another thread and uses queue for communication
def
refreshUI
(
guiRef
,
model
,
guiQueue
):
msg
=
None
while
True
:
try
:
msg
=
guiQueue
.
get
(
timeout
=
TIME_OUT
)
if
msg
==
Messages
.
HRSETALARM
:
model
.
count
+=
1
guiRef
.
entry
[
"
state
"
]
=
'
normal
'
guiRef
.
entry
.
delete
(
0
,
"
end
"
)
guiRef
.
entry
.
insert
(
0
,
"
12
"
)
guiRef
.
entry
[
"
state
"
]
=
'
disabled
'
guiRef
.
entry_two
.
delete
(
0
,
"
end
"
)
guiRef
.
entry_two
.
insert
(
0
,
"
Prueba
"
)
except
queue
.
Empty
:
pass
#Other tasks...
print
(
"
Han pasado 500 milisegundos
"
)
#Class for the User Interface
class
Application
(
tk
.
Frame
):
def
__init__
(
self
,
master
=
None
):
super
().
__init__
(
master
)
...
...
@@ -83,10 +117,10 @@ class Application(tk.Frame):
self
.
label_units_heartrate_three
=
tk
.
Label
(
self
.
frame_four
,
text
=
"
bpm
"
)
self
.
label_units_heartrate_three
.
pack
(
side
=
LEFT
)
self
.
button_heartrate
=
tk
.
Button
(
self
.
frame_four
,
text
=
"
Configurar alarma
"
,
command
=
self
.
configure_heartrate_alarm
)
self
.
button_heartrate
=
tk
.
Button
(
self
.
frame_four
,
text
=
"
Configurar alarma
"
)
#self.button_heartrate = tk.Button(self.frame_four, text = "Configurar alarma", command = self.configure_heartrate_alarm)
self
.
button_heartrate
.
pack
(
side
=
LEFT
)
#FRAME 5
self
.
frame_five
=
tk
.
Frame
(
self
.
master
)
self
.
frame_five
.
pack
(
fill
=
X
)
...
...
@@ -102,6 +136,35 @@ 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
):
entry
=
self
.
entry_heartrate
entry
[
"
state
"
]
=
'
normal
'
entry
.
delete
(
0
,
"
end
"
)
entry
.
insert
(
0
,
"
10
"
)
entry
[
"
state
"
]
=
'
disabled
'
self
.
button_heartrate
[
"
command
"
]
=
lambda
:
queue
.
put
(
Messages
.
HRSETALARM
)
#Just for testing purposes to emit reference to another widget
entry_two
=
self
.
entry_heartrate_minimum
return
SimpleNamespace
(
entry
=
entry
,
entry_two
=
entry_two
)
if
__name__
==
'
__main__
'
:
root
=
tk
.
Tk
()
app
=
Application
(
master
=
root
)
#Queue used to communicate the UI with the exterior
guiQueue
=
queue
.
Queue
()
guiRef
=
app
.
gui_reference
(
root
,
guiQueue
)
model
=
SimpleNamespace
(
count
=
0
)
#Thread used to separate the use interface with the front end. It is used to not slow down the GUI when processing information
t
=
Thread
(
target
=
refreshUI
,
args
=
(
guiRef
,
model
,
guiQueue
,))
t
.
daemon
=
True
t
.
start
()
app
.
mainloop
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment