Optimizing code.

This commit is contained in:
Marat Fayzullin 2023-05-10 11:25:50 -04:00
parent cfd2b919d1
commit f69f19e9e0
2 changed files with 36 additions and 43 deletions

View File

@ -2,22 +2,16 @@ function Scanner(bookmarkBar, msec) {
this.bbar = bookmarkBar;
this.bookmarks = null;
this.msec = msec;
this.current = null;
this.current = -1;
this.threshold = -80;
this.data = [];
}
Scanner.prototype.tuneBookmark = function(b) {
// Must have frequency, skip digital voice modes
if (!b || !b.frequency || !b.modulation || b.modulation=='dmr') {
return false;
}
if (!b.frequency || !b.modulation || b.modulation=='dmr') return false;
//console.log("TUNE: " + b.name + " at " + b.frequency + ": " + b.modulation);
// Make this bookmark current
this.current = b;
// Tune to the bookmark frequency
var panel = $('#openwebrx-panel-receiver').demodulatorPanel();
panel.getDemodulator().set_offset_frequency(b.frequency - center_freq);
@ -27,58 +21,45 @@ Scanner.prototype.tuneBookmark = function(b) {
return true;
}
Scanner.prototype.getLevel = function(b) {
// Must have bookmark and frequency
if(!b || !b.frequency || !this.data.length) return -1000;
// Compute relevant FFT slot index
var x = this.data.length * ((b.frequency - center_freq) / bandwidth + 0.5);
return this.data[x | 0];
}
Scanner.prototype.update = function(data) {
// Do not update if no timer
if (!this.timer) return;
// Do not update if no timer or no bookmarks
if (!this.timer || !this.bookmarks || !this.bookmarks.length) return;
var i = this.data.length < data.length? this.data.length : data.length;
// Truncate stored data length, add and fill missing data
if (this.data.length > i) {
this.data.length = i;
} else if(this.data.length < data.length) {
this.data.length = data.length;
for(var j=i; j<data.length; ++j) this.data[j] = data[j];
// Average level over time for each bookmark
for(var j=0 ; j<this.bookmarks.length ; ++j) {
var b = this.bookmarks[j];
if (b.frequency > 0) {
var l = data[(b.pos * data.length) | 0];
b.level += (l - b.level) / 5.0;
}
}
// Average level over time
for(var j=0; j<i; ++j) this.data[j] += (data[j] - this.data[j]) / 10.0;
}
Scanner.prototype.scan = function() {
// Do not scan if no timer or no data
if (!this.bookmarks || !this.bookmarks.length) return;
if (!this.timer || !this.data.length) return;
// Do not scan if no timer or no bookmarks
if (!this.timer || !this.bookmarks || !this.bookmarks.length) return;
// Get current squelch threshold from the slider
var $slider = $('#openwebrx-panel-receiver .openwebrx-squelch-slider');
this.threshold = $slider.val();
// If there is currently selected bookmark...
if (this.current) {
var level = this.getLevel(this.current);
if (level>this.threshold) return; else this.current = null;
if (this.current>=0) {
var level = this.bookmarks[this.current].level;
if (level>this.threshold) return; else this.current = -1;
}
// For every shown bookmark...
for(var j=0 ; j<this.bookmarks.length ; ++j) {
// Get bookmark's current level
var b = this.bookmarks[j];
var level = this.getLevel(b);
//console.log("SCAN: " + b.name + " at " + b.frequency + ": " + level);
//console.log("SCAN: " + b.name + " at " + b.frequency + ": " + b.level);
// If level exceeds threshold, tune to the bookmark
if (level>this.threshold && this.tuneBookmark(b)) return;
if (b.level>this.threshold && this.tuneBookmark(b)) {
this.current = j;
return;
}
}
};
@ -88,10 +69,8 @@ Scanner.prototype.stop = function() {
// Stop redraw timer
clearInterval(this.timer);
this.timer = 0;
// Remove current bookmarks and data
// Remove current bookmarks
this.bookmarks = null;
this.current = null;
this.data.length = 0;
}
// Done
@ -103,9 +82,17 @@ Scanner.prototype.start = function() {
if (!this.timer) {
// Get all bookmarks from the bookmark bar
this.bookmarks = this.bbar.getAllBookmarks();
this.current = -1;
// If there are bookmarks to scan...
if (this.bookmarks && this.bookmarks.length>0) {
// Precompute FFT offsets, initialize levels
for(var j=0 ; j<this.bookmarks.length ; ++j) {
var f = this.bookmarks[j].frequency;
this.bookmarks[j].level = -1000;
this.bookmarks[j].pos = f>0? (f - center_freq) / bandwidth + 0.5 : 0;
}
// Start redraw timer
var me = this;
this.timer = setInterval(function() { me.scan(); }, this.msec);

View File

@ -1051,6 +1051,7 @@ function on_ws_recv(evt) {
currentprofile['profile_id'] = config['profile_id'] || currentprofile['profile_id'];
$('#openwebrx-sdr-profiles-listbox').val(currentprofile.toString());
stopScanner();
tuning_step_reset();
waterfall_clear();
zoom_set(0);
@ -1710,6 +1711,11 @@ function toggleSpectrum() {
spectrum.toggle();
}
function stopScanner() {
$('.openwebrx-scan-button').css('animation-name', '');
scanner.stop();
}
function toggleScanner() {
var $scanButton = $('.openwebrx-scan-button');
var on = scanner.toggle();