Class: Px4LogReader::LogBuffer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ LogBuffer

Returns a new instance of LogBuffer.



9
10
11
12
13
# File 'lib/px4_log_reader/log_buffer.rb', line 9

def initialize( size )
  @data = Array.new( size, 0x00 )
  @read_position = 0
  @write_position = 0
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/px4_log_reader/log_buffer.rb', line 5

def data
  @data
end

#read_positionObject (readonly)

Returns the value of attribute read_position.



6
7
8
# File 'lib/px4_log_reader/log_buffer.rb', line 6

def read_position
  @read_position
end

#write_positionObject (readonly)

Returns the value of attribute write_position.



7
8
9
# File 'lib/px4_log_reader/log_buffer.rb', line 7

def write_position
  @write_position
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/px4_log_reader/log_buffer.rb', line 53

def empty?
  return ( @read_position == @write_position )
end

#read(num_bytes) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/px4_log_reader/log_buffer.rb', line 38

def read( num_bytes )
  data = ''

  if !empty?
    last_index = @read_position + num_bytes
    last_index = @data.size if last_index > @data.size

    read_count = last_index - @read_position
    data = @data[ @read_position, read_count ].pack('C*')
    @read_position += read_count
  end

  return data
end

#resetObject



15
16
17
18
# File 'lib/px4_log_reader/log_buffer.rb', line 15

def reset
  @read_position = 0
  @write_position = 0
end

#write(file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/px4_log_reader/log_buffer.rb', line 20

def write( file )
  while ( @write_position < @data.size ) do
    begin

      bytes = file.read( @data.size - @write_position )

      if bytes
        write_bytes( bytes.unpack('C*') )
      else
        break
      end

    rescue EOFError => error
      break
    end
  end
end