Andrew Boyson / gps

Dependencies:   net lpc1768 crypto clock web log

Committer:
andrewboyson
Date:
Sun Apr 28 08:31:50 2019 +0000
Revision:
55:a1bd0572c8b6
Parent:
http-derived/nmea/http-nmea-script.inc@49:115a5e4fac0c
Update web-base library and renamed http-derived to web-derived

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 48:9f1ab7784067 1 "'use strict';\n"
andrewboyson 48:9f1ab7784067 2 "\n"
andrewboyson 49:115a5e4fac0c 3 "const NMEA_DEG_UNIT = 10000000;\n"
andrewboyson 49:115a5e4fac0c 4 "const NMEA_HGT_UNIT = 1000000;\n"
andrewboyson 49:115a5e4fac0c 5 "\n"
andrewboyson 49:115a5e4fac0c 6 "var response = '';\n"
andrewboyson 49:115a5e4fac0c 7 "var headers = '';\n"
andrewboyson 49:115a5e4fac0c 8 "var gpsTrace = false;\n"
andrewboyson 49:115a5e4fac0c 9 "var gpsVerbose = false;\n"
andrewboyson 49:115a5e4fac0c 10 "var msgTrace = false;\n"
andrewboyson 49:115a5e4fac0c 11 "var cmdTrace = false;\n"
andrewboyson 49:115a5e4fac0c 12 "var lat = 0;\n"
andrewboyson 49:115a5e4fac0c 13 "var lng = 0;\n"
andrewboyson 49:115a5e4fac0c 14 "var hgt = 0;\n"
andrewboyson 49:115a5e4fac0c 15 "var hgtAboveMsl = 0;\n"
andrewboyson 49:115a5e4fac0c 16 "var mslAboveWgs = 0;\n"
andrewboyson 49:115a5e4fac0c 17 "var dop = 0;\n"
andrewboyson 49:115a5e4fac0c 18 "var satCount = 0;\n"
andrewboyson 49:115a5e4fac0c 19 "var fixQuality = 0;\n"
andrewboyson 49:115a5e4fac0c 20 "var sensorHeight = 0;\n"
andrewboyson 49:115a5e4fac0c 21 "var bucketCount = 0;\n"
andrewboyson 49:115a5e4fac0c 22 "var buckets = '';\n"
andrewboyson 48:9f1ab7784067 23 "\n"
andrewboyson 48:9f1ab7784067 24 "function hexToBit(iChar, iBit)\n"
andrewboyson 48:9f1ab7784067 25 "{\n"
andrewboyson 48:9f1ab7784067 26 " var value = parseInt(response.charAt(iChar), 16);\n"
andrewboyson 48:9f1ab7784067 27 " value >>= iBit;\n"
andrewboyson 48:9f1ab7784067 28 " return value & 1;\n"
andrewboyson 48:9f1ab7784067 29 "}\n"
andrewboyson 49:115a5e4fac0c 30 "function parseSint32fromHex(value)\n"
andrewboyson 49:115a5e4fac0c 31 "{\n"
andrewboyson 49:115a5e4fac0c 32 " value = parseInt(value, 16);\n"
andrewboyson 49:115a5e4fac0c 33 " if (value > 0x7FFFFFFF) value -= 0xFFFFFFFF;\n"
andrewboyson 49:115a5e4fac0c 34 " return value;\n"
andrewboyson 49:115a5e4fac0c 35 "}\n"
andrewboyson 48:9f1ab7784067 36 "function parseAjax()\n"
andrewboyson 48:9f1ab7784067 37 "{\n"
andrewboyson 49:115a5e4fac0c 38 " gpsTrace = hexToBit(0, 0);\n"
andrewboyson 49:115a5e4fac0c 39 " gpsVerbose = hexToBit(0, 1);\n"
andrewboyson 49:115a5e4fac0c 40 " msgTrace = hexToBit(0, 2);\n"
andrewboyson 49:115a5e4fac0c 41 " cmdTrace = hexToBit(0, 3);\n"
andrewboyson 49:115a5e4fac0c 42 " lat = parseSint32fromHex(response.substr( 1, 8));\n"
andrewboyson 49:115a5e4fac0c 43 " lng = parseSint32fromHex(response.substr( 9, 8));\n"
andrewboyson 49:115a5e4fac0c 44 " hgt = parseSint32fromHex(response.substr(17, 8));\n"
andrewboyson 49:115a5e4fac0c 45 " hgtAboveMsl = parseSint32fromHex(response.substr(25, 8));\n"
andrewboyson 49:115a5e4fac0c 46 " mslAboveWgs = parseSint32fromHex(response.substr(33, 8));\n"
andrewboyson 49:115a5e4fac0c 47 " dop = parseSint32fromHex(response.substr(41, 8));\n"
andrewboyson 49:115a5e4fac0c 48 " satCount = parseSint32fromHex(response.substr(49, 8));\n"
andrewboyson 49:115a5e4fac0c 49 " fixQuality = parseSint32fromHex(response.substr(57, 8));\n"
andrewboyson 49:115a5e4fac0c 50 " sensorHeight = parseSint32fromHex(response.substr(65, 8));\n"
andrewboyson 49:115a5e4fac0c 51 " bucketCount = parseSint32fromHex(response.substr(73, 8));\n"
andrewboyson 49:115a5e4fac0c 52 " buckets = '';\n"
andrewboyson 49:115a5e4fac0c 53 " for (var i = 0; i < bucketCount; i++)\n"
andrewboyson 49:115a5e4fac0c 54 " {\n"
andrewboyson 49:115a5e4fac0c 55 " buckets += (i * 50).toString().padStart(3, '0') + ' ' + parseInt(response.substr(81 + i * 8, 8), 16) + '\\r\\n';\n"
andrewboyson 49:115a5e4fac0c 56 " }\n"
andrewboyson 49:115a5e4fac0c 57 "}\n"
andrewboyson 49:115a5e4fac0c 58 "function degToString(plus, minus, deg)\n"
andrewboyson 49:115a5e4fac0c 59 "{\n"
andrewboyson 49:115a5e4fac0c 60 " deg /= NMEA_DEG_UNIT;\n"
andrewboyson 49:115a5e4fac0c 61 " if (deg >= 0) return plus + deg.toFixed(5);\n"
andrewboyson 49:115a5e4fac0c 62 " else return minus + (-deg).toFixed(5);\n"
andrewboyson 49:115a5e4fac0c 63 "}\n"
andrewboyson 49:115a5e4fac0c 64 "function hgtToString(plus, minus, hgt)\n"
andrewboyson 49:115a5e4fac0c 65 "{\n"
andrewboyson 49:115a5e4fac0c 66 " hgt /= NMEA_HGT_UNIT;\n"
andrewboyson 49:115a5e4fac0c 67 " if (hgt >= 0) return plus + hgt.toFixed(1);\n"
andrewboyson 49:115a5e4fac0c 68 " else return minus + (-hgt).toFixed(1);\n"
andrewboyson 48:9f1ab7784067 69 "}\n"
andrewboyson 48:9f1ab7784067 70 "function displayGeneral()\n"
andrewboyson 48:9f1ab7784067 71 "{\n"
andrewboyson 48:9f1ab7784067 72 " var elem;\n"
andrewboyson 49:115a5e4fac0c 73 " elem = document.getElementById('ajax-trace-gps' ); if (elem) elem.setAttribute('dir', gpsTrace ? 'rtl' : 'ltr');\n"
andrewboyson 49:115a5e4fac0c 74 " elem = document.getElementById('ajax-trace-verbose'); if (elem) elem.setAttribute('dir', gpsVerbose ? 'rtl' : 'ltr');\n"
andrewboyson 49:115a5e4fac0c 75 " elem = document.getElementById('ajax-trace-msg' ); if (elem) elem.setAttribute('dir', msgTrace ? 'rtl' : 'ltr');\n"
andrewboyson 49:115a5e4fac0c 76 " elem = document.getElementById('ajax-trace-cmd' ); if (elem) elem.setAttribute('dir', cmdTrace ? 'rtl' : 'ltr');\n"
andrewboyson 48:9f1ab7784067 77 "\n"
andrewboyson 49:115a5e4fac0c 78 " elem = document.getElementById('ajax-position' ); if (elem) elem.textContent = degToString('N', 'S', lat) + ' ' +\n"
andrewboyson 49:115a5e4fac0c 79 " degToString('E', 'W', lng) + ' ' +\n"
andrewboyson 49:115a5e4fac0c 80 " hgtToString('H', 'D', hgt);\n"
andrewboyson 49:115a5e4fac0c 81 " elem = document.getElementById('ajax-msl-above-wgs'); if (elem) elem.textContent = hgtToString('H', 'D', mslAboveWgs);\n"
andrewboyson 49:115a5e4fac0c 82 " elem = document.getElementById('ajax-hgt-above-msl'); if (elem) elem.textContent = hgtToString('H', 'D', hgtAboveMsl);\n"
andrewboyson 49:115a5e4fac0c 83 " elem = document.getElementById('ajax-gnd-above-msl'); if (elem) elem.textContent = hgtToString('H', 'D', hgtAboveMsl - sensorHeight * NMEA_HGT_UNIT);\n"
andrewboyson 48:9f1ab7784067 84 "\n"
andrewboyson 49:115a5e4fac0c 85 " elem = document.getElementById('ajax-dop' ); if (elem) elem.textContent = dop / 100;\n"
andrewboyson 49:115a5e4fac0c 86 " elem = document.getElementById('ajax-sat-count' ); if (elem) elem.textContent = satCount;\n"
andrewboyson 49:115a5e4fac0c 87 " elem = document.getElementById('ajax-fix-quality' ); if (elem) elem.textContent = fixQuality;\n"
andrewboyson 48:9f1ab7784067 88 " \n"
andrewboyson 49:115a5e4fac0c 89 " elem = document.getElementById('ajax-sensor-hgt' ); if (elem) elem.value = sensorHeight;\n"
andrewboyson 48:9f1ab7784067 90 " \n"
andrewboyson 48:9f1ab7784067 91 " elem = document.getElementById('ajax-bucket-count' ); if (elem) elem.textContent = bucketCount;\n"
andrewboyson 49:115a5e4fac0c 92 " elem = document.getElementById('ajax-buckets' ); if (elem) elem.textContent = buckets;\n"
andrewboyson 48:9f1ab7784067 93 "\n"
andrewboyson 48:9f1ab7784067 94 " elem = document.getElementById('ajax-response' ); if (elem) elem.textContent = response;\n"
andrewboyson 48:9f1ab7784067 95 " elem = document.getElementById('ajax-headers' ); if (elem) elem.textContent = headers;\n"
andrewboyson 48:9f1ab7784067 96 "}\n"
andrewboyson 48:9f1ab7784067 97 "\n"
andrewboyson 48:9f1ab7784067 98 "var ajax;\n"
andrewboyson 48:9f1ab7784067 99 "function AjaxRequest(request) //Used by this script and from HTML page\n"
andrewboyson 48:9f1ab7784067 100 "{\n"
andrewboyson 48:9f1ab7784067 101 " ajax=new XMLHttpRequest();\n"
andrewboyson 48:9f1ab7784067 102 " ajax.onreadystatechange=handleAjaxResponse;\n"
andrewboyson 48:9f1ab7784067 103 " if (request) ajax.open('GET', '/nmea-ajax' + '?' + request, true);\n"
andrewboyson 48:9f1ab7784067 104 " else ajax.open('GET', '/nmea-ajax' , true);\n"
andrewboyson 48:9f1ab7784067 105 " ajax.send();\n"
andrewboyson 48:9f1ab7784067 106 "}\n"
andrewboyson 48:9f1ab7784067 107 "function requestAjax() //Used in this script\n"
andrewboyson 48:9f1ab7784067 108 "{\n"
andrewboyson 48:9f1ab7784067 109 " AjaxRequest('');\n"
andrewboyson 48:9f1ab7784067 110 "}\n"
andrewboyson 48:9f1ab7784067 111 "\n"
andrewboyson 48:9f1ab7784067 112 "function handleAjaxResponse()\n"
andrewboyson 48:9f1ab7784067 113 "{\n"
andrewboyson 48:9f1ab7784067 114 " if (ajax.readyState==4 && ajax.status==200)\n"
andrewboyson 48:9f1ab7784067 115 " {\n"
andrewboyson 48:9f1ab7784067 116 " response = ajax.responseText;\n"
andrewboyson 48:9f1ab7784067 117 " headers = ajax.getAllResponseHeaders();\n"
andrewboyson 48:9f1ab7784067 118 " parseAjax();\n"
andrewboyson 48:9f1ab7784067 119 " displayGeneral();\n"
andrewboyson 48:9f1ab7784067 120 " }\n"
andrewboyson 48:9f1ab7784067 121 "}\n"
andrewboyson 48:9f1ab7784067 122 "\n"
andrewboyson 48:9f1ab7784067 123 "function init()\n"
andrewboyson 48:9f1ab7784067 124 "{\n"
andrewboyson 48:9f1ab7784067 125 " setInterval(requestAjax, 10000);\n"
andrewboyson 48:9f1ab7784067 126 " requestAjax();\n"
andrewboyson 48:9f1ab7784067 127 "}\n"
andrewboyson 48:9f1ab7784067 128 "if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init ); // Loading hasn't finished yet\n"
andrewboyson 48:9f1ab7784067 129 "else init(); //`DOMContentLoaded` has already fired\n"
andrewboyson 48:9f1ab7784067 130 ""