|
| 1 | +<html> |
| 2 | + <head> |
| 3 | + <link rel="stylesheet" href="../../css/spectre.min.css"> |
| 4 | + </head> |
| 5 | + <body> |
| 6 | + |
| 7 | + <h2>Assisted GPS</h2> |
| 8 | + <p>GPS can take a long time (~5 minutes) to get an accurate position the first time it is used. |
| 9 | + AGPS uploads a few hints to the GPS receiver about satellite positions that allow it |
| 10 | + to get a faster, more accurate fix - however they are only valid for a short period of time.</p> |
| 11 | + <p>You can upload data that covers a longer period of time, but the upload will take longer.</p> |
| 12 | + <div class="form-group"> |
| 13 | + <label class="form-label">AGPS Validity time</label> |
| 14 | + <label class="form-radio"> |
| 15 | + <input type="radio" name="agpsperiod" value="1d" checked><i class="form-icon"></i> 1 day (8kB) |
| 16 | + </label> |
| 17 | + <label class="form-radio"> |
| 18 | + <input type="radio" name="agpsperiod" value="2d"><i class="form-icon"></i> 2 days (14kB) |
| 19 | + </label> |
| 20 | + <label class="form-radio"> |
| 21 | + <input type="radio" name="agpsperiod" value="3d"><i class="form-icon"></i> 3 days (20kB) |
| 22 | + </label> |
| 23 | + <label class="form-radio"> |
| 24 | + <input type="radio" name="agpsperiod" value="1wk"><i class="form-icon"></i> 1 week (46kB) |
| 25 | + </label> |
| 26 | + </div> |
| 27 | + <p>Click <button id="upload" class="btn btn-primary">Upload</button></p> |
| 28 | + |
| 29 | + <script src="../../core/lib/customize.js"></script> |
| 30 | + |
| 31 | + <script> |
| 32 | + // When the 'upload' button is clicked... |
| 33 | + document.getElementById("upload").addEventListener("click", function() { |
| 34 | + var radios = document.getElementsByName('agpsperiod'); |
| 35 | + var url = "https://www.espruino.com/agps/assistnow_1d.base64"; |
| 36 | + for (var i=0; i<radios.length; i++) |
| 37 | + if (radios[i].checked) |
| 38 | + url = "https://www.espruino.com/agps/assistnow_"+radios[i].value+".base64"; |
| 39 | + console.log("Sending..."); |
| 40 | + //var text = document.getElementById("agpsperiod").value; |
| 41 | + get(url, function(b64) { |
| 42 | + var js = jsFromBase64(b64); |
| 43 | + sendCustomizedApp({ |
| 44 | + storage:[ |
| 45 | + {name:"RAM", content:js}, |
| 46 | + ] |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + function UBX_CMD(cmd) { |
| 52 | + var d = [0xB5,0x62]; // sync chars |
| 53 | + d = d.concat(cmd); |
| 54 | + var a=0,b=0; |
| 55 | + for (var i=2;i<d.length;i++) { |
| 56 | + a += d[i]; |
| 57 | + b += a; |
| 58 | + } |
| 59 | + d.push(a&255,b&255); |
| 60 | + return d; |
| 61 | + } |
| 62 | + function UBX_MGA_INI_TIME_UTC() { |
| 63 | + var a = new Uint8Array(4+24); |
| 64 | + a.set([0x13,0x40,24,0]); |
| 65 | + a.set([ 0x10, // 0: type |
| 66 | + 0, // 1: version |
| 67 | + 0, // 2: ref - none |
| 68 | + 0x80] ); // 3: leapsecs - unknown |
| 69 | + var d = new Date(); |
| 70 | + d.setTime(d.getTime()+d.getTimezoneOffset()*60000); // get as UTC |
| 71 | + var dv = new DataView(a.buffer, 4); |
| 72 | + dv.setUint16(4, d.getFullYear()); |
| 73 | + dv.setUint8(6, d.getMonth()+1); |
| 74 | + dv.setUint8(7, d.getDate()); |
| 75 | + dv.setUint8(8, d.getHours()); |
| 76 | + dv.setUint8(9, d.getMinutes()); |
| 77 | + dv.setUint8(10, d.getSeconds()); |
| 78 | + dv.setUint16(16, 10*60); // seconds part of accuracy - 10 minutes |
| 79 | + return UBX_CMD([].slice.call(a)); |
| 80 | + } |
| 81 | + |
| 82 | + function jsFromBase64(b64) { |
| 83 | + var bin = atob(b64); |
| 84 | + var chunkSize = 128; |
| 85 | + var js = "\x10Bangle.setGPSPower(1);\n"; // turn GPS on |
| 86 | + //js += `\x10Bangle.on('GPS-raw',function (d) { if (d.startsWith("\\xB5\\x62\\x05\\x01")) Terminal.println("GPS ACK"); else if (d.startsWith("\\xB5\\x62\\x05\\x00")) Terminal.println("GPS NACK"); })\n`; |
| 87 | + js += "\x10E.showMessage('Uploading...','AGPS');function p(n) {g.fillRect(0,g.getHeight()-80,g.getWidth()*n,g.getHeight()-60);}"; |
| 88 | + //js += "\x10var t=getTime()+1;while(t>getTime());\n"; // wait 1 sec |
| 89 | + js += `\x10Serial1.write(atob("${btoa(String.fromCharCode.apply(null,UBX_MGA_INI_TIME_UTC()))}"))\n`; // set GPS time |
| 90 | + |
| 91 | + for (var i=0;i<bin.length;i+=chunkSize) { |
| 92 | + var chunk = bin.substr(i,chunkSize); |
| 93 | + js += `\x10p(${Math.round(100*i/bin.length)/100});Serial1.write(atob("${btoa(chunk)}"))\n`; |
| 94 | + } |
| 95 | + js += "\x10p(1);\n"; // finish progress bar |
| 96 | + return js; |
| 97 | + } |
| 98 | + |
| 99 | + function get(url,callback) { |
| 100 | + console.log("Loading "+url); |
| 101 | + var oReq = new XMLHttpRequest(); |
| 102 | + oReq.addEventListener("load", function() { |
| 103 | + var b64 = this.responseText; |
| 104 | + console.log("Response received..."); |
| 105 | + callback(b64); |
| 106 | + }); |
| 107 | + oReq.open("GET", url); |
| 108 | + oReq.send(); |
| 109 | + } |
| 110 | + |
| 111 | + </script> |
| 112 | + </body> |
| 113 | +</html> |
0 commit comments