-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrequencyViewMain.cpp
272 lines (228 loc) · 8.84 KB
/
FrequencyViewMain.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
#include "FrequencyViewMain.h"
#include <wx/config.h>
#include <wx/ffile.h>
#include <wx/filedlg.h>
#include <wx/log.h>
#include <wx/menu.h>
#include <wx/msgdlg.h>
#include <wx/splitter.h>
#include <wx/textctrl.h>
#include <wx/textdlg.h>
#include "DevConnDialog.h"
#include "FftSessionPanel.h"
#include "ImportOptionsDialog.h"
#include "ImportSessionPanel.h"
#include "ImportSessionThread.h"
#include "SessionEvent.h"
#include "SpectrumAnalyzerPanel.h"
namespace{
static const int ID_ABOUT = wxNewId();
static const int ID_CONNECT = wxNewId();
static const int ID_DRAW_SYMBOLS = wxNewId();
static const int ID_DRAW_LINES = wxNewId();
static const int ID_DRAW_SPLINE = wxNewId();
}
BEGIN_EVENT_TABLE(FrequencyViewFrame, wxFrame)
EVT_MENU (wxID_EXIT, FrequencyViewFrame::onExit)
EVT_MENU (ID_ABOUT, FrequencyViewFrame::onAbout)
EVT_MENU (ID_CONNECT, FrequencyViewFrame::onConnect)
EVT_SESSION_UPDATE (wxID_ANY, FrequencyViewFrame::onSessionEvent)
EVT_SESSION_ENDED (wxID_ANY, FrequencyViewFrame::onSessionEvent)
END_EVENT_TABLE()
FrequencyViewFrame::FrequencyViewFrame(wxFrame *frame, const wxString& title):
wxFrame(frame, -1, title, wxDefaultPosition, wxSize(1024, 768)),
context_(sigrok::Context::create()),
config_(new wxConfig("FrequencyView"))
{
auiManager_.SetManagedWindow(this);
wxMenuBar *mbar = new wxMenuBar();
wxMenu *fileMenu = new wxMenu("");
wxMenu *load = new wxMenu("");
populateFileLoaderMenu(load);
fileMenu->AppendSubMenu(load, "Import from");
fileMenu->Append(wxID_EXIT, _("&Quit\tAlt-F4"), _("Quit the application"));
mbar->Append(fileMenu, _("&File"));
wxMenu *plotMenu = new wxMenu("");
plotMenu->Append(ID_DRAW_SYMBOLS, _T("Draw curve symbols"), _T("Draw symbols for each curve data point"), true);
plotMenu->Append(ID_DRAW_LINES, _T("Draw curve lines"), _T("Draw the curves as interconnecting lines"), true);
plotMenu->Append(ID_DRAW_SPLINE, _T("Draw curve spline"), _T("Draw the curves as splines"), true);
mbar->Append(plotMenu, _T("&Plot"));
wxMenu *speciMenu = new wxMenu("");
speciMenu->Append(ID_CONNECT, _("&Connect"), _("Connect to spectrum Analyzer"));
mbar->Append(speciMenu, _("Analyzer"));
wxMenu* helpMenu = new wxMenu("");
helpMenu->Append(ID_ABOUT, _("&About\tF1"), _("Show info about this application"));
mbar->Append(helpMenu, _("&Help"));
SetMenuBar(mbar);
CreateStatusBar(2);
SetStatusText(_("Welcome to FrequencyView!"),0);
logTxtCtrl_ = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxTE_READONLY);
wxLog* logger = new wxLogTextCtrl(logTxtCtrl_);
wxLog::SetActiveTarget(logger);
wxPanel* ntbkPanel = new wxPanel(this, wxID_ANY);
auiNotebook_ = new wxAuiNotebook(ntbkPanel, wxID_ANY);
wxBoxSizer* ntbkPanelSizer = new wxBoxSizer(wxHORIZONTAL);
ntbkPanelSizer->Add(auiNotebook_, 1, wxEXPAND);
ntbkPanel->SetSizer(ntbkPanelSizer);
auiManager_.AddPane(ntbkPanel, wxAuiPaneInfo().Center().CaptionVisible(false).CloseButton(false));
auiManager_.AddPane(logTxtCtrl_,
wxAuiPaneInfo().Caption("Log").Bottom().Show()
.Floatable()
.CloseButton(false) // TODO: could be enabled/disable from menu later
);
auiManager_.Update();
}
FrequencyViewFrame::~FrequencyViewFrame()
{
auiManager_.UnInit();
delete config_;
}
void FrequencyViewFrame::onExit(wxCommandEvent &event)
{
Destroy();
}
void FrequencyViewFrame::onAbout(wxCommandEvent &event)
{
wxMessageBox("FrequencyView", _("Welcome to..."));
}
void FrequencyViewFrame::initLogicAnalyzer(std::shared_ptr<sigrok::Session> session, std::shared_ptr<sigrok::HardwareDevice> device)
{
FftSessionPanel *pnl = new FftSessionPanel(auiNotebook_, this, wxNewId(), session, device);
auiNotebook_->AddPage(pnl, "fft'd data from Device", true);
}
#ifdef HAS_SPECTRUM_ANALYZER
void FrequencyViewFrame::initSpectrumAnalyzer(std::shared_ptr<sigrok::Session> session, std::shared_ptr<sigrok::HardwareDevice> device)
{
SpectrumAnalyzerPanel *pnl = new SpectrumAnalyzerPanel(auiNotebook_, this, wxNewId(), session, device);
auiNotebook_->AddPage(pnl, "data from Device", true);
}
#endif
#ifdef HAS_NETWORK_ANALYZER
void FrequencyViewFrame::initNetworkAnalyzer(std::shared_ptr<sigrok::Session> session, std::shared_ptr<sigrok::HardwareDevice> device)
{
device_->config_set(sigrok::ConfigKey::SPAN, Glib::Variant<gdouble>::create(span));
device_->config_set(sigrok::ConfigKey::BAND_CENTER_FREQUENCY, Glib::Variant<gdouble>::create(frequency));
}
#endif
void FrequencyViewFrame::onConnect(wxCommandEvent &event)
{
DevConnDialog dlg(this, context_, config_);
if (dlg.ShowModal() != wxID_OK)
return;
std::shared_ptr<sigrok::HardwareDevice> device = dlg.getSelectedDevice();
if(!device)
return;
auto session = context_->create_session();
device->open();
session->add_device(device);
if(session->devices().size() != 1)
{
wxLogMessage("No devices in session");
return;
}
for (auto key : device->driver()->config_keys())
{
if (key == sigrok::ConfigKey::LOGIC_ANALYZER)
{
initLogicAnalyzer(session, device);
}
#ifdef HAS_SPECTRUM_ANALYZER
else if (key == sigrok::ConfigKey::SPECTRUM_ANALYZER)
{
initSpectrumAnalyzer(session, device);
}
#endif
#ifdef HAS_NETWORK_ANALYZER
else if (key == sigrok::ConfigKey::NETWORK_ANALYZER)
{
initNetworkAnalyzer(session, device);
}
#endif
}
}
void FrequencyViewFrame::populateFileLoaderMenu(wxMenu *loadMenu)
{
for (auto& entry : context_->input_formats())
{
int id = wxNewId();
loadMenu->Append(id, wxString(entry.second->description().c_str()) + " ..." );
Connect(id, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(FrequencyViewFrame::onImport));
inputFormatCmdId_[id] = entry.first;
}
}
wxString FrequencyViewFrame::generateWildcardString(const std::string &description, const std::vector<std::string> &exts)
{
wxString wildcard;
if (!exts.empty())
{
wildcard = description.c_str();
wildcard += "|";
bool first = true;
for (auto ext : exts)
{
if (!first)
wildcard += ";";
wildcard += "*.";
wildcard += ext.c_str();
first = false;
}
wildcard += "|";
}
wildcard += "All Files|*.*";
return wildcard;
}
wxString FrequencyViewFrame::getImportfileFromUser(const wxString &wildcard)
{
wxString ret = wxEmptyString;
wxFileDialog dlg(this, _("Import File"), config_->Read("/import/lastDir", ""),
"", wildcard, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if(dlg.ShowModal() == wxID_CANCEL)
return wxEmptyString;
wxString filepath = dlg.GetPath();
if (!filepath.IsEmpty())
config_->Write("/import/lastDir", wxFileName(filepath).GetPath());
return filepath;
}
static bool getOptionsFromUser(const wxString &InputFormatDescription,
std::map<std::string, std::shared_ptr<sigrok::Option>> options,
std::map<std::string, Glib::VariantBase> &selected_opts)
{
if (options.size())
{
wxString title = wxString("Import ") + InputFormatDescription + " options";
ImportOptionsDialog imoptdlg(nullptr, title, options);
if (imoptdlg.ShowModal() == wxID_CANCEL)
return false;
selected_opts = imoptdlg.getOption();
}
return true;
}
void FrequencyViewFrame::onImport(wxCommandEvent &event)
{
int id = event.GetId();
auto input_formats = context_->input_formats();
if (inputFormatCmdId_.find(id) == inputFormatCmdId_.end())
return;
auto input_format = input_formats[inputFormatCmdId_[id]];
wxString wildcard = generateWildcardString(input_format->description(),
input_format->extensions());
wxString filepath = getImportfileFromUser(wildcard);
if (filepath.IsEmpty())
return;
std::map<std::string, Glib::VariantBase> sel_opts = std::map<std::string, Glib::VariantBase>();
if (!getOptionsFromUser(input_format->description().c_str(),
input_format->options(), sel_opts))
return;
std::shared_ptr<sigrok::Input> input = input_format->create_input(sel_opts);
std::shared_ptr<sigrok::Session> session = context_->create_session();
ImportSessionPanel *pnl = new ImportSessionPanel(auiNotebook_, this, wxNewId(), session, input, filepath);
auiNotebook_->AddPage(pnl, "data from import", true);
}
void FrequencyViewFrame::onSessionEvent(SessionEvent &event)
{
wxWindow *wnd = FindWindowById(event.GetId());
if (wnd)
wnd->GetEventHandler()->AddPendingEvent(event);
}