Class: MmGPS::Beacon

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mm_gps/mm_gps_beacon.rb

Overview

Main interface. Represents a connection to a MarvelMind beacon/hedgehog. You may want (and can) to have more than one instance.

Examples:

Typical usage:

beacon = MmGPS::Beacon.new(PORT, baud: BAUD)
beacon.trap # installs signal handler for CTRL-C

# Standard each loop. Type CTRL-C for interrupting it
beacon.each do |packet, raw|
  p packet
  puts MmGPS::hexify(raw)
end

Using the enumerator:

# Use the enumerator:
beacon.reopen      # Needed, since CTRL-C in previous example also closes the Serialport connection
enum = beacon.each # gets the Enumerator
p enum.take 10     # next 10 packets from enum

Constant Summary collapse

START_TOKEN =
"\xFFG"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, baud: 115200) ⇒ Beacon

Open a new connection on the given serial port. It also encapsulates the underlying SerialPort object instance, setting a read timeout of 1 s and enabling the binary mode.

Parameters:

  • port (String)

    ‘COM1’ on Windows, ‘/dev/ttyNNN’ on *nix

  • baud (Fixnum) (defaults to: 115200)

    baudrate, value must be supported by the platform



33
34
35
36
# File 'lib/mm_gps/mm_gps_beacon.rb', line 33

def initialize(port, baud: 115200)
  @port, @baud = port, baud
  self.open
end

Instance Attribute Details

#baudObject (readonly)

Returns the value of attribute baud.



25
26
27
# File 'lib/mm_gps/mm_gps_beacon.rb', line 25

def baud
  @baud
end

#last_pktObject (readonly)

Returns the value of attribute last_pkt.



24
25
26
# File 'lib/mm_gps/mm_gps_beacon.rb', line 24

def last_pkt
  @last_pkt
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/mm_gps/mm_gps_beacon.rb', line 25

def port
  @port
end

Instance Method Details

#closeBeacon

Close the serialport

Returns:



53
54
55
56
# File 'lib/mm_gps/mm_gps_beacon.rb', line 53

def close
  @sp.close unless (!@sp || @sp.closed?)
  return self
end

#closed?Bool

Check wether the serialport is closed

Returns:

  • (Bool)

    true if closed, false if open



79
80
81
# File 'lib/mm_gps/mm_gps_beacon.rb', line 79

def closed?
  return @sp.closed?
end

#each {|pkt| ... } ⇒ Object

Iterates block to each packet

Yields:

  • (pkt)

    the decoded packet

Yield Parameters:

  • pkt (Hash|Array)

    the decoded packet as returned by MmGPS#parse_packet



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mm_gps/mm_gps_beacon.rb', line 98

def each
  return enum_for(:each) unless block_given?
  loop do
    begin
      yield self.get_packet, @last_pkt
    rescue MmGPSError => e
      warn "Packet Error: #{e.inspect}, reason: #{e.data[:reason]}"
      warn "Packet: #{MmGPS.hexify(e.data[:packet])}"
      if e.data[:reason] == :nocrc then
        warn "CRC16: #{e.data[:crc]}"
      end
    rescue IOError => e
      warn "Port closed #{e.inspect}"
      return
    end
  end
end

#get_packetHash|Array

Reads a raw packet, checks its CRC, and returns its contents as a Hash.

Returns:

  • (Hash|Array)

    typically in the form %I(ts x y f).zip(payload).to_h

Raises:

  • (MmGPSError)

    when CRC16 check does not pass, setting e.data [:reason] to :nocrc



132
133
134
# File 'lib/mm_gps/mm_gps_beacon.rb', line 132

def get_packet
  return MmGPS::parse_packet(self.get_raw_packet)
end

#get_raw_packetString

Reads a raw packet.

Returns:

  • (String)

    a byte stream encoded with Encoding::BINARY

Raises:

  • (MmGPSError)

    when data are not available, setting e.data [:reason] to :noavail



120
121
122
123
124
125
126
# File 'lib/mm_gps/mm_gps_beacon.rb', line 120

def get_raw_packet
  pkt = @buffer.next
  if pkt.empty? then
    raise MmGPSError.new("Data unavailable", {reason: :noavail})
  end
  return @last_pkt = pkt
end

#openBeacon Also known as: reopen

Open the serialport connection with #port at #baud rate. It also resets internals to a clean state.

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mm_gps/mm_gps_beacon.rb', line 62

def open
  self.close 
  @sp = SerialPort.new(@port, "baud" => @baud)
  @sp.read_timeout = 1000 #1 sec
  @sp.binmode
  @last_pkt = ''.force_encoding(Encoding::BINARY)
  @buffer = Buffer.new(START_TOKEN)
  @buffer.when_updating do
    @sp.read(1)
  end
  return self
end

#syncBeacon

Deprecated.

No more needed, since this version automatically takes care of the incomplete packages.

Reads and discards incoming bytes until the START_TOKEN marker is received. Call this metod immediately after opening the connection and before start reading the data.

Returns:



89
90
91
92
# File 'lib/mm_gps/mm_gps_beacon.rb', line 89

def sync
  warn "Beacon#sync is deprecated and no more necessary!"
  return self
end

#trap(signal = "INT") ⇒ Object

Installs a signal handler for the given signal, default to ‘SIGINT’, which closes the serialport connection. Further readings are likely to trigger an IOError.

Parameters:

  • signal (String) (defaults to: "INT")

    the signal to be trapped, default to ‘SIGINT’



43
44
45
46
47
48
# File 'lib/mm_gps/mm_gps_beacon.rb', line 43

def trap(signal="INT")
  Signal.trap(signal) do
    @sp.close
    puts "\nBeacon port: #{@sp.inspect}"
  end
end