A GPS disciplined clock

Dependencies:   net lpc1768 crypto clock web log

Committer:
andrewboyson
Date:
Wed Mar 27 18:40:21 2019 +0000
Revision:
48:9f1ab7784067
Child:
49:115a5e4fac0c
Published to allow compiler diagnosis

Who changed what in which revision?

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