-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSF_Soft_Sensor_GUI.py
613 lines (536 loc) · 26 KB
/
NSF_Soft_Sensor_GUI.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import tkinter as tk
from tkinter import ttk
from tkinter import font as tkfont
from threading import Thread
from datetime import datetime
from tkinter.filedialog import askdirectory
import time
import serial
import serial.tools.list_ports
import asyncio
from bleak import BleakScanner, BleakClient, backends
import csv
import queue
from functools import wraps
import math
from ratelimit import limits, sleep_and_retry
glb_command = str
class FlexSense_Gui(tk.Tk):
"""
__init__
Initializes the object and sets everything up for use
"""
def __init__(self, *args, **kwargs):
self.experimentName = "qtpy"
self.experimentPath = ""
self.samplerate = 60
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
self.mocapList = []
self.can_take = bool
self.startTime = float
self.endTesting = queue.Queue()
self.bluetooth_command = queue.Queue()
self.bleClient = None
self.dataChar = "eb523251-6513-11ed-9c9d-0800200c9a66".format(0xFFE1)
self.isActive = True
"""
Construct GUI
Construst the GUI and sets up all buttons and functionality
Variables are denoted by the naming convention below:
Variable Type]VariableName ex. lFilePath
l = Label
e = Entry
b = Button
f = Frame
d = Dropdown Menu
c = Checkbox
s = Slider
sp = Spinbox
"""
async def construct_gui(self):
self.title("NSF Soft Sensor Data Collection")
self.resizable()
self.tasks = []
#icon = tk.PhotoImage(file="uml-logo.png")
#self.iconphoto(False, icon)
s = ttk.Style()
s.theme_use('alt')
s.configure("red.Horizontal.TProgressbar", background='red')
s.configure("yellow.Horizontal.TProgressbar", background='yellow')
s.configure("green.Horizontal.TProgressbar", background='green')
BatteryFrame = tk.Frame(master=self)
self.vBattery = tk.IntVar(master=self, value=0)
lBatteryLabel = tk.Label(master=BatteryFrame, text="Battery Level")
self.pbBattery = ttk.Progressbar(master=BatteryFrame, orient='horizontal', mode="determinate", name="battery percentage", variable=self.vBattery, style='green.Horizontal.TProgressbar')
lBattery = tk.Label(master=BatteryFrame, textvariable=self.vBattery)
lPercent = tk.Label(master=BatteryFrame, text="%")
#sTestScale = tk.Scale(master=BatteryFrame, variable=self.vBattery)
lBatteryLabel.grid(column=0, row=0)
self.pbBattery.grid(column=0, row=1)
lBattery.grid(column=1, row=1)
lPercent.grid(column=2, row=1)
#lHeader = tk.Label(master=BatteryFrame, text="NSF Soft Sensor Data Collection", font=self.title_font, anchor='ne')
#lHeader.grid(column=4, row=1, padx=5)
#sTestScale.pack(side=tk.LEFT, pady=5)
f = [tk.Frame(master=self)]
lHeader = tk.Label(master=f[-1], text="NSF Soft Sensor Data Collection", font=self.title_font, anchor='center')
lHeader.pack(side=tk.LEFT, pady=5)
f.append(tk.Frame(master=self))
lFilePath = tk.Label(master=f[-1], text="Log File Path ")
self.bFilePath = tk.Button(master=f[-1], text="Select File Path",
command=lambda: self.filePathQuery(), width=50)
lFilePath.pack(side=tk.LEFT, pady=5)
self.bFilePath.pack(side=tk.LEFT, pady=5)
f.append(tk.Frame(master=self))
lFileName = tk.Label(master=f[-1], text="Log File Name ")
self.eFileName = tk.Entry(master=f[-1], width=100)
self.eFileName.insert(0, self.experimentName)
lFileName.pack(side=tk.LEFT, pady=5)
self.eFileName.pack(side=tk.LEFT, pady=5)
f.append(tk.Frame(master=self))
self.mocapBool = tk.IntVar(master=f[-1], value=0)
lMocapCheck = tk.Label(master=f[-1], text="Check for Smart MOCAP Enabling ")
cMocapCheck = tk.Checkbutton(master=f[-1], variable=self.mocapBool, command=self.mocapChecked)
lMocapCheck.pack(side=tk.LEFT, pady=5)
cMocapCheck.pack(side=tk.LEFT, pady=5)
f.append(tk.Frame(master=self))
self.mocapCOM = tk.StringVar(master=f[-1])
def acquireCOMList():
self.dMocapList['menu'].delete(0,'end')
self.mocapList = ['None']
self.mocapCOM.set('None')
for p in serial.tools.list_ports.comports():
self.mocapList.append(p)
for option in self.mocapList:
self.dMocapList['menu'].add_command(label=option, command=tk._setit(self.mocapCOM, option))
self.print("COM Options Reset\n")
lMocapList = tk.Label(master=f[-1], text="Choose COM Port if using MOCAP ")
self.dMocapList = ttk.OptionMenu(f[-1], self.mocapCOM, self.mocapList, *self.mocapList)
bMocapReset = tk.Button(master=f[-1], text="Update COM Options", command=lambda: acquireCOMList(), width=20)
lMocapList.pack(side=tk.LEFT, pady=5)
self.dMocapList.pack(side=tk.LEFT, pady=5)
bMocapReset.pack(side=tk.LEFT, pady=5)
# tk.Slider widget for hz here
f.append(tk.Frame(master=self))
lHz = tk.Label(master=f[-1], text="Slide to Desired Sample Rate (Hz):")
def sliderCommand(event):
self.samplerate = self.sHz.get()
self.print("Sample Rate set to " + str(self.samplerate) + "Hz\n")
self.sHz = tk.Scale(master=f[-1], from_= 60, to_=120, resolution=20, tickinterval=20, length=200, orient="horizontal", command=sliderCommand)
lHz.pack(side=tk.LEFT, pady=5)
self.sHz.pack(side=tk.LEFT, pady=5)
f.append(tk.Frame(master=self))
def connect():
global glb_command
glb_command = "connect"
return
def capture():
global glb_command
glb_command = "capture"
if(self.bTesting['text'] == "Stop Testing"):
self.endTesting.put(1)
#print("Test")
return
self.bBluetooth = tk.Button(master=f[-1], text="Connect Bluetooth", command= lambda: Thread(connect()), width=50)
self.bTesting = tk.Button(master=f[-1], text="Begin Testing", command=lambda: Thread(capture()), width=50, state=tk.DISABLED)
self.bBluetooth.pack(side=tk.LEFT, pady=5, padx=5)
self.bTesting.pack(side=tk.LEFT, pady=5, padx=5)
f.append(tk.Frame(master=self))
self.bIdle = tk.Button(master=f[-1], text="Idle/Transition", command= lambda: Thread(setLabel(self.bIdle, "Idle")), state=tk.DISABLED)
self.disabledButton = self.bIdle
bWalking = tk.Button(master=f[-1], text="Walking", command= lambda: Thread(setLabel(bWalking, "Walking")))
bJogging = tk.Button(master=f[-1], text="Jogging", command= lambda: Thread(setLabel(bJogging, "Jogging")))
bSitToStand = tk.Button(master=f[-1], text="Sitting to Standing", command= lambda: Thread(setLabel(bSitToStand, "SitToStand")))
bWalkInc = tk.Button(master=f[-1], text="Walking Incline", command= lambda: Thread(setLabel(bWalkInc, "WalkingIncline")))
bWalkDec = tk.Button(master=f[-1], text="Walking Decline", command= lambda: Thread(setLabel(bWalkDec, "WalkingDecline")))
bSquating = tk.Button(master=f[-1], text="Squating", command= lambda: Thread(setLabel(bSquating, "Squating")))
bStepUp = tk.Button(master=f[-1], text="Step Up", command= lambda: Thread(setLabel(bStepUp, "StepUp")))
bStepDown = tk.Button(master=f[-1], text="Step Down", command= lambda: Thread(setLabel(bStepDown, "StepDown")))
def setLabel(button, label):
self.disabledButton['state'] = tk.NORMAL
self.disabledButton = button
self.vLabel.set(label)
self.disabledButton['state'] = tk.DISABLED
return
self.bIdle.pack(side=tk.LEFT, pady=5, padx=5)
bWalking.pack(side=tk.LEFT, pady=5, padx=5)
bJogging.pack(side=tk.LEFT, pady=5, padx=5)
bSitToStand.pack(side=tk.LEFT, pady=5, padx=5)
bWalkInc.pack(side=tk.LEFT, pady=5, padx=5)
bWalkDec.pack(side=tk.LEFT, pady=5, padx=5)
bSquating.pack(side=tk.LEFT, pady=5, padx=5)
bStepUp.pack(side=tk.LEFT, pady=5, padx=5)
bStepDown.pack(side=tk.LEFT, pady=5, padx=5)
f.append(tk.Frame(master=self))
lTimer = tk.Label(master=f[-1], text="Set how long you want the label to run for:")
lTimer.pack(side=tk.LEFT, pady=5, padx=5)
f.append(tk.Frame(master=self))
self.spHours = ttk.Spinbox(master=f[-1], from_= 0, to=1000, increment=1, width=5)
self.spMinutes = ttk.Spinbox(master=f[-1], from_= 0, to=1000, increment=1, width=5)
self.spSeconds = ttk.Spinbox(master=f[-1], from_= 0, to=1000, increment=1, width=5)
self.spHours.set(0)
self.spMinutes.set(0)
self.spSeconds.set(0)
self.spHours.grid(column=0, row=0, padx=5, pady=5)
self.spMinutes.grid(column=1, row=0, padx=5, pady=5)
self.spSeconds.grid(column=2, row=0, padx=5, pady=5)
lHours = tk.Label(master=f[-1], text="Hours")
lMinutes = tk.Label(master=f[-1], text="Minutes")
lSeconds = tk.Label(master=f[-1], text="Seconds")
lHours.grid(column=0, row=1, padx=5, pady=5)
lMinutes.grid(column=1, row=1, padx=5, pady=5)
lSeconds.grid(column=2, row=1, padx=5, pady=5)
f.append(tk.Frame(master=self))
lDataLabel1 = tk.Label(master=f[-1], text="Sensor 1")
lDataLabel1.grid(column=0, row=0, padx=5, pady=5)
lDataLabel2 = tk.Label(master=f[-1], text="Sensor 2")
lDataLabel2.grid(column=1, row=0, padx=5, pady=5)
lDataLabel3 = tk.Label(master=f[-1], text="Sensor 3")
lDataLabel3.grid(column=2, row=0, padx=5, pady=5)
lDataLabel4 = tk.Label(master=f[-1], text="Sensor 4")
lDataLabel4.grid(column=3, row=0, padx=5, pady=5)
lDataLabel5 = tk.Label(master=f[-1], text="Sensor 5")
lDataLabel5.grid(column=4, row=0, padx=5, pady=5)
lDataLabel6 = tk.Label(master=f[-1], text="Sensor 6")
lDataLabel6.grid(column=5, row=0, padx=5, pady=5)
lDataLabel7 = tk.Label(master=f[-1], text="Sensor 7")
lDataLabel7.grid(column=6, row=0, padx=5, pady=5)
lDataLabel8 = tk.Label(master=f[-1], text="Sensor 8")
lDataLabel8.grid(column=7, row=0, padx=5, pady=5)
lLabelLabel = tk.Label(master=f[-1], text="Label")
lLabelLabel.grid(column=8, row=0, padx=5, pady=5)
self.vData0 = tk.StringVar(master=f[-1], value=0)
self.lData0 = tk.Label(master=f[-1], textvariable=self.vData0, anchor=tk.CENTER)
self.lData0.grid(column=0, row=1, padx=5, pady=5)
self.vData1 = tk.StringVar(master=f[-1], value=0)
self.lData1 = tk.Label(master=f[-1], textvariable=self.vData1, anchor=tk.CENTER)
self.lData1.grid(column=1, row=1, padx=5, pady=5)
self.vData2 = tk.StringVar(master=f[-1], value=0)
self.lData2 = tk.Label(master=f[-1], textvariable=self.vData2, anchor=tk.CENTER)
self.lData2.grid(column=2, row=1, padx=5, pady=5)
self.vData3 = tk.StringVar(master=f[-1], value=0)
self.lData3 = tk.Label(master=f[-1], textvariable=self.vData3, anchor=tk.CENTER)
self.lData3.grid(column=3, row=1, padx=5, pady=5)
self.vData4 = tk.StringVar(master=f[-1], value=0)
self.lData4 = tk.Label(master=f[-1], textvariable=self.vData4, anchor=tk.CENTER)
self.lData4.grid(column=4, row=1, padx=5, pady=5)
self.vData5 = tk.StringVar(master=f[-1], value=0)
self.lData5 = tk.Label(master=f[-1], textvariable=self.vData5, anchor=tk.CENTER)
self.lData5.grid(column=5, row=1, padx=5, pady=5)
self.vData6 = tk.StringVar(master=f[-1], value=0)
self.lData6 = tk.Label(master=f[-1], textvariable=self.vData6, anchor=tk.CENTER)
self.lData6.grid(column=6, row=1, padx=5, pady=5)
self.vData7 = tk.StringVar(master=f[-1], value=0)
self.lData7 = tk.Label(master=f[-1], textvariable=self.vData7, anchor=tk.CENTER)
self.lData7.grid(column=7, row=1, padx=5, pady=5)
self.vLabel = tk.StringVar(master=f[-1], value="Idle")
self.lLabel = tk.Label(master= f[-1], textvariable=self.vLabel, anchor=tk.CENTER)
self.lLabel.grid(column=8, row=1, padx=5, pady=5)
f.append(tk.Frame(master=self))
hor = tk.Scrollbar(f[-1], orient="horizontal")
hor.pack(side=tk.BOTTOM, fill=tk.X)
vert = tk.Scrollbar(f[-1])
vert.pack(side=tk.RIGHT, fill=tk.Y)
self.terminal = tk.Text(f[-1], state=tk.DISABLED, background="light gray", wrap=tk.NONE, undo=True, xscrollcommand=hor.set, yscrollcommand=vert.set)
self.terminal.pack(side=tk.LEFT, pady=5)
hor.config(command=self.terminal.xview)
vert.config(command=self.terminal.yview)
grip = ttk.Sizegrip(self)
grip.pack(side="right", anchor="se")
acquireCOMList()
BatteryFrame.pack(anchor="nw", padx=5)
for task in self.tasks:
await task
for frame in f:
frame.pack()
"""
filePathQuery
Queries the user for what file path they would like
"""
def filePathQuery(self):
temp_name = askdirectory(title='Please enter a file path')
temp_name = temp_name[2:]
if(temp_name == None):
self.experimentPath = temp_name
else:
self.experimentPath = (temp_name + "/")
"""
mocapChecked
Event that happens when the MOCAP checkbox is checked
"""
def mocapChecked(self):
if(self.mocapBool.get()):
self.mocapBool.set(1)
self.print("Smart MOCAP Enabled\n")
else:
self.print("Smart MOCAP Disabled\n")
self.mocapBool.set(0)
"""
connect_bluetooth
Takes a name and connects to a bluetooth device given that name.
"""
async def connect_bluetooth(self, name : str):
#self.bleDevice = backends.device.BLEDevice(0)
#self.bleClient = None
self.bBluetooth['state'] = tk.DISABLED
self.update()
if(self.bBluetooth['text'] != "Disconnect Bluetooth"):
self.print("Connection processing...\n")
try:
devices = await BleakScanner.discover()
except OSError as e:
if e.strerror == "The device is not ready for use":
self.bBluetooth['state'] = tk.NORMAL
self.print("Bluetooth adapter not accessible; Is the Bluetooth adapter turned off?")
return
for d in devices:
#self.print(str(d) + "\n")
if d.name == name:
try:
self.bleClient = BleakClient(d, disconnected_callback= self.disconnect_callback, timeout=3.0)
await self.bleClient.connect()
self.bBluetooth['text'] = "Disconnect Bluetooth"
self.bTesting['state'] = tk.NORMAL
self.print("Connected to " + name + "\n")
break
except Exception as e:
self.bBluetooth['state'] = tk.NORMAL
self.print("ERR: " + str(e) + "\n")
else:
self.bleClient = None
else:
self.print("Disconnection processing...\n")
if (self.bleClient.is_connected):
await self.bleClient.disconnect()
#self.bBluetooth['text'] = "Connect Bluetooth"
#self.bTesting['state'] = tk.DISABLED
self.print(" " + name +" successfully!", "end-2c")
else:
self.print("Wasn't connected at first!\n")
if(self.bleClient is None):
self.print("Could not find " + name + "\n")
self.bBluetooth['state'] = tk.NORMAL
self.update()
"""
callback
A callback ran after every packet is received
"""
async def callback(self, characteristic, d : bytearray):
#if (self.can_take):
#print(self.lastPacketTime) - pow(10,9)/60
#if((self.lastPacketTime + pow(10,9)/30 >= time.time_ns()) and (self.lastPacketTime + pow(10,9)/120 <= time.time_ns())):
#if((self.lastPacketTime + pow(10,9)/120 <= time.time_ns())):
#print(self.lastPacketTime)
#print("Start Time:" + str(time.time()))
data_array = d.decode().rstrip().split('\t')
currenttime = int(data_array[0])
if (self.lastPacketTime is None or currenttime - self.lastPacketTime >= (1000 - 130)/int(self.samplerate)):
data_array.append(self.vLabel.get())
self.clearln()
self.print("\n")
#self.print(data_array)
self.write(data_array)
self.vData0.set(data_array[1])
self.vData1.set(data_array[2])
self.vData2.set(data_array[3])
self.vData3.set(data_array[4])
self.vData4.set(data_array[5])
self.vData5.set(data_array[6])
self.vData6.set(data_array[7])
self.vData7.set(data_array[8])
self.lastPacketTime = currenttime
#print("Final Time:" + str(time.time()))
"""
capture_bluetooth
Begins capturing data from the bluetooth device connected
"""
async def capture_bluetooth(self):
self.bTesting['state'] = tk.DISABLED
self.sHz['state'] = tk.DISABLED
self.bBluetooth['state'] = tk.DISABLED
self.update()
if(self.bTesting['text'] == "Begin Testing"):
self.print("Beginning Test...\n")
if (self.bleClient.is_connected):
if self.experimentPath == "":
self.print("File path not set, please set and try again\n")
self.bTesting['state'] = tk.NORMAL
self.sHz['state'] = tk.NORMAL
self.bBluetooth['state'] = tk.NORMAL
return
with open(self.experimentPath + self.eFileName.get() + datetime.now().strftime("%m_%d_%Y-%H_%M_%S") + ".csv",
'w+',newline='') as self.outfile:
self.outfileWritable = csv.writer(self.outfile)
try:
self.bTesting['text'] = "Stop Testing"
self.bTesting['state'] = tk.NORMAL
self.update()
if(self.mocapBool.get()):
port_name = self.mocapCOM.get()
port_name = port_name[:port_name.find('-') - 1]
if port_name != '':
mocap = serial.Serial(port=port_name, baudrate=300)
mocap.write(1)
mocap.close()
self.outfileWritable.writerow(["Time","Data 0", "Data 1", "Data 2", "Data 3", "Data 4", "Data 5", "Data 6", "Data 7", "Label"])
self.lastPacketTime = None
await self.bleClient.start_notify(self.dataChar, callback= self.callback)
await asyncio.sleep(0)
while self.endTesting.empty():
self.update()
await asyncio.sleep(0)
await self.bleClient.stop_notify(self.dataChar)
await asyncio.sleep(0)
except Exception as e:
await asyncio.sleep(0)
self.outfile.close()
self.print(str(e) + "\n")
self.print("\n")
self.endTesting.get()
await asyncio.sleep(0)
self.outfile.close()
else:
self.sHz['state'] = tk.NORMAL
self.bTesting['state'] = tk.DISABLED
self.bBluetooth['text'] = "Connect Bluetooth"
self.bBluetooth['state'] = tk.NORMAL
self.print("Bluetooth disconnected unexpectedly. Please try connecting again...\n")
else:
self.print("Ending Test...\n")
self.sHz['state'] = tk.NORMAL
self.bBluetooth['state'] = tk.NORMAL
self.bTesting['text'] = "Begin Testing"
self.bTesting['state'] = tk.NORMAL
"""
disconnect_callback
Runs after device disconnects and puts app into disconnected state
"""
def disconnect_callback(self, client):
self.bBluetooth['text'] = "Connect Bluetooth"
self.bBluetooth['state'] = tk.NORMAL
self.bTesting['text'] = "Begin Testing"
self.bTesting['state'] = tk.DISABLED
self.print("Disconnected from device\n")
return
"""
clearln
Clears the line on the terminal
"""
def clearln(self):
self.terminal['state'] = tk.NORMAL
#self.terminal.replace()
self.terminal.delete("end-1l", tk.END)
self.terminal['state'] = tk.DISABLED
"""
print
Prints whatever is given as printable at the given index
"""
def print(self, printable : str, index = tk.END):
self.terminal['state'] = tk.NORMAL
self.terminal.insert(index, printable)
self.terminal['state'] = tk.DISABLED
self.terminal.see(tk.END)
self.update()
"""
write
Writes whatever was given as writable into a csv file
"""
def write(self, writable : list):
#writable.insert(0, datetime.time.microseconds())
self.outfileWritable.writerow(writable)
"""
bluetoothDaemon
Runs a daemon that looks for a command and runs a certain function given the command
"""
async def bluetoothDaemon(self):
global glb_command
while(self.isActive):
#time.sleep(0.01)
self.update()
if (glb_command == "connect"):
glb_command = str
task = asyncio.create_task(self.connect_bluetooth("FlexibleSensorBLE"))
await task
elif (glb_command == "capture"):
glb_command = str
task = asyncio.create_task(self.capture_bluetooth())
await task
await asyncio.sleep(0)
"""
timeDaemon
Daemon for the built in timer for labels
"""
async def timeDaemon(self):
self.timeStarted = False
while(self.isActive):
if(self.disabledButton != self.bIdle):
if(self.spSeconds.get() == "0" and self.spMinutes.get() == "0" and self.spHours.get() == "0"):
self.spHours['state'] = tk.NORMAL
self.spMinutes['state'] = tk.NORMAL
self.spSeconds['state'] = tk.NORMAL
self.disabledButton['state'] = tk.NORMAL
self.disabledButton = self.bIdle
self.vLabel.set("Idle")
self.disabledButton['state'] = tk.DISABLED
self.timeStarted = False
self.print("Label time has finished\n")
i = 3
while(i != 0):
self.terminal.config(background= "red")
await asyncio.sleep(.5)
self.terminal.config(background="light gray")
await asyncio.sleep(0.25)
i = i - 1
else:
if(not self.timeStarted):
self.print("Label time has Started\n")
self.timeStarted = True
self.spHours['state'] = tk.DISABLED
self.spMinutes['state'] = tk.DISABLED
self.spSeconds['state'] = tk.DISABLED
if(self.spHours.get() == ""):
self.spHours.set(0)
if(self.spMinutes.get() == ""):
self.spMinutes.set(0)
if(self.spSeconds.get() == ""):
self.spSeconds.set(0)
temp = int(self.spHours.get())*3600 + int(self.spMinutes.get()) * 60 + int(self.spSeconds.get())
if(temp != 0):
await asyncio.sleep(1)
temp = temp - 1
hours = int(temp/3600)
minutes = int((temp - hours*3600)/60)
seconds = int(temp - minutes*60 - hours*3600)
self.spHours.set(hours)
self.spMinutes.set(minutes)
self.spSeconds.set(seconds)
else:
self.timeStarted = False
self.spHours['state'] = tk.NORMAL
self.spMinutes['state'] = tk.NORMAL
self.spSeconds['state'] = tk.NORMAL
await asyncio.sleep(0)
"""
destroy
Deletes the object and closes all daemons
"""
def destroy(self):
self.isActive = False
if(self.bleClient is not None and self.bleClient.is_connected):
self.bleClient.disconnect()
tk.Tk.destroy(self)
if __name__ == "__main__":
async def asyncio_main():
app = FlexSense_Gui()
await app.construct_gui()
task1 = asyncio.create_task(app.bluetoothDaemon())
#task2 = asyncio.create_task(app.clockDaemon())
task3 = asyncio.create_task(app.timeDaemon())
def endProg():
app.destroy()
app.protocol('WM_DELETE_WINDOW', endProg)
await task1
#await task2
await task3
asyncio.run(asyncio_main())