-
Notifications
You must be signed in to change notification settings - Fork 0
/
superpower.js
780 lines (780 loc) · 29.8 KB
/
superpower.js
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
// Global settings
var superpower_settings;
(function (superpower_settings) {
superpower_settings.increase_zoom_levels = true;
superpower_settings.increase_zoom_levels_count = 128;
superpower_settings.spectrum_fluidity = true;
superpower_settings.spectrum_fluidity_refresh_time = 30; // in ms
superpower_settings.spectrum_enlarge = true;
superpower_settings.spectrum_enlarge_height = 200; // in px
superpower_settings.spectrum_enlarge_resizable = true; // experimental
superpower_settings.frequency_change = true;
superpower_settings.gain_change = true;
superpower_settings.waterfall_refreshing = false; // experimental
superpower_settings.profile_memory = false; // experimental
})(superpower_settings || (superpower_settings = {}));
$.fn.onClassChange = function (callback) {
return $(this).each(({}, element) => {
new MutationObserver(mutations => {
mutations.forEach((mutation) => {
callback($(mutation.target), mutation.target.className);
});
}).observe(element, {
attributes: true,
attributeFilter: ['class']
});
});
};
$.fn.onResize = function (callback) {
return $(this).each(({}, element) => {
new ResizeObserver((entries) => {
entries.forEach((entry) => {
callback($(entry.target));
});
}).observe(element);
});
};
$.fn.isEventBound = function (type, fn) {
var _a;
const data = (_a = this.data('events')) === null || _a === void 0 ? void 0 : _a[type];
if (data === undefined || data.length === 0) {
return false;
}
return $.inArray(fn, data) !== -1;
};
// Utilities
function waitFor(predicate, callback, delay = 100) {
const timer = setInterval(() => {
if (predicate()) {
clearInterval(timer);
callback();
}
}, delay);
}
function debounce(threshold, callback) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
callback(...args);
}, threshold);
};
}
function isProfileUnlocked(sdr_profile) {
return sdr_profile.id.toLowerCase().startsWith('unlocked');
}
function getSelectedProfile() {
var _a;
const value = (_a = $('#openwebrx-sdr-profiles-listbox')) === null || _a === void 0 ? void 0 : _a.val();
const desc = value === null || value === void 0 ? void 0 : value.split('|');
if (desc === undefined || desc[0] === undefined || desc[1] === undefined) {
return undefined;
}
return {
device: desc[0],
id: desc[1],
};
}
function readSettings(sdr_profile) {
return __awaiter(this, void 0, void 0, function* () {
if (!isProfileUnlocked(sdr_profile)) {
return undefined;
}
// FIXME: This is a hack for newer versions of OpenWebRX
const fix_exponents = (settings) => {
return settings.map((setting) => {
if (setting.name.endsWith('-exponent')) {
return {
name: setting.name,
value: '0',
};
}
return setting;
});
};
try {
const doc = yield $.ajax({
url: `settings/sdr/${sdr_profile.device}/profile/${sdr_profile.id}`,
type: 'GET',
xhrFields: {
withCredentials: true
},
});
return fix_exponents($(doc).find('form.settings-body').serializeArray());
}
catch (_err) {
return undefined;
}
});
}
function writeSettings(sdr_profile, settings) {
return __awaiter(this, void 0, void 0, function* () {
if (!isProfileUnlocked(sdr_profile)) {
return undefined;
}
const current_settings = yield readSettings(sdr_profile);
if (current_settings === undefined) {
return undefined;
}
const combine_settings = (existing_settings, settings) => {
return existing_settings.map((existing_setting) => {
const setting = getSettingByName(settings, existing_setting.name);
if (setting !== undefined) {
return setting;
}
return existing_setting;
});
};
// Preserve existing settings
const data = combine_settings(current_settings, settings);
// Save data to profile "Unlocked" with POST query
try {
yield $.ajax({
url: `settings/sdr/${sdr_profile.device}/profile/${sdr_profile.id}`,
type: 'POST',
data: $.param(data),
xhrFields: {
withCredentials: true
},
});
return yield readSettings(sdr_profile);
}
catch (_err) {
return undefined;
}
});
}
function getSettingByName(settings, name) {
return settings.find((setting) => setting.name === name);
}
function getSdrFreqRange(sdr_profile) {
const sdr_freq_range = {
'airspy': { min: { exp: 0, value: 1 }, max: { exp: 9, value: 1.8 } },
'rtlsdr': { min: { exp: 0, value: 1 }, max: { exp: 9, value: 2.2 } },
'sdrplay': { min: { exp: 0, value: 1 }, max: { exp: 9, value: 2.0 } },
};
return sdr_freq_range[sdr_profile.device];
}
function rangeMidpoint(range) {
return Math.floor((range.max.value - range.min.value) / 2);
}
function getSdrGainRange(sdr_profile) {
const sdr_gain_range = {
'airspy': { min: { value: 0.0 }, max: { value: 45.0 }, step: 0.1, reversed: false },
'rtlsdr': { min: { value: 0.0 }, max: { value: 49.6 }, step: 0.1, reversed: false },
'sdrplay': { min: { value: 20.0 }, max: { value: 59.0 }, step: 1.0, reversed: true },
};
return sdr_gain_range[sdr_profile.device];
}
function getSdrSecondaryGainRange(sdr_profile) {
const sdr_secondary_gain_range = {
'sdrplay': { min: { value: 0.0 }, max: { value: 3.0 }, step: 1.0, reversed: true },
};
return sdr_secondary_gain_range[sdr_profile.device];
}
function isGainInRange(gain_range, gain) {
return gain.value >= gain_range.min.value && gain.value <= gain_range.max.value;
}
function clampGainToRange(range, gain) {
return { value: Math.min(Math.max(gain.value, range.min.value), range.max.value) };
}
function isFreqInRange(freq_range, freq) {
const freq_value = freqIntoNumber(freq);
return freq_value >= freqIntoNumber(freq_range.min) && freq_value <= freqIntoNumber(freq_range.max);
}
function clampFreqToRange(range, freq) {
return { exp: 0, value: Math.min(Math.max(freqIntoNumber(freq), freqIntoNumber(range.min)), freqIntoNumber(range.max)) };
}
function freqIntoNumber(freq) {
return freq.value * Math.pow(10, freq.exp);
}
function setFrequency(jump_freq) {
return __awaiter(this, void 0, void 0, function* () {
const sdr_profile = getSelectedProfile();
if (sdr_profile === undefined) {
return undefined;
}
const freq_range = getSdrFreqRange(sdr_profile);
if (freq_range === undefined) {
return undefined;
}
const freq_decoded = (() => {
if (jump_freq.type === 'value') {
if (Number.isNaN(jump_freq.exp) || Number.isNaN(jump_freq.value)) {
return undefined;
}
return jump_freq;
}
const visible_freq_range = get_visible_freq_range();
const freq_width = visible_freq_range.end - visible_freq_range.start;
switch (jump_freq.type) {
case 'prev': return { exp: 0, value: visible_freq_range.center - freq_width };
case 'next': return { exp: 0, value: visible_freq_range.center + freq_width };
}
})();
if (freq_decoded === undefined) {
return undefined;
}
const freq_new = clampFreqToRange(freq_range, freq_decoded);
// Preserve current primary/secondary demodulator
const freq_mod = getCurrentDemodulator();
yield writeSettings(sdr_profile, [
{ name: 'center_freq', value: `${freq_new.value}` },
{ name: 'center_freq-exponent', value: `${freq_new.exp}` },
{ name: 'start_mod', value: `${freq_mod}` },
]);
updateDisplayedWaterfall();
updateDisplayedZoomLevel();
});
}
function getCurrentGain() {
const $gain_level = $('#gain-level');
if ($gain_level === undefined) {
return undefined;
}
const is_mode_manual = !$gain_level.prop('disabled');
if (is_mode_manual) {
const value = Number($gain_level.val());
if (value === undefined || Number.isNaN(value)) {
return undefined;
}
return {
type: 'manual',
value: Number(value.toFixed(1)),
};
}
else {
return {
type: 'auto',
};
}
}
function getGainFromSettings(settings) {
var _a, _b;
const value = (_a = getSettingByName(settings, 'rf_gain-select')) === null || _a === void 0 ? void 0 : _a.value;
switch (value) {
case 'manual': {
const value = Number((_b = getSettingByName(settings, 'rf_gain-manual')) === null || _b === void 0 ? void 0 : _b.value);
if (value === undefined || Number.isNaN(value)) {
return undefined;
}
return {
type: 'manual',
value: Number(value.toFixed(1)),
};
}
case 'auto': {
return {
type: 'auto',
};
}
}
return undefined;
}
function getSecondaryGainFromSettings(settings) {
var _a;
const value = Number((_a = getSettingByName(settings, 'rfgain_sel')) === null || _a === void 0 ? void 0 : _a.value);
if (value === undefined || Number.isNaN(value)) {
return undefined;
}
return { value: Number(value.toFixed(1)) };
}
function setGain(gain) {
return __awaiter(this, void 0, void 0, function* () {
const sdr_profile = getSelectedProfile();
if (sdr_profile === undefined) {
return undefined;
}
if (gain.type === 'auto') {
// Save auto gain to profile "Unlocked"
const sdr_settings = yield writeSettings(sdr_profile, [
{ name: 'rf_gain-select', value: 'auto' }
]);
if (sdr_settings !== undefined) {
yield updateDisplayedGain(sdr_profile, sdr_settings);
}
return;
}
const gain_range = getSdrGainRange(sdr_profile);
if (gain_range === undefined || Number.isNaN(gain.value)) {
return undefined;
}
const gain_new = clampGainToRange(gain_range, { value: Number(gain.value.toFixed(1)) });
// Save new gain to profile "Unlocked"
const sdr_settings = yield writeSettings(sdr_profile, [
{ name: 'rf_gain-select', value: 'manual' },
{ name: 'rf_gain-manual', value: `${gain_new.value}` }
]);
if (sdr_settings !== undefined) {
yield updateDisplayedGain(sdr_profile, sdr_settings);
}
});
}
function setSecondaryGain(gain) {
return __awaiter(this, void 0, void 0, function* () {
const sdr_profile = getSelectedProfile();
if (sdr_profile === undefined) {
return undefined;
}
const secondary_gain_range = getSdrSecondaryGainRange(sdr_profile);
if (secondary_gain_range === undefined || Number.isNaN(gain.value)) {
return undefined;
}
const gain_new = clampGainToRange(secondary_gain_range, { value: Number(gain.value.toFixed(1)) });
// Save new gain to profile "Unlocked"
const sdr_settings = yield writeSettings(sdr_profile, [
{ name: 'rfgain_sel', value: `${gain_new.value}` }
]);
if (sdr_settings !== undefined) {
yield updateDisplayedSecondaryGain(sdr_profile, sdr_settings);
}
});
}
function toggleGainMode() {
return __awaiter(this, void 0, void 0, function* () {
const current_gain = getCurrentGain();
if (current_gain === undefined) {
return undefined;
}
const $gain_level = $('#gain-level');
const [disable_attr, gain_value] = (() => {
switch (current_gain.type) {
case 'auto': return [false, { type: 'manual', value: Number($gain_level.val()) }];
case 'manual': return [true, { type: 'auto' }];
}
})();
$gain_level.prop('disabled', disable_attr);
return yield setGain(gain_value);
});
}
// Demodulator related stuff
function getCurrentDemodulator() {
var _a;
const demodulator = (_a = getDemodulators()) === null || _a === void 0 ? void 0 : _a[0];
if (demodulator === undefined) {
return 'nfm';
}
const secondary_demod = demodulator.get_secondary_demod();
if (secondary_demod !== false) {
return secondary_demod;
}
return demodulator.get_modulation();
}
// Displayed elements/values related stuff
function updateDisplayedWaterfall() {
if (!superpower_settings.waterfall_refreshing) {
return;
}
waterfall_measure_minmax_now = true;
}
function updateDisplayedZoomLevel() {
if (!superpower_settings.increase_zoom_levels) {
return;
}
// Smoother zooming
zoom_levels_count = superpower_settings.increase_zoom_levels_count;
mkzoomlevels();
}
function updateDisplayedGain(sdr_profile, sdr_settings) {
return __awaiter(this, void 0, void 0, function* () {
const gain_range = getSdrGainRange(sdr_profile);
if (gain_range === undefined) {
return undefined;
}
const settings_gain = getGainFromSettings(sdr_settings);
if (settings_gain === undefined) {
return undefined;
}
switch (settings_gain.type) {
case 'auto': {
$('#gain-mode')
.html(`Gain: auto`)
.prop('title', 'Gain (auto)');
$('#gain-level')
.prop('title', `Gain (auto)`)
.prop('disabled', true);
break;
}
case 'manual': {
const gain_value = settings_gain.value;
const displayed_gain = gain_range.reversed ? -gain_value : gain_value;
$('#gain-mode')
.html(`Gain: ${displayed_gain}dB`)
.prop('title', `Gain (${displayed_gain}dB)`);
$('#gain-level')
.prop('title', `Gain (${displayed_gain}dB)`)
.prop('value', gain_value)
.prop('disabled', false);
break;
}
}
});
}
function updateDisplayedSecondaryGain(sdr_profile, sdr_settings) {
return __awaiter(this, void 0, void 0, function* () {
const secondary_gain_range = getSdrSecondaryGainRange(sdr_profile);
if (secondary_gain_range === undefined) {
return undefined;
}
const settings_secondary_gain = getSecondaryGainFromSettings(sdr_settings);
if (settings_secondary_gain === undefined) {
return undefined;
}
const secondary_gain_value = settings_secondary_gain.value;
const displayed_secondary_gain = secondary_gain_range.reversed ? -secondary_gain_value : secondary_gain_value;
$('#secondary-gain-level')
.prop('title', `Secondary gain (${displayed_secondary_gain}dB)`)
.prop('value', secondary_gain_value);
});
}
function updateDisplayedGainRange(sdr_profile, sdr_settings) {
return __awaiter(this, void 0, void 0, function* () {
const gain_range = getSdrGainRange(sdr_profile);
if (gain_range === undefined) {
return undefined;
}
const $gain_level = $('#gain-level');
$gain_level.prop({
'min': gain_range.min.value,
'max': gain_range.max.value,
'step': gain_range.step,
});
if (!gain_range.reversed) {
$gain_level.css({ 'transform': '' });
$gain_level.off('wheel');
}
else {
$gain_level.css({ 'transform': 'rotate(180deg)' });
$gain_level.off('wheel');
$gain_level.on('wheel', (event) => {
const gain = clampGainToRange(gain_range, {
value: (() => {
const value = Number($gain_level.val());
const step = Number($gain_level.prop('step'));
if (event.originalEvent.deltaY > 0) {
return value + step;
}
else {
return value - step;
}
})(),
});
$gain_level.prop('value', gain.value);
$gain_level.trigger('change');
event.preventDefault();
event.stopPropagation();
});
}
const settings_gain = getGainFromSettings(sdr_settings);
if (settings_gain === undefined) {
return undefined;
}
const gain_value = (() => {
switch (settings_gain.type) {
case 'auto': return rangeMidpoint(gain_range);
case 'manual': return settings_gain.value;
}
})();
$gain_level.prop('value', gain_value);
});
}
function updateDisplayedSecondaryGainRange(sdr_profile, sdr_settings) {
return __awaiter(this, void 0, void 0, function* () {
const $secondary_gain_level = $('#secondary-gain-level');
const secondary_gain_range = getSdrSecondaryGainRange(sdr_profile);
if (secondary_gain_range === undefined) {
$secondary_gain_level.hide();
$secondary_gain_level.prop('disabled', true);
return undefined;
}
$secondary_gain_level.show();
$secondary_gain_level.prop('disabled', false);
$secondary_gain_level.prop({
'min': secondary_gain_range.min.value,
'max': secondary_gain_range.max.value,
'step': secondary_gain_range.step,
});
if (!secondary_gain_range.reversed) {
$secondary_gain_level.css({ 'transform': '' });
$secondary_gain_level.off('wheel');
}
else {
$secondary_gain_level.css({ 'transform': 'rotate(180deg)' });
$secondary_gain_level.off('wheel');
$secondary_gain_level.on('wheel', (event) => {
const secondary_gain = clampGainToRange(secondary_gain_range, {
value: (() => {
const value = Number($secondary_gain_level.val());
const step = Number($secondary_gain_level.prop('step'));
if (event.originalEvent.deltaY > 0) {
return value + step;
}
else {
return value - step;
}
})(),
});
$secondary_gain_level.prop('value', secondary_gain.value);
$secondary_gain_level.trigger('change');
event.preventDefault();
event.stopPropagation();
});
}
const settings_secondary_gain = getSecondaryGainFromSettings(sdr_settings);
if (settings_secondary_gain === undefined) {
return undefined;
}
const gain_value = settings_secondary_gain.value;
$secondary_gain_level.prop('value', gain_value);
});
}
function updateDisplay(sdr_profile, sdr_settings) {
return __awaiter(this, void 0, void 0, function* () {
yield updateDisplayedGain(sdr_profile, sdr_settings);
yield updateDisplayedSecondaryGain(sdr_profile, sdr_settings);
yield updateDisplayedGainRange(sdr_profile, sdr_settings);
yield updateDisplayedSecondaryGainRange(sdr_profile, sdr_settings);
updateDisplayedWaterfall();
updateDisplayedZoomLevel();
});
}
// New functionality/elements related stuff
function fluidizeSpectrum() {
var _a;
if (!superpower_settings.spectrum_fluidity) {
return;
}
// Replace instance of spectrum to improve fps
const canvas = (_a = $('#openwebrx-spectrum-canvas')) === null || _a === void 0 ? void 0 : _a[0];
if (canvas === undefined) {
return undefined;
}
spectrum = undefined;
spectrum = new Spectrum(canvas, superpower_settings.spectrum_fluidity_refresh_time);
}
function resizeSpectrum() {
if (!superpower_settings.spectrum_enlarge) {
return;
}
// Change height of spectrum and remove background image and add event on hide/show spectrum
const $frequency_container = $('#openwebrx-frequency-container');
const $spectrum_container = $('.openwebrx-spectrum-container');
$frequency_container.css({
'background-image': 'none',
});
let spectrum_height = (() => {
if (superpower_settings.spectrum_enlarge_height !== undefined) {
return `${superpower_settings.spectrum_enlarge_height}px`;
}
else {
return `${$spectrum_container.height()}px`;
}
})();
$spectrum_container.onClassChange(($spectrum_container) => {
const height = (() => {
if ($spectrum_container.hasClass('expanded')) {
return spectrum_height;
}
else {
return '';
}
})();
$spectrum_container.css({
'maxHeight': height,
'height': height,
});
});
if (!superpower_settings.spectrum_enlarge_resizable) {
return;
}
$spectrum_container.css({
'resize': 'vertical'
});
// Crude way to implement resizing. Current #openwebrx-frequency-container mousemove event ruines standard resizing.
let spectrum_resizing = false;
$spectrum_container.on('mousemove', (event) => {
if (!spectrum_resizing) {
return;
}
const bounds = event.target.getBoundingClientRect();
spectrum_height = `${event.clientY - bounds.top}px`;
$spectrum_container.css({
'maxHeight': spectrum_height,
'height': spectrum_height,
});
}).on('mousedown', (event) => {
const bounds = event.target.getBoundingClientRect();
const edge_margin = 20;
if (bounds.right - event.clientX < edge_margin && bounds.bottom - event.clientY < edge_margin) {
spectrum_resizing = true;
}
}).on('mouseup', () => {
spectrum_resizing = false;
});
}
function modifySpectrum() {
fluidizeSpectrum();
resizeSpectrum();
}
function addProfileMemory() {
var _a;
if (!superpower_settings.profile_memory) {
return;
}
const $profiles_listbox = $('#openwebrx-sdr-profiles-listbox');
const last_profile_item_key = 'openwebrx-last-profile';
const last_profile = localStorage.getItem(last_profile_item_key);
if (last_profile !== null) {
if (((_a = $profiles_listbox.find(`option[value='${last_profile}']`)) === null || _a === void 0 ? void 0 : _a.length) === 0) {
localStorage.removeItem(last_profile_item_key);
}
else {
$profiles_listbox.val(last_profile).trigger('change');
}
}
$profiles_listbox.on('change', () => {
const sdr_profile = getSelectedProfile();
if (sdr_profile === undefined) {
localStorage.removeItem(last_profile_item_key);
}
else {
localStorage.setItem(last_profile_item_key, `${sdr_profile.device}|${sdr_profile.id}`);
}
});
}
function addFrequencyChange() {
if (!superpower_settings.frequency_change) {
return;
}
// Place jump arrow DIVs
const $frequencies_div = $('.frequencies-container');
$frequencies_div.prepend('<div id="freq-jump-prev" class="openwebrx-button"><</div>');
$frequencies_div.append('<div id="freq-jump-next" class="openwebrx-button">></div>');
$('#freq-jump-prev').on('click', () => __awaiter(this, void 0, void 0, function* () {
yield setFrequency({ type: 'prev' });
}));
$('#freq-jump-next').on('click', () => __awaiter(this, void 0, void 0, function* () {
yield setFrequency({ type: 'next' });
}));
// Add change event on freq input element
const $panel_receiver = $('#openwebrx-panel-receiver');
$panel_receiver.find('.input-group > input').on('change', (event) => __awaiter(this, void 0, void 0, function* () {
const input_exp = $panel_receiver.find('.input-group > select').val();
const input_freq = $(event.target).val();
const freq = {
type: 'value',
exp: Number(input_exp.length !== 0 ? input_exp : NaN),
value: Number(input_freq.length !== 0 ? input_freq : NaN),
};
yield setFrequency(freq);
}));
onProfileChangeEvent((sdr_profile, sdr_settings) => {
if (!isProfileUnlocked(sdr_profile) || sdr_settings === undefined) {
$('#freq-jump-prev').hide();
$('#freq-jump-next').hide();
}
else {
$('#freq-jump-prev').show();
$('#freq-jump-next').show();
}
});
}
function addGainChange(sdr_profile) {
return __awaiter(this, void 0, void 0, function* () {
if (!superpower_settings.gain_change) {
return;
}
// Place gain title and slider
$('#openwebrx-section-modes').before(`\
<div id="openwebrx-section-gain" class="openwebrx-section-divider" onclick="UI.toggleSection(this);">▾ Gain</div>
<div class="openwebrx-section">
<div class="openwebrx-panel-line" style="display: flex;"> \
<div id="gain-mode" class="openwebrx-button openwebrx-slider-button" title="Gain mode" style="font-size: 9pt">Gain: --.-</div> \
<input id="gain-level" class="openwebrx-panel-slider" title="Gain" type="range" style="flex-grow: 1" disabled="" /> \
<input id="secondary-gain-level" class="openwebrx-panel-slider" title="Secondary gain" type="range" style="width: 24px" disabled="" />
</div>
</div>`);
const $gain_mode = $('#gain-mode');
const $gain_level = $('#gain-level');
const $secondary_gain_level = $('#secondary-gain-level');
// Add click event on gain mode toggle button
$gain_mode.on('click', () => __awaiter(this, void 0, void 0, function* () {
yield toggleGainMode();
}));
// Add input/change event on gain level slider
const sdr_settings = yield readSettings(sdr_profile);
if (sdr_settings !== undefined) {
$gain_level.prop('disabled', false);
}
const gain_set_debounce_time = 80; // in ms
$gain_level.on('input change', debounce(gain_set_debounce_time, (gain) => __awaiter(this, void 0, void 0, function* () {
yield setGain(gain);
})), (event) => {
event.data({ type: 'manual', value: Number($(event.target).val()) });
});
$secondary_gain_level.hide();
$secondary_gain_level.on('input change', debounce(gain_set_debounce_time, (gain) => __awaiter(this, void 0, void 0, function* () {
yield setSecondaryGain(gain);
})), (event) => {
event.data({ value: Number($(event.target).val()) });
});
onProfileChangeEvent((sdr_profile, sdr_settings) => {
const $section_gain = $('#openwebrx-section-gain');
if (!isProfileUnlocked(sdr_profile) || sdr_settings === undefined) {
$section_gain.hide();
$section_gain.next('.openwebrx-section:first').hide();
}
else {
$section_gain.show();
$section_gain.next('.openwebrx-section:first').show();
}
});
});
}
function onProfileChangeEvent(callback) {
$(document).on('event:superpower_sdr_profile_changed', ({}, sdr_profile, sdr_settings) => {
callback(sdr_profile, sdr_settings);
});
}
function triggerProfileChangeEvent() {
return __awaiter(this, void 0, void 0, function* () {
const sdr_profile = getSelectedProfile();
if (sdr_profile !== undefined) {
const sdr_settings = yield readSettings(sdr_profile);
$(document).trigger('event:superpower_sdr_profile_changed', [sdr_profile, sdr_settings]);
}
});
}
$(document).on('DOMContentLoaded', () => {
waitFor(() => 'spectrum' in window && spectrum !== undefined, () => {
modifySpectrum();
});
waitFor(() => getSelectedProfile() !== undefined, () => __awaiter(void 0, void 0, void 0, function* () {
const sdr_profile = getSelectedProfile();
if (sdr_profile === undefined) {
return undefined;
}
$('#openwebrx-sdr-profiles-listbox').on('change', () => __awaiter(void 0, void 0, void 0, function* () {
yield triggerProfileChangeEvent();
}));
addProfileMemory();
addFrequencyChange();
yield addGainChange(sdr_profile);
onProfileChangeEvent((sdr_profile, sdr_settings) => {
if (sdr_settings !== undefined) {
updateDisplay(sdr_profile, sdr_settings);
}
});
yield triggerProfileChangeEvent();
}));
});