You are reading a single comment by @HughB and its replies. Click here to read the full conversation.
  • 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
About

Avatar for HughB @HughB started