-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuresetController.cpp
More file actions
253 lines (205 loc) · 8.22 KB
/
NeuresetController.cpp
File metadata and controls
253 lines (205 loc) · 8.22 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
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
#include "NeuresetController.h"
NeuresetController* NeuresetController::control = 0;
NeuresetController::NeuresetController(): isPaused(false), isStarted(false), pausedTime(0), pauseOffset(0) {
for(int i = 0; i < NUM_EEGSITES; i++){
eegSites[i] = new EEGSite(i+1);
connect(eegSites[i], &EEGSite::contactLost, this, &NeuresetController::contactLost);
for (int j = 0; j < MAX_NUM_SESSIONS; j++){
sessionLogA[i][j] = 0;
sessionLogB[i][j] = 0;
}
}
treatmentTimer = new QTimer(this);
connect(treatmentTimer, &QTimer::timeout, this, &NeuresetController::updateTimer);
timerForPausing = new QTimer(this);
timerForPausing->setSingleShot(true);
connect(timerForPausing, &QTimer::timeout, this, &NeuresetController::handlePauseTimeout);
//timer for treatment
treatmentRoundTimer = new QTimer(this);
treatmentRoundTimer->setInterval(ROUND_TIME); // Set interval to 15 seconds for each round
treatmentRoundTimer->setSingleShot(false); // Make sure the timer repeats
connect(treatmentRoundTimer, &QTimer::timeout, this, &NeuresetController::handleTreatmentRound);
}
NeuresetController* NeuresetController::getInstance(){
if (control == 0) {
control = new NeuresetController();
}
return control;
}
EEGSite* NeuresetController::getEEGSite(int eegId){
return eegSites[eegId];
}
void NeuresetController::disconnectSite(int eegId){
eegSites[eegId-1]->disconnectSite();
}
void NeuresetController::reconnectSites(){
for (int i = 0; i< NUM_EEGSITES; i++) {
if(!eegSites[i]->getIsConnected()){
eegSites[i]->reconnectSite();
}
}
}
void NeuresetController::startTimer() {
elapsedTime.start(); // Starts or restarts the elapsed timer
pausedTime = 0;
pauseOffset = 0;
isPaused = false;
treatmentTimer->start(1000); // Start the timer with a 1-second interval
isStarted = true;
}
void NeuresetController::pauseTimer() {
if (!isPaused && isStarted) {
treatmentTimer->stop(); // Stop the QTimer
treatmentRoundTimer->stop(); //stop the treatment timer.
pausedTime = elapsedTime.elapsed(); // record the elapsed time
isPaused = true;
timerForPausing->start(5000);
}
}
void NeuresetController::resumeTimer() {
if (isPaused && isStarted) {
qint64 currentPauseDuration = elapsedTime.elapsed() - pausedTime;
pauseOffset += currentPauseDuration;
treatmentTimer->start(1000);
isPaused = false;
timerForPausing->stop();
// Calculate how much time had elapsed in the current round at pause
qint64 timeInCurrentRound = (elapsedTime.elapsed() - pauseOffset) % ROUND_TIME; // Modulo gives time in the current round
qint64 remainingTimeForRound = ROUND_TIME - timeInCurrentRound; // Time left to finish the round
isResumed = true;
// Restart the treatment timer for the remaining time of the current round
treatmentRoundTimer->start(remainingTimeForRound);
}
}
void NeuresetController::stopTimer() {
if (isStarted) {
treatmentTimer->stop();
elapsedTime.restart();
isPaused = false;
pausedTime = 0;
pauseOffset = 0;
emit timeUpdated("01:00"); // Reset the display to the initial time
treatmentRoundTimer->stop();
currentRound = 0;
isStarted = false;
//reset the progress bar.
emit updatedProgressBar(0);
}
}
void NeuresetController::startNewSession(char type){
qDebug() << "Starting new session";
currentRound = 1; // Reset current round
for (int i=0; i< NUM_EEGSITES; ++i){
int baseline = eegSites[i]->calculateBaseline(eegSites[i]->getWaveform(type));
eegSites[i]->setBaseline(baseline);
sessionLogB[i][numberOfSessions] = baseline;
}
emit treatmentDelivered(false);
treatmentRoundTimer->start(); // Start the treatment rounds
}
QString NeuresetController::sessionLogToString(int session){
QString log;
for(int i = 0; i < NUM_EEGSITES; i++){
QString baseline;
qInfo() << "EEG site #" << i+1 << ":" << sessionLogB[i][session] << "hz ->" << sessionLogA[i][session] << "hz";
log.append("EEG site #");
log.append(QString::number(i+1));
log.append(": ");
log.append(QString::number(sessionLogB[i][session]));
log.append("hz -> ");
log.append(QString::number(sessionLogA[i][session]));
log.append("hz\n");
}
log.append("\n");
return log;
}
QString NeuresetController::sessionLog(){
history = "";
if(numberOfSessions==0){
return history;
}
for(int i = 0; i < numberOfSessions; i++){
history.append("Session #");
qInfo() << "\nSession #" << (i+1) << "At:" << sessionLogDT[i].toString("dd/MM/yy hh:mm:ss AP");
history.append(QString::number(i+1));
history.append(", At:");
history.append(sessionLogDT[i].toString("dd/MM/yy hh:mm:ss AP\n"));
history.append(sessionLogToString(i));
}
return history;
}
//this function generates and returns a QChart for the given eeg site and band
QChart* NeuresetController::generateChart(int eegSite, char type){
QLineSeries *series = new QLineSeries();
QChart *chart = new QChart();
int *data = eegSites[eegSite-1]->getWaveform(type);
for (int i=0; i<60; ++i){
series->append(i, data[i]);
}
QString chartName = "EEG Waveform #";
chart->setTitle(chartName.append(QString::number(eegSite)));
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).back()->setRange(0, 60);
chart->axes(Qt::Vertical).back()->setRange(0, 30);
chart->axes(Qt::Horizontal).back()->setTitleText("time");
chart->axes(Qt::Vertical).back()->setTitleText("frequency");
return chart;
}
void NeuresetController::contactLost(bool x){
if(x){
qDebug() << "NeuresetController recives contactLost from EEG site";
}else{
qDebug() << "NeuresetController recives NOT contactLost from EEG site";
}
emit lostContact(x);
}
void NeuresetController::updateTimer() {
qint64 currentElapsed = elapsedTime.elapsed() - pauseOffset;
qint64 timeRemainingMs = treatmentDurationMs - currentElapsed;
if (timeRemainingMs < 0) {
timeRemainingMs = 0;
stopTimer();
}
qint64 remainingSeconds = timeRemainingMs / 1000;
QTime remainingTime = QTime(0, 0).addSecs(remainingSeconds);
emit timeUpdated(remainingTime.toString("mm:ss"));
int progress = 100 - static_cast<int>((timeRemainingMs * 100) / treatmentDurationMs);
emit updatedProgressBar(progress); // Emit signal with the calculated progress
}
void NeuresetController::handlePauseTimeout() {
qDebug() << "Pause timeout reached. Ending session.";
stopTimer();
emit reset();
}
void NeuresetController::handleTreatmentRound() {
qInfo() << "Beginning round #" << currentRound << " with " << currentRound * 5 << "Hz offset frequency";
if (currentRound == 1) {
QDateTime currentDateTime = QDateTime::currentDateTime();
sessionLogDT[numberOfSessions] = currentDateTime;
}
// Treatment logic for each EEG site
for (int i=0; i< NUM_EEGSITES; ++i){
sessionLogB[i][currentRound-1] = eegSites[i]->getBaselineFrequency();
eegSites[i]->deliverTreatment(currentRound*5);
sessionLogA[i][currentRound-1] = eegSites[i]->getBaselineFrequency();
}
qInfo() << "Round #" << currentRound << " completed\n*****";
if (isResumed && treatmentRoundTimer->interval() != ROUND_TIME) {
treatmentRoundTimer->start(ROUND_TIME); // Reset to normal interval for next rounds
isResumed = false; // Reset the flag after handling the resumed round
}
if (currentRound == totalRounds) {
treatmentRoundTimer->stop(); // Stop the timer if all rounds are completed
qDebug() << "Treatment session completed";
for (int i=0; i< NUM_EEGSITES; ++i){
sessionLogA[i][numberOfSessions] = eegSites[i]->getBaselineFrequency();
}
// Increment the session count now that treatment is complete.
numberOfSessions++;
emit treatmentDelivered(true);
return;
}
currentRound++; // Prepare for the next round
}