Class: Lignite::Connection

Inherits:
Object
  • Object
show all
Extended by:
Logger
Includes:
Bytes, Logger
Defined in:
lib/lignite/connection.rb,
lib/lignite/connection/tap.rb,
lib/lignite/connection/usb.rb,
lib/lignite/connection/replay.rb,
lib/lignite/connection/bluetooth.rb

Overview

The communication channel to the robot. The callers use #send and #receive. Subclasses implement #read, #write and #close.

Direct Known Subclasses

Bluetooth, Replay, Tap, Usb

Defined Under Namespace

Classes: Bluetooth, Replay, ReplayError, Tap, Usb

Subclasses must implement collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

default_logger, logger

Methods included from Bytes

#bin_to_hex, #f32, #hex_to_bin, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8

Constructor Details

#initializeConnection

Returns a new instance of Connection.



31
32
33
# File 'lib/lignite/connection.rb', line 31

def initialize
  @buf = ""
end

Class Method Details

.createConnection

Returns Try a Usb connection first, then a Bluetooth one.

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lignite/connection.rb', line 11

def self.create
  @c ||= Replay.new(ENV["LIGNITE_REPLAY"]) if ENV["LIGNITE_REPLAY"]

  @c ||= begin
           logger.debug "Connection: trying USB"
           Usb.new
         rescue NoUsbDevice
           logger.debug "Connection: trying BT"
           Bluetooth.new
         end

  @c = Tap.new(@c, ENV["LIGNITE_TAP"]) if ENV["LIGNITE_TAP"]
  logger.debug "Connection: #{@c.inspect}"
  @c
end

.resetObject



27
28
29
# File 'lib/lignite/connection.rb', line 27

def self.reset
  @c = nil
end

Instance Method Details

#closeObject



45
46
47
# File 'lib/lignite/connection.rb', line 45

def close
  Connection.reset
end

#read(maxlen) ⇒ Object

Parameters:

  • maxlen (Integer)


# File 'lib/lignite/connection.rb', line 37

#receiveByteString

Returns a complete message.

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lignite/connection.rb', line 58

def receive
  size = nil
  loop do
    lenbuf = bufread(2)
    size = unpack_u16(lenbuf)
    break unless size.zero?
    # leftover data?
    @buf = ""
  end

  res = bufread(size)
  res
end

#send(payload) ⇒ Object

Parameters:



50
51
52
53
54
55
# File 'lib/lignite/connection.rb', line 50

def send(payload)
  packet = u16(payload.bytesize) + payload
  logger.debug "-> #{packet.inspect}"

  write(packet)
end

#write(data) ⇒ Object

Parameters:



# File 'lib/lignite/connection.rb', line 40