BangleApps/apps/tinyVario/app.js

103 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-05-30 18:00:34 +00:00
Bangle.setBarometerPower(true, "tinyVario");
Bangle.setGPSPower(true, "tinyVario");
2022-06-02 09:14:16 +00:00
var intTime=10,pressureInterval=100;
2022-05-30 18:00:34 +00:00
var altH = [];
2022-06-20 02:27:33 +00:00
var altFast=-10000, altSlow=0;
var fastGain=0.2, slowGain=0.168;
2022-06-02 09:14:16 +00:00
var roc=0,rocAvg=0;
2022-05-30 18:00:34 +00:00
var gs;
var lastPressure = Date.now();
var flying=false;
var takeoffTime, flyingTime;
2022-06-20 02:27:33 +00:00
var Layout = require("Layout");
function drawVario() {
var p = pfd.vario;
//roc=(altFast-altSlow)/(pressureInterval/1000/slowGain)-(pressureInterval/1000/fastGain);
g.reset();
g.drawRect(p.x,p.y,p.x+p.w,p.y+p.h);
g.clearRect(p.x+1,p.y+1,p.x+p.w-1,p.y+p.h-1);
2022-05-30 18:00:34 +00:00
2022-06-20 02:27:33 +00:00
if (roc>0.1) g.setColor(0,1,0);
if (roc<-1) g.setColor(1,0,0);
var y=p.y+p.h/2-roc*(p.h/2)/5;
g.fillRect(p.x+1,p.y+(p.h/2),p.x+p.w-1,Math.clip(y,p.y+1,p.y+p.h-1));
//print (pfd.vario);
}
2022-05-30 18:00:34 +00:00
2022-06-20 02:27:33 +00:00
var pfd = new Layout(
{type:"v",c: [
{type:"h",c: [
{type:"", fillx:1, height:"1"}
]},
{type:"h",c: [
{type:"custom", width:"20", render:drawVario, id:"vario",filly:1 },
{type:"v",fillx:"1", c: [
{type:"txt", font:"25%", halign:1, filly:1, label:"-", id:"alt"},
{type:"", fillx:1, height:"1", bgCol:"#FFF"},
{type:"txt", font:"18%", halign:1, filly:1, label:"-", id:"roc" },
{type:"", fillx:1, height:"1", bgCol:"#FFF"},
{type:"txt", font:"18%", halign:1, filly:1, label:"-", id:"gs" },
{type:"", fillx:1, height:"1", bgCol:"#FFF"}
]}
]},
{type:"h",pad:"5",c: [
{type:"txt", font:"15%",fillx:"1", label:"-", id:"time"},
{type:"", width:"1", bgCol:"#FFF"},
{type:"txt", font:"15%", fillx:"1", label:"-", id:"flyingtime" }
]}
]},{lazy:"true"}
);
2022-05-30 18:00:34 +00:00
Bangle.on('pressure', function(e) {
2022-06-20 02:27:33 +00:00
if ((altFast)==-10000) {
altFast=e.altitude;
altSlow=e.altitude;
}
altFast=altFast+(e.altitude-altFast)*fastGain;
altSlow=altSlow+(e.altitude-altSlow)*0.09093;
2022-05-30 18:00:34 +00:00
});
Bangle.on('GPS', function(fix) {
gs=fix.speed;
});
/*setWatch(function() {
2022-06-02 07:48:54 +00:00
}, BTN1);*/
2022-05-30 18:00:34 +00:00
setInterval(function () {
2022-06-20 02:27:33 +00:00
altH.push(altSlow);
2022-06-02 09:14:16 +00:00
while (altH.length>intTime*1000/pressureInterval) altH.shift();
}, pressureInterval);
2022-05-30 18:00:34 +00:00
setInterval(function() {
2022-06-02 07:48:54 +00:00
if ((!flying) && ((rocAvg>1) || (rocAvg<-1) || (gs>10))) { //take-off detected
2022-05-30 18:00:34 +00:00
takeoffTime=Date().getTime();
flying=true;
2022-06-20 02:27:33 +00:00
// flyingTime=0;
2022-05-30 18:00:34 +00:00
}
if (flying) {
flyingTime=Date().getTime()-takeoffTime;
2022-06-20 02:27:33 +00:00
pfd.flyingtime.label=(flyingTime / 3600000).toFixed(0)+":"+(flyingTime / 60000 % 60).toFixed(0).padStart(2,'0');
} else pfd.flyingtime.label="--:--";
if (altH.length==intTime*1000/pressureInterval) rocAvg=(altH[altH.length-1]-altH[0])/intTime;
roc=(altFast-altSlow)/(pressureInterval/1000/slowGain)-(pressureInterval/1000/fastGain);
pfd.alt.label=(altSlow).toFixed(0)+"m";
if (rocAvg>0.1) {pfd.roc.col="#0f0";}
else if (rocAvg<-1) {pfd.roc.col="#f00";}
else {pfd.roc.col="#fff";}
pfd.roc.label=rocAvg.toFixed(1)+"m/s";
if (!isNaN(gs)) pfd.gs.label=gs.toFixed(0)+"km/h";
else pfd.gs.label="NO GPS";
pfd.time.label=require("locale").time(Date(),1);
pfd.update();
2022-06-02 09:14:16 +00:00
2022-06-20 02:27:33 +00:00
pfd.render();
drawVario();
2022-05-30 18:00:34 +00:00
}, 250);
2022-06-20 02:27:33 +00:00
g.clear();