Class: Lignite::Connection

Inherits:
Object
  • Object
show all
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.



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

def initialize
  @buf = ""
end

Class Method Details

.createConnection

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

Returns:



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

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

  @c ||= begin
           Usb.new
         rescue NoUsbDevice
           Bluetooth.new
         end

  @c = Tap.new(@c, ENV["LIGNITE_TAP"]) if ENV["LIGNITE_TAP"]
  @c
end

.resetObject



23
24
25
# File 'lib/lignite/connection.rb', line 23

def self.reset
  @c = nil
end

Instance Method Details

#closeObject



41
42
43
# File 'lib/lignite/connection.rb', line 41

def close
  Connection.reset
end

#read(maxlen) ⇒ Object

Parameters:

  • maxlen (Integer)


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

#receiveByteString

Returns a complete message.

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lignite/connection.rb', line 54

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:



46
47
48
49
50
51
# File 'lib/lignite/connection.rb', line 46

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 36