-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
455 lines (387 loc) · 14.1 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
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
/** @file
@brief Header
@date 2014
@author
Razer, Inc.
<http://www.razerzone.com>
*/
// Copyright 2015 Razer, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <string>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <fstream>
#include <QFile>
#include <QFileDialog>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QList>
#include <QMessageBox>
#include <QProcess>
#include <QDir>
#include <QtSerialPort>
#include "json/json.h"
// Helper function for validating numerical input
class myValidator : public QDoubleValidator
{
public:
myValidator(double bottom, double top, int decimals, QObject * parent) :
QDoubleValidator(bottom, top, decimals, parent)
{
}
QValidator::State validate(QString &s, int &i) const
{
if (s.isEmpty() || s == "-") {
return QValidator::Intermediate;
}
QLocale locale;
QChar decimalPoint = locale.decimalPoint();
int charsAfterPoint = s.length() - s.indexOf(decimalPoint) -1;
if (charsAfterPoint > decimals() && s.indexOf(decimalPoint) != -1) {
return QValidator::Invalid;
}
bool ok;
double d = locale.toDouble(s, &ok);
if (ok && d >= bottom() && d <= top()) {
return QValidator::Acceptable;
} else {
return QValidator::Invalid;
}
}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->standingHeight->setValidator( new myValidator(0, 300, 2, this) );
ui->seatedHeight->setValidator( new myValidator(0, 300, 2, this) );
ui->ipd->setValidator(new myValidator(0,100,2,0));
ui->dOsSpherical->setValidator( new myValidator(-100, 100, 2, this) );
ui->dOsCylindrical->setValidator( new myValidator(-100, 100, 2, this) );
ui->dOsAxis->setValidator( new myValidator(-100, 300, 2, this) );
ui->nOsAdd->setValidator( new myValidator(-100, 100, 2, this) );
ui->dOdSpherical->setValidator( new myValidator(-100, 100, 2, this) );
ui->dOdCylindrical->setValidator( new myValidator(-100, 100, 2, this) );
ui->dOdAxis->setValidator( new myValidator(-100, 300, 2, this) );
ui->nOdAdd->setValidator( new myValidator(-100, 100, 2, this) );
ui->tabWidget->setCurrentIndex(0);
m_osvrUserConfigFilename = QString("C:/ProgramData/OSVR/osvr_user_settings.json");
loadConfigFile(m_osvrUserConfigFilename);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::loadConfigFile(QString filename)
{
char* cstr;
std::string fname = filename.toStdString();
cstr = new char [fname.size()+1];
strcpy( cstr, fname.c_str() );
std::ifstream file_id;
file_id.open(cstr);
Json::Reader reader;
Json::Value value;
if (!reader.parse (file_id, value)){
qWarning("Couldn't open save file, creating file.");
// new file just has default values
saveConfigFile(filename);
}else{
m_osvrUser.read(value);
}
updateFormValues();
return true;
}
void MainWindow::updateFormValues(){
if ("Male" == m_osvrUser.gender())
ui->gender->setCurrentIndex(0);
else
ui->gender->setCurrentIndex(1);
ui->standingHeight->setText(QString::number(m_osvrUser.standingEyeHeight()));
ui->seatedHeight->setText(QString::number(m_osvrUser.seatedEyeHeight()));
ui->ipd->setText(QString::number(m_osvrUser.pupilDistance(OS)+m_osvrUser.pupilDistance(OD)));
ui->dOsSpherical->setText(QString::number(m_osvrUser.spherical(OS)));
ui->dOsCylindrical->setText(QString::number(m_osvrUser.cylindrical(OS)));
ui->dOsAxis->setText(QString::number(m_osvrUser.axis(OS)));
ui->nOsAdd->setText(QString::number(m_osvrUser.addNear(OS)));
ui->OSdominant->setChecked(m_osvrUser.dominant(OS));
ui->dOdSpherical->setText(QString::number(m_osvrUser.spherical(OD)));
ui->dOdCylindrical->setText(QString::number(m_osvrUser.cylindrical(OD)));
ui->dOdAxis->setText(QString::number(m_osvrUser.axis(OD)));
ui->nOdAdd->setText(QString::number(m_osvrUser.addNear(OD)));
ui->ODdominant->setChecked(m_osvrUser.dominant(OD));
}
void MainWindow::on_resetButton_clicked()
{
updateFormValues();
}
void MainWindow::on_aboutButton_clicked()
{
QMessageBox::information(0, QString("OSVR Configuration Utility"), QString("Simple utility for helping you set up and configure your personal OSVR settings... For more information, visit www.osvr.com."), QMessageBox::Ok);
// loadConfigFile(QString("start.json"));
}
void MainWindow::loadValuesFromForm(OSVRUser *oo)
{
double ipd;
oo->setGender(ui->gender->currentText().toStdString());
// IPD settings
if (!ui->ipd->text().isEmpty()){
ipd = ui->ipd->text().toDouble();
oo->setPupilDistance(OS,ipd/2);
oo->setPupilDistance(OD,ipd/2);
}
//Left eye settings
if (!ui->dOsSpherical->text().isEmpty())
oo->setSpherical(OS,ui->dOsSpherical->text().toDouble());
if (!ui->dOsCylindrical->text().isEmpty())
oo->setCylindrical(OS,ui->dOsCylindrical->text().toDouble());
if (!ui->dOsAxis->text().isEmpty())
oo->setAxis(OS,ui->dOsAxis->text().toDouble());
if (!ui->nOsAdd->text().isEmpty())
oo->setAddNear(OS,ui->nOsAdd->text().toDouble());
// right eye settings
if (!ui->dOdSpherical->text().isEmpty())
oo->setSpherical(OD,ui->dOdSpherical->text().toDouble());
if (!ui->dOdCylindrical->text().isEmpty())
oo->setCylindrical(OD,ui->dOdCylindrical->text().toDouble());
if (!ui->dOdAxis->text().isEmpty())
oo->setAxis(OD,ui->dOdAxis->text().toDouble());
if (!ui->nOdAdd->text().isEmpty())
oo->setAddNear(OD,ui->nOdAdd->text().toDouble());
if(ui->ODdominant->isChecked())
oo->setDominant(OD);
else
oo->setDominant(OS);
// anthropometric settings
if (!ui->standingHeight->text().isEmpty())
oo->setStandingEyeHeight(ui->standingHeight->text().toDouble());
if (!ui->seatedHeight->text().isEmpty())
oo->setSeatedEyeHeight(ui->seatedHeight->text().toDouble());
}
void MainWindow::saveConfigFile(QString filename)
{
char* cstr;
std::string fname = filename.toStdString();
cstr = new char [fname.size()+1];
strcpy( cstr, fname.c_str() );
Json::Value ooo;
m_osvrUser.write(ooo);
std::ofstream out_file;
out_file.open(cstr);
Json::StyledWriter styledWriter;
out_file <<styledWriter.write(ooo);
out_file.close();
}
void MainWindow::on_saveButton_clicked()
{
loadValuesFromForm(&m_osvrUser);
saveConfigFile(m_osvrUserConfigFilename);
}
void MainWindow::on_exitButton_clicked()
{
QApplication::quit();
}
void MainWindow::on_resetYawButton_clicked()
{
QProcess *process = new QProcess(this);
QString file = "reset_yaw.bat";
process->start(file);
}
void MainWindow::on_disableButton_clicked()
{
QProcess *process = new QProcess(this);
QString file = "disableOSVRDirect.bat";
process->start(file);
}
void MainWindow::on_enableButton_clicked()
{
QProcess *process = new QProcess(this);
QString file = "enableOSVRDirect.bat";
process->start(file);
}
// FW Update button
void MainWindow::on_updateFWButton_clicked()
{
QMessageBox msgBox;
QMessageBox::StandardButton reply;
QString portName;
QString hexFile;
QString fileFilter = "*.hex";
// find the OSVR HDK and get current FW version
QString fwVersion = sendCommandWaitForResults("#?v\n");
if (fwVersion != ""){
reply = QMessageBox::question(this,tr("FW version"),
"Current FW Version: " + fwVersion + "\nDo you wish to proceed?",
QMessageBox::Yes|QMessageBox::No);
if (reply==QMessageBox::No)
return;
}else
return;
// ask User for the HEX file to update with
hexFile = QFileDialog::getOpenFileName(this,QString("Open FW Update File"),fileFilter,0);
if (hexFile == ""){
return; // user pressed cancel
}
msgBox.setText("FW hex file: "+ hexFile);
msgBox.exec();
sendCommandNoResult("#?b1948\n");
msgBox.setText("This application uses the opensource dfu-programmer which can be found here: dfu-programmer.github.io \n\nAt this time your device should now be in ATMEL bootloader mode. If you haven't loaded the ATMEL drivers yet, you should do so now. You can find the drivers in this distribution in the dfu-prog-usb-1.2.2 folder. \n\nRight click on the device in the device manager and Update the Driver Softare...");
msgBox.exec();
atmel_erase();
atmel_load(hexFile);
atmel_launch();
// Verify FW version
fwVersion = sendCommandWaitForResults("#?v\n");
if (fwVersion == ""){
fwVersion = "Error: Cannot read FW version.";
}
msgBox.setText("FW Version: " + fwVersion);
msgBox.exec();
}
void MainWindow::on_checkFWButton_clicked()
{
QMessageBox msgBox;
// get FW version
QString fwVersion = sendCommandWaitForResults("#?v\n");
if (fwVersion == ""){
fwVersion = "Error: Cannot read FW version.";
}
msgBox.setText("FW Version: " + fwVersion);
msgBox.exec();
return;
}
void MainWindow::atmel_erase()
{
QProcess *process = new QProcess(this);
QString file = "dfu-programmer.exe atxmega256a3bu erase";
process->start(file);
QThread::sleep(3);
}
void MainWindow::atmel_load(QString fwFile)
{
QProcess *process = new QProcess(this);
QString file = "dfu-programmer.exe atxmega256a3bu flash \"" + fwFile + "\"";
process->start(file);
QThread::sleep(3);
}
void MainWindow::atmel_launch()
{
QProcess *process = new QProcess(this);
QString file = "dfu-programmer.exe atxmega256a3bu launch";
process->start(file);
QThread::sleep(5);
}
QString MainWindow::findSerialPort(int VID, int PID)
{
QString outputString = "";
const QString blankString = QObject::tr("N/A");
QString description;
QString manufacturer;
QString serialNumber;
QString portName;
bool deviceFound = false;
portName = "Not found";
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
if (info.vendorIdentifier()==VID && info.productIdentifier()==PID){
description = info.description();
manufacturer = info.manufacturer();
serialNumber = info.serialNumber();
outputString += "\n";
outputString += QObject::tr("Port: ") + info.portName() + "\n"
+ QObject::tr("Location: ") + info.systemLocation() + "\n"
+ QObject::tr("Description: ") + info.description() + "\n"
+ QObject::tr("Manufacturer: ") + info.manufacturer() + "\n"
+ QObject::tr("Serial number: ") + info.serialNumber() + "\n"
+ QObject::tr("Vendor Identifier: ") + (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + "\n"
+ QObject::tr("Product Identifier: ") + (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()) + "\n"
+ QObject::tr("Busy: ") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n";
deviceFound = true;
portName = info.portName();
}
}
QMessageBox msgBox;
if (deviceFound){
msgBox.setText("COM port: "+ outputString);
}else{
msgBox.setText("Could not find device");
}
if (m_verbose)
msgBox.exec();
return portName;
}
QSerialPort *MainWindow::openSerialPort(QString portName)
{
QSerialPort *thePort = new QSerialPort(this);
thePort->setPortName(portName);
thePort->setBaudRate(QSerialPort::Baud57600);
thePort->setDataBits(QSerialPort::Data8);
thePort->setParity(QSerialPort::NoParity);
thePort->setStopBits(QSerialPort::OneStop);
thePort->setFlowControl(QSerialPort::NoFlowControl);
if (thePort->open(QIODevice::ReadWrite)) {
return thePort;
} else {
if (m_verbose)
QMessageBox::critical(this,tr("Open port fail"),"Cannot open serial port " + portName + ".\n Please check your connections and try again.\n");
return NULL;
}
}
void MainWindow::writeSerialData(QSerialPort *thePort, const QByteArray &data)
{
if (thePort->write(data)== -1){
if (m_verbose)
QMessageBox::critical(this,tr("Write serial port failure"),"Cannot write to serial port.\n Please check your connections and try again.\n");
}
thePort->flush();
thePort->waitForBytesWritten(5000);
QThread::sleep(1);
}
QString MainWindow::sendCommandWaitForResults(QByteArray theCommand){
QByteArray theResult="";
QSerialPort *thePort;
QString portName;
// find the OSVR HDK and get current FW version
portName = findSerialPort(0x1532, 0x0B00);
if (portName != "Not found"){
thePort = openSerialPort(portName);
if (thePort){
writeSerialData(thePort,theCommand);
if (thePort->waitForReadyRead(5000)){
theResult = thePort->readAll();
}
thePort->close();
}
}else{
if (m_verbose)
QMessageBox::critical(this,tr("Alert"),"Could not retrieve results from " + theCommand + ". Check your connections and try again.");
}
return theResult;
}
void MainWindow::sendCommandNoResult(QByteArray theCommand){
QSerialPort *thePort;
QString portName;
// find the OSVR HDK and get current FW version
portName = findSerialPort(0x1532, 0x0B00);
if (portName != "Not found"){
thePort = openSerialPort(portName);
if (thePort){
writeSerialData(thePort,theCommand);
}
thePort->close();
}
}