From 9c2512c5b909103f20881bb5624fba88a54775e2 Mon Sep 17 00:00:00 2001 From: xxDUxx <96152564+xxDUxx@users.noreply.github.com> Date: Thu, 23 Jun 2022 12:14:53 +0200 Subject: [PATCH] Update app.js --- apps/tinyVario/app.js | 173 +++++++++++++++++++++++++++++++++--------- 1 file changed, 137 insertions(+), 36 deletions(-) diff --git a/apps/tinyVario/app.js b/apps/tinyVario/app.js index 315db3934..4f654fa1f 100644 --- a/apps/tinyVario/app.js +++ b/apps/tinyVario/app.js @@ -1,56 +1,143 @@ +/* +To do: + -setup page + -different units + -flight log + -statistics page + -nav page +*/ Bangle.setBarometerPower(true, "tinyVario"); Bangle.setGPSPower(true, "tinyVario"); +const unitsRoc=[ + {name:"m/s", factor:1, precision:1, layoutCode:{type:"v", halign:1, c: [ + {type:"txt", font:"12%", halign:0, filly:0, label:"m"}, + {type:"", height:1,width:"20", bgCol:"#FFF"}, + {type:"txt", font:"12%", halign:0, filly:0, label:"s"}]}}, + {name:"ft/m", factor:196.85039370078738, precision:0, layoutCode:{type:"v", halign:1, c: [ + {type:"txt", font:"12%", halign:0, filly:0, label:"ft"}, + {type:"", height:1,width:"30", bgCol:"#FFF"}, + {type:"txt", font:"12%", halign:0, filly:0, label:"min"}]}}, + {name:"kt", factor:1.9438444924406, precision:1, layoutCode: + {type:"txt", font:"12%", halign:0, filly:0, label:"kt"}} + ]; + +const unitsGs=[ + {name:"km/h", factor:1, precision:1, layoutCode:{type:"v", halign:1, c: [ + {type:"txt", font:"12%", halign:0, filly:0, label:"km"}, + {type:"", height:1,width:"30", bgCol:"#FFF"}, + {type:"txt", font:"12%", halign:0, filly:0, label:"h"}]}}, + {name:"kt", factor:196.85039370078738, precision:0, layoutCode:{type:"txt", font:"12%", halign:0, filly:0, label:"kt"}}, + {name:"m/s", factor:0.2777777777777778, precision:1, layoutCode:{type:"v", halign:1, c: [ + {type:"txt", font:"12%", halign:0, filly:0, label:"m"}, + {type:"", height:1,width:"20", bgCol:"#FFF"}, + {type:"txt", font:"12%", halign:0, filly:0, label:"s"}]}} + ]; + +const unitsAlt=[ + {name:"m", factor:1, precision:0, layoutCode:{type:"txt", font:"12%", halign:0, filly:0, label:"m"}}, + {name:"ft", factor:3.280839895013123, precision:0, layoutCode:{type:"txt", font:"12%", halign:0, filly:0, label:"ft"}} + ]; + var intTime=10,pressureInterval=100; var altH = []; var altFast=-10000, altSlow=0; var fastGain=0.2, slowGain=0.168; -var roc=0,rocAvg=0; +var roc=2,rocAvg=0; var gs; var lastPressure = Date.now(); var flying=false; var takeoffTime, flyingTime; var Layout = require("Layout"); +var speedUnit="km/h", speedFactor=1; +var altUnit="m", altFactor=1; +var rocUnit=unitsRoc[0]; +var altUnit=unitsAlt[0]; +var gsUnit=unitsGs[0]; 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); - + g.clearRect(p.x,p.y,p.x+p.w-1,p.y+p.h-1); 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); + g.fillRect(p.x,p.y+(p.h/2),p.x+p.w-1,Math.clip(y,p.y,p.y+p.h-1)); } +function updateText(t) { + if (t.halign==1) + g.setFont(t.font).setFontAlign(1,0,0).drawString(t.label, t.x+t.w, t.y+(t.h>>1)); + else if (t.halign==-1) + g.setFont(t.font).setFontAlign(-1,0,0).drawString(t.label, t.x, t.y+(t.h>>1)); + else + g.setFont(t.font).setFontAlign(0,0,0).drawString(t.label, t.x+(t.w>>1), t.y+(t.h>>1)); +} + +unitROC={type:"v", halign:1, c: [ + {type:"txt", font:"12%", halign:0, filly:0, label:"m"}, + {type:"", height:1,width:"20", bgCol:"#FFF"}, + {type:"txt", font:"12%", halign:0, filly:0, label:"s"} + ]}; + 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:"h", c: [ + /*{type:"v", c:[ + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"}, + {type:"", filly:1}, + {type:"", width:"3", height:"3", bgCol:"#FFF"} + ]},*/ + {type:"custom", width:"25", render:drawVario, id:"vario",filly:1 }, + {type:"", filly:1, width:1, bgCol:"#FFF"}, + {type:"v",fillx:1, c: [ + {type:"h", halign:1, c:[ + {type:"txt",font:"22%", halign:1, filly:1, label:"19999", id:"alt"}, + altUnit.layoutCode + ]}, {type:"", fillx:1, height:"1", bgCol:"#FFF"}, - {type:"txt", font:"18%", halign:1, filly:1, label:"-", id:"roc" }, + {type:"h", halign:1, c:[ + {type:"txt", font:"25%", halign:1, filly:1, label:"-99.9", id:"avg" }, + rocUnit.layoutCode + ]}, {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", halign:1, c:[ + {type:"txt", font:"25%", halign:1, filly:1, label:"XXX", id:"gs" }, + gsUnit.layoutCode + ]} ]} ]}, - {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" } + {type:"", fillx:1, height:"1", bgCol:"#FFF"}, + {type:"h",c: [ + {type:"txt",pad:"1", halign:0, font:"15%",fillx:"1", label:"99:99", id:"time"}, + {type:"", width:"1", height:g.getHeight()*0.15+2, bgCol:"#FFF"}, + {type:"txt",pad:"1", halign:0, font:"15%", fillx:"1", label:"99:99", id:"flyingtime" } ]} - ]},{lazy:"true"} + ]}//,{lazy:"true"} ); +pfd.update(); + Bangle.on('pressure', function(e) { if ((altFast)==-10000) { altFast=e.altitude; @@ -70,33 +157,47 @@ Bangle.on('GPS', function(fix) { setInterval(function () { altH.push(altSlow); - while (altH.length>intTime*1000/pressureInterval) altH.shift(); + if (altH.length>intTime*1000/pressureInterval) { + altH.shift(); + rocAvg=(altH[altH.length-1]-altH[0])/intTime; + } }, pressureInterval); +g.clear(); +pfd.render(); + setInterval(function() { + pfd.clear(pfd.alt); + pfd.clear(pfd.avg); + pfd.clear(pfd.time); + rocAvg=1*rocUnit.factor; if ((!flying) && ((rocAvg>1) || (rocAvg<-1) || (gs>10))) { //take-off detected takeoffTime=Date().getTime(); flying=true; - // flyingTime=0; } if (flying) { + pfd.clear(pfd.flyingtime); flyingTime=Date().getTime()-takeoffTime; 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.alt.label=(altSlow).toFixed(0); + if (rocAvg>0.1) pfd.avg.col="#0f0"; + else if (rocAvg<-1) pfd.avg.col="#f00"; + else pfd.avg.col="#fff"; + pfd.avg.label=(rocAvg*rocUnit.factor).toFixed(rocUnit.precision); + if (!isNaN(gs)) { + pfd.gs.label=gs.toFixed(0); + pfd.clear(pfd.gs); + } pfd.time.label=require("locale").time(Date(),1); - pfd.update(); - - pfd.render(); + updateText(pfd.alt); + updateText(pfd.avg); + updateText(pfd.gs); + updateText(pfd.time); + updateText(pfd.flyingtime); drawVario(); + // pfd.debug(pfd.roc); + //pfd.render(); }, 250); -g.clear();