Skip to content

Commit b835d07

Browse files
finish custom offset definitions and fix bug in source selection
1 parent 6916125 commit b835d07

File tree

2 files changed

+109
-69
lines changed

2 files changed

+109
-69
lines changed

core/src/core.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,15 @@ int sdrpp_main(int argc, char* argv[]) {
232232

233233
defConfig["modules"] = json::array();
234234

235-
defConfig["offsets"] = json::array();
236-
defConfig["offsets"][0]["name"] = "SpyVerter";
237-
defConfig["offsets"][0]["offset"] = 120000000;
238-
defConfig["offsets"][1]["name"] = "Ham-It-Up";
239-
defConfig["offsets"][1]["offset"] = 125000000;
240-
defConfig["offsets"][2]["name"] = "MMDS S-band (1998MHz)";
241-
defConfig["offsets"][2]["offset"] = -1998000000;
242-
defConfig["offsets"][3]["name"] = "DK5AV X-Band";
243-
defConfig["offsets"][3]["offset"] = -6800000000;
244-
defConfig["offsets"][4]["name"] = "Ku LNB (9750MHz)";
245-
defConfig["offsets"][4]["offset"] = -9750000000;
246-
defConfig["offsets"][5]["name"] = "Ku LNB (10700MHz)";
247-
defConfig["offsets"][5]["offset"] = -10700000000;
248-
249-
defConfig["offsetMode"] = (int)0; // Off
250-
defConfig["offset"] = 0.0;
235+
defConfig["offsets"]["SpyVerter"] = 120000000.0;
236+
defConfig["offsets"]["Ham-It-Up"] = 125000000.0;
237+
defConfig["offsets"]["MMDS S-band (1998MHz)"] = -1998000000.0;
238+
defConfig["offsets"]["DK5AV X-Band"] = -6800000000.0;
239+
defConfig["offsets"]["Ku LNB (9750MHz)"] = -9750000000.0;
240+
defConfig["offsets"]["Ku LNB (10700MHz)"] = -10700000000.0;
241+
242+
defConfig["selectedOffset"] = "None";
243+
defConfig["manualOffset"] = 0.0;
251244
defConfig["showMenu"] = true;
252245
defConfig["showWaterfall"] = true;
253246
defConfig["source"] = "";
@@ -332,12 +325,18 @@ int sdrpp_main(int argc, char* argv[]) {
332325

333326
// Remove unused elements
334327
auto items = core::configManager.conf.items();
328+
auto newConf = core::configManager.conf;
329+
bool configCorrected = false;
335330
for (auto const& item : items) {
336331
if (!defConfig.contains(item.key())) {
337332
flog::info("Unused key in config {0}, repairing", item.key());
338-
core::configManager.conf.erase(item.key());
333+
newConf.erase(item.key());
334+
configCorrected = true;
339335
}
340336
}
337+
if (configCorrected) {
338+
core::configManager.conf = newConf;
339+
}
341340

342341
// Update to new module representation in config if needed
343342
for (auto [_name, inst] : core::configManager.conf["moduleInstances"].items()) {

core/src/gui/menus/source.cpp

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,77 @@
99
#include <gui/dialogs/dialog_box.h>
1010

1111
namespace sourcemenu {
12-
int offsetMode = 0;
1312
int sourceId = 0;
14-
double customOffset = 0.0;
15-
double effectiveOffset = 0.0;
16-
int decimId = 0;
17-
bool iqCorrection = false;
18-
bool invertIQ = false;
19-
2013
EventHandler<std::string> sourcesChangedHandler;
2114
EventHandler<std::string> sourceUnregisterHandler;
22-
2315
OptionList<std::string, std::string> sources;
2416
std::string selectedSource;
25-
OptionList<std::string, int> offsets;
26-
std::vector<double> customOffsets;
17+
18+
int decimId = 0;
2719
OptionList<int, int> decimations;
2820

21+
bool iqCorrection = false;
22+
bool invertIQ = false;
23+
24+
int offsetId = 0;
25+
double manualOffset = 0.0;
26+
std::string selectedOffset;
27+
double effectiveOffset = 0.0;
28+
OptionList<std::string, double> offsets;
29+
std::map<std::string, double> namedOffsets;
30+
2931
bool showAddOffsetDialog = false;
3032
char newOffsetName[1024];
3133
double newOffset = 0.0;
3234

3335
bool showDelOffsetDialog = false;
3436
std::string delOffsetName = "";
3537

38+
// Offset IDs
3639
enum {
37-
OFFSET_MODE_NONE,
38-
OFFSET_MODE_MANUAL,
39-
OFFSET_MODE_CUSTOM_BASE
40+
OFFSET_ID_NONE,
41+
OFFSET_ID_MANUAL,
42+
OFFSET_ID_CUSTOM_BASE
4043
};
4144

4245
void updateOffset() {
4346
// Compute the effective offset
44-
switch (offsetMode) {
45-
case OFFSET_MODE_NONE:
47+
switch (offsetId) {
48+
case OFFSET_ID_NONE:
4649
effectiveOffset = 0;
4750
break;
48-
case OFFSET_MODE_MANUAL:
49-
effectiveOffset = customOffset;
51+
case OFFSET_ID_MANUAL:
52+
effectiveOffset = manualOffset;
5053
break;
5154
default:
52-
effectiveOffset = customOffsets[offsetMode - OFFSET_MODE_CUSTOM_BASE];
55+
effectiveOffset = namedOffsets[offsets.name(offsetId)];
5356
break;
5457
}
5558

5659
// Apply it
5760
sigpath::sourceManager.setTuningOffset(effectiveOffset);
5861
}
5962

63+
void selectOffsetById(int id) {
64+
// Update the offset mode
65+
offsetId = id;
66+
selectedOffset = offsets.name(id);
67+
68+
// Update the offset
69+
updateOffset();
70+
}
71+
72+
void selectOffsetByName(const std::string& name) {
73+
// If the name doesn't exist, select 'None'
74+
if (!offsets.nameExists(name)) {
75+
selectOffsetById(OFFSET_ID_NONE);
76+
return;
77+
}
78+
79+
// Select using the ID associated with the name
80+
selectOffsetById(offsets.nameId(name));
81+
}
82+
6083
void refreshSources() {
6184
// Get sources
6285
auto sourceNames = sigpath::sourceManager.getSourceNames();
@@ -83,7 +106,7 @@ namespace sourcemenu {
83106
}
84107

85108
// Update the GUI variables
86-
sourceId = sources.valueExists(name);
109+
sourceId = sources.valueId(name);
87110
selectedSource = name;
88111

89112
// Select the source module
@@ -104,23 +127,22 @@ namespace sourcemenu {
104127
// TODO: Stop everything
105128
}
106129

107-
void loadOffsets() {
130+
void reloadOffsets() {
108131
// Clear list
109132
offsets.clear();
110-
customOffsets.clear();
133+
namedOffsets.clear();
111134

112135
// Define special offset modes
113-
offsets.define("None", 0);
114-
offsets.define("Manual", 1);
136+
offsets.define("None", OFFSET_ID_NONE);
137+
offsets.define("Manual", OFFSET_ID_MANUAL);
115138

116139
// Acquire the config file
117140
core::configManager.acquire();
118141

119142
// Load custom offsets
120-
std::vector<json> offsetList = core::configManager.conf["offsets"];
121-
for (auto& o : offsetList) {
122-
customOffsets.push_back(o["offset"]);
123-
offsets.define(o["name"], offsets.size());
143+
namedOffsets = (std::map<std::string, double>)core::configManager.conf["offsets"];
144+
for (auto& [name, offset] : namedOffsets) {
145+
offsets.define(name, offsets.size());
124146
}
125147

126148
// Release the config file
@@ -129,7 +151,7 @@ namespace sourcemenu {
129151

130152
void init() {
131153
// Load offset modes
132-
loadOffsets();
154+
reloadOffsets();
133155

134156
// Define decimation values
135157
decimations.define(1, "None", 1);
@@ -144,9 +166,9 @@ namespace sourcemenu {
144166
core::configManager.acquire();
145167

146168
// Load other settings
147-
std::string selected = core::configManager.conf["source"];
148-
customOffset = core::configManager.conf["offset"];
149-
offsetMode = core::configManager.conf["offsetMode"];
169+
std::string selectedSource = core::configManager.conf["source"];
170+
manualOffset = core::configManager.conf["manualOffset"];
171+
std::string selectedOffset = core::configManager.conf["selectedOffset"];
150172
iqCorrection = core::configManager.conf["iqCorrection"];
151173
invertIQ = core::configManager.conf["invertIQ"];
152174
int decimation = core::configManager.conf["decimation"];
@@ -159,13 +181,13 @@ namespace sourcemenu {
159181

160182
// Select the source module
161183
refreshSources();
162-
selectSource(selected);
184+
selectSource(selectedSource);
163185

164186
// Update frontend settings
165187
sigpath::iqFrontEnd.setDCBlocking(iqCorrection);
166188
sigpath::iqFrontEnd.setInvertIQ(invertIQ);
167-
updateOffset();
168189
sigpath::iqFrontEnd.setDecimation(decimations.value(decimId));
190+
selectOffsetByName(selectedOffset);
169191

170192
// Register handlers
171193
sourcesChangedHandler.handler = onSourcesChanged;
@@ -180,23 +202,33 @@ namespace sourcemenu {
180202
core::configManager.acquire();
181203

182204
// Define a new offset
183-
auto newOffsetObj = json::object();
184-
newOffsetObj["name"] = newOffsetName;
185-
newOffsetObj["offset"] = newOffset;
186-
core::configManager.conf["offsets"].push_back(newOffsetObj);
205+
core::configManager.conf["offsets"][name] = offset;
187206

188207
// Acquire the config file
189208
core::configManager.release(true);
190209

191210
// Reload the offsets
192-
loadOffsets();
211+
reloadOffsets();
193212

194-
// Re-select the same one
195-
// TODO: Switch from an array to a map, because two can't have the same name anyway and it'll just make things easier...+
213+
// Attempt to re-select the same one
214+
selectOffsetByName(selectedOffset);
196215
}
197216

198217
void delOffset(const std::string& name) {
199-
218+
// Acquire the config file
219+
core::configManager.acquire();
220+
221+
// Define a new offset
222+
core::configManager.conf["offsets"].erase(name);
223+
224+
// Acquire the config file
225+
core::configManager.release(true);
226+
227+
// Reload the offsets
228+
reloadOffsets();
229+
230+
// Attempt to re-select the same one
231+
selectOffsetByName(selectedOffset);
200232
}
201233

202234
bool addOffsetDialog() {
@@ -217,7 +249,16 @@ namespace sourcemenu {
217249
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
218250
ImGui::InputDouble("##sdrpp_add_offset_offset", &newOffset);
219251

220-
bool denyApply = !newOffsetName[0] || offsets.nameExists(newOffsetName);
252+
bool nameExists = offsets.nameExists(newOffsetName);
253+
bool reservedName = !strcmp(newOffsetName, "None") || !strcmp(newOffsetName, "Manual");
254+
bool denyApply = !newOffsetName[0] || nameExists || reservedName;
255+
256+
if (nameExists) {
257+
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "An offset with the given name already exists.");
258+
}
259+
else if (reservedName) {
260+
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "The given name is reserved.");
261+
}
221262

222263
if (denyApply) { style::beginDisabled(); }
223264
if (ImGui::Button("Apply")) {
@@ -271,20 +312,20 @@ namespace sourcemenu {
271312

272313
ImGui::LeftLabel("Offset mode");
273314
ImGui::SetNextItemWidth(itemWidth - ImGui::GetCursorPosX() - 2.0f*(lineHeight + 1.5f*spacing));
274-
if (ImGui::Combo("##_sdrpp_offset_mode", &offsetMode, offsets.txt)) {
275-
updateOffset();
315+
if (ImGui::Combo("##_sdrpp_offset", &offsetId, offsets.txt)) {
316+
selectOffsetById(offsetId);
276317
core::configManager.acquire();
277-
core::configManager.conf["offsetMode"] = offsetMode;
318+
core::configManager.conf["selectedOffset"] = offsets.key(offsetId);
278319
core::configManager.release(true);
279320
}
280321
ImGui::SameLine();
281322
ImGui::SetCursorPosX(ImGui::GetCursorPosX() - spacing);
282-
if (offsetMode < OFFSET_MODE_CUSTOM_BASE) { ImGui::BeginDisabled(); }
323+
if (offsetId < OFFSET_ID_CUSTOM_BASE) { ImGui::BeginDisabled(); }
283324
if (ImGui::Button("-##_sdrpp_offset_del_", ImVec2(lineHeight + 0.5f*spacing, 0))) {
284-
delOffsetName = "TEST";
325+
delOffsetName = selectedOffset;
285326
showDelOffsetDialog = true;
286327
}
287-
if (offsetMode < OFFSET_MODE_CUSTOM_BASE) { ImGui::EndDisabled(); }
328+
if (offsetId < OFFSET_ID_CUSTOM_BASE) { ImGui::EndDisabled(); }
288329
ImGui::SameLine();
289330
ImGui::SetCursorPosX(ImGui::GetCursorPosX() - spacing);
290331
if (ImGui::Button("+##_sdrpp_offset_add_", ImVec2(lineHeight + 0.5f*spacing, 0))) {
@@ -294,7 +335,7 @@ namespace sourcemenu {
294335

295336
// Offset delete confirmation
296337
if (ImGui::GenericDialog("sdrpp_del_offset_confirm", showDelOffsetDialog, GENERIC_DIALOG_BUTTONS_YES_NO, []() {
297-
ImGui::Text("Deleting offset named \"%s\". Are you sure?", delOffsetName);
338+
ImGui::Text("Deleting offset named \"%s\". Are you sure?", delOffsetName.c_str());
298339
}) == GENERIC_DIALOG_BUTTON_YES) {
299340
delOffset(delOffsetName);
300341
}
@@ -304,11 +345,11 @@ namespace sourcemenu {
304345

305346
ImGui::LeftLabel("Offset");
306347
ImGui::FillWidth();
307-
if (offsetMode == OFFSET_MODE_MANUAL) {
308-
if (ImGui::InputDouble("##freq_offset", &customOffset, 1.0, 100.0)) {
348+
if (offsetId == OFFSET_ID_MANUAL) {
349+
if (ImGui::InputDouble("##freq_offset", &manualOffset, 1.0, 100.0)) {
309350
updateOffset();
310351
core::configManager.acquire();
311-
core::configManager.conf["offset"] = customOffset;
352+
core::configManager.conf["offset"] = manualOffset;
312353
core::configManager.release(true);
313354
}
314355
}

0 commit comments

Comments
 (0)