You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • For Espruino module, that is what you get: a

    The module is at http://www.espruino.com/modules/GPS.js .

    I list it in this thrad for discussion purpose. This source is what I cloned and enhanced for my purposes. You can to that easily for yourself as well.

    This are the steps:

    1. In Web IDE go to Setting (Cogwheel) - Projects
    2. Select a Directory (you made on your system) for Sandbox
    3. Copy the module code into the IDE editor (right side pane)
    4. Save the moduel under GPS_JB.js in the folder /modules
    5. Load your application code into the IDE editor
    6. Change the Require to to "GPS_JB.js"
    7. When you got it running to the point it runs now, you are ready to modify the moduel - either in the IDE editor - or what ever multi-document editor you are used to.
    8. Everytime you upload your application code to the board, it does not find
      the module in the official place (or you are not connected), it goes after your sandbox module folder and tries to load / loads it from there
    9. For now disable minification - because if you do iteratively - you may get flagged by the free Google closure compiler service that's invoked every time you upload and you are stuck until you change to no minification or the 'flag wears down' (may be that is not the case anymore... I do not have latest status from @Gorden about built in minifier / closure compiler / etc. But it does not hurt you yet, since you do not have much code with memory nor performance challenges).

    Now to the module code:

    /* Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. */
    /*
    Module for interfacing with serial (NMEA) GPS devices
    
    Serial4.setup(9600,{tx:C10,rx:C11});
    var gps = connect(Serial4, function(data) {
      console.log(data);
    });
    
    */
    function handleGPSLine(line, callback) {
      var tag = line.substr(3,3);
      if (tag=="GGA") {
        var d = line.split(",");
        var dlat = d[2].indexOf(".");
        var dlon = d[4].indexOf(".");
        callback(
          { time: d[1].substr(0,2) + ":"+d[1].substr(2,2)+":"
                 + d[1].substr(4,2)
          , lat: (parseInt(d[2].substr(0,dlat-2),10)
                 + parseFloat(d[2].substr(dlat-2))/60)*(d[3­]=="S"?-1:1)
          , lon: (parseInt(d[4].substr(0,dlon-2),10)
                 + parseFloat(d[4].substr(dlon-2))/60)*(d[5­]=="W"?-1:1)
          , fix: parseInt(d[6],10)
          , satellites: parseInt(d[7],10)
          , altitude: parseFloat(d[9])
        });
      }
    }
    
    exports.connect = function(serial, callback) {
      var gps = {line:""};
      serial.on('data', function(data) {
        gps.line += data;
        var idx = gps.line.indexOf("\n");
        while (idx>=0) {
          var line = gps.line.substr(0, idx);
          gps.line = gps.line.substr(idx+1);
          handleGPSLine(line, callback); 
          idx = gps.line.indexOf("\n");     
        }
        if (gps.line.length > 80)
          gps.line = gps.line.substr(-80);
      });
      return gps;
    }
    

    When you say require("GPS") in your code, you get an object back that understands .connect(), where you pass the serial and your application function. The application function has to accept a prameter object, which is the object crated from the GPS data in the GPS module by the module only line handler in lines 18 through 26.

    Unfortunately the way the module is built and without changing it/that, it does you not give access to change this handler with something of your's that would be more powerful. The only thing accessible to you in the object reteurned from .connect() is an object with one property, and that is the data recieved but not processed yet by the handler.

    When the moudule is changed so that the module's integrated handler is put into thet returned gps object as a "handler:" property (in line 31) and then referenced in line 38, you could put just any other linehandler in there right afert connect.

    You have serveral options of remedy:

    First option:

    • in your cloned GPS_JB module:

      1. make line 31: var gps = {line:"", handle:handleGPSLine}
      2. make line 38: ```gps.handle(line,callback);

    • in your application:

      Serial1.setup(9600, {tx: B6, rx: B7});
      var gps = require('GPS_JB').connect(Serial1, function(data){
      console.log(data);
      });
      gps.handle = function(line,callback) {
      // your line handler processing creating data object
      var data = { ... };
      callback(data);
      };  
      

    Second (preferred option) {

    • in your cloned GPS_JB module:

      1. make line 30:

        2. make line 31:
        

        var gps = {line:"", handle:(handle) ? handle : handleGPSLine }```

      2. make line 38:

        
        - in your appliacation:
        
        

    Serial1.setup(9600, {tx: B6, rx: B7});
    var gps = require('GPS_JB').connect( Serial1,
    function(data){

    console.log(data);
    

    },
    function(line,callback){

    // your line handler processing creating data object
    var data = { ... };
    callback(data);
    

    } );

    
    For the (anonymously) provided handle *function(line,callback)*  you can do something like that:
    
    

    function(line,callback){

      // your line handler processing create data object
      var tag = line.substr(3,3), data;
      switch (tag) {
         case "GGA":
            data = {.... };
            break;
         case "XYZ":
            data = {.... };
            break;
      }
      if {data) {callback(data); }
    

    }
    ```

    Make sure your line handler as well as your applicaiton callback processing happens within the timeframe the next data come...

About

Avatar for allObjects @allObjects started