From c389d3b61931694625cbfc8b1be1bb82e37581dd Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 27 Feb 2021 22:15:19 +0100 Subject: [PATCH] implement a frequency input with switchable exponent --- htdocs/lib/settings/FrequencyInput.js | 21 ++++++++++++ htdocs/settings.js | 1 + owrx/controllers/assets.py | 1 + owrx/form/__init__.py | 46 +++++++++++++++++++++++++++ owrx/source/__init__.py | 4 +-- 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 htdocs/lib/settings/FrequencyInput.js diff --git a/htdocs/lib/settings/FrequencyInput.js b/htdocs/lib/settings/FrequencyInput.js new file mode 100644 index 0000000..482f658 --- /dev/null +++ b/htdocs/lib/settings/FrequencyInput.js @@ -0,0 +1,21 @@ +$.fn.frequencyInput = function() { + this.each(function(){ + var $group = $(this); + var currentExponent = 0; + $input = $group.find('input'); + + var setExponent = function() { + var newExponent = parseInt($exponent.val()); + var delta = currentExponent - newExponent; + $input.val(parseFloat($input.val()) * 10 ** delta); + currentExponent = newExponent; + }; + + $exponent = $group.find('select.frequency-exponent'); + $exponent.on('change', setExponent); + + // calculate initial exponent + $exponent.val(Math.floor(Math.log10($input.val()) / 3) * 3); + setExponent(); + }) +}; \ No newline at end of file diff --git a/htdocs/settings.js b/htdocs/settings.js index 3ac5e53..f3a2d44 100644 --- a/htdocs/settings.js +++ b/htdocs/settings.js @@ -7,4 +7,5 @@ $(function(){ $('#rf_gain').gainInput(); $('.optional-section').optionalSection(); $('#scheduler').schedulerInput(); + $('.frequency-input').frequencyInput(); }); \ No newline at end of file diff --git a/owrx/controllers/assets.py b/owrx/controllers/assets.py index 1613eec..d42ae23 100644 --- a/owrx/controllers/assets.py +++ b/owrx/controllers/assets.py @@ -153,6 +153,7 @@ class CompiledAssetsController(GzipMixin, ModificationAwareController): "lib/settings/GainInput.js", "lib/settings/OptionalSection.js", "lib/settings/SchedulerInput.js", + "lib/settings/FrequencyInput.js", "settings.js", ], } diff --git a/owrx/form/__init__.py b/owrx/form/__init__.py index 08d6408..f603738 100644 --- a/owrx/form/__init__.py +++ b/owrx/form/__init__.py @@ -319,3 +319,49 @@ class ModesInput(DropdownInput): def __init__(self, id, label): options = [Option(m.modulation, m.name) for m in Modes.getAvailableModes()] super().__init__(id, label, options) + + +class FrequencyInput(Input): + def __init__(self, id, label): + super().__init__(id, label) + + def defaultConverter(self): + return IntConverter() + + def input_properties(self, value): + props = super().input_properties(value) + props["type"] = "number" + props["step"] = "any" + return props + + def render_input(self, value): + append = """ +
+ +
+ """.format( + id=self.id, + ) + + return """ +
+ {input} + {append} +
+ """.format( + input=super().render_input(value), + append=append, + ) + + def parse(self, data): + exponent_id = "{}-exponent".format(self.id) + if self.id in data and exponent_id in data: + value = int(float(data[self.id][0]) * 10 ** int(data[exponent_id][0])) + return {self.id: value} + return {} diff --git a/owrx/source/__init__.py b/owrx/source/__init__.py index ba93773..317fe18 100644 --- a/owrx/source/__init__.py +++ b/owrx/source/__init__.py @@ -11,7 +11,7 @@ from owrx.command import CommandMapper from owrx.socket import getAvailablePort from owrx.property import PropertyStack, PropertyLayer, PropertyFilter from owrx.property.filter import ByLambda -from owrx.form import Input, TextInput, NumberInput, CheckboxInput, ModesInput +from owrx.form import Input, TextInput, NumberInput, CheckboxInput, ModesInput, FrequencyInput from owrx.form.converter import OptionalConverter from owrx.form.device import GainInput, SchedulerInput, WaterfallLevelsInput from owrx.controllers.settings import Section @@ -503,7 +503,7 @@ class SdrDeviceDescription(object): ), WaterfallLevelsInput("waterfall_levels", "Waterfall levels"), SchedulerInput("scheduler", "Scheduler"), - NumberInput("center_freq", "Center frequency", append="Hz"), + FrequencyInput("center_freq", "Center frequency"), NumberInput("samp_rate", "Sample rate", append="S/s"), NumberInput("start_freq", "Initial frequency", append="Hz"), ModesInput("start_mod", "Initial modulation"),