BangleApps/apps/clkinfogps/clkinfo.js

128 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-04-21 23:51:30 +00:00
(function () {
var timeout;
var last_fix;
var fixTs;
2023-04-22 00:12:49 +00:00
var geo = require("geotools");
2023-04-21 23:51:30 +00:00
var debug = function(o) {
//console.log(o);
};
2023-04-26 18:25:50 +00:00
2023-04-21 23:51:30 +00:00
var resetLastFix = function() {
last_fix = {
fix: 0,
alt: 0,
lat: 0,
lon: 0,
speed: 0,
time: 0,
course: 0,
satellites: 0
};
};
2023-04-26 18:25:50 +00:00
2023-04-21 23:51:30 +00:00
var formatTime = function(now) {
try {
var fd = now.toUTCString().split(" ");
return fd[4];
} catch (e) {
return "00:00:00";
}
};
var clearTimer = function() {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
debug("timer cleared");
}
};
var queueGPSon = function() {
clearTimer();
// power on the GPS again in 90 seconds
timeout = setTimeout(function() {
timeout = undefined;
Bangle.setGPSPower(1,"clkinfo");
}, 90000);
debug("gps on queued");
};
2023-04-21 23:51:30 +00:00
var onGPS = function(fix) {
2023-04-29 09:49:14 +00:00
debug(fix);
2023-04-21 23:51:30 +00:00
last_fix.time = fix.time;
// we got a fix
if (fix.fix) {
last_fix = fix;
fixTs = Math.round(getTime());
2023-04-21 23:51:30 +00:00
// cancel the timeout, if not already timed out
clearTimer();
2023-04-21 23:51:30 +00:00
// power off the GPS
Bangle.setGPSPower(0,"clkinfo");
queueGPSon();
2023-04-21 23:51:30 +00:00
}
// if our last fix was more than 4 minutes ago, reset the fix to show gps time + satelites
if (Math.round(getTime()) - fixTs > 240) {
resetLastFix();
fixTs = Math.round(getTime());
// cancel the timeout and power off the gps, tap required to restart
clearTimer();
Bangle.setGPSPower(0,"clkinfo");
}
2023-04-26 18:25:50 +00:00
info.items[0].emit("redraw");
2023-04-21 23:51:30 +00:00
};
var img = function() {
return atob("GBgBAAAAAAAAABgAAb2ABzzgB37gD37wHn54AAAADEwIPn58Pn58Pv58Pn58FmA4AAAAHn54D37wD37gBzzAAb2AABgAAAAAAAAA");
};
2023-04-26 18:25:50 +00:00
2023-04-21 23:51:30 +00:00
var gpsText = function() {
if (last_fix === undefined)
2023-04-22 00:12:49 +00:00
return '';
2023-04-21 23:51:30 +00:00
// show gps time and satelite count
2023-04-26 18:25:50 +00:00
if (!last_fix.fix)
return formatTime(last_fix.time) + ' ' + last_fix.satellites;
2023-04-22 01:00:01 +00:00
return geo.gpsToOSMapRef(last_fix);
2023-04-21 23:51:30 +00:00
};
2023-04-26 18:25:50 +00:00
2023-04-21 23:51:30 +00:00
var info = {
2024-05-17 15:33:19 +00:00
name: "GPS",
2023-04-21 23:51:30 +00:00
items: [
{
name: "gridref",
get: function () { return ({
2023-04-26 18:25:50 +00:00
img: img(),
2023-04-21 23:51:30 +00:00
text: gpsText()
}); },
2023-04-26 18:25:50 +00:00
run : function() {
2023-04-29 09:49:14 +00:00
debug("run");
2023-04-26 18:25:50 +00:00
// if the timer is already runnuing reset it, we can get multiple run calls by tapping
clearTimer();
2023-04-21 23:51:30 +00:00
Bangle.setGPSPower(1,"clkinfo");
2023-04-26 18:25:50 +00:00
},
2023-04-21 23:51:30 +00:00
show: function () {
2023-04-29 09:49:14 +00:00
debug("show");
2023-04-21 23:51:30 +00:00
resetLastFix();
2023-04-26 18:25:50 +00:00
fixTs = Math.round(getTime());
Bangle.on("GPS",onGPS);
this.run();
2023-04-21 23:51:30 +00:00
},
hide: function() {
2023-04-29 09:49:14 +00:00
debug("hide");
2023-04-26 18:25:50 +00:00
clearTimer();
2023-04-21 23:51:30 +00:00
Bangle.setGPSPower(0,"clkinfo");
Bangle.removeListener("GPS", onGPS);
resetLastFix();
}
}
]
};
return info;
});