-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.py
190 lines (157 loc) · 7.04 KB
/
Controller.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
import asyncio
from enum import Enum
import time
from PySide6.QtCore import Signal, Slot, QTimer, QTime, QObject
from Model import Model
from View import View
import numpy as np
import vars
'''
TODO:
- Write README.md
- Show Accuracy and Awareness scores over time
'''
class ControlState(Enum):
SCANNING = 1
INITIALISING = 2
SESSION_INTRO = 3
READY_TO_START = 4
RECORDING_BEATS = 5
RECORDING_INPUT = 6
RECORDING_CONFIDENCE = 7
RESULTS = 8
class Controller:
def __init__(self):
self.model = Model()
self.view = View()
self.view.setWindowTitle("Beat Tracker")
self.view.resize(800, 500)
self.view.show()
self.state = ControlState.SCANNING
self.initialising_timer = QTimer()
self.recording_timer = CountdownTimer()
self.model.sensorConnected.connect(self.sensorConnectedHandler)
self.initialising_timer.timeout.connect(self.initialisingTimerFinishedHandler)
self.recording_timer.timerFinished.connect(self.recordingTimerFinishedHandler)
self.view.controls_widget.start_button.clicked.connect(self.buttonPressedHandler)
self.configureSeriesTimer()
self.trials_per_session = len(vars.TRIAL_LENGTHS_S)
self.trial_id = -1
self.trial_lengths_s = np.array(vars.TRIAL_LENGTHS_S, copy=True)
np.random.shuffle(self.trial_lengths_s)
@Slot()
def sensorConnectedHandler(self):
if self.state == ControlState.SCANNING:
self.changeState(ControlState.INITIALISING)
@Slot()
def initialisingTimerFinishedHandler(self):
if self.state == ControlState.INITIALISING:
self.changeState(ControlState.SESSION_INTRO)
@Slot()
def recordingTimerFinishedHandler(self):
if self.state == ControlState.RECORDING_BEATS:
self.changeState(ControlState.RECORDING_INPUT)
@Slot()
def buttonPressedHandler(self):
if self.state == ControlState.READY_TO_START:
self.changeState(ControlState.RECORDING_BEATS)
elif self.state == ControlState.SESSION_INTRO:
self.changeState(ControlState.READY_TO_START)
elif self.state == ControlState.RECORDING_INPUT:
self.changeState(ControlState.RECORDING_CONFIDENCE)
elif self.state == ControlState.RECORDING_CONFIDENCE:
if self.trial_id < self.trials_per_session-1:
self.changeState(ControlState.READY_TO_START)
else:
self.changeState(ControlState.RESULTS)
elif self.state == ControlState.RESULTS:
self.changeState(ControlState.SESSION_INTRO)
def changeState(self, newState):
exitStateHandler = {
ControlState.SCANNING: None,
ControlState.INITIALISING: None,
ControlState.SESSION_INTRO: None,
ControlState.READY_TO_START: None,
ControlState.RECORDING_BEATS: None,
ControlState.RECORDING_INPUT: None,
ControlState.RECORDING_CONFIDENCE: self.exitRecordingConfidenceState,
ControlState.RESULTS: None
}
if exitStateHandler[self.state] is not None:
exitStateHandler[self.state]()
enterStateHandler = {
ControlState.SCANNING: None,
ControlState.INITIALISING: self.enterInitialisingState,
ControlState.SESSION_INTRO: self.enterSessionIntroState,
ControlState.READY_TO_START: self.enterReadyToStartState,
ControlState.RECORDING_BEATS: self.enterRecordingBeatsState,
ControlState.RECORDING_INPUT: self.enterRecordingInputState,
ControlState.RECORDING_CONFIDENCE: self.enterRecordingConfidenceState,
ControlState.RESULTS: self.enterResultsState
}
if enterStateHandler[newState] is not None:
enterStateHandler[newState]()
self.state = newState
def enterInitialisingState(self):
self.initialising_timer.setSingleShot(True)
self.initialising_timer.start(4000)
def enterSessionIntroState(self):
self.view.control_session_intro(self.trial_lengths_s)
self.model.resetSession()
self.trial_id = -1
def enterReadyToStartState(self):
self.trial_id += 1
self.recording_timer.initTimer(self.trial_lengths_s[self.trial_id])
self.view.control_ready_to_start(self.trial_id+1, self.trials_per_session)
self.record_start_time = None
self.record_end_time = None
self.beat_count_estimate = None
def enterRecordingBeatsState(self):
self.view.control_recording_beats()
self.recording_timer.startTimer()
self.record_start_time = time.time_ns()/1.0e9
def enterRecordingInputState(self):
self.record_end_time = time.time_ns()/1.0e9
self.view.control_recording_input()
def enterRecordingConfidenceState(self):
self.beat_count_estimate = self.view.controls_widget.beat_count_input.value()
self.view.control_recording_confidence()
def exitRecordingConfidenceState(self):
self.model.calculateTrialResults(self.trial_lengths_s[self.trial_id], self.record_start_time, self.record_end_time, \
self.beat_count_estimate, self.view.controls_widget.confidence_scale.value())
def enterResultsState(self):
session_results = self.model.calculateSessionResults()
self.view.control_results(session_results["accuracy_score"], session_results["accuracy_percentile"], \
session_results["awareness_score"], session_results["awareness_percentile"], \
session_results["awareness_p_value"])
self.model.viewResults()
# View update functions
def updateViewWithModelData(self):
ecg_times_rel_s = self.model.beat_tracker.ecg_times - time.time_ns()/1.0e9
self.view.update_ecg_series(ecg_times_rel_s, self.model.beat_tracker.ecg_hist)
def configureSeriesTimer(self):
self.update_ecg_series_timer = QTimer()
self.update_ecg_series_timer.timeout.connect(self.updateViewWithModelData)
self.update_ecg_series_timer.setInterval(vars.UPDATE_ECG_SERIES_PERIOD)
self.update_ecg_series_timer.start()
async def main(self):
await self.model.connect_polar()
await asyncio.gather(self.model.update_ecg())
class CountdownTimer(QObject):
timerFinished = Signal()
def __init__(self):
super().__init__()
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateTimer)
def initTimer(self, duration_s):
self.countdown_time = QTime(0, 0, duration_s)
def updateTimer(self):
self.countdown_time = self.countdown_time.addSecs(-1)
print(f"Countdown: {self.countdown_time.toString('mm:ss')}")
# Stop the timer if the countdown has finished
if self.countdown_time == QTime(0, 0, 0):
self.timer.stop()
self.timerFinished.emit()
def startTimer(self):
if not self.timer.isActive():
self.timer.start(1000)