Skip to content

Commit

Permalink
Merge pull request espruino#1100 from nujw/master
Browse files Browse the repository at this point in the history
speedalt2 updates
  • Loading branch information
gfwilliams authored Dec 16, 2021
2 parents 0745fef + 1bd8e5a commit 987a521
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 90 deletions.
4 changes: 2 additions & 2 deletions apps.json
Original file line number Diff line number Diff line change
Expand Up @@ -3478,8 +3478,8 @@
{
"id": "speedalt2",
"name": "GPS Adventure Sports II",
"shortName": "GPS Adv Sport II",
"version": "0.07",
"shortName":"GPS Adv Sport II",
"version":"1.10",
"description": "GPS speed, altitude and distance to waypoint display. Designed for easy viewing and use during outdoor activities such as para-gliding, hang-gliding, sailing, cycling etc.",
"icon": "app.png",
"type": "app",
Expand Down
1 change: 1 addition & 0 deletions apps/speedalt2/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
0.01: Initial import.
0.07: Add swipe to change screens.
1.06: Misc memory and screen optimisations.
167 changes: 80 additions & 87 deletions apps/speedalt2/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*
Speed and Altitude [speedalt2]
Mike Bennett mike[at]kereru.com
0.01 : Initial
0.06 : Add Posn screen
0.07 : Add swipe to change screens same as BTN3
1.10 : add inverted colours
*/
var v = '1.05';
var v = '1.10';

/*kalmanjs, Wouter Bulten, MIT, https://github.com/wouterbulten/kalmanjs */
var KalmanFilter = (function () {
Expand Down Expand Up @@ -173,6 +171,15 @@ var KalmanFilter = (function () {

var buf = Graphics.createArrayBuffer(240,160,2,{msb:true});

let LED = // LED as minimal and only definition (as instance / singleton)
{ isOn: false // status on / off, not needed if you don't need to ask for it
, set: function(v) { // turn on w/ no arg or truey, else off
g.setColor((this.isOn=(v===undefined||!!v))?1:0,0,0).fillCircle(40,10,10); }
, reset: function() { this.set(false); } // turn off
, write: function(v) { this.set(v); } // turn on w/ no arg or truey, else off
, toggle: function() { this.set( ! this.isOn); } // toggle the LED
}, LED1 = LED; // LED1 as 'synonym' for LED

// Load fonts
//require("Font7x11Numeric7Seg").add(Graphics);

Expand All @@ -183,17 +190,16 @@ var canDraw = 1;
var time = ''; // Last time string displayed. Re displayed in background colour to remove before drawing new time.
var tmrLP; // Timer for delay in switching to low power after screen turns off

var max = {};
max.spd = 0;
max.alt = 0;
max.n = 0; // counter. Only start comparing for max after a certain number of fixes to allow kalman filter to have smoohed the data.
var maxSpd = 0;
var maxAlt = 0;
var maxN = 0; // counter. Only start comparing for max after a certain number of fixes to allow kalman filter to have smoohed the data.

var emulator = (process.env.BOARD=="EMSCRIPTEN")?1:0; // 1 = running in emulator. Supplies test values;

var wp = {}; // Waypoint to use for distance from cur position.

function nxtWp(inc){
cfg.wp+=inc;
function nxtWp(){
cfg.wp++;
loadWp();
}

Expand Down Expand Up @@ -227,7 +233,8 @@ function drawScrn(dat) {
if (!canDraw) return;

buf.clear();

buf.setBgColor(0);

var n;
n = dat.val.toString();

Expand All @@ -252,29 +259,21 @@ function drawScrn(dat) {
buf.setFontVector(35);
buf.drawString(dat.unit,5,164);

if ( dat.max ) drawMax(); // MAX display indicator
if ( dat.wp ) drawWP(); // Waypoint name

//Sats
if ( dat.sat ) {
if ( dat.age > 10 ) {
if ( dat.age > 90 ) dat.age = '>90';
drawSats('Age:'+dat.age);
}
else drawSats('Sats:'+dat.sats);
}

drawMax(dat.max); // MAX display indicator
drawWP(dat.wp); // Waypoint name
drawSats(dat.sats);

g.reset();
g.drawImage(img,0,40);

if ( pwrSav ) LED1.reset();
else LED1.set();
LED1.write(!pwrSav);

}

function drawPosn(dat) {
if (!canDraw) return;
buf.clear();
buf.setBgColor(0);

var x, y;
x=210;
Expand All @@ -293,27 +292,21 @@ function drawPosn(dat) {
buf.drawString(dat.ew,x,y+70);


//Sats
if ( dat.sat ) {
if ( dat.age > 10 ) {
if ( dat.age > 90 ) dat.age = '>90';
drawSats('Age:'+dat.age);
}
else drawSats('Sats:'+dat.sats);
}
drawSats(dat.sats);

g.reset();
g.drawImage(img,0,40);

if ( pwrSav ) LED1.reset();
else LED1.set();
LED1.write(!pwrSav);

}

function drawClock() {
if (!canDraw) return;

buf.clear();
buf.setBgColor(0);

var x, y;
x=185;
y=0;
Expand All @@ -329,19 +322,14 @@ function drawClock() {
g.reset();
g.drawImage(img,0,40);

if ( pwrSav ) LED1.reset();
else LED1.set();
LED1.write(!pwrSav);
}

function drawWP() {
var nm = wp.name;
if ( nm == undefined || nm == 'NONE' || cfg.modeA ==1 ) nm = '';
buf.setColor(2);

function drawWP(wp) {
buf.setColor(3);
buf.setFontAlign(0,1); //left, bottom
buf.setFontVector(48);
buf.drawString(nm.substring(0,8),120,140);

buf.drawString(wp,120,140);
}

function drawSats(sats) {
Expand All @@ -351,16 +339,15 @@ function drawSats(sats) {
buf.drawString(sats,240,160);
}

function drawMax() {
function drawMax(max) {
buf.setFontVector(30);
buf.setColor(2);
buf.setFontAlign(0,1); //centre, bottom
buf.drawString('MAX',120,164);
buf.drawString(max,120,164);
}

function onGPS(fix) {

if ( emulator ) {
if ( emulator ) {
fix.fix = 1;
fix.speed = 10 + (Math.random()*5);
fix.alt = 354 + (Math.random()*50);
Expand All @@ -382,21 +369,25 @@ function onGPS(fix) {
var ns = '';
var ew = '';
var lon = '---.--';
var sats = '---';

// Waypoint name
var wpName = wp.name;
if ( wpName == undefined || wpName == 'NONE' ) wpName = '';
wpName = wpName.substring(0,8);

if (fix.fix) lf = fix;

if (lf.fix) {

// Smooth data
if ( lf.smoothed !== 1 ) {
if ( cfg.spdFilt ) lf.speed = spdFilter.filter(lf.speed);
if ( cfg.altFilt ) lf.alt = altFilter.filter(lf.alt);
lf.smoothed = 1;
if ( max.n <= 15 ) max.n++;
if ( maxN <= 15 ) maxN++;
}


// Speed
if ( cfg.spd == 0 ) {
m = require("locale").speed(lf.speed).match(/([0-9,\.]+)(.*)/); // regex splits numbers from units
Expand All @@ -407,18 +398,19 @@ function onGPS(fix) {

if ( sp < 10 ) sp = sp.toFixed(1);
else sp = Math.round(sp);
if (isNaN(sp)) sp = '---';

if (parseFloat(sp) > parseFloat(max.spd) && max.n > 15 ) max.spd = sp;
if (parseFloat(sp) > parseFloat(maxSpd) && maxN > 15 ) maxSpd = sp;

// Altitude
al = lf.alt;
al = Math.round(parseFloat(al)/parseFloat(cfg.alt));

if (parseFloat(al) > parseFloat(max.alt) && max.n > 15 ) max.alt = al;
if (parseFloat(al) > parseFloat(maxAlt) && maxN > 15 ) maxAlt = al;
if (isNaN(al)) al = '---';

// Distance to waypoint
di = distance(lf,wp);
if (isNaN(di)) di = 0;
if (isNaN(di)) di = '--------';

// Age of last fix (secs)
age = Math.max(0,Math.round(getTime())-(lf.time.getTime()/1000));
Expand All @@ -431,54 +423,57 @@ function onGPS(fix) {
ew = 'E';
if ( lf.lon < 0 ) ew = 'W';
lon = Math.abs(lf.lon.toFixed(2));

// Sats
if ( age > 10 ) {
sats = 'Age:'+Math.round(age);
if ( age > 90 ) sats = 'Age:>90';
}
else sats = 'Sats:'+lf.satellites;

}

if ( cfg.modeA == 0 ) {
// Speed
if ( showMax )
drawScrn({
val:max.spd,
val:maxSpd,
unit:cfg.spd_unit,
sats:lf.satellites,
sats:sats,
age:age,
max:true,
wp:false,
sat:true
max:'MAX',
wp:''
}); // Speed maximums
else
drawScrn({
val:sp,
unit:cfg.spd_unit,
sats:lf.satellites,
sats:sats,
age:age,
max:false,
wp:false,
sat:true
max:'SPD',
wp:''
});
}

if ( cfg.modeA == 1 ) {
// Alt
if ( showMax )
drawScrn({
val:max.alt,
val:maxAlt,
unit:cfg.alt_unit,
sats:lf.satellites,
sats:sats,
age:age,
max:true,
wp:false,
sat:true
max:'MAX',
wp:''
}); // Alt maximums
else
drawScrn({
val:al,
unit:cfg.alt_unit,
sats:lf.satellites,
sats:sats,
age:age,
max:false,
wp:false,
sat:true
max:'ALT',
wp:''
});
}

Expand All @@ -487,24 +482,22 @@ function onGPS(fix) {
drawScrn({
val:di,
unit:cfg.dist_unit,
sats:lf.satellites,
sats:sats,
age:age,
max:false,
wp:true,
sat:true
max:'DST',
wp:wpName
});
}

if ( cfg.modeA == 3 ) {
// Position
drawPosn({
sats:lf.satellites,
drawPosn({
sats:sats,
age:age,
lat:lat,
lon:lon,
ns:ns,
ew:ew,
sat:true
ew:ew
});
}

Expand Down Expand Up @@ -534,9 +527,9 @@ function nextFunc(dur) {
if ( cfg.modeA == 0 || cfg.modeA == 1 ) {
// Spd+Alt mode - Switch between fix and MAX
if ( dur < 2 ) showMax = !showMax; // Short press toggle fix/max display
else { max.spd = 0; max.alt = 0; } // Long press resets max values.
else { maxSpd = 0; maxAlt = 0; } // Long press resets max values.
}
else if ( cfg.modeA == 2) nxtWp(1); // Dist mode - Select next waypoint
else if ( cfg.modeA == 2) nxtWp(); // Dist mode - Select next waypoint
onGPS(lf);
}

Expand All @@ -545,7 +538,7 @@ function updateClock() {
if (!canDraw) return;
if ( cfg.modeA != 4 ) return;
drawClock();
if ( emulator ) {max.spd++;max.alt++;}
if ( emulator ) {maxSpd++;maxAlt++;}
}

function startDraw(){
Expand Down Expand Up @@ -585,16 +578,15 @@ function setButtons(){
setWatch(function(e){
pwrSav=!pwrSav;
if ( pwrSav ) {
LED1.reset();
var s = require('Storage').readJSON('setting.json',1)||{};
var t = s.timeout||10;
Bangle.setLCDTimeout(t);
}
else {
Bangle.setLCDTimeout(0);
// Bangle.setLCDPower(1);
LED1.set();
}
LED1.write(!pwrSav);
}, BTN2, {repeat:true,edge:"falling"});

// BTN3 - next screen
Expand Down Expand Up @@ -690,7 +682,8 @@ var img = {
};

if ( cfg.colour == 1 ) img.palette = new Uint16Array([0,0xFFFF,0xFFF6,0xDFFF]);
if ( cfg.colour == 2 ) img.palette = new Uint16Array([0,0xFF800,0xFAE0,0xF813]);
if ( cfg.colour == 2 ) img.palette = new Uint16Array([0,0xF800,0xFAE0,0xF813]);
if ( cfg.colour == 3 ) img.palette = new Uint16Array([0xFFFF,0x007F,0x0054,0x0054]);

var SCREENACCESS = {
withApp:true,
Expand Down
Loading

0 comments on commit 987a521

Please sign in to comment.