2022-09-21 19:33:14 +00:00
|
|
|
(() => {
|
2022-11-01 23:09:34 +00:00
|
|
|
const SAMPLES=5;
|
2022-10-21 15:04:53 +00:00
|
|
|
function initState(){
|
|
|
|
//cleanup volatile state here
|
|
|
|
state = {};
|
2022-11-01 23:09:34 +00:00
|
|
|
state.compassSamples = new Array(SAMPLES).fill(0);
|
|
|
|
state.lastSample = 0;
|
|
|
|
state.sampleIndex = 0;
|
2022-10-21 15:04:53 +00:00
|
|
|
state.currentPos={};
|
|
|
|
state.steps = 0;
|
|
|
|
state.calibAltDiff = 0;
|
|
|
|
state.numberOfSlices = 3;
|
|
|
|
state.steps = 0;
|
|
|
|
state.up = 0;
|
|
|
|
state.down = 0;
|
|
|
|
state.saved = 0;
|
2022-11-01 23:09:34 +00:00
|
|
|
state.avgComp = 0;
|
2022-10-21 15:04:53 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:33:14 +00:00
|
|
|
const STORAGE=require('Storage');
|
2022-10-21 15:04:53 +00:00
|
|
|
let state = STORAGE.readJSON("gpstrek.state.json");
|
|
|
|
if (!state) {
|
|
|
|
state = {};
|
|
|
|
initState();
|
|
|
|
}
|
2022-10-13 19:13:07 +00:00
|
|
|
let bgChanged = false;
|
2022-09-21 19:33:14 +00:00
|
|
|
|
|
|
|
function saveState(){
|
|
|
|
state.saved = Date.now();
|
|
|
|
STORAGE.writeJSON("gpstrek.state.json", state);
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:04:53 +00:00
|
|
|
function onKill(){
|
2022-11-05 22:41:37 +00:00
|
|
|
if (bgChanged || state.route || state.waypoint){
|
2022-09-21 19:33:14 +00:00
|
|
|
saveState();
|
|
|
|
}
|
2022-10-21 15:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
E.on("kill", onKill);
|
2022-09-21 19:33:14 +00:00
|
|
|
|
|
|
|
function onPulse(e){
|
|
|
|
state.bpm = e.bpm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onGPS(fix) {
|
|
|
|
if(fix.fix) state.currentPos = fix;
|
|
|
|
}
|
|
|
|
|
2022-11-01 23:09:34 +00:00
|
|
|
let radians = function(a) {
|
|
|
|
return a*Math.PI/180;
|
|
|
|
};
|
|
|
|
|
|
|
|
let degrees = function(a) {
|
|
|
|
let d = a*180/Math.PI;
|
|
|
|
return (d+360)%360;
|
|
|
|
};
|
|
|
|
|
|
|
|
function average(samples){
|
|
|
|
let s = 0;
|
|
|
|
let c = 0;
|
|
|
|
for (let h of samples){
|
|
|
|
s += Math.sin(radians(h));
|
|
|
|
c += Math.cos(radians(h));
|
|
|
|
}
|
|
|
|
s /= samples.length;
|
|
|
|
c /= samples.length;
|
|
|
|
let result = degrees(Math.atan(s/c));
|
|
|
|
|
|
|
|
if (c < 0) result += 180;
|
|
|
|
if (s < 0 && c > 0) result += 360;
|
|
|
|
|
|
|
|
result%=360;
|
|
|
|
return result;
|
2022-11-05 23:38:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:33:14 +00:00
|
|
|
function onMag(e) {
|
2022-11-01 23:09:34 +00:00
|
|
|
if (!isNaN(e.heading)){
|
2022-11-05 23:38:12 +00:00
|
|
|
if (Bangle.isLocked() || (Bangle.getGPSFix() && Bangle.getGPSFix().lon))
|
2022-11-01 23:09:34 +00:00
|
|
|
state.avgComp = e.heading;
|
|
|
|
else {
|
|
|
|
state.compassSamples[state.sampleIndex++] = e.heading;
|
|
|
|
state.lastSample = Date.now();
|
|
|
|
if (state.sampleIndex > SAMPLES - 1){
|
|
|
|
state.sampleIndex = 0;
|
|
|
|
let avg = average(state.compassSamples);
|
|
|
|
state.avgComp = average([state.avgComp,avg]);
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 19:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onStep(e) {
|
|
|
|
state.steps++;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onPressure(e) {
|
|
|
|
state.pressure = e.pressure;
|
|
|
|
|
|
|
|
if (!state.altitude){
|
|
|
|
state.altitude = e.altitude;
|
|
|
|
state.up = 0;
|
|
|
|
state.down = 0;
|
|
|
|
}
|
|
|
|
let diff = state.altitude - e.altitude;
|
|
|
|
if (Math.abs(diff) > 3){
|
|
|
|
if (diff > 0){
|
|
|
|
state.up += diff;
|
|
|
|
} else {
|
|
|
|
state.down -= diff;
|
|
|
|
}
|
|
|
|
state.altitude = e.altitude;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:04:53 +00:00
|
|
|
function onAcc (e){
|
|
|
|
state.acc = e;
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:04:53 +00:00
|
|
|
function update(){
|
|
|
|
if (state.active){
|
|
|
|
start(false);
|
|
|
|
}
|
|
|
|
if (state.active == !(WIDGETS.gpstrek.width)) {
|
|
|
|
if(WIDGETS.gpstrek) WIDGETS.gpstrek.width = state.active?24:0;
|
|
|
|
Bangle.drawWidgets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:30:12 +00:00
|
|
|
function start(bg){
|
2022-11-02 22:11:30 +00:00
|
|
|
Bangle.removeListener('GPS', onGPS);
|
|
|
|
Bangle.removeListener("HRM", onPulse);
|
|
|
|
Bangle.removeListener("mag", onMag);
|
|
|
|
Bangle.removeListener("step", onStep);
|
|
|
|
Bangle.removeListener("pressure", onPressure);
|
|
|
|
Bangle.removeListener('accel', onAcc);
|
2022-09-21 19:33:14 +00:00
|
|
|
Bangle.on('GPS', onGPS);
|
|
|
|
Bangle.on("HRM", onPulse);
|
|
|
|
Bangle.on("mag", onMag);
|
|
|
|
Bangle.on("step", onStep);
|
|
|
|
Bangle.on("pressure", onPressure);
|
2022-10-21 15:04:53 +00:00
|
|
|
Bangle.on('accel', onAcc);
|
2022-09-21 19:33:14 +00:00
|
|
|
|
|
|
|
Bangle.setGPSPower(1, "gpstrek");
|
|
|
|
Bangle.setHRMPower(1, "gpstrek");
|
|
|
|
Bangle.setCompassPower(1, "gpstrek");
|
|
|
|
Bangle.setBarometerPower(1, "gpstrek");
|
2023-06-11 08:31:22 +00:00
|
|
|
|
2022-10-13 18:30:12 +00:00
|
|
|
if (bg){
|
2022-10-13 19:13:07 +00:00
|
|
|
if (!state.active) bgChanged = true;
|
2022-10-13 18:30:12 +00:00
|
|
|
state.active = true;
|
2022-10-21 15:04:53 +00:00
|
|
|
update();
|
2022-10-13 18:30:12 +00:00
|
|
|
saveState();
|
|
|
|
}
|
2022-09-21 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 18:30:12 +00:00
|
|
|
function stop(bg){
|
|
|
|
if (bg){
|
2022-10-13 19:13:07 +00:00
|
|
|
if (state.active) bgChanged = true;
|
2022-10-13 18:30:12 +00:00
|
|
|
state.active = false;
|
2022-10-21 15:04:53 +00:00
|
|
|
} else if (!state.active) {
|
|
|
|
Bangle.setGPSPower(0, "gpstrek");
|
|
|
|
Bangle.setHRMPower(0, "gpstrek");
|
|
|
|
Bangle.setCompassPower(0, "gpstrek");
|
|
|
|
Bangle.setBarometerPower(0, "gpstrek");
|
|
|
|
Bangle.removeListener('GPS', onGPS);
|
|
|
|
Bangle.removeListener("HRM", onPulse);
|
|
|
|
Bangle.removeListener("mag", onMag);
|
|
|
|
Bangle.removeListener("step", onStep);
|
|
|
|
Bangle.removeListener("pressure", onPressure);
|
|
|
|
Bangle.removeListener('accel', onAcc);
|
2022-10-21 15:04:53 +00:00
|
|
|
E.removeListener("kill", onKill);
|
2022-10-13 18:30:12 +00:00
|
|
|
}
|
2022-10-21 15:04:53 +00:00
|
|
|
update();
|
2022-10-21 15:04:53 +00:00
|
|
|
saveState();
|
2022-09-21 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.active){
|
2022-10-13 19:13:07 +00:00
|
|
|
start(false);
|
2022-09-21 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 19:46:16 +00:00
|
|
|
WIDGETS.gpstrek={
|
2022-09-21 19:33:14 +00:00
|
|
|
area:"tl",
|
|
|
|
width:state.active?24:0,
|
|
|
|
resetState: initState,
|
|
|
|
getState: function() {
|
2022-10-21 15:04:53 +00:00
|
|
|
if (state.saved && Date.now() - state.saved > 60000 || !state){
|
|
|
|
initState();
|
|
|
|
}
|
2022-09-21 19:33:14 +00:00
|
|
|
return state;
|
|
|
|
},
|
|
|
|
start:start,
|
|
|
|
stop:stop,
|
|
|
|
draw:function() {
|
2022-10-21 15:04:53 +00:00
|
|
|
update();
|
2022-09-21 19:33:14 +00:00
|
|
|
if (state.active){
|
|
|
|
g.reset();
|
|
|
|
g.drawImage(atob("GBiBAAAAAAAAAAAYAAAYAAAYAAA8AAA8AAB+AAB+AADbAADbAAGZgAGZgAMYwAMYwAcY4AYYYA5+cA3/sB/D+B4AeBAACAAAAAAAAA=="), this.x, this.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|