class SerialPortGPS

Attributes

latitude[R]
longitude[R]
time[R]
to_h[R]

Public Class Methods

new(port: "/dev/ttyAMA0", baud_rate: 9600, refresh_rate: 8, callback: ->(gps){ puts gps.to_h.inspect} click to toggle source
# File lib/serialport_gps.rb, line 14
def initialize(port: "/dev/ttyAMA0", baud_rate: 9600, 
              refresh_rate: 8, callback: ->(gps){ puts gps.to_h.inspect})


  # if the refresh rate is any less than 8 seconds the serial connection
  # will have a higher probability of containing corrupted data

  refresh_rate ||= 8
  refresh_rate = 8 if refresh_rate < 8
  @refresh_rate = refresh_rate - 4
  @callback = callback

  #params for serial port
  @port, @baud_rate, @data_bits, @stop_bits, @parity = port, baud_rate, 
                                                       8, 1, SerialPort::NONE
  @np = NMEAParser.new
end

Public Instance Methods

start() click to toggle source
# File lib/serialport_gps.rb, line 32
def start

  @running = true

  RunEvery.new(seconds: @refresh_rate, thread_name: 'GPS listener') do 

    Thread.stop unless @running

    begin

      sp = SerialPort.new(@port, @baud_rate, @data_bits, @stop_bits, @parity)
      sleep 4
      s = ''

      (s = sp.gets.scrub; @np.parse(s)) until s =~ /^\$GPGGA/
      sp.close

    rescue
      puts 'warning: ' + ($!).inspect
      retry
    end

    @time, @latitude, @longitude, @to_h = @np.time, @np.latitude, 
                                                      @np.longitude, @np.to_h
    @callback.call @np.to_struct
  end
end
stop() click to toggle source
# File lib/serialport_gps.rb, line 60
def stop()
  @running = false
end