-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurationwidget.cpp
More file actions
200 lines (164 loc) · 8.03 KB
/
configurationwidget.cpp
File metadata and controls
200 lines (164 loc) · 8.03 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
#include "configurationwidget.h"
#include "ui_configurationwidget.h"
#include "stateindicatorwidget.h"
#include <QDebug>
#include <QDateTime>
#include "mediadialog.h"
ConfigurationWidget::ConfigurationWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ConfigurationWidget)
, m_settings(nullptr)
, m_state(nullptr)
, m_media(nullptr)
{
ui->setupUi(this);
// Connecting the back button the so we can emit a
// signal to leave the configuration screen.
connect(ui->backButton, &QPushButton::clicked,
this, &ConfigurationWidget::clickedBack);
}
ConfigurationWidget::~ConfigurationWidget() {
delete ui;
}
void ConfigurationWidget::init(ApplicationState *state, MediaDialog *media) {
// Loading all of the current configuratation
assert(!m_settings && "Cannot call init twice");
assert(!m_state && "Cannot call init twice");
assert(!m_media && "Cannot initialize media dialog reference twice");
m_state = state;
m_media = media;
m_settings = m_state->data()->settings();
// Connecting all of the signals and for each sub-menu.
initPlayerOneConfig();
initPlayerTwoConfig();
initLedConfig();
initSpotLightConfig();
initArduinoConfig();
initMediaDialog();
}
void ConfigurationWidget::initPlayerOneConfig() {
connectButtonState(m_state->physicalState()->playerOne()->readyButton(),
ui->p1StatusIndicator->readyState());
connectButtonState(m_state->physicalState()->playerOne()->doorButton(),
ui->p1StatusIndicator->doorState());
connectButtonState(m_state->physicalState()->playerOne()->trapDoorButton(),
ui->p1StatusIndicator->trapDoorState());
connectButtonState(m_state->physicalState()->playerOne()->conceedButton(),
ui->p1StatusIndicator->conceedState());
connectWiringState(m_state->physicalState()->playerOne()->readyButton(), ui->p1ReadySwitchKind);
connectWiringState(m_state->physicalState()->playerOne()->doorButton(), ui->p1DoorSwitchKind);
connectWiringState(m_state->physicalState()->playerOne()->conceedButton(), ui->p1ConceedSwitchKind);
connectWiringState(m_state->physicalState()->playerOne()->trapDoorButton(), ui->p1TrapDoorSwitchKind);
}
void ConfigurationWidget::initPlayerTwoConfig() {
connectButtonState(m_state->physicalState()->playerTwo()->readyButton(),
ui->p2StatusIndicator->readyState());
connectButtonState(m_state->physicalState()->playerTwo()->doorButton(),
ui->p2StatusIndicator->doorState());
connectButtonState(m_state->physicalState()->playerTwo()->trapDoorButton(),
ui->p2StatusIndicator->trapDoorState());
connectButtonState(m_state->physicalState()->playerTwo()->conceedButton(),
ui->p2StatusIndicator->conceedState());
connectWiringState(m_state->physicalState()->playerTwo()->readyButton(), ui->p2ReadySwitchKind);
connectWiringState(m_state->physicalState()->playerTwo()->doorButton(), ui->p2DoorSwitchKind);
connectWiringState(m_state->physicalState()->playerTwo()->conceedButton(), ui->p2ConceedSwitchKind);
connectWiringState(m_state->physicalState()->playerTwo()->trapDoorButton(), ui->p2TrapDoorSwitchKind);
}
void ConfigurationWidget::initLedConfig() {
ui->ledConfig->init(m_state);
}
void ConfigurationWidget::initSpotLightConfig() {
}
constexpr size_t LOG_LINES = 1000;
void ConfigurationWidget::initArduinoConfig() {
ui->arduinoConnectionIndicator->setState(m_state->physicalState()->connectionManager()->isConnected());
connect(m_state->physicalState()->connectionManager(), &ArduinoConnectionManager::connected,
[&](QString port) {
ui->arduinoConnectionIndicator->setState(m_state->physicalState()->connectionManager()->isConnected());
ui->availableComPorts->setCurrentText(port);
});
connect(m_state->physicalState()->connectionManager(), &ArduinoConnectionManager::disconnected,
[&](QString port) {
ui->arduinoConnectionIndicator->setState(m_state->physicalState()->connectionManager()->isConnected());
ui->availableComPorts->setCurrentText("");
});
// Adding initial empty elements.
ui->availableComPorts->addItem("");
ui->availableComPorts->addItems(m_state->physicalState()->connectionManager()->availableSerialPorts());
connect(m_state->physicalState()->connectionManager(), &ArduinoConnectionManager::availableSerialPortsChanged,
[&](const QStringList &ports) {
auto text = ui->availableComPorts->currentText();
ui->availableComPorts->clear();
ui->availableComPorts->addItem("");
ui->availableComPorts->addItems(m_state->physicalState()->connectionManager()->availableSerialPorts());
ui->availableComPorts->setCurrentText(text);
});
connect(m_state->physicalState()->connectionManager(), &ArduinoConnectionManager::error,
[&](QString msg){
// Update state because this could me we are disconnected.
ui->arduinoConnectionIndicator->setState(m_state->physicalState()->connectionManager()->isConnected());
qDebug() << "Received an error message from arduino connection";
});
// Making sure to update connected serial port values from the selection.
connect(ui->availableComPorts, &QComboBox::currentTextChanged,
[&](const QString& text) {
// The remainder of the state will be updated by the rest of
// the slots.
m_state->physicalState()->connectionManager()->connectToSerialPort(text);
qDebug() << "Selecting new arduino connection:" << text;
m_settings->setValue("arduino/com_port", text);
});
connect(m_state->physicalState()->connectionManager(), &ArduinoConnectionManager::receivedData,
[&](QString msg) {
QString cleanMsg = msg.remove('\r').remove('\n');
// Add things to the QTableWidget
QDateTime now = QDateTime::currentDateTime();
ui->arduinoTextLog->appendPlainText("[" + now.toString(Qt::ISODate) +"]" + cleanMsg);
auto newlines = ui->arduinoTextLog->toPlainText().count('\n');
if (newlines > LOG_LINES) {
QTextCursor cursor = ui->arduinoTextLog->textCursor();
cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, newlines - LOG_LINES);
cursor.select(QTextCursor::LineUnderCursor);
cursor.removeSelectedText();
cursor.deleteChar(); // Delete old stuff
cursor.movePosition(QTextCursor::End);
ui->arduinoTextLog->setTextCursor(cursor);
}
});
connect(ui->sendArduinoCmdButton, &QPushButton::pressed,
this, &ConfigurationWidget::sendCommand);
}
void ConfigurationWidget::sendCommand() {
auto msg = ui->arduinoCmdLineEdit->text();
qDebug() << "Sending message: " << msg;
m_state->physicalState()->connectionManager()->sendData(msg);
ui->arduinoCmdLineEdit->setText("");
}
void ConfigurationWidget::initMediaDialog() {
connect(ui->showCannonButton, &QPushButton::pressed,
[&]{
m_media->showWinnerScreen("cannon/main.qml", ui->winningPlayerNameInpuit->text());
});
connect(ui->showPlayerWinsButton, &QPushButton::pressed,
[&]{
m_media->showWinnerScreen("confetti/main.qml", ui->winningPlayerNameInpuit->text());
});
connect(ui->restoreMediaScreen, &QPushButton::pressed,
[&]{
m_media->show();
});
}
void ConfigurationWidget::connectButtonState(PhysicalButton *sw, StateIndicatorWidget *indicator) {
// Set the initial state.
indicator->setState(sw->state());
connect(sw, &PhysicalButton::stateChanged,
indicator, &StateIndicatorWidget::setState);
}
void ConfigurationWidget::connectWiringState(PhysicalButton *sw, ButtonConfigWidget *wiringCfg) {
wiringCfg->setSwitchKind(sw->switchKind());
connect(sw, &PhysicalButton::switchKindChanged,
wiringCfg, &ButtonConfigWidget::setSwitchKind);
connect(wiringCfg, &ButtonConfigWidget::switchKindChanged,
sw, &PhysicalButton::setSwitchKind);
}