Files
gods-eye-view/src/sdr/gain.js
T
Sameh Khamis 73e789489b chore: drop exports nothing imports
Knip, configured with the application, package exports, scripts, QA
harnesses, workers, tools and tests as entry points, reports exports
that no module imports. Each was checked by hand against string-based
and namespace imports.

- Symbols that are still used inside their own module lose only the
  `export` keyword (176 constants and helpers).
- Unused re-exports are removed from barrel modules (flight, military,
  vessel, CCTV, directions, transit history, local ADS-B and street-level
  policy modules).
- Declarations that nothing uses at all are deleted, among them
  AIS_WATCHDOG_STATUSES, applyFilter, isRadioCountryCode, compactText,
  DENSITY_STOPS, the unused CCTV endpoint constants, and the SDR band
  constants.
- Default exports that nothing imports are removed. The ones in
  src/data/earthquakes.js, src/data/telegeographySubmarineCables.js,
  src/data/wind.js and src/data/bhoteKoshiLocator.js each constructed a
  layer instance at import time that nothing used; those instances are
  no longer created.

Every export of a package.json entry stays, as does TOMTOM_CREDIT,
which the traffic layer reads from the credits namespace it is given.
2026-10-22 19:00:40 -07:00

94 lines
3.0 KiB
JavaScript

/**
* Tuner gain settings for the local RTL-SDR, remembered per receiver mode.
*
* `'auto'` hands gain to the tuner's AGC. Manual values are the R820T gain
* steps librtlsdr and dump1090 expose, in dB. Receivers with an onboard LNA
* (for example a Nooelec FlyCatcher with its LNA on) overload at AGC or high
* gain, so the ADS-B default is a fixed step. It is 28.0 dB rather than the
* lower steps dump1090 users pick because the WebUSB library maps dB to tuner
* gain stages differently from librtlsdr: on a FlyCatcher, 20.7 dB here gave
* about 1 msg/s and 28.0 dB about 9 msg/s on the same antenna.
*/
const SDR_GAIN_AUTO = 'auto';
/** R820T tuner gain steps in dB, as listed by librtlsdr. */
export const R820T_GAIN_STEPS_DB = Object.freeze([
0.0, 0.9, 1.4, 2.7, 3.7, 7.7, 8.7, 12.5, 14.4, 15.7, 16.6, 19.7, 20.7, 22.9,
25.4, 28.0, 29.7, 32.8, 33.8, 36.4, 37.2, 38.6, 40.2, 42.1, 43.4, 43.9, 44.5,
48.0, 49.6,
]);
export const SDR_GAIN_DEFAULTS = Object.freeze({
fm: SDR_GAIN_AUTO,
adsb: 28.0,
});
export const SDR_GAIN_STORAGE_KEY = 'gev:sdr:gain:v1';
/**
* Normalize a requested gain to `'auto'` or the nearest listed R820T step.
* @param {*} value `'auto'`, a dB number or a numeric string.
* @returns {'auto'|number|null} Normalized setting, or null when invalid.
*/
export function normalizeSdrGain(value) {
if (value === null || value === undefined || value === '') return null;
if (String(value).trim().toLowerCase() === SDR_GAIN_AUTO)
return SDR_GAIN_AUTO;
const requested = Number(value);
if (!Number.isFinite(requested)) return null;
let nearest = R820T_GAIN_STEPS_DB[0];
for (const step of R820T_GAIN_STEPS_DB) {
if (Math.abs(step - requested) < Math.abs(nearest - requested))
nearest = step;
}
return nearest;
}
/**
* Read per-mode gain settings, falling back to the defaults.
* @param {Storage|null} storage Browser storage or a test double.
* @returns {{fm:'auto'|number, adsb:'auto'|number}}
*/
export function readSdrGainSettings(storage) {
let stored = null;
try {
stored = JSON.parse(storage?.getItem?.(SDR_GAIN_STORAGE_KEY) || 'null');
} catch {
stored = null;
}
return Object.fromEntries(
Object.entries(SDR_GAIN_DEFAULTS).map(([mode, fallback]) => [
mode,
normalizeSdrGain(stored?.[mode]) ?? fallback,
]),
);
}
/**
* Persist per-mode gain settings. Storage failures are ignored.
* @param {Storage|null} storage Browser storage or a test double.
* @param {{fm:'auto'|number, adsb:'auto'|number}} settings Settings to save.
* @returns {boolean} Whether the write succeeded.
*/
export function writeSdrGainSettings(storage, settings) {
try {
storage?.setItem?.(
SDR_GAIN_STORAGE_KEY,
JSON.stringify({ fm: settings.fm, adsb: settings.adsb }),
);
return Boolean(storage?.setItem);
} catch {
return false;
}
}
/**
* The value handed to the tuner: null selects AGC.
* @param {'auto'|number} setting Normalized gain setting.
* @returns {number|null}
*/
export function tunerGainValue(setting) {
return setting === SDR_GAIN_AUTO ? null : setting;
}