-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
383 lines (272 loc) · 11.6 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLCDNumber>
#include <QString>
#include <QTimer>
#include <QRandomGenerator>
#include "Transitions.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
microwave = new QStateMachine();
// Idle
s1 = new QState();
microwave->addState(s1);
QObject::connect(s1, &QState::entered, [=]{this->ui->mwDisplay->setText("");});
// stop state
stopState = new QState();
microwave->addState(stopState);
stopState->addTransition(ui->stopButton, SIGNAL(clicked()), s1);
// initializing the other states
cookingState = new QState(stopState);
hoursState = new QState(stopState);
minutesState = new QState(stopState);
powerState = new QState(stopState);
durationState = new QState(stopState);
modeState = new QState(stopState);
defrostState = new QState(stopState);
// Idle to cooking
addTrans(s1, cookingState, ui->startButton, SIGNAL(clicked()), this, SLOT(setDefaultTimeNPower()));
// clock
s1->addTransition(ui->clockButton, SIGNAL(clicked()), hoursState);
hoursState->addTransition(ui->clockButton, SIGNAL(clicked()), minutesState);
// minutesState->addTransition(ui->clockButton, SIGNAL(clicked()), s1);
addTrans(minutesState, s1, ui->clockButton, SIGNAL(clicked()), this, SLOT(setOffsetTime()));
QObject::connect(hoursState, SIGNAL(entered()), this, SLOT(saveTime()));
QObject::connect(hoursState, &QState::entered, [=]{this->ui->mwDisplay->setText("Set the hours");});
QObject::connect(minutesState, &QState::entered, [=]{this->ui->mwDisplay->setText("Set the minutes");});
// power
s1->addTransition(ui->powerButton, SIGNAL(clicked()), powerState);
// powerState->addTransition(ui->powerButton, SIGNAL(clicked()), durationState);
// durationState->addTransition(ui->powerButton, SIGNAL(clicked()), s1);
addTrans(powerState, durationState, ui->powerButton, SIGNAL(clicked()), this, SLOT(setPower()));
addTrans(durationState, s1, ui->powerButton, SIGNAL(clicked()), this, SLOT(setCookingDuration()));
QObject::connect(powerState, SIGNAL(entered()), this, SLOT(resetLabels()));
QObject::connect(powerState, &QState::entered, [=]{this->ui->mwDisplay->setText("Set the power");});
// mode
s1->addTransition(ui->modeButton, SIGNAL(clicked()), modeState);
// modeState->addTransition(ui->modeButton, SIGNAL(clicked()), durationState);
// durationState->addTransition(ui->modeButton, SIGNAL(clicked()), s1);
addTrans(modeState, durationState, ui->modeButton, SIGNAL(clicked()), this, SLOT(setCurrentMode()));
addTrans(durationState, s1, ui->modeButton, SIGNAL(clicked()), this, SLOT(setCookingDuration()));
QObject::connect(modeState, SIGNAL(entered()), this, SLOT(resetLabels()));
QObject::connect(modeState, &QState::entered, [=]{this->ui->mwDisplay->setText("Select the mode");});
// duration power + mode
// durationState->addTransition(ui->startButton, SIGNAL(clicked()), cookingState);
addTrans(durationState, cookingState, ui->startButton, SIGNAL(clicked()), this, SLOT(setCookingDuration()));
QObject::connect(durationState, SIGNAL(entered()), this, SLOT(resetLabels()));
QObject::connect(cookingState, SIGNAL(entered()), this, SLOT(startCooking()));
QObject::connect(durationState, &QState::entered, [=]{this->ui->mwDisplay->setText("Set the cooking duration");});
QObject::connect(cookingState, &QState::entered, [=]{
QString stringCooking = "Cooking with a power of " + QString::number(power) + "...";
this->ui->mwDisplay->setText(stringCooking);
});
// defrost
s1->addTransition(ui->defrostButton, SIGNAL(clicked()), defrostState);
defrostState->addTransition(ui->defrostButton, SIGNAL(clicked()), s1);
defrostState->addTransition(ui->startButton, SIGNAL(clicked()), cookingState);
QObject::connect(defrostState, SIGNAL(entered()), this, SLOT(resetLabels()));
QObject::connect(defrostState, &QState::entered, [=]{this->ui->mwDisplay->setText("Defrost mode");});
// start the state machine
microwave->setInitialState(s1);
microwave->start();
// Timers
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start();
// Dial
QObject::connect(ui->dial, SIGNAL(valueChanged(int)), this, SLOT(slide(int)));
// Stop Button
QObject::connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stopButtonClicked()));
// Start Button
QObject::connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startButtonClicked()));
}
MainWindow::~MainWindow()
{
delete ui;
delete microwave;
}
void MainWindow::changeDisplay(){
qDebug() << "changeDisplay: On passe au state s1" << microwave->configuration().contains(s1);
qDebug() << "changeDisplay: On passe au state stop" << microwave->configuration().contains(stopState);
qDebug() << "changeDisplay: On passe au state hours" << microwave->configuration().contains(hoursState);
qDebug() << "changeDisplay: On passe au state hours" << hoursState->active();
}
void MainWindow::showTime(){
// checks that we are not in clock mode
if( !(microwave->configuration().contains(hoursState) || microwave->configuration().contains(minutesState)) ) {
QTime t = QTime::currentTime();
currentTime= t.addSecs(offsetTime);
}
// updates the field only if in idle state
if(microwave->configuration().contains(s1)){
QString doubleD = ":";
if((currentTime.second() % 2) == 0)
doubleD = "";
ui->doubleDot->setText(doubleD);
ui->hoursLabel->setText(currentTime.toString("hh"));
ui->minutesLabel->setText(currentTime.toString("mm"));
}
}
void MainWindow::slide(int value){
if(microwave->configuration().contains(hoursState)){
ui->dial->setRange(0, 23);
if(value >= 0 && value <= 23){
QTime temp = QTime(value, ui->minutesLabel->text().toInt());
ui->hoursLabel->setText(temp.toString("hh"));
// offsetTime = currentTime.secsTo(temp);
// newOffsetTime = currentTime.secsTo(temp);
}
}
if(microwave->configuration().contains(minutesState)){
ui->dial->setRange(0, 59);
if(value >= 0 && value <= 59){
QTime temp = QTime(ui->hoursLabel->text().toInt(), value);
ui->minutesLabel->setText(temp.toString("mm"));
// offsetTime = currentTime.secsTo(temp);
newOffsetTime = currentTime.secsTo(temp);
}
}
if(microwave->configuration().contains(powerState)){
ui->dial->setRange(0, 100);
if (value >= 0 && value <= 100){
ui->minutesLabel->setText(QString::number(value));
// power=value;
newPower=value;
}
}
if(microwave->configuration().contains(durationState)){
ui->dial->setRange(0, (30*60)); // for the total number of seconds up to 59 minutes and 59 seconds
if(value >= 0 && value <= (30*60)){
QTime t0 = QTime(0, 0, 0);
int stepDuration = (value / 30) * 30;
QTime t1 = t0.addSecs(stepDuration);
ui->hoursLabel->setText(t1.toString("mm"));
ui->minutesLabel->setText(t1.toString("ss"));
// cookingDuration=stepDuration;
newCookingDuration=stepDuration;
}
}
if(microwave->configuration().contains(modeState)){
ui->dial->setRange(0, 2);
if(value >= 0 && value <= 2){
ui->minutesLabel->setText(modes[value]);
// currentMode=value;
newCurrentMode=value;
}
}
}
void MainWindow::saveTime(){
currentTime = QTime::currentTime();
ui->doubleDot->setText(":");
}
void MainWindow::resetLabels(){
ui->hoursLabel->setText("");
if(microwave->configuration().contains(powerState)){
ui->doubleDot->setText("");
ui->minutesLabel->setText(QString::number(power));
}
if(microwave->configuration().contains(durationState)){
ui->doubleDot->setText(":");
QTime t0 = QTime(0, 0, 0);
QTime t1 = t0.addSecs(cookingDuration);
ui->hoursLabel->setText(t1.toString("mm"));
ui->minutesLabel->setText(t1.toString("ss"));
}
if(microwave->configuration().contains(modeState)){
ui->doubleDot->setText("");
ui->minutesLabel->setText(modes[currentMode]);
}
if(microwave->configuration().contains(defrostState)){
ui->doubleDot->setText("");
ui->minutesLabel->setText("Calculating...");
int stepDuration = ( QRandomGenerator::global()->bounded(30*60) / 30) * 30;
QTime t0 = QTime(0, 0, 0);
QTime t1 = t0.addSecs(stepDuration);
// Timer to simulate calculation
defrostTimer = new QTimer(this);
defrostTimer->setSingleShot(true);
QObject::connect(defrostTimer, &QTimer::timeout, [=]{
this->ui->hoursLabel->setText(t1.toString("mm"));
this->ui->doubleDot->setText(":");
this->ui->minutesLabel->setText(t1.toString("ss"));
cookingDuration=stepDuration;
});
// QObject::connect(defrostTimer, &QTimer::timeout, [=]{this->ui->doubleDot->setText(":");});
// QObject::connect(defrostTimer, &QTimer::timeout, [=]{this->ui->minutesLabel->setText(t1.toString("ss"));});
defrostTimer->start(2000);
// cookingDuration=stepDuration;
}
}
void MainWindow::startCooking(){
seconds = new QTimer(this);
cookingTimer = new QTimer(this);
cookingTimer->setSingleShot(true);
QTime t0 = QTime(0, 0, 0);
QTime t1 = t0.addSecs(cookingDuration);
QString doubleD = ":";
if((t1.second() % 2) == 0)
doubleD = "";
ui->doubleDot->setText(doubleD);
ui->hoursLabel->setText(t1.toString("mm"));
ui->minutesLabel->setText(t1.toString("ss"));
seconds->start(1000);
cookingTimer->start(1000*cookingDuration);
QObject::connect(seconds, &QTimer::timeout, [=]{
cookingDuration = cookingDuration - 1;
QTime t0 = QTime(0, 0, 0);
QTime t1 = t0.addSecs(cookingDuration);
QString doubleD = ":";
if((t1.second() % 2) == 0)
doubleD = "";
ui->doubleDot->setText(doubleD);
ui->hoursLabel->setText(t1.toString("mm"));
ui->minutesLabel->setText(t1.toString("ss"));
});
QObject::connect(cookingTimer, &QTimer::timeout, [=]{
seconds->stop();
cookingDuration = 60;
});
cookingState->addTransition(cookingTimer, SIGNAL(timeout()), s1);
}
void MainWindow::setOffsetTime(){
offsetTime = newOffsetTime;
}
void MainWindow::setPower(){
power = newPower;
}
void MainWindow::setCookingDuration(){
cookingDuration = newCookingDuration;
}
void MainWindow::setCurrentMode(){
currentMode = newCurrentMode;
}
void MainWindow::setDefaultTimeNPower(){
cookingDuration = 60;
power = 100;
}
void MainWindow::stopButtonClicked(){
if(microwave->configuration().contains(cookingState)){
cookingTimer->stop();
seconds->stop();
cookingDuration = 60;
}
if(microwave->configuration().contains(defrostState)){
defrostTimer->stop();
}
}
void MainWindow::startButtonClicked(){
if(microwave->configuration().contains(cookingState)){
cookingDuration = cookingDuration + 60;
QTime t0 = QTime(0, 0, 0);
QTime t1 = t0.addSecs(cookingDuration);
QString doubleD = ":";
if((t1.second() % 2) == 0)
doubleD = "";
ui->doubleDot->setText(doubleD);
ui->hoursLabel->setText(t1.toString("mm"));
ui->minutesLabel->setText(t1.toString("ss"));
}
}