<commit_before>// var tessel = require('tessel');

var util = require('util');
var EventEmitter = require('events').EventEmitter;
var nmea = require('nmea');
var Packetizer = require('./lib/packetizer');

var DEBUG = true;

var GPS = function (hardware, callback) {
  //set this.types for each type
  //type: fix, nav-info, 2waypoint, autopilot-b
  this.hardware = hardware;
  this.onOff = this.hardware.gpio(3);
  this.powerState = 'off';
  this.timeoutDuration = 10*1000;
  this.format = 'deg-min-dec';
  this.numSats = 0;

  // Turn the module on
  this.initialPowerSequence(function(err) {
    if (!err) {
      // Once we are on, emit the connected event
      this.beginDecoding(function() {

        this.on('fix', this.onFix.bind(this));

        this.on('coordinates', function() {
          console.log('Why can\'t I pick it up externally?');
        });

        setImmediate(function() {
          this.emit('ready');
          if (callback) {
            callback();
          }
        }.bind(this));
      }.bind(this));
    }
    else {
      setImmediate(function() {
        this.emit('error', err);
      }.bind(this));
    }
  }.bind(this));
};

util.inherits(GPS, EventEmitter);

GPS.prototype.initialPowerSequence = function (callback) {

  var self = this;
  // Tell Tessel to start UART at GPS baud rate
  this.uart = this.hardware.UART({baudrate : 115200});

  var noReceiveTimeout;

  function waitForData () {
    // If the leading byte is the header bit
    // Remove this listener
    self.removeListener('data', waitForData);

    clearTimeout(noReceiveTimeout);

    // And note that we are on
    self.powerState = 'on';

    self.uartExchange(callback);
  }

  function noDataRecieved() {
    // Remove the listener for any data
    self.removeListener('data', waitForData);
    // Clear the timeout 
    clearTimeout(noReceiveTimeout);
    // Call the callback
    if (callback) {
      callback(new Error('Unable to connect to module...'));
    }
  }

  /*
  Wait for a second to see if we get data. If we do, call callback;
  If we don't, toggle power and set timeout again
  If we still don't get it, something bigger is wrong
  */

  // This event listener will wait for valid data to note that we are on
  this.uart.once('data', waitForData);

  // Set the timeout to try to turn on if not already
  noReceiveTimeout = setTimeout(function alreadyOn() {
    // Try to turn on
    this.powerOn(function() {
      // If it's still not on
      if (DEBUG) {
        console.log('state after power cycle:', this.powerState);
      }
      if (this.powerState === 'off') {
        // Set the timeout once more
        if (DEBUG) {
          console.log('Trying once more...');
        }
        noReceiveTimeout = setTimeout(function() {
          // If we still didn't get anything, fail the connect
          noDataRecieved();
        }, 1000);
      }
    }.bind(this));
  }.bind(this), 1000);
};

// Try to turn the power on.
// The GPS holds toggle state but we can't sometimes we
// may have to toggle power twice. 
GPS.prototype.powerOn = function (callback) {
  this.power('on', function() {
    setImmediate(function() {
      this.emit('powerOn');
    }.bind(this));
    if (callback) {
      callback();
    }
  }.bind(this));
};

GPS.prototype.powerOff = function (callback) {
  this.power('off', function() {
    setImmediate(function() {
      this.emit('powerOff');
    }.bind(this));
    if (callback) {
      callback();
    }
  }.bind(this));
};

GPS.prototype.power = function(state, callback) {
  // We should switch from this state to passed in state
  var switchState = (state === 'on' ? 'off' : 'on');
  var self = this;

  // Pull power high
  if (this.powerState === switchState) {
    if (DEBUG) {
      console.log('high');
    }
    self.onOff.output().high();
    setTimeout(function backLow() {
      if (DEBUG) {
        console.log('low');
      }
      self.onOff.low();
      // Pull power low
      if (this.powerState === switchState) {
        // Set a timeout for it to have time to turn on and start sending
        setTimeout(callback, 500);
      }
    }.bind(this), 250);

  }
  else if (callback) {
    callback();
  }
};

