Class: NickelSilver::Server::Interface::LocoBufferUSB

Inherits:
Object
  • Object
show all
Defined in:
lib/LocoBufferUSB.rb

Overview

A simple IO wrapper for the LocoBuffer-USB.

See the documentation for LocoNetServer for details on how this should be used.

Stand-alone usage

lb = LocoBufferUSB.new( '/dev/ttys0' )

lb.run

loop do
  sleep(1)

  until lb.input_buffer.empty? do
    lb.io_mutex.synchronize do
      puts "Got byte #{ format( '%02x', lb.input_buffer.shift ) } from LocoNet"
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serial_port) ⇒ LocoBufferUSB

Connect to a LocoBuffer-USB using the specified serial port.



31
32
33
34
35
36
37
38
39
40
# File 'lib/LocoBufferUSB.rb', line 31

def initialize( serial_port )
  @locobuffer = SerialPort.new( serial_port, 57_600 )
    
  # these may be modified at any time by the server
  @input_buffer = []
  @output_buffer = []
    
  # only make changes when locked using @io_mutex
  @io_mutex = @iomutex = Mutex.new
end

Instance Attribute Details

#input_bufferObject

Returns the value of attribute input_buffer.



28
29
30
# File 'lib/LocoBufferUSB.rb', line 28

def input_buffer
  @input_buffer
end

#io_mutexObject

Returns the value of attribute io_mutex.



28
29
30
# File 'lib/LocoBufferUSB.rb', line 28

def io_mutex
  @io_mutex
end

#output_bufferObject

Returns the value of attribute output_buffer.



28
29
30
# File 'lib/LocoBufferUSB.rb', line 28

def output_buffer
  @output_buffer
end

Instance Method Details

#runObject

Handle packets moving in and out of the LocoBuffer-USB.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/LocoBufferUSB.rb', line 43

def run
  loop do
    while select( [@locobuffer], nil, nil, 0 ) do
      @io_mutex.synchronize do
        @input_buffer << @locobuffer.getc
      end
    end
  
    # puts "outbuf length = #{output_buffer.length}"
    until output_buffer.empty? do
      @io_mutex.synchronize do
        @locobuffer.putc( @output_buffer.shift )
      end
    end
  end
end