diff --git a/apps/gpssetup/ChangeLog b/apps/gpssetup/ChangeLog index e57d53d8ee..6a0c028392 100644 --- a/apps/gpssetup/ChangeLog +++ b/apps/gpssetup/ChangeLog @@ -1,2 +1,3 @@ 0.01: First version of GPS Setup app 0.02: Created gppsetup module +0.03: Added support for Bangle.js2 diff --git a/apps/gpssetup/README.md b/apps/gpssetup/README.md index 8c9445ec99..f1fa230908 100644 --- a/apps/gpssetup/README.md +++ b/apps/gpssetup/README.md @@ -48,13 +48,17 @@ used. These settings will remain for all apps that use the GPS. the interval the more time the GPS will spend sleeping in low power mode (7mA) between obtaining fixes (35mA). For walking in open country an update once every 60 seconds is adequate to put - you within a 6 digit grid refernce sqaure. + you within a 6 digit grid refernce sqaure. + + **Note:** For the Bangle.js2, the GPS module does not have a PSMOO mode, and thus this is emulated using on/off timeouts specified using the update and search options. - update - the time between two position fix attempts. - search - the time between two acquisition attempts if the receiver is unable to get a position fix. +- fix_req (Bangle.js2 only) - the number of fixes required before the GPS turns off until next search for GPS signal. default is 1. + ## Module A module is provided that'll allow you to set GPS configuration from your own diff --git a/apps/gpssetup/app.js b/apps/gpssetup/app.js index e0d188af56..966000681b 100644 --- a/apps/gpssetup/app.js +++ b/apps/gpssetup/app.js @@ -34,6 +34,7 @@ function loadSettings() { settings = require("Storage").readJSON(SETTINGS_FILE,1)||{}; settings.update = settings.update||120; settings.search = settings.search||5; + settings.fix_req = settings.fix_req||1; settings.power_mode = settings.power_mode||"SuperE"; log_debug(settings); } @@ -85,6 +86,16 @@ function showMainMenu() { settings.search = v; updateSettings(); } + }, + 'Fix Req (#)': { + value: settings.fix_req, + min: 1, + max: 100, + step: 1, + onchange: v => { + settings.fix_req = v; + updateSettings(); + } } }; diff --git a/apps/gpssetup/gpssetup.js b/apps/gpssetup/gpssetup.js index f8fed68ff5..b56bd50cf3 100644 --- a/apps/gpssetup/gpssetup.js +++ b/apps/gpssetup/gpssetup.js @@ -1,5 +1,5 @@ const SETTINGS_FILE = "gpssetup.settings.json"; - +const BANGLE_VER = process.env.HWVERSION; //BangleJS2 support function log_debug(o) { //let timestamp = new Date().getTime(); //console.log(timestamp + " : " + o); @@ -106,49 +106,97 @@ function delay(ms) { function setupSuperE() { log_debug("setupGPS() Super-E"); - return Promise.resolve().then(function() { - UBX_CFG_RESET(); - return delay(100); - }).then(function() { - UBX_CFG_PMS(); - return delay(20); - }).then(function() { - UBX_CFG_SAVE(); - return delay(20); - }).then(function() { - log_debug("Powering GPS Off"); - /* - * must be part of the promise chain to ensure that - * setup does not return and powerOff before config functions - * have run - */ - return delay(20); - }); + switch(BANGLE_VER){ + case(1): { + return Promise.resolve().then(function() { + UBX_CFG_RESET(); + return delay(100); + }).then(function() { + UBX_CFG_PMS(); + return delay(20); + }).then(function() { + UBX_CFG_SAVE(); + return delay(20); + }).then(function() { + log_debug("Powering GPS Off"); + /* + * must be part of the promise chain to ensure that + * setup does not return and powerOff before config functions + * have run + */ + return delay(20); + }); + } + case(2):{ + //nothing more to do. + return; + } + } + } function setupPSMOO(settings) { log_debug("setupGPS() PSMOO"); - return Promise.resolve().then(function() { - UBX_CFG_RESET(); - return delay(100); - }).then(function() { - UBX_CFG_PM2(settings.update, settings.search); - return delay(20); - }).then(function() { - UBX_CFG_RXM(); - return delay(20); - }).then(function() { - UBX_CFG_SAVE(); - return delay(20); - }).then(function() { - log_debug("Powering GPS Off"); - /* - * must be part of the promise chain to ensure that - * setup does not return and powerOff before config functions - * have run - */ - return delay(20); - }); + switch(BANGLE_VER){ + case(1):{ + return Promise.resolve().then(function() { + UBX_CFG_RESET(); + return delay(100); + }).then(function() { + UBX_CFG_PM2(settings.update, settings.search); + return delay(20); + }).then(function() { + UBX_CFG_RXM(); + return delay(20); + }).then(function() { + UBX_CFG_SAVE(); + return delay(20); + }).then(function() { + log_debug("Powering GPS Off"); + /* + * must be part of the promise chain to ensure that + * setup does not return and powerOff before config functions + * have run + */ + return delay(20); + }); + } + case(2): { + var gpsTimeout = null; + var gpsActive = false; + var fix = 0; + function cb(f){ + if(parseInt(f.fix) === 1){ + fix++; + if(fix >= settings.fix_req){ + fix = 0; + turnOffGPS(); + } + } + } + function turnOffGPS() { + if (!gpsActive) return; + gpsActive = false; + clearTimeout(gpsTimeout); + Bangle.setGPSPower(0,settings.appName); + Bangle.removeListener('GPS', cb); // cleaning it up + gpsTimeout = setTimeout(() => { + turnOnGPS(); + }, settings.update * 1000); + } + function turnOnGPS(){ + if (gpsActive) return; + if(!Bangle.isGPSOn()) Bangle.setGPSPower(1,settings.appName); + Bangle.on('GPS',cb); + gpsActive = true; + gpsTimeout = setTimeout(() => { + turnOffGPS(); + }, settings.search * 1000); + } + turnOnGPS(); + break; + } + } } /** Set GPS power mode (assumes GPS on), returns a promise. @@ -161,16 +209,21 @@ require("gpssetup").setPowerMode({power_mode:"SuperE"}) // <-- Super E mode See the README for more information */ exports.setPowerMode = function(options) { - settings = require("Storage").readJSON(SETTINGS_FILE,1)||{}; + var settings = require("Storage").readJSON(SETTINGS_FILE,1)||{}; if (options) { if (options.update) settings.update = options.update; if (options.search) settings.search = options.search; + if (options.fix_req) settings.fix_req = options.fix_req; if (options.power_mode) settings.power_mode = options.power_mode; + if (options.appName) settings.appName = options.appName; } settings.update = settings.update||120; settings.search = settings.search||5; + settings.fix_req = settings.fix_req||1; //default to just one fix and will turn off settings.power_mode = settings.power_mode||"SuperE"; + settings.appName = settings.appName || "gpssetup"; if (options) require("Storage").write(SETTINGS_FILE, settings); + if(!Bangle.isGPSOn()) Bangle.setGPSPower(1,settings.appName); //always know its on - no point calling this otherwise!!! if (settings.power_mode === "PSMOO") { return setupPSMOO(settings); } else { diff --git a/apps/gpssetup/metadata.json b/apps/gpssetup/metadata.json index b8b6dfc238..ffe8d3fd88 100644 --- a/apps/gpssetup/metadata.json +++ b/apps/gpssetup/metadata.json @@ -2,11 +2,11 @@ "id": "gpssetup", "name": "GPS Setup", "shortName": "GPS Setup", - "version": "0.02", + "version": "0.03", "description": "Configure the GPS power options and store them in the GPS nvram", "icon": "gpssetup.png", "tags": "gps,tools,outdoors", - "supports": ["BANGLEJS"], + "supports": ["BANGLEJS","BANGLEJS2"], "readme": "README.md", "storage": [ {"name":"gpssetup","url":"gpssetup.js"},