// Eventually replace this with stream packetizing... sorry Kolker
GPS.prototype.beginDecoding = function(callback) {

  // Initializer our packetizer
  var packetizer = new Packetizer(this.uart);
  // Tell it to start packetizing
  packetizer.packetize();
  // When we get a packet
  packetizer.on('packet', function(packet) {
    if (DEBUG) {
      console.log('Got packet.');
    }
    // Make sure this is a valid packet
    if (packet[0] === '$') {
      // Parse it
      var datum = nmea.parse(packet);

      if (DEBUG) { console.log('Got Data: ', datum);}
      // If sucessful
      if (datum) {
        setImmediate(function() {<commit_msg>debug messages<commit_after>// var tessel = require('tessel');

var util = require('util');
var EventEmitter = require('events').EventEmitter;
var nmea = require('nmea');
var Packetizer = require('./lib/packetizer');

var DEBUG = true;

var GPS = function (hardware, callback) {
  //set this.types for each type
  //type: fix, nav-info, 2waypoint, autopilot-b
  this.hardware = hardware;
  this.onOff = this.hardware.gpio(3);
  this.powerState = 'off';
  this.timeoutDuration = 10*1000;
  this.format = 'deg-min-dec';
  this.numSats = 0;

  // Turn the module on
  this.initialPowerSequence(function(err) {
    if (!err) {
      // Once we are on, emit the connected event
      this.beginDecoding(function() {

        this.on('fix', this.onFix.bind(this));

        this.on('coordinates', function() {
          console.log('Why can\'t I pick it up externally?');
        });

        setImmediate(function() {
          this.emit('ready');
          if (callback) {
            callback();
          }
        }.bind(this));
      }.bind(this));
    }
    else {
      setImmediate(function() {
        this.emit('error', err);
      }.bind(this));
    }
  }.bind(this));
};

util.inherits(GPS, EventEmitter);

GPS.prototype.initialPowerSequence = function (callback) {

  var self = this;
  // Tell Tessel to start UART at GPS baud rate
  this.uart = this.hardware.UART({baudrate : 115200});

  var noReceiveTimeout;

  function waitForData () {
    // If the leading byte is the header bit
    // Remove this listener
    self.removeListener('data', waitForData);

    clearTimeout(noReceiveTimeout);

    // And note that we are on
    self.powerState = 'on';

    self.uartExchange(callback);
  }

  function noDataRecieved() {
    // Remove the listener for any data
    self.removeListener('data', waitForData);
    // Clear the timeout 
    clearTimeout(noReceiveTimeout);
    // Call the callback
    if (callback) {
      callback(new Error('Unable to connect to module...'));
    }
  }

  /*
  Wait for a second to see if we get data. If we do, call callback;
  If we don't, toggle power and set timeout again
  If we still don't get it, something bigger is wrong
  */

  // This event listener will wait for valid data to note that we are on
  this.uart.once('data', waitForData);

  // Set the timeout to try to turn on if not already
  noReceiveTimeout = setTimeout(function alreadyOn() {
    // Try to turn on
    this.powerOn(function() {
      // If it's still not on
      if (DEBUG) {
        console.log('state after power cycle:', this.powerState);
      }
      if (this.powerState === 'off') {
        // Set the timeout once more
        if (DEBUG) {
          console.log('Trying once more...');
        }
        noReceiveTimeout = setTimeout(function() {
          // If we still didn't get anything, fail the connect
          noDataRecieved();
        }, 1000);
      }
    }.bind(this));
  }.bind(this), 1000);
};

// Try to turn the power on.
// The GPS holds toggle state but we can't sometimes we
// may have to toggle power twice. 
GPS.prototype.powerOn = function (callback) {
  this.power('on', function() {
    setImmediate(function() {
      this.emit('powerOn');
    }.bind(this));
    if (callback) {
      callback();
    }
  }.bind(this));
};

GPS.prototype.powerOff = function (callback) {
  this.power('off', function() {
    setImmediate(function() {
      this.emit('powerOff');
    }.bind(this));
    if (callback) {
      callback();
    }
  }.bind(this));
};

GPS.prototype.power = function(state, callback) {
  // We should switch from this state to passed in state
  var switchState = (state === 'on' ? 'off' : 'on');
  var self = this;

  // Pull power high
  if (this.powerState === switchState) {
    if (DEBUG) {
      console.log('high');
    }
    self.onOff.output().high();
    setTimeout(function backLow() {
      if (DEBUG) {
        console.log('low');
      }
      self.onOff.low();
      // Pull power low
      if (this.powerState === switchState) {
        // Set a timeout for it to have time to turn on and start sending
        setTimeout(callback, 500);
      }
    }.bind(this), 250);

  }
  else if (callback) {
    callback();
  }
};

// Eventually replace this with stream packetizing... sorry Kolker
GPS.prototype.beginDecoding = function(callback) {

  // Initializer our packetizer
  var packetizer = new Packetizer(this.uart);
  // Tell it to start packetizing
  packetizer.packetize();
  // When we get a packet
  packetizer.on('packet', function(packet) {
    if (DEBUG) {
      // console.log('Got packet.');
      console.log('  Packet\t', packet);
    }
    // Make sure this is a valid packet
    if (packet[0] === '$') {
      // Parse it
      var datum = nmea.parse(packet);

      if (DEBUG) { 
        console.log('    Got Data:');
        Object.keys(datum).forEach(function (key) {
          console.log('     ', key, '\n       ', datum[key]);
          });
        console.log();
      }
      // If sucessful
      if (datum) {
        setImmediate(function() {
{'fraction_non_alphanumeric': 0.14089624545821558, 'fraction_numerical': 0.006055712555510699, 'mean_word_length': 2.446608695652174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '840140', 'n_tokens_mistral': 3141, 'n_tokens_neox': 3001, 'n_words': 887}