main.cpp
6.71 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
// SPDX-License-Identifier: GPL-2.0+
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QLibraryInfo>
#include <QLocale>
#include <QSurfaceFormat>
#include <QTranslator>
#include <iostream>
#include <libusb-1.0/libusb.h>
#include <memory>
#include "post/graphgenerator.h"
#include "post/mathchannelgenerator.h"
#include "post/postprocessing.h"
#include "post/spectrumgenerator.h"
#include "dsomodel.h"
#include "hantekdsocontrol.h"
#include "mainwindow.h"
#include "selectdevice/selectsupporteddevice.h"
#include "settings.h"
#include "usb/usbdevice.h"
#include "viewconstants.h"
#include "glscope.h"
#ifndef VERSION
#error "You need to run the cmake buildsystem!"
#endif
using namespace Hantek;
/// \brief Initialize the device with the current settings.
void applySettingsToDevice(HantekDsoControl *dsoControl, DsoSettingsScope *scope,
const Dso::ControlSpecification *spec) {
bool mathUsed = scope->anyUsed(spec->channels);
for (ChannelID channel = 0; channel < spec->channels; ++channel) {
dsoControl->setCoupling(channel, scope->coupling(channel, spec));
dsoControl->setGain(channel, scope->gain(channel) * DIVS_VOLTAGE);
dsoControl->setOffset(channel, (scope->voltage[channel].offset / DIVS_VOLTAGE) + 0.5);
dsoControl->setTriggerLevel(channel, scope->voltage[channel].trigger);
dsoControl->setChannelUsed(channel, mathUsed | scope->anyUsed(channel));
}
if (scope->horizontal.samplerateSource == DsoSettingsScopeHorizontal::Samplerrate)
dsoControl->setSamplerate(scope->horizontal.samplerate);
else
dsoControl->setRecordTime(scope->horizontal.timebase * DIVS_TIME);
if (dsoControl->getAvailableRecordLengths().empty())
dsoControl->setRecordLength(scope->horizontal.recordLength);
else {
auto recLenVec = dsoControl->getAvailableRecordLengths();
ptrdiff_t index = std::distance(recLenVec.begin(),
std::find(recLenVec.begin(), recLenVec.end(), scope->horizontal.recordLength));
dsoControl->setRecordLength(index < 0 ? 1 : (unsigned)index);
}
dsoControl->setTriggerMode(scope->trigger.mode);
dsoControl->setPretriggerPosition(scope->trigger.position * scope->horizontal.timebase * DIVS_TIME);
dsoControl->setTriggerSlope(scope->trigger.slope);
dsoControl->setTriggerSource(scope->trigger.special, scope->trigger.source);
}
/// \brief Initialize resources and translations and show the main window.
int main(int argc, char *argv[]) {
//////// Set application information ////////
QCoreApplication::setOrganizationName("OpenHantek");
QCoreApplication::setOrganizationDomain("www.openhantek.org");
QCoreApplication::setApplicationName("OpenHantek");
QCoreApplication::setApplicationVersion(VERSION);
bool useGles = false;
{
QCoreApplication parserApp(argc, argv);
QCommandLineParser p;
p.addHelpOption();
p.addVersionOption();
QCommandLineOption useGlesOption("useGLES", QCoreApplication::tr("Use OpenGL ES instead of OpenGL"));
p.addOption(useGlesOption);
p.process(parserApp);
useGles = p.isSet(useGlesOption);
}
GlScope::fixOpenGLversion(useGles ? QSurfaceFormat::OpenGLES : QSurfaceFormat::OpenGL);
QApplication openHantekApplication(argc, argv);
//////// Load translations ////////
QTranslator qtTranslator;
if (qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
openHantekApplication.installTranslator(&qtTranslator);
QTranslator openHantekTranslator;
if (openHantekTranslator.load(QLocale(), QLatin1String("openhantek"), QLatin1String("_"),
QLatin1String(":/translations"))) {
openHantekApplication.installTranslator(&openHantekTranslator);
}
//////// Find matching usb devices ////////
libusb_context *context = nullptr;
int error = libusb_init(&context);
if (error) {
SelectSupportedDevice().showLibUSBFailedDialogModel(error);
return -1;
}
std::unique_ptr<USBDevice> device = SelectSupportedDevice().showSelectDeviceModal(context);
QString errorMessage;
if (device == nullptr || !device->connectDevice(errorMessage)) {
libusb_exit(context);
return -1;
}
//////// Create DSO control object and move it to a separate thread ////////
QThread dsoControlThread;
dsoControlThread.setObjectName("dsoControlThread");
HantekDsoControl dsoControl(device.get());
dsoControl.moveToThread(&dsoControlThread);
QObject::connect(&dsoControlThread, &QThread::started, &dsoControl, &HantekDsoControl::run);
QObject::connect(&dsoControl, &HantekDsoControl::communicationError, QCoreApplication::instance(),
&QCoreApplication::quit);
QObject::connect(device.get(), &USBDevice::deviceDisconnected, QCoreApplication::instance(),
&QCoreApplication::quit);
//////// Create settings object ////////
auto settings = std::unique_ptr<DsoSettings>(new DsoSettings(device->getModel()->spec()));
//////// Create post processing objects ////////
QThread postProcessingThread;
postProcessingThread.setObjectName("postProcessingThread");
PostProcessing postProcessing(settings->scope.countChannels());
SpectrumGenerator spectrumGenerator(&settings->scope);
MathChannelGenerator mathchannelGenerator(&settings->scope, device->getModel()->spec()->channels);
GraphGenerator graphGenerator(&settings->scope, device->getModel()->spec()->isSoftwareTriggerDevice);
postProcessing.registerProcessor(&mathchannelGenerator);
postProcessing.registerProcessor(&spectrumGenerator);
postProcessing.registerProcessor(&graphGenerator);
postProcessing.moveToThread(&postProcessingThread);
QObject::connect(&dsoControl, &HantekDsoControl::samplesAvailable, &postProcessing, &PostProcessing::input);
//////// Create main window ////////
MainWindow openHantekMainWindow(&dsoControl, settings.get());
QObject::connect(&postProcessing, &PostProcessing::processingFinished, &openHantekMainWindow,
&MainWindow::showNewData);
openHantekMainWindow.show();
applySettingsToDevice(&dsoControl, &settings->scope, device->getModel()->spec());
//////// Start DSO thread and go into GUI main loop
dsoControl.startSampling();
postProcessingThread.start();
dsoControlThread.start();
int res = openHantekApplication.exec();
//////// Clean up ////////
dsoControlThread.quit();
dsoControlThread.wait(10000);
postProcessingThread.quit();
postProcessingThread.wait(10000);
if (context && device != nullptr) { libusb_exit(context); }
return res;
}