/* Copyright (c) 2023-forever Douglas Malnati. All rights reserved. See the /faq/tos page for details. (If this generated header is stamped on a file which is a 3rd party file or under a different license or copyright, then ignore this copyright statement and use that file's terms.) */ import { WsprCodecMaker } from '/pro/codec/WsprCodec.js'; import { WSPREncoded } from '/js/WSPREncoded.js'; export class CodecExpandedBasicTelemetry extends WsprCodecMaker { static HDR_TYPE = 2; static HDR_TELEMETRY_TYPE = 0; static HDR_RESERVED = 0; static REFERENCE_GRID_WIDTH_DEG = 2; static REFERENCE_GRID_HEIGHT_DEG = 1; constructor() { super(); this.SetCodecDefFragment("ExpandedBasicTelemetry", ` { "name": "Temp", "unit": "F", "valueSegmentList": [[-60, 5, -30], [-30, 3, 30], [30, 8, 70]] }, { "name": "Voltage", "unit": "V", "valueSegmentList": [[1.8, 0.300, 3.0], [3.0, 0.0625, 5.0], [5.0, 0.200, 6.0], [6.0, 0.500, 7.0]] }, { "name": "GpsValid", "unit": "Bool", "lowValue": 0, "highValue": 1, "stepSize": 1 }, { "name": "Latitude", "unit": "Idx", "lowValue": 0, "highValue": 15, "stepSize": 1 }, { "name": "Longitude", "unit": "Idx", "lowValue": 0, "highValue": 35, "stepSize": 1 }, { "name": "Altitude", "unit": "Ft", "valueSegmentList": [[0, 75, 3300], [3300, 300, 33000], [33000, 75, 45000], [45000, 500, 60000], [60000, 1500, 120000]] }, `); } GetHdrTypeValue() { return CodecExpandedBasicTelemetry.HDR_TYPE; } GetHdrTelemetryTypeValue() { return CodecExpandedBasicTelemetry.HDR_TELEMETRY_TYPE; } GetHdrReservedValue() { return CodecExpandedBasicTelemetry.HDR_RESERVED; } GetReferencedGridWidthDeg() { return CodecExpandedBasicTelemetry.REFERENCE_GRID_WIDTH_DEG; } GetReferencedGridHeightDeg() { return CodecExpandedBasicTelemetry.REFERENCE_GRID_HEIGHT_DEG; } GetLatitudeBinCount() { let codec = this.GetCodecInstance(); return codec.GetLatitudeIdxHighValue() - codec.GetLatitudeIdxLowValue() + 1; } GetLongitudeBinCount() { let codec = this.GetCodecInstance(); return codec.GetLongitudeIdxHighValue() - codec.GetLongitudeIdxLowValue() + 1; } GetReferenceGridSouthwestCorner(grid4) { return WSPREncoded.DecodeMaidenheadToDeg(grid4.substring(0, 4), { snap: "southwest" }); } IsCodecExpandedBasicTelemetry(codec) { return codec?.GetHdrTypeEnum?.() == this.GetHdrTypeValue(); } EncodeLocationToFieldValues(lat, lng) { let grid4 = WSPREncoded.GetReferenceGrid4(lat, lng); let [baseLat, baseLng] = this.GetReferenceGridSouthwestCorner(grid4); let latDegDiff = Number(lat) - baseLat; let lngDegDiff = Number(lng) - baseLng; let latIsInBounds = latDegDiff >= 0 && latDegDiff < this.GetReferencedGridHeightDeg(); let lngIsInBounds = lngDegDiff >= 0 && lngDegDiff < this.GetReferencedGridWidthDeg(); if (!latIsInBounds || !lngIsInBounds) { throw new RangeError(`Location ${lat}, ${lng} is outside reference grid ${grid4}.`); } let latitudeIdx = Math.floor(latDegDiff * this.GetLatitudeBinCount() / this.GetReferencedGridHeightDeg()); let longitudeIdx = Math.floor(lngDegDiff * this.GetLongitudeBinCount() / this.GetReferencedGridWidthDeg()); return { grid4, latitudeIdx, longitudeIdx, }; } DecodeFieldValuesToLocation(grid4, latitudeIdx, longitudeIdx) { latitudeIdx = Number(latitudeIdx); longitudeIdx = Number(longitudeIdx); if (isNaN(latitudeIdx) || isNaN(longitudeIdx)) { return null; } let [baseLat, baseLng] = this.GetReferenceGridSouthwestCorner(grid4); let lat = baseLat + (latitudeIdx + 0.5) * this.GetReferencedGridHeightDeg() / this.GetLatitudeBinCount(); let lng = baseLng + (longitudeIdx + 0.5) * this.GetReferencedGridWidthDeg() / this.GetLongitudeBinCount(); return { lat, lng, }; } }