GPS clock_info - a few questions

Posted on
  • I'm working on a GPS clock_info. But have a few questions as I am not sure I have got the code right below.

    GOAL: I am buiding a simple GPS clock_info that will show the OS map grid coordinates for use when walking. When you swipe to the gps clock info it turns on the GPS. The first time it gets a fix display the time value from the fix.time from the GPS. Once a fix is established turn off the GPS and then start a timer to turn it back on 3 minutes (I will probably choose 90 seconds later on). The goal is to be able to get a GPS fix every 3 mins or so. This is good enough when walking. As long as you have downloaded AGPS the time to first fix is a few minutes. After that it takes about 20 seconds from power to next fix providing the distance travelled is small (which it will be when on foot). In this way the battery drain of turning on the GPS is reduced to 20 seconds every 3 minutes. I have a library module that I made for GPStouch that will do the lat/lon to OS grid conversion. For now I am just displaying the lat/lon.

    My code is below.

    Problem is that while onGPS() is getting called the info.emit('redraw'); does not seem to result in a redraw. The only way to get an update is to tap the screen again, then it will update.

    Would appreciate any feedback on the code as stuff with timers like this can go wrong.

    (function () {
      var timeout;
      var last_fix;
    
      var resetLastFix = function() {
        last_fix = {
          fix: 0,
          alt: 0,
          lat: 0,
          lon: 0,
          speed: 0,
          time: 0,
          course: 0,
          satellites: 0
        };
      };
      
      var formatTime = function(now) {
        try {
          var fd = now.toUTCString().split(" ");
          return fd[4];
        } catch (e) {
          return "00:00:00";
        }
      };
    
      var onGPS = function(fix) {
        console.log(fix);
        last_fix.time = fix.time;
    
        // we got a fix
        if (fix.fix) {
          last_fix = fix;
          // cancel the timeout, if not already timed out
          if (this.timeout) {
    	clearTimeout(timeout);
    	this.timeout = undefined;
          }
          // power off the GPS
          Bangle.setGPSPower(0,"clkinfo");
          // power on the GPS again in 3 minutes
          timeout = setTimeout(function() {
    	timeout = undefined;
    	Bangle.setGPSPower(1,"clkinfo");
          }, 180000);
        }
        info.emit("redraw");
      };
    
      var gpsText = function() {
        if (last_fix === undefined)
          return '----- , -----';
    
        if (!last_fix.fix) 
          return formatTime(last_fix.time);
        
        // use basic lat,lon for now
        return last_fix.lat.toFixed(3) + ' , ' + last_fix.lon.toFixed(3);
      };
      
      var info = {
        name: "Gps",
        items: [
          {
            name: "gridref",
            get: function () { return ({
              text: gpsText()
            }); },
    	run : function() {
              Bangle.setGPSPower(1,"clkinfo");
              /* turn off after 5 minutes, sooner if we get a fix */
              this.timeout = setTimeout(function() {
                this.timeout = undefined;
                Bangle.setGPSPower(0,"clkinfo");
              }, 300000);
    	},
            show: function () {
              resetLastFix();
    	  Bangle.on("GPS",onGPS);
    	  this.run();
            },
            hide: function() {
              Bangle.setGPSPower(0,"clkinfo");
              Bangle.removeListener("GPS", onGPS);
              if (this.timeout) {
                clearTimeout(this.timeout);
                this.timeout = undefined;
              }
              resetLastFix();
            }
          }
        ]
      };
    
      return info;
    });
    

    1 Attachment

    • download.png
  • Looks good, although I think you ought to provide an icon - IIRC most clocks expect one.

    I think the issue is just that you're calling info.emit("redraw"); on info, but that is a list of one or more clockinfos. Just doing info.items[0].emit("redraw"); should sort you out.

  • Thanks for that, that worked.

    I have been testing the next iteration.
    I have noticed that after I have hada fix that after swiping out of the clock_info back to say the battery display that the GPS gets powered on again.

    My question is:

      var timeout;   // is this the same variable as
    
            hide: function() {
              Bangle.setGPSPower(0,"clkinfo");
              Bangle.removeListener("GPS", onGPS);
              if (this.timeout) {
                clearTimeout(this.timeout);  /// <<<<  as the variable here ?
                this.timeout = undefined;
              }
              resetLastFix();
            }
    

    I'm not 100% sure I understand how this.timeout gets defined in code at:

    https://github.com/espruino/BangleApps/b­lob/master/apps/clock_info/lib.js
    line 83

          hide : function() {
            Bangle.setHRMPower(0,"clkinfo");
            Bangle.removeListener("HRM", hrmUpdateHandler);
            if (this.timeout) {
              clearTimeout(this.timeout);
              this.timeout = undefined;   //////  where does timeout get declared, just seems to spring into existance
            }
            hrm = 0;
          },
        }
    
    
  • is this var timeout the same variable as this.timeout

    Nope :)

    I'm not 100% sure I understand how this.timeout gets defined in code at:

    It's defined in .run just a few lines earlier: https://github.com/espruino/BangleApps/b­lob/master/apps/clock_info/lib.js#L83

    run : function() {
            Bangle.setHRMPower(1,"clkinfo");
            if (settings.hrmOn==1/*Tap*/) {
              /* turn off after 1 minute. If Health HRM monitoring is
              enabled we will still get HRM events every so often */
              this.timeout = setTimeout(function() {
                this.timeout = undefined;
                Bangle.setHRMPower(0,"clkinfo");
              }, 60000);
            }
          },
    
  • Nope!

    Thought so. Will fix to be consistent

    My App is available for test at:
    https://hughbarney.github.io/BangleApps/­?id=clkinfogps

    Latest code at:
    https://github.com/hughbarney/BangleApps­/tree/master/apps/clkinfogps

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

GPS clock_info - a few questions

Posted by Avatar for HughB @HughB

Actions