BangleApps/apps/assistedgps/custom.html

112 lines
4.2 KiB
HTML

<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<h2>Assisted GPS</h2>
<p>GPS can take a long time (~5 minutes) to get an accurate position the first time it is used.
AGPS uploads a few hints to the GPS receiver about satellite positions that allow it
to get a faster, more accurate fix - however they are only valid for a short period of time.</p>
<p>You can upload data that covers a longer period of time, but the upload will take longer.</p>
<div class="form-group">
<label class="form-label">AGPS Validity time</label>
<label class="form-radio">
<input type="radio" name="agpsperiod" value="1d"><i class="form-icon"></i> 1 day (8kB)
</label>
<label class="form-radio">
<input type="radio" name="agpsperiod" value="2d" checked><i class="form-icon"></i> 2 days (14kB)
</label>
<label class="form-radio">
<input type="radio" name="agpsperiod" value="3d"><i class="form-icon"></i> 3 days (20kB)
</label>
<label class="form-radio">
<input type="radio" name="agpsperiod" value="1wk"><i class="form-icon"></i> 1 week (46kB)
</label>
</div>
<p>Click <button id="upload" class="btn btn-primary">Upload</button></p>
<script src="../../core/lib/customize.js"></script>
<script>
// When the 'upload' button is clicked...
document.getElementById("upload").addEventListener("click", function() {
var radios = document.getElementsByName('agpsperiod');
var url = "https://www.espruino.com/agps/assistnow_1d.base64";
for (var i=0; i<radios.length; i++)
if (radios[i].checked)
url = "https://www.espruino.com/agps/assistnow_"+radios[i].value+".base64";
console.log("Sending...");
//var text = document.getElementById("agpsperiod").value;
get(url, function(b64) {
var js = jsFromBase64(b64);
sendCustomizedApp({
storage:[
{name:"RAM", content:js},
]
});
});
});
function UBX_CMD(cmd) {
var d = [0xB5,0x62]; // sync chars
d = d.concat(cmd);
var a=0,b=0;
for (var i=2;i<d.length;i++) {
a += d[i];
b += a;
}
d.push(a&255,b&255);
return d;
}
function UBX_MGA_INI_TIME_UTC() {
var a = new Uint8Array(4+24);
a.set([0x13,0x40,24,0]);
a.set([ 0x10, // 0: type
0, // 1: version
0, // 2: ref - none
0x80] ); // 3: leapsecs - unknown
var d = new Date();
d.setTime(d.getTime()+d.getTimezoneOffset()*60000); // get as UTC
var dv = new DataView(a.buffer, 4);
dv.setUint16(4, d.getFullYear());
dv.setUint8(6, d.getMonth()+1);
dv.setUint8(7, d.getDate());
dv.setUint8(8, d.getHours());
dv.setUint8(9, d.getMinutes());
dv.setUint8(10, d.getSeconds());
dv.setUint16(16, 10*60); // seconds part of accuracy - 10 minutes
return UBX_CMD([].slice.call(a));
}
function jsFromBase64(b64) {
var bin = atob(b64);
var chunkSize = 128;
var js = "\x10Bangle.setGPSPower(1);\n"; // turn GPS on
//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`;
//js += "\x10var t=getTime()+1;while(t>getTime());\n"; // wait 1 sec
js += `\x10Serial1.write(atob("${btoa(String.fromCharCode.apply(null,UBX_MGA_INI_TIME_UTC()))}"))\n`; // set GPS time
for (var i=0;i<bin.length;i+=chunkSize) {
var chunk = bin.substr(i,chunkSize);
js += `\x10Serial1.write(atob("${btoa(chunk)}"))\n`;
}
return js;
}
function get(url,callback) {
console.log("Loading "+url);
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
var b64 = this.responseText;
console.log("Response received...");
callback(b64);
});
oReq.open("GET", url);
oReq.send();
}
</script>
</body>
</html